Skip to content

feat(admin): implement TOTP two-factor authentication (#1277) - #1294

Open
cold-13 wants to merge 4 commits into
accesslayerorg:mainfrom
cold-13:feat/1277-admin-totp-2fa
Open

cold-13 wants to merge 4 commits into
accesslayerorg:mainfrom
cold-13:feat/1277-admin-totp-2fa

Conversation

@cold-13

@cold-13 cold-13 commented Nov 22, 2026 •

Copy link
Copy Markdown

Summary

Implements TOTP two-factor authentication for admin sessions as requested in #1277. Admin JWTs carry a twoFactorStatus claim: privileged admin routes reject pending_2fa sessions with 401, while the /v1/admin/2fa/* endpoints allow a pending session to enroll and upgrade itself.

Model and migration

  • Adds the AdminTotp Prisma model (userId, secretEnc, enabledAt, recoveryHashes) and a PostgreSQL migration under prisma/schema/migrations.
  • Additional columns lastUsedStep, createdAt, and updatedAt support replay protection and auditing.

Endpoints and session behavior

  • POST /v1/admin/2fa/setup returns an otpauth:// URI, plus the ten one-time recovery codes (shown only once).
  • POST /v1/admin/2fa/verify enables TOTP after a valid code.
  • POST /v1/admin/2fa/login accepts a TOTP code or recovery code and returns a new verified admin session token.
  • signAdminSessionToken issues pending_2fa sessions by default. adminGuard rejects them on protected admin routes, and pendingAdminGuard limits them to the 2FA endpoints.

Recovery, replay and rate limiting

  • Ten recovery codes are generated with a CSPRNG, normalized, and stored only as keyed HMAC-SHA256 hashes. Consumption uses an exact-list conditional update so concurrent requests cannot consume the same code twice.
  • One TOTP code can be accepted per 30-second step, with a one-step clock-skew window. Replay protection is persisted in lastUsedStep and enforced with a conditional database update.
  • Verification is rate limited per admin identity to five attempts per 15 minutes. The sixth failed attempt returns 429 with Retry-After.

Security considerations

  • TOTP secrets are encrypted at rest with AES-256-GCM using a key derived from APP_SECRET, with the admin ID as additional authenticated data.
  • Neither plaintext nor encrypted secrets are logged or returned except the otpauth URI during enrollment.
  • Recovery codes use keyed hashes so a database leak does not permit offline brute forcing.
  • All verification failures leave the session in pending_2fa; only a successful code or recovery code upgrades it.

Test coverage added

Unit tests now cover setup output and storage, setup verification, valid/invalid login, session upgrade, same-step replay, setup-to-login replay, one-time and concurrent recovery-code consumption, rate-limit escalation to 429, and adminGuard/pendingAdminGuard handling of pending_2fa and verified sessions.

Tests were added but were not executed locally, per request. Dependency installation, tests, build, and lint were intentionally not run.

Closes #1277

@Chucks1093

Copy link
Copy Markdown
Member

⚠️ Merge Conflict Detected

This PR currently has a merge conflict and cannot be merged until it is resolved.

Please rebase or merge the latest changes from the base branch into your branch and push again.

…p-2fa

# Conflicts:
#	src/app.ts
#	src/modules/admin/admin.routes.ts
…org#1277)

Make recovery-code consumption conditional on the exact stored hash list so concurrent verifications cannot both consume the same code. Add unit coverage for setup, verification, login/replay, recovery, clock skew, rate limiting and pending_2fa session rejection.
@cold-13 cold-13 changed the title feat(admin): add TOTP two-factor authentication (#1277) feat(admin): implement TOTP two-factor authentication (#1277) Nov 23, 2026
@drips-wave

drips-wave Bot commented Nov 23, 2026

Copy link
Copy Markdown

@cold-13 Great news! 🎉 Based on an automated assessment of this PR, the linked Wave issue(s) no longer count against your application limits.

You can now already apply to more issues while waiting for a review of this PR. Keep up the great work! 🚀

Learn more about application limits

@Chucks1093

Copy link
Copy Markdown
Member

✅ PR Ready for Review

Due to a recent malware incident affecting our repositories, all PRs are required to pass automated security checks before being reviewed for merging.

This PR has passed all automated checks:

  • ✅ Security scan — no malicious code detected
  • ✅ No merge conflicts
  • ✅ CI checks passed

Next step: Please share this PR link in our Telegram group so a maintainer can review and merge it: https://t.me/c/accesslayerorg/8514

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Implement TOTP two-factor authentication for admin accounts

3 participants