Files
website/server/test/authMe.test.js
wtclaude 60ebacff2c
All checks were successful
PR Checks / bot-install (pull_request) Successful in 19s
PR Checks / server-tests (pull_request) Successful in 42s
PR Checks / client-build (pull_request) Successful in 9m24s
feat(auth): trusted devices, recovery codes, and admin MFA management
Add opt-in "Trust this device" so a browser/app skips the TOTP step (never
the password) for 30 days, single-use bcrypt recovery codes as a 2FA-lockout
fallback, and admin trusted-device/MFA-reset management — backend, web UI,
OpenAPI spec, and tests.

- Schema: trusted_devices (sha256 token hash, looked up by unique index) and
  recovery_codes (bcrypt, single-use). Both additive/idempotent.
- Session service: trust-token mint/hash/resolve + cap helpers; new rg_trust
  httpOnly cookie (survives logout, revoked on untrust/password change/reset/
  TOTP disable). JWTs stay stateless — trust is a server-side row, not a claim.
- Web + mobile login accept a trusted-device token / recovery code; login/totp
  gains trustDevice + recoveryCode. Cap of 10/user with NO silent pruning — an
  over-cap trust returns 409/trustLimitReached and the client prompts to revoke.
- Self-service /auth/me/trusted-devices* + recovery-codes*; admin
  /admin/users/:id/trusted-devices* + /mfa/reset. All actions audit-logged.
- Client: "Trust this device" + recovery-code login options, one-time recovery
  code display, Trusted Devices + Recovery Codes account panels, a TOTP-styled
  revoke-to-continue cap modal, and admin per-user security controls.
- OpenAPI regenerated; 33 new server tests (all suites green).

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
2026-07-21 23:38:48 -05:00

50 lines
2.2 KiB
JavaScript

// Point the DB at a closed port BEFORE requiring anything that builds the pool,
// so any stray DB path fails fast instead of holding the process open. The cases
// here reject at requireAuth (no session token) before any query runs.
process.env.DB_HOST = '127.0.0.1'
process.env.DB_PORT = '59999'
const { test, after } = require('node:test')
const assert = require('node:assert/strict')
const { startApp } = require('./_helper')
const authRouter = require('../src/router/v1/auth/auth.routes')
const db = require('../src/utils/db')
after(() => db.close())
// The role-agnostic /auth/me/* self surface must be gated: every route sits behind
// requireAuth (any role), so an unauthenticated caller gets 401 — never a 404
// (which would mean the route isn't mounted) and never a 200.
test('/auth/me/account* rejects unauthenticated callers with 401', async () => {
const app = await startApp((a) => a.use('/api/v1/auth', authRouter))
try {
const calls = [
['GET', '/api/v1/auth/me/account'],
['GET', '/api/v1/auth/me/account/identities'],
['PATCH', '/api/v1/auth/me/account/username', { username: 'someone' }],
['PATCH', '/api/v1/auth/me/account/password', { newPassword: 'abcd1234' }],
['POST', '/api/v1/auth/me/account/totp/setup'],
['POST', '/api/v1/auth/me/account/totp/enable', { code: '123456' }],
['DELETE', '/api/v1/auth/me/account/identities/google'],
// Trusted devices + recovery codes are self-service too — same gate.
['GET', '/api/v1/auth/me/trusted-devices'],
['POST', '/api/v1/auth/me/trusted-devices', { deviceName: 'X' }],
['DELETE', '/api/v1/auth/me/trusted-devices'],
['DELETE', '/api/v1/auth/me/trusted-devices/1'],
['GET', '/api/v1/auth/me/account/recovery-codes/status'],
['POST', '/api/v1/auth/me/account/recovery-codes/generate', { currentPassword: 'x' }],
]
for (const [method, path, body] of calls) {
const res = await fetch(app.url + path, {
method,
headers: body ? { 'Content-Type': 'application/json' } : {},
body: body ? JSON.stringify(body) : undefined,
})
assert.equal(res.status, 401, `${method} ${path} should be 401, got ${res.status}`)
}
} finally {
await app.close()
}
})