- Product enum (events, accounts, website, app) on Licence and Install, Install.publicKey; existing rows are events - One open licence per product per customer: partial unique index, with a migration guard that names customers with duplicates (npm run check:hub2 lists them beforehand) - Licence keys per product (CEH-, CCA-, CCW-, CCP-); free Accounts licence created with the first other product licence - Protocol v1 registers Events licences only (409 WRONG_PRODUCT); only Events licences can be invoiced until billing per customer - Cross Code Hub in the UI, emails and PDFs - @crosscode/contracts 1.0.0 vendored (backend/vendor) - Steps merge into release/2.0, not main (plan updated) Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
61 lines
2.5 KiB
JavaScript
61 lines
2.5 KiB
JavaScript
/**
|
|
* Secrets at rest (install secrets, two-factor secrets) and random identifiers.
|
|
*
|
|
* Encryption is AES-256-GCM with a key derived from HUB_ENCRYPTION_KEY, stored as
|
|
* "v1:<iv>:<tag>:<ciphertext>" (base64url). The key must not change once installs are
|
|
* registered — their secrets couldn't be decrypted any more.
|
|
*/
|
|
|
|
const crypto = require('crypto');
|
|
const { KEY_PREFIXES } = require('./products');
|
|
|
|
let cachedKey = null;
|
|
function key() {
|
|
if (cachedKey) return cachedKey;
|
|
const secret = process.env.HUB_ENCRYPTION_KEY || '';
|
|
if (secret.length < 32) throw new Error('HUB_ENCRYPTION_KEY must be at least 32 characters');
|
|
cachedKey = crypto.scryptSync(secret, 'events-hub-secrets-v1', 32);
|
|
return cachedKey;
|
|
}
|
|
|
|
function encrypt(plain) {
|
|
const iv = crypto.randomBytes(12);
|
|
const cipher = crypto.createCipheriv('aes-256-gcm', key(), iv);
|
|
const data = Buffer.concat([cipher.update(String(plain), 'utf8'), cipher.final()]);
|
|
return ['v1', iv.toString('base64url'), cipher.getAuthTag().toString('base64url'), data.toString('base64url')].join(':');
|
|
}
|
|
|
|
function decrypt(stored) {
|
|
const [version, iv, tag, data] = String(stored || '').split(':');
|
|
if (version !== 'v1' || !iv || !tag || !data) throw new Error('Not an encrypted value');
|
|
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(data, 'base64url')), decipher.final()]).toString('utf8');
|
|
}
|
|
|
|
// No 0/O, 1/I/L, 5/S, 8/B — easy to read out over the phone.
|
|
const KEY_ALPHABET = 'ACDEFGHJKMNPQRTUVWXY234679';
|
|
|
|
/**
|
|
* A licence key for a product: CEH-XXXX-XXXX-XXXX-XXXX for Events, CCA-/CCW-/CCP- for Accounts,
|
|
* Website and App (16 characters from 26, ~75 bits of randomness).
|
|
*/
|
|
function licenceKey(product = 'events') {
|
|
const prefix = KEY_PREFIXES[product];
|
|
if (!prefix) throw new Error(`Unknown product: ${product}`);
|
|
const bytes = crypto.randomBytes(20);
|
|
const chars = Array.from(bytes, b => KEY_ALPHABET[b % KEY_ALPHABET.length]).join('');
|
|
return `${prefix}-${chars.slice(0, 4)}-${chars.slice(4, 8)}-${chars.slice(8, 12)}-${chars.slice(12, 16)}`;
|
|
}
|
|
|
|
/** Tidy what someone typed: upper case, no spaces. */
|
|
function normaliseLicenceKey(input) {
|
|
return String(input || '').toUpperCase().replace(/\s+/g, '').trim();
|
|
}
|
|
|
|
function randomSecret(bytes = 32) {
|
|
return crypto.randomBytes(bytes).toString('base64url');
|
|
}
|
|
|
|
module.exports = { encrypt, decrypt, licenceKey, normaliseLicenceKey, randomSecret, KEY_ALPHABET, _resetKeyForTests: () => { cachedKey = null; } };
|