Hub v1: install API, licences, health dashboard, alerts
- Protocol 1 (docs/hub-protocol.md): public key, register, signed heartbeats - Ed25519 licence tokens (30-day validity, status, expiry, grace, limits) - Dashboard: installs, install detail, customers and licences, alerts, settings and audit log; two-factor login required - Alert checks every 5 minutes, emailed once; heartbeat roll-up after 30 days - Unit tests for encryption, tokens, signatures, TOTP and alert rules Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_012mt6TCv85T9VKRq3cV7rXq
This commit is contained in:
@@ -0,0 +1,54 @@
|
||||
/**
|
||||
* 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');
|
||||
|
||||
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: CEH-XXXX-XXXX-XXXX-XXXX (16 characters from 26, ~75 bits of randomness). */
|
||||
function licenceKey() {
|
||||
const bytes = crypto.randomBytes(20);
|
||||
const chars = Array.from(bytes, b => KEY_ALPHABET[b % KEY_ALPHABET.length]).join('');
|
||||
return `CEH-${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; } };
|
||||
Reference in New Issue
Block a user