Ingen beskrivning
Du kan inte välja fler än 25 ämnen Ämnen måste starta med en bokstav eller siffra, kan innehålla bindestreck ('-') och vara max 35 tecken långa.

routes.js 18KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349
  1. // HTTP JSON API routes (auth, MFA, users, machines, report, audit, media uploads, SSO).
  2. // Returns a { "METHOD /path": handler } map consumed by server.js.
  3. const fs = require('fs');
  4. const path = require('path');
  5. const R = require('./repos');
  6. const A = require('./auth');
  7. const BZ = require('./bizgaze');
  8. const { now, json, readBody, parseCookies } = require('./lib');
  9. const { audit, currentUser } = require('./session');
  10. const { onlineAgents } = require('./presence');
  11. const { REC_DIR, TRANS_DIR, SESSION_TTL } = require('./config');
  12. const routes = {};
  13. const route = (method, p, fn) => (routes[`${method} ${p}`] = fn);
  14. // Register: creates a team + admin user. MFA must be set up before full access.
  15. route('POST', '/api/register', async (req, res) => {
  16. const anyUser = R.users.anyExists();
  17. if (anyUser && process.env.ALLOW_REGISTRATION !== '1')
  18. return json(res, 403, { error: 'Registration is closed. Contact your administrator.' });
  19. const { email, password, teamName } = await readBody(req);
  20. if (!email || !password) return json(res, 400, { error: 'email and password required' });
  21. if (R.users.emailExists(email))
  22. return json(res, 409, { error: 'email already registered' });
  23. const { hash, salt } = A.hashPassword(password);
  24. const team = R.teams.create(teamName || `${email}'s team`);
  25. const userId = R.users.create({ tenantId: team.id, email, hash, salt, role: 'admin', name: null, mfaSecret: A.newMfaSecret() });
  26. audit({ team_id: team.id, user_id: userId, user_email: email, action: 'user_registered' });
  27. json(res, 200, { ok: true });
  28. });
  29. // Verify MFA enrollment (confirm the user scanned the QR / entered code)
  30. route('POST', '/api/mfa/enable', async (req, res) => {
  31. const { email, code } = await readBody(req);
  32. const u = R.users.byEmail(email);
  33. if (!u) return json(res, 404, { error: 'no such user' });
  34. if (!A.verifyTotp(u.mfa_secret, code)) return json(res, 401, { error: 'invalid code' });
  35. R.users.enableMfa(u.id);
  36. json(res, 200, { ok: true });
  37. });
  38. // Provision (or refresh) a local user from a successful BizGaze identity check.
  39. // The local row exists so sessions, audit, and team-scoped data work; BizGaze stays
  40. // the source of truth for credentials (the local password is random + unused).
  41. function provisionFromBizgaze(email, bz) {
  42. const existing = R.users.byEmail(email);
  43. if (!existing) {
  44. const team = R.teams.first() || R.teams.create('BizGaze');
  45. const { hash, salt } = A.hashPassword(A.token());
  46. const role = bz.isAdmin ? 'admin' : 'technician';
  47. const id = R.users.create({ tenantId: team.id, email, hash, salt, role, name: bz.name || null, mfaSecret: A.newMfaSecret() });
  48. audit({ team_id: team.id, user_id: id, user_email: email, action: 'sso_user_created', detail: 'via BizGaze' });
  49. return R.users.byId(id);
  50. }
  51. if (bz.name && bz.name !== existing.name) R.users.setName(existing.id, bz.name);
  52. return R.users.byId(existing.id);
  53. }
  54. // Login: validates locally first (seeded/bootstrap accounts), then against BizGaze
  55. // (the identity provider) when BIZGAZE_LOGIN_URL is configured. Sets a session cookie.
  56. route('POST', '/api/login', async (req, res) => {
  57. const { email, password, remember } = await readBody(req);
  58. if (!email || !password) return json(res, 400, { error: 'email and password required' });
  59. const existing = R.users.byEmail(email);
  60. if (existing && existing.active === 0) return json(res, 403, { error: 'This account has been deactivated' });
  61. let u = (existing && A.verifyPassword(password, existing.pw_salt, existing.pw_hash)) ? existing : null;
  62. let bzMsg = null;
  63. if (!u) {
  64. const bz = await BZ.validateLogin(email, password);
  65. if (bz.ok) u = provisionFromBizgaze(email, bz);
  66. else if (bz.error) return json(res, 503, { error: bz.error });
  67. else bzMsg = bz.message || null; // BizGaze was configured and rejected the credentials
  68. }
  69. if (!u) {
  70. // Specific feedback where we can be truthful:
  71. if (existing) return json(res, 401, { error: 'Incorrect password. Please try again.' });
  72. // No local account. BizGaze (the identity provider) doesn't reveal whether an email
  73. // exists, so when it rejects we surface its own message (covers wrong password +
  74. // any lockout warning). Only when BizGaze isn't in play can we say "not registered".
  75. if (bzMsg) return json(res, 401, { error: bzMsg });
  76. return json(res, 404, { error: 'This email is not registered.' });
  77. }
  78. const tok = A.token();
  79. const ttl = remember ? 1000 * 60 * 60 * 24 * 30 : SESSION_TTL; // 30 days if remembered, else 24h
  80. R.authSessions.create({ token: tok, userId: u.id, mfaPassed: true, ttl });
  81. res.setHeader('Set-Cookie', `sid=${tok}; HttpOnly; Path=/; Max-Age=${ttl / 1000}`);
  82. audit({ team_id: u.team_id, user_id: u.id, user_email: u.email, action: 'login' });
  83. // Cookie for the web app; token in the body for native desktop/mobile clients
  84. // (they send it back as `Authorization: Bearer <token>`).
  85. json(res, 200, { ok: true, mfaRequired: false, token: tok, expiresAt: now() + ttl });
  86. });
  87. // Login step 2: TOTP code -> marks session mfa_passed
  88. route('POST', '/api/login/mfa', async (req, res) => {
  89. const { code } = await readBody(req);
  90. const tok = parseCookies(req).sid;
  91. const s = tok && R.authSessions.byToken(tok);
  92. if (!s) return json(res, 401, { error: 'no session' });
  93. const u = R.users.byId(s.user_id);
  94. if (!A.verifyTotp(u.mfa_secret, code)) return json(res, 401, { error: 'invalid code' });
  95. R.authSessions.markMfaPassed(tok);
  96. audit({ team_id: u.team_id, user_id: u.id, user_email: u.email, action: 'login' });
  97. json(res, 200, { ok: true });
  98. });
  99. route('POST', '/api/logout', async (req, res) => {
  100. const tok = parseCookies(req).sid;
  101. if (tok) R.authSessions.deleteByToken(tok);
  102. res.setHeader('Set-Cookie', 'sid=; HttpOnly; Path=/; Max-Age=0');
  103. json(res, 200, { ok: true });
  104. });
  105. route('GET', '/api/setup-state', async (req, res) => {
  106. const anyUser = R.users.anyExists();
  107. json(res, 200, { registrationOpen: !anyUser || process.env.ALLOW_REGISTRATION === '1' });
  108. });
  109. // ICE servers for WebRTC. Always includes a public STUN; adds managed TURN if
  110. // configured via env (TURN_URLS comma-separated, TURN_USERNAME, TURN_CREDENTIAL).
  111. route('GET', '/api/ice', async (req, res) => {
  112. const iceServers = [{ urls: 'stun:stun.l.google.com:19302' }];
  113. if (process.env.TURN_URLS) {
  114. iceServers.push({
  115. urls: process.env.TURN_URLS.split(',').map((u) => u.trim()).filter(Boolean),
  116. username: process.env.TURN_USERNAME || '',
  117. credential: process.env.TURN_CREDENTIAL || '',
  118. });
  119. }
  120. json(res, 200, { iceServers });
  121. });
  122. route('GET', '/api/me', async (req, res) => {
  123. const u = currentUser(req);
  124. if (!u) return json(res, 401, { error: 'unauthorized' });
  125. json(res, 200, { id: u.id, email: u.email, role: u.role, teamId: u.team_id, name: u.name || null });
  126. });
  127. // ---------- BizGaze SSO: agent arrives already logged in ----------
  128. route('GET', '/sso', async (req, res) => {
  129. if (!process.env.SSO_SECRET) { res.writeHead(503); return res.end('SSO not configured'); }
  130. const q = new URLSearchParams(req.url.split('?')[1] || '');
  131. const token = q.get('token') || '';
  132. const [payloadB64, sig] = token.split('.');
  133. const fail = (msg) => { res.writeHead(403, { 'Content-Type': 'text/plain' }); res.end(msg); };
  134. if (!payloadB64 || !sig) return fail('Invalid SSO token');
  135. const crypto = require('crypto');
  136. const expect = crypto.createHmac('sha256', process.env.SSO_SECRET).update(payloadB64).digest('base64url');
  137. const sigBuf = Buffer.from(sig), expBuf = Buffer.from(expect);
  138. if (sigBuf.length !== expBuf.length || !crypto.timingSafeEqual(sigBuf, expBuf)) return fail('Invalid SSO signature');
  139. let p; try { p = JSON.parse(Buffer.from(payloadB64, 'base64url').toString()); } catch { return fail('Invalid SSO payload'); }
  140. if (!p.email || !p.exp || p.exp < Math.floor(now() / 1000)) return fail('SSO token expired');
  141. let u = R.users.byEmail(p.email);
  142. if (!u) {
  143. const team = R.teams.first();
  144. if (!team) return fail('No team configured');
  145. const { hash, salt } = A.hashPassword(A.token());
  146. const role = (p.role === 'admin' || p.role === 'viewer') ? p.role : 'technician';
  147. const userId = R.users.create({ tenantId: team.id, email: p.email, hash, salt, role, name: p.name || null, mfaSecret: A.newMfaSecret() });
  148. u = R.users.byId(userId);
  149. audit({ team_id: team.id, user_id: userId, user_email: p.email, action: 'sso_user_created', detail: p.name || '' });
  150. } else if (p.name && p.name !== u.name) {
  151. R.users.setName(u.id, p.name);
  152. }
  153. if (u.active === 0) return fail('Account deactivated');
  154. const tok = A.token();
  155. R.authSessions.create({ token: tok, userId: u.id, mfaPassed: true, ttl: SESSION_TTL });
  156. audit({ team_id: u.team_id, user_id: u.id, user_email: u.email, action: 'login', detail: 'via BizGaze SSO' });
  157. const dest = '/connect' + (p.ticket ? ('?ticket=' + encodeURIComponent(p.ticket)) : '');
  158. res.writeHead(302, { 'Set-Cookie': `sid=${tok}; HttpOnly; Path=/; Max-Age=${SESSION_TTL / 1000}`, Location: dest });
  159. res.end();
  160. });
  161. // Admin adds an agent login to their team
  162. route('POST', '/api/users', async (req, res) => {
  163. const u = currentUser(req);
  164. if (!u) return json(res, 401, { error: 'unauthorized' });
  165. if (u.role !== 'admin') return json(res, 403, { error: 'only admins can add agents' });
  166. const { email, password, name, role } = await readBody(req);
  167. if (!email || !password) return json(res, 400, { error: 'email and temporary password required' });
  168. if (R.users.emailExists(email))
  169. return json(res, 409, { error: 'email already registered' });
  170. const { hash, salt } = A.hashPassword(password);
  171. const r = (role === 'admin' || role === 'viewer') ? role : 'technician';
  172. const userId = R.users.create({ tenantId: u.team_id, email, hash, salt, role: r, name: name || null, mfaSecret: A.newMfaSecret() });
  173. audit({ team_id: u.team_id, user_id: u.id, user_email: u.email, action: 'agent_added', detail: email + ' (' + r + ')' });
  174. json(res, 200, { ok: true, id: userId, email, role: r });
  175. });
  176. // List the team's agents
  177. route('GET', '/api/users', async (req, res) => {
  178. const u = currentUser(req);
  179. if (!u) return json(res, 401, { error: 'unauthorized' });
  180. const rows = R.users.listByTenant(u.team_id);
  181. json(res, 200, rows);
  182. });
  183. // First-login MFA self-setup: a logged-in (password ok) user who hasn't enabled MFA yet
  184. route('GET', '/api/mfa/setup', async (req, res) => {
  185. const u = currentUser(req, { requireMfa: false });
  186. if (!u) return json(res, 401, { error: 'unauthorized' });
  187. if (u.mfa_enabled) return json(res, 400, { error: 'MFA already enabled' });
  188. json(res, 200, { secret: u.mfa_secret, otpauthUrl: A.otpauthUrl(u.mfa_secret, u.email) });
  189. });
  190. // Admin manages an agent: reset password, rename, deactivate/activate, delete.
  191. route('POST', '/api/users/manage', async (req, res) => {
  192. const u = currentUser(req);
  193. if (!u) return json(res, 401, { error: 'unauthorized' });
  194. if (u.role !== 'admin') return json(res, 403, { error: 'only admins can manage agents' });
  195. const { id, action, password, name } = await readBody(req);
  196. const target = R.users.inTenant(id, u.team_id);
  197. if (!target) return json(res, 404, { error: 'no such agent' });
  198. switch (action) {
  199. case 'reset-password': {
  200. if (!password || String(password).length < 8) return json(res, 400, { error: 'new password must be at least 8 characters' });
  201. const { hash, salt } = A.hashPassword(password);
  202. R.users.setPassword(target.id, hash, salt);
  203. R.authSessions.deleteByUser(target.id); // force re-login
  204. audit({ team_id: u.team_id, user_id: u.id, user_email: u.email, action: 'agent_password_reset', detail: target.email });
  205. return json(res, 200, { ok: true });
  206. }
  207. case 'rename': {
  208. const clean = String(name || '').trim().slice(0, 60);
  209. if (!clean) return json(res, 400, { error: 'name required' });
  210. R.users.setName(target.id, clean);
  211. audit({ team_id: u.team_id, user_id: u.id, user_email: u.email, action: 'agent_renamed', detail: target.email + ' -> ' + clean });
  212. return json(res, 200, { ok: true, name: clean });
  213. }
  214. case 'deactivate': {
  215. if (target.id === u.id) return json(res, 400, { error: 'you cannot deactivate your own account' });
  216. R.users.setActive(target.id, false);
  217. R.authSessions.deleteByUser(target.id);
  218. audit({ team_id: u.team_id, user_id: u.id, user_email: u.email, action: 'agent_deactivated', detail: target.email });
  219. return json(res, 200, { ok: true });
  220. }
  221. case 'activate': {
  222. R.users.setActive(target.id, true);
  223. audit({ team_id: u.team_id, user_id: u.id, user_email: u.email, action: 'agent_activated', detail: target.email });
  224. return json(res, 200, { ok: true });
  225. }
  226. case 'delete': {
  227. if (target.id === u.id) return json(res, 400, { error: 'you cannot delete your own account' });
  228. R.authSessions.deleteByUser(target.id);
  229. R.users.remove(target.id);
  230. audit({ team_id: u.team_id, user_id: u.id, user_email: u.email, action: 'agent_deleted', detail: target.email });
  231. return json(res, 200, { ok: true });
  232. }
  233. default: return json(res, 400, { error: 'unknown action' });
  234. }
  235. });
  236. // Session report: one row per session, filterable by agent and date period
  237. route('GET', '/api/report', async (req, res) => {
  238. const u = currentUser(req);
  239. if (!u) return json(res, 401, { error: 'unauthorized' });
  240. const q = new URLSearchParams(req.url.split('?')[1] || '');
  241. // Admins see the whole team (and may filter by agent); everyone else sees only
  242. // their own sessions, regardless of any agent filter passed.
  243. const agentEmail = u.role !== 'admin' ? u.email : (q.get('agent') || null);
  244. const from = q.get('from') ? new Date(q.get('from') + 'T00:00:00').getTime() : null;
  245. const to = q.get('to') ? new Date(q.get('to') + 'T23:59:59').getTime() : null;
  246. json(res, 200, R.sessionsLog.report({ tenantId: u.team_id, agentEmail, from, to }));
  247. });
  248. // List machines for the team (with live online status from signaling layer)
  249. route('GET', '/api/machines', async (req, res) => {
  250. const u = currentUser(req);
  251. if (!u) return json(res, 401, { error: 'unauthorized' });
  252. const rows = R.machines.listByTenant(u.team_id);
  253. json(res, 200, rows.map((m) => ({ ...m, online: onlineAgents.has(m.id) })));
  254. });
  255. // Create a machine enrollment token (admin/technician). Agent uses it to come online.
  256. route('POST', '/api/machines', async (req, res) => {
  257. const u = currentUser(req);
  258. if (!u) return json(res, 401, { error: 'unauthorized' });
  259. if (u.role === 'viewer') return json(res, 403, { error: 'forbidden' });
  260. const { name, unattended } = await readBody(req);
  261. const enroll = A.token();
  262. const mId = R.machines.create({ tenantId: u.team_id, name: name || 'Unnamed PC', enrollToken: enroll, unattended: !!unattended });
  263. audit({ team_id: u.team_id, user_id: u.id, user_email: u.email, machine_id: mId, machine_name: name, action: 'machine_enrolled' });
  264. json(res, 200, { id: mId, enrollToken: enroll });
  265. });
  266. route('GET', '/api/audit', async (req, res) => {
  267. const u = currentUser(req);
  268. if (!u) return json(res, 401, { error: 'unauthorized' });
  269. const rows = R.audit.listByTenant(u.team_id);
  270. json(res, 200, rows);
  271. });
  272. // ---------- session recording: upload (agent) ----------
  273. const MAX_REC_BYTES = 500 * 1024 * 1024; // 500 MB safety cap
  274. route('POST', '/api/recording', async (req, res) => {
  275. const u = currentUser(req);
  276. if (!u) return json(res, 401, { error: 'unauthorized' });
  277. const params = new URLSearchParams(req.url.split('?')[1] || '');
  278. const sid = params.get('sessionId');
  279. const ext = params.get('ext') === 'mp4' ? 'mp4' : 'webm'; // container chosen by the recorder
  280. if (!sid) return json(res, 400, { error: 'sessionId required' });
  281. const row = R.sessionsLog.byIdInTenant(sid, u.team_id);
  282. if (!row) return json(res, 404, { error: 'no such session' });
  283. const chunks = []; let total = 0, aborted = false;
  284. req.on('data', (c) => { total += c.length; if (total > MAX_REC_BYTES) { aborted = true; req.destroy(); return; } chunks.push(c); });
  285. req.on('end', () => {
  286. if (aborted) return json(res, 413, { error: 'recording too large' });
  287. const fname = sid + '.' + ext;
  288. try {
  289. fs.writeFileSync(path.join(REC_DIR, fname), Buffer.concat(chunks));
  290. R.sessionsLog.setRecording(sid, fname);
  291. audit({ team_id: u.team_id, user_id: u.id, user_email: u.email, action: 'recording_saved', detail: 'session ' + sid });
  292. json(res, 200, { ok: true });
  293. } catch (e) { json(res, 500, { error: 'could not save recording' }); }
  294. });
  295. req.on('error', () => { try { res.end(); } catch (e) {} });
  296. });
  297. route('POST', '/api/transcript', async (req, res) => {
  298. const u = currentUser(req);
  299. if (!u) return json(res, 401, { error: 'unauthorized' });
  300. const sid = new URLSearchParams(req.url.split('?')[1] || '').get('sessionId');
  301. if (!sid) return json(res, 400, { error: 'sessionId required' });
  302. const row = R.sessionsLog.byIdInTenant(sid, u.team_id);
  303. if (!row) return json(res, 404, { error: 'no such session' });
  304. const chunks = []; let total = 0, aborted = false;
  305. req.on('data', (c) => { total += c.length; if (total > 5 * 1024 * 1024) { aborted = true; req.destroy(); return; } chunks.push(c); });
  306. req.on('end', () => {
  307. if (aborted) return json(res, 413, { error: 'transcript too large' });
  308. const fname = sid + '.txt';
  309. try {
  310. fs.writeFileSync(path.join(TRANS_DIR, fname), Buffer.concat(chunks));
  311. R.sessionsLog.setTranscript(sid, fname);
  312. json(res, 200, { ok: true });
  313. } catch (e) { json(res, 500, { error: 'could not save transcript' }); }
  314. });
  315. req.on('error', () => { try { res.end(); } catch (e) {} });
  316. });
  317. // API versioning: alias every /api/* route under /api/v1/* — a frozen contract for
  318. // native desktop/mobile clients. The web app keeps using the unversioned paths, and
  319. // both share the same handlers. (/sso is a browser redirect, intentionally unversioned.)
  320. for (const key of Object.keys(routes)) {
  321. const m = key.match(/^(\S+) \/api\/(.+)$/);
  322. if (m) routes[`${m[1]} /api/v1/${m[2]}`] = routes[key];
  323. }
  324. module.exports = routes;