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:
2026-10-26 12:12:51 +02:00
co-authored by Claude Opus 5.5
commit e72330893a
56 changed files with 16952 additions and 0 deletions
+45
View File
@@ -0,0 +1,45 @@
const jwt = require('jsonwebtoken');
const prisma = require('../config/db');
const SESSION_HOURS = 12;
function secret() {
if (!process.env.JWT_SECRET) throw new Error('JWT_SECRET is not set');
return process.env.JWT_SECRET;
}
/**
* Tokens carry a scope: 'session' (logged in), 'totp' (password checked, waiting for the
* two-factor code) or 'setup' (password checked, two-factor not set up yet).
*/
function issue(admin, scope, expiresIn) {
return jwt.sign({ sub: admin.id, v: admin.tokenVersion, scope }, secret(), { expiresIn, algorithm: 'HS256' });
}
const sessionToken = admin => issue(admin, 'session', `${SESSION_HOURS}h`);
const stepToken = (admin, scope) => issue(admin, scope, '10m');
/** The admin for a token of the given scope, or null. */
async function adminFromToken(token, scope) {
try {
const decoded = jwt.verify(String(token || ''), secret(), { algorithms: ['HS256'] });
if (decoded.scope !== scope) return null;
const admin = await prisma.adminUser.findUnique({ where: { id: decoded.sub } });
if (!admin || admin.tokenVersion !== decoded.v) return null;
return admin;
} catch {
return null;
}
}
/** Requires a logged-in admin (Authorization: Bearer <session token>). */
async function protect(req, res, next) {
const header = req.headers.authorization || '';
const token = header.startsWith('Bearer ') ? header.slice(7) : null;
const admin = await adminFromToken(token, 'session');
if (!admin) return res.status(401).json({ message: 'Please log in again.', code: 'UNAUTHENTICATED' });
req.admin = admin;
next();
}
module.exports = { protect, sessionToken, stepToken, adminFromToken, SESSION_HOURS };