Skip to content
Toolkit

ADR-0173 — A second factor has one-time recovery codes

Status: Accepted · Date: 2026-10-11 · Track: launch wave 2, api workstream (launch roadmap #33) · Builds on: ADR-0168 (a password reset no longer clears 2FA)

Context

Since ADR-0168, a password reset keeps 2FA. That closed the hole where email access alone defeated the second factor. It also means an account that loses its authenticator has no way back in except an operator. The help articles for 2FA were blocked on there being a recovery path.

Decision

  • Issued on enable. When POST /auth/2fa/verify turns 2FA on, it returns ten codes in recovery_codes. This is the only time they appear.
  • Shape. Each code is 16 characters from a 32-symbol alphabet with no look-alikes (no i l o 1), displayed as xxxx-xxxx-xxxx-xxxx, which is 80 bits of randomness. When checked, case, spaces and dashes are ignored.
  • Stored as hashes. twofa_recovery_codes (migration 000048) holds only a salted SHA-256 of the normalised code. At 80 bits a fast hash cannot be searched back to a code, and it lets sign-in match and spend a code in one statement.
  • Sign-in. POST /auth/login accepts recovery_code in place of twofa_token, only after the password is right. The code is spent by UPDATE … WHERE used_at IS NULL, so it works once even under concurrent sign-ins. A wrong or used code is a 401. The route has the same login lockout as the password. A success returns recovery_codes_remaining.
  • Audit. These events go to the account's users_logs, the same record a sign-in writes:
    • login.recovery_code (a successful sign-in with a code)
    • login.recovery_code_rejected
    • twofa.enabled
    • twofa.recovery_codes_regenerated
    • twofa.disabled
  • Manage. GET /auth/2fa/recovery-codes returns {twofa_enabled, remaining}. POST /auth/2fa/recovery-codes {password} replaces the whole set in one transaction and returns the new codes once; it answers 409 when 2FA is off. Turning 2FA off deletes the codes.

Consequences

  • The web security page shows the codes on enable and on regenerate, offers a "Use a recovery code" path at sign-in, and prompts for a new set when few remain. That work is web's (contract in the wave-2 hand-off).
  • Found, not changed: Setup2FA stores the secret before it is verified, and sign-in checks any stored secret. So starting setup and abandoning it turns 2FA on with no codes issued. A pending-secret column or flag would fix this; it is a separate item.
  • OAuth sign-in does not ask for the second factor at all. That is an existing gap, also a separate item.

Source: docs/decisions/0173-a-second-factor-has-one-time-recovery-codes.md

Library