apps/app (not released; app@1.0.0 is built in slices, APP-1.0-PLAN §15): - backend (Express, Prisma, pg-boss, as the Website): the Hub client (CCP-, product app, the active-member band in the heartbeat), links and church.changed, sign-in through Accounts (everyone a Member; 8 days renewed up to 90; every role only with 2FA; the first Owner from HUB_ADMIN_EMAIL), Accounts' user.updated/export/deleted, active members once a day, modules on and off, GET /api/v1/app. Migration init. - frontend (Next.js 16): header, tab bar, footer, manifest and icons from the church profile, service worker, Add to home screen, Me, the admin's first dashboard. - env.template.json, PM2 template, README, CHANGELOG, WHATS-NEW; 27 tests. Docs: the Hub runs 2.26.1 with two years of Cross Code's devotionals; an App gets 2 weeks, today included. HANDOVER lists the App's slices. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
40 lines
1.6 KiB
TypeScript
40 lines
1.6 KiB
TypeScript
/**
|
|
* Secrets at rest (AES-256-GCM, as Accounts' and the Website's lib/crypto.ts), and tokens. The key
|
|
* comes from APP_SECRET; encrypted values start with "enc:".
|
|
*/
|
|
import crypto from 'node:crypto';
|
|
|
|
const SENTINEL = 'enc:';
|
|
|
|
export class Secrets {
|
|
private readonly key: Buffer;
|
|
|
|
constructor(secret: string) {
|
|
this.key = crypto.scryptSync(secret, 'crosscode-app-aes256gcm-v1', 32);
|
|
}
|
|
|
|
encrypt(plain: string): string {
|
|
if (plain.startsWith(SENTINEL)) return plain;
|
|
const iv = crypto.randomBytes(12);
|
|
const cipher = crypto.createCipheriv('aes-256-gcm', this.key, iv);
|
|
const data = Buffer.concat([cipher.update(plain, 'utf8'), cipher.final()]);
|
|
return `${SENTINEL}${iv.toString('hex')}:${cipher.getAuthTag().toString('hex')}:${data.toString('hex')}`;
|
|
}
|
|
|
|
/** Throws if the value was changed or encrypted with another key. */
|
|
decrypt(value: string): string {
|
|
if (!value.startsWith(SENTINEL)) return value;
|
|
const [iv, tag, data] = value.slice(SENTINEL.length).split(':');
|
|
if (!iv || !tag || data === undefined) throw new Error('Invalid encrypted value');
|
|
const decipher = crypto.createDecipheriv('aes-256-gcm', this.key, Buffer.from(iv, 'hex'));
|
|
decipher.setAuthTag(Buffer.from(tag, 'hex'));
|
|
return Buffer.concat([decipher.update(Buffer.from(data, 'hex')), decipher.final()]).toString('utf8');
|
|
}
|
|
}
|
|
|
|
/** A random token for a cookie (32 bytes, base64url). */
|
|
export const randomToken = () => crypto.randomBytes(32).toString('base64url');
|
|
|
|
/** What's stored for a token: only its SHA-256. */
|
|
export const hashToken = (token: string) => crypto.createHash('sha256').update(token).digest('hex');
|