'use strict'; const express = require('express'); const bcrypt = require('bcrypt'); const router = express.Router(); const { getDb } = require('../db'); const { getServerPublicKeyJwk, hybridDecrypt, hybridEncryptForClient } = require('../crypto'); const { createToken, requireAuth } = require('../auth'); const BCRYPT_ROUNDS = 12; // --------------------------------------------------------------------------- // GET /api/auth/server-key // Returns the server's RSA public key as JWK so the browser can import it. // --------------------------------------------------------------------------- router.get('/server-key', (req, res) => { res.json({ publicKey: getServerPublicKeyJwk() }); }); // --------------------------------------------------------------------------- // GET /api/auth/setup-needed // Returns { needed: true } if no users exist yet. // --------------------------------------------------------------------------- router.get('/setup-needed', (req, res) => { const db = getDb(); const count = db.prepare('SELECT COUNT(*) as n FROM users').get().n; res.json({ needed: count === 0 }); }); // --------------------------------------------------------------------------- // POST /api/auth/setup // Creates the first admin account (only when no users exist). // Body: { encrypted: { encryptedKey, iv, ciphertext, tag }, clientPublicKey } // The encrypted payload must be: { username, password } // --------------------------------------------------------------------------- router.post('/setup', (req, res) => { const db = getDb(); const count = db.prepare('SELECT COUNT(*) as n FROM users').get().n; if (count > 0) { return res.status(403).json({ error: 'Setup already completed.' }); } let credentials; try { const plain = hybridDecrypt(req.body.encrypted); credentials = JSON.parse(plain); } catch { return res.status(400).json({ error: 'Invalid encrypted payload.' }); } const { username, password } = credentials; if (!username || !password || username.length < 1 || password.length < 8) { return res.status(400).json({ error: 'Username required and password must be at least 8 characters.' }); } const hash = bcrypt.hashSync(password, BCRYPT_ROUNDS); db.prepare('INSERT INTO users (username, password_hash, is_admin, permissions) VALUES (?, ?, 1, ?)').run( [username, hash, '{}'] ); // Issue token const token = createToken(username, req, req.body.fingerprint || ''); const encrypted = hybridEncryptForClient(token, req.body.clientPublicKey); res.json({ encrypted }); }); // --------------------------------------------------------------------------- // POST /api/auth/login // Body: { encrypted: { encryptedKey, iv, ciphertext, tag }, clientPublicKey, fingerprint } // The encrypted payload must be: { username, password } // --------------------------------------------------------------------------- router.post('/login', (req, res) => { let credentials; try { const plain = hybridDecrypt(req.body.encrypted); credentials = JSON.parse(plain); } catch { return res.status(400).json({ error: 'Invalid encrypted payload.' }); } const { username, password } = credentials; if (!username || !password) { return res.status(400).json({ error: 'Invalid credentials.' }); } const db = getDb(); const user = db.prepare('SELECT * FROM users WHERE username = ?').get([username]); if (!user || !bcrypt.compareSync(password, user.password_hash)) { return res.status(401).json({ error: 'Invalid credentials.' }); } const fingerprint = req.body.fingerprint || ''; const token = createToken(username, req, fingerprint); const encrypted = hybridEncryptForClient(token, req.body.clientPublicKey); res.json({ encrypted }); }); // --------------------------------------------------------------------------- // GET /api/auth/me // Returns the current user's basic info. // --------------------------------------------------------------------------- router.get('/me', requireAuth, (req, res) => { res.json({ username: req.user.username, isAdmin: req.user.isAdmin, permissions: req.user.permissions, }); }); module.exports = router;