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.
IPasswordSignInFlowchecks 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 butDisabled; an account without a factor is sent to enrol underRequiredonceEnforceFrompassed (or immediately when absent). - Browser channel:
POST /v1/auth/sessionanswers 401 witherrorCodeAuth.MfaRequiredorAuth.MfaEnrollmentRequiredandexpiresAt, and writes the ticket to anHttpOnlycookie scoped to/v1/auth(MfaChallengeCookie). It is not an authentication scheme — nothing reachesHttpContext.Useruntil the challenge is answered.GET|POST /session/mfa/enrollment,POST /session/mfa(code or recovery code) andPOST /session/mfa/cancelfinish or abandon it. A wrong code keeps the cookie. - Token channel:
POST /v1/auth/tokenreturns the ticket in the problem body (no cookie jar there), answered atPOST /v1/auth/token/mfa. An account that still has to enrol is refused withAuth.MfaEnrollmentRequired— enrolling means scanning a QR, which needs the browser. - Self-enrolment (
Optional):POST /v1/auth/me/mfa/enrollmentopens 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/csrftoken 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) andRejectSessionsPredatingMfadrops it after the cutover;TokenIssuer.RefreshAsyncrefuses a refresh token whoseIssuedAtpredates 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 (
IQrCodeRendereroverNet.Codecrete.QrCodeGenerator).
Consequences
- One migration (
AddMfa):identity.MfaChallenges,identity.MfaCodeNonces,identity.MfaEvents,identity.RefreshTokens.IssuedAt. - Porting note:
IIdentityService.ValidateCredentialsAsyncis replaced byCheckPasswordAsync+ClearFailedAttemptsAsync; the login commands returnPasswordSignIn/TokenSignIn. WithMode != Disabled,/sessionand/tokencan answer 401 with the new codes, and after a cutover older sessions and refresh tokens are refused. MfaEventis allowlisted inAssertEveryMultiTenantEntityIsFiltered: itsTenantIdis 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.