BlueGit

timfa filemanager

Files

filemanager / server / routes / auth.js

auth.js

import project
timfa Tim Falken · pushed by timfa · 6 months ago
d3c0b911 History
auth.js 4.1 KiB Download Raw
1'use strict';
2
3const express = require('express');
4const bcrypt = require('bcrypt');
5const router = express.Router();
6
7const { getDb } = require('../db');
8const { getServerPublicKeyJwk, hybridDecrypt, hybridEncryptForClient } = require('../crypto');
9const { createToken, requireAuth } = require('../auth');
10
11const BCRYPT_ROUNDS = 12;
12
13// ---------------------------------------------------------------------------
14// GET /api/auth/server-key
15// Returns the server's RSA public key as JWK so the browser can import it.
16// ---------------------------------------------------------------------------
17router.get('/server-key', (req, res) => {
18 res.json({ publicKey: getServerPublicKeyJwk() });
19});
20
21// ---------------------------------------------------------------------------
22// GET /api/auth/setup-needed
23// Returns { needed: true } if no users exist yet.
24// ---------------------------------------------------------------------------
25router.get('/setup-needed', (req, res) => {
26 const db = getDb();
27 const count = db.prepare('SELECT COUNT(*) as n FROM users').get().n;
28 res.json({ needed: count === 0 });
29});
30
31// ---------------------------------------------------------------------------
32// POST /api/auth/setup
33// Creates the first admin account (only when no users exist).
34// Body: { encrypted: { encryptedKey, iv, ciphertext, tag }, clientPublicKey }
35// The encrypted payload must be: { username, password }
36// ---------------------------------------------------------------------------
37router.post('/setup', (req, res) => {
38 const db = getDb();
39 const count = db.prepare('SELECT COUNT(*) as n FROM users').get().n;
40 if (count > 0) {
41 return res.status(403).json({ error: 'Setup already completed.' });
42 }
43
44 let credentials;
45 try {
46 const plain = hybridDecrypt(req.body.encrypted);
47 credentials = JSON.parse(plain);
48 } catch {
49 return res.status(400).json({ error: 'Invalid encrypted payload.' });
50 }
51
52 const { username, password } = credentials;
53 if (!username || !password || username.length < 1 || password.length < 8) {
54 return res.status(400).json({ error: 'Username required and password must be at least 8 characters.' });
55 }
56
57 const hash = bcrypt.hashSync(password, BCRYPT_ROUNDS);
58 db.prepare('INSERT INTO users (username, password_hash, is_admin, permissions) VALUES (?, ?, 1, ?)').run(
59 [username, hash, '{}']
60 );
61
62 // Issue token
63 const token = createToken(username, req, req.body.fingerprint || '');
64 const encrypted = hybridEncryptForClient(token, req.body.clientPublicKey);
65 res.json({ encrypted });
66});
67
68// ---------------------------------------------------------------------------
69// POST /api/auth/login
70// Body: { encrypted: { encryptedKey, iv, ciphertext, tag }, clientPublicKey, fingerprint }
71// The encrypted payload must be: { username, password }
72// ---------------------------------------------------------------------------
73router.post('/login', (req, res) => {
74 let credentials;
75 try {
76 const plain = hybridDecrypt(req.body.encrypted);
77 credentials = JSON.parse(plain);
78 } catch {
79 return res.status(400).json({ error: 'Invalid encrypted payload.' });
80 }
81
82 const { username, password } = credentials;
83 if (!username || !password) {
84 return res.status(400).json({ error: 'Invalid credentials.' });
85 }
86
87 const db = getDb();
88 const user = db.prepare('SELECT * FROM users WHERE username = ?').get([username]);
89
90 if (!user || !bcrypt.compareSync(password, user.password_hash)) {
91 return res.status(401).json({ error: 'Invalid credentials.' });
92 }
93
94 const fingerprint = req.body.fingerprint || '';
95 const token = createToken(username, req, fingerprint);
96 const encrypted = hybridEncryptForClient(token, req.body.clientPublicKey);
97 res.json({ encrypted });
98});
99
100// ---------------------------------------------------------------------------
101// GET /api/auth/me
102// Returns the current user's basic info.
103// ---------------------------------------------------------------------------
104router.get('/me', requireAuth, (req, res) => {
105 res.json({
106 username: req.user.username,
107 isAdmin: req.user.isAdmin,
108 permissions: req.user.permissions,
109 });
110});
111
112module.exports = router;