暂无描述
您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符

1234567891011121314151617181920212223242526272829303132333435363738
  1. // Session/auth helpers: resolve the current user from the cookie, write audit rows.
  2. const R = require('./repos');
  3. const { parseCookies, now } = require('./lib');
  4. function audit(entry) {
  5. R.audit.add(entry);
  6. }
  7. // Resolve the session token from a request, supporting every client transport:
  8. // - `Authorization: Bearer <token>` → native desktop/mobile apps (HTTP + WS upgrade)
  9. // - `sid` cookie → the web app (HTTP + same-origin WS)
  10. // - `?access_token=`/`?token=` query → browser WS fallback when a cookie isn't usable
  11. // All three resolve to the same opaque token in `sessions_auth`.
  12. function tokenFromReq(req) {
  13. const h = req.headers && (req.headers.authorization || req.headers.Authorization);
  14. if (h && /^Bearer\s+/i.test(h)) return h.replace(/^Bearer\s+/i, '').trim();
  15. const cookieTok = parseCookies(req).sid;
  16. if (cookieTok) return cookieTok;
  17. try {
  18. const qs = (req.url || '').split('?')[1];
  19. if (qs) { const t = new URLSearchParams(qs).get('access_token') || new URLSearchParams(qs).get('token'); if (t) return t; }
  20. } catch (_) {}
  21. return null;
  22. }
  23. // Resolve the logged-in user from the request. Returns user row (with mfa state) or null.
  24. function currentUser(req, { requireMfa = true } = {}) {
  25. const tok = tokenFromReq(req);
  26. if (!tok) return null;
  27. const s = R.authSessions.byToken(tok);
  28. if (!s || s.expires_at < now()) return null;
  29. if (requireMfa && !s.mfa_passed) return null;
  30. const u = R.users.byId(s.user_id);
  31. if (!u || u.active === 0) return null;
  32. return { ...u, _session: s };
  33. }
  34. module.exports = { audit, currentUser, tokenFromReq };