/** * Generates the RSA keypair used to sign (Manager) and verify (every company app) handoff * tokens. Run once per environment: npm run generate-handoff-keys */ import fs from 'fs'; import path from 'path'; import crypto from 'crypto'; const keysDir = path.resolve(__dirname, '..', 'keys'); function main() { if (!fs.existsSync(keysDir)) { fs.mkdirSync(keysDir, { recursive: true }); } const privateKeyPath = path.join(keysDir, 'handoff-private.pem'); const publicKeyPath = path.join(keysDir, 'handoff-public.pem'); if (fs.existsSync(privateKeyPath) || fs.existsSync(publicKeyPath)) { console.log('Handoff keypair already exists at manager/keys/ — not overwriting.'); console.log(` Private: ${privateKeyPath}`); console.log(` Public: ${publicKeyPath}`); return; } const { publicKey, privateKey } = crypto.generateKeyPairSync('rsa', { modulusLength: 2048, publicKeyEncoding: { type: 'spki', format: 'pem' }, privateKeyEncoding: { type: 'pkcs8', format: 'pem' }, }); fs.writeFileSync(privateKeyPath, privateKey, { mode: 0o600 }); fs.writeFileSync(publicKeyPath, publicKey); console.log('Generated handoff RS256 keypair:'); console.log(` Private (Manager only, never share): ${privateKeyPath}`); console.log(` Public (copy into every company app's HANDOFF_PUBLIC_KEYS): ${publicKeyPath}`); } main();