Pular para o conteúdo principal
Versão: main (dev) 🚧

ADR-016 — TOTP second factor at sign-in (opt-in)

Date: 2026-10-01 Status: Accepted Extends: ADR-006 (local Identity + dual scheme), ADR-008 (CSRF gating). Library side: DevPack ADR-013 (Identity/Mfa, 10.18.0). Origin: ABBC Hub backport, wave W4 (plan decisions D4 and P1).

Context​

The template signs local accounts in on a password alone. Derived products asked for a second factor, and the ABBC Hub built one — challenge table, replay guard, attempt counter, reset, cutover — coupled to its own user and context. The library half (the challenge and its guarantees) now lives in the DevPack. What remains is the template's half: where the factor sits in the sign-in flow, how the ticket travels on each channel, and what a deployment must configure before turning it on.

Decision​

The factor is opt-in (Authentication:Mfa:Mode, default Disabled); with it off, every sign-in answer is byte-for-byte what it was.

  • One flow for both channels. IPasswordSignInFlow checks the password (IIdentityService.CheckPasswordAsync, which no longer clears the failure counter), decides the step owed, and only clears the counter once the whole sign-in succeeded. An enrolled account is challenged in every mode but Disabled; an account without a factor is sent to enrol under Required once EnforceFrom passed (or immediately when absent).
  • Browser channel: POST /v1/auth/session answers 401 with errorCode Auth.MfaRequired or Auth.MfaEnrollmentRequired and expiresAt, and writes the ticket to an HttpOnly cookie scoped to /v1/auth (MfaChallengeCookie). It is not an authentication scheme — nothing reaches HttpContext.User until the challenge is answered. GET|POST /session/mfa/enrollment, POST /session/mfa (code or recovery code) and POST /session/mfa/cancel finish or abandon it. A wrong code keeps the cookie.
  • Token channel: POST /v1/auth/token returns the ticket in the problem body (no cookie jar there), answered at POST /v1/auth/token/mfa. An account that still has to enrol is refused with Auth.MfaEnrollmentRequired — enrolling means scanning a QR, which needs the browser.
  • Self-enrolment (Optional): POST /v1/auth/me/mfa/enrollment opens an enrolment for the signed-in account and reuses the same screen and completion. An account that already holds a factor is refused — replacing one goes through a reset.
  • CSRF: the challenge cookie is an ambient credential, so writes carrying it are validated (the anonymous GET /v1/auth/csrf token satisfies it — no deadlock).
  • Rate limit: auth-mfa, 12/min per hash of the ticket (RateLimiting:Auth:Mfa:*). It caps volume per challenge; the anti-guessing control is the per-challenge allowance plus the account lockout (DevPack).
  • Cutover: the session cookie carries the instant it was opened (StampOpenedAt, kept on refresh and tenant switch) and RejectSessionsPredatingMfa drops it after the cutover; TokenIssuer.RefreshAsync refuses a refresh token whose IssuedAt predates it (Auth.SessionPredatesMfa).
  • Secrets: the authenticator key is encrypted and recovery codes are hashed (DevPack ProtectedAuthenticatorUserStore). Outside Development the API refuses to boot with the factor on and no persistent key ring (MfaKeyRingGuard, DataProtection:BlobUri) — the in-memory ring would lock every enrolled account out on the next restart.
  • QR: rendered server-side as SVG (IQrCodeRenderer over Net.Codecrete.QrCodeGenerator).

Consequences​

  • One migration (AddMfa): identity.MfaChallenges, identity.MfaCodeNonces, identity.MfaEvents, identity.RefreshTokens.IssuedAt.
  • Porting note: IIdentityService.ValidateCredentialsAsync is replaced by CheckPasswordAsync + ClearFailedAttemptsAsync; the login commands return PasswordSignIn / TokenSignIn. With Mode != Disabled, /session and /token can answer 401 with the new codes, and after a cutover older sessions and refresh tokens are refused.
  • MfaEvent is allowlisted in AssertEveryMultiTenantEntityIsFiltered: its TenantId is the scope at the time, often none.

Alternatives considered​

  • A partial principal on the cookie scheme between password and code: it would satisfy RequireAuthorization() everywhere. Rejected — the ticket must confer nothing but the right to answer.
  • The ticket in the response body for the browser too: puts a token-shaped value in reach of the SPA's scripts, against ADR-006.
  • Enrolment over the token channel: a native client would have to render and scan its own QR; not worth the surface for the template's default.