Initial commit: page-manager Next.js app with Coolify deploy support
Replace Vercel-specific infra (Cron, Blob) with self-hosted equivalents for deployment to Coolify: in-container cron scheduler (scripts/cron-runner.mjs) and disk-based upload storage served via /api/uploads/[...path]. Add Dockerfile, .dockerignore, and /api/health for container health checks. Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>
This commit is contained in:
@@ -0,0 +1,30 @@
|
||||
import crypto from "node:crypto";
|
||||
|
||||
// Page access tokens are stored encrypted at rest. Format: "v1:<iv>:<tag>:<ciphertext>",
|
||||
// each part base64url. AES-256-GCM so a tampered row fails to decrypt instead of
|
||||
// yielding a garbage token.
|
||||
const VERSION = "v1";
|
||||
|
||||
function key(): Buffer {
|
||||
const raw = process.env.TOKEN_ENCRYPTION_KEY;
|
||||
if (!raw) throw new Error("TOKEN_ENCRYPTION_KEY env var is not set");
|
||||
const buf = Buffer.from(raw, "base64");
|
||||
if (buf.length !== 32) throw new Error("TOKEN_ENCRYPTION_KEY must be 32 bytes (base64)");
|
||||
return buf;
|
||||
}
|
||||
|
||||
export function encryptToken(plain: string): string {
|
||||
const iv = crypto.randomBytes(12);
|
||||
const cipher = crypto.createCipheriv("aes-256-gcm", key(), iv);
|
||||
const enc = Buffer.concat([cipher.update(plain, "utf8"), cipher.final()]);
|
||||
const tag = cipher.getAuthTag();
|
||||
return [VERSION, iv.toString("base64url"), tag.toString("base64url"), enc.toString("base64url")].join(":");
|
||||
}
|
||||
|
||||
export function decryptToken(boxed: string): string {
|
||||
const [version, iv, tag, enc] = boxed.split(":");
|
||||
if (version !== VERSION || !iv || !tag || !enc) throw new Error("Unrecognised token cipher format");
|
||||
const decipher = crypto.createDecipheriv("aes-256-gcm", key(), Buffer.from(iv, "base64url"));
|
||||
decipher.setAuthTag(Buffer.from(tag, "base64url"));
|
||||
return Buffer.concat([decipher.update(Buffer.from(enc, "base64url")), decipher.final()]).toString("utf8");
|
||||
}
|
||||
Reference in New Issue
Block a user