ADR-0174 — A second factor is on once confirmed, and asked for everywhere
Status: Accepted · Date: 2026-10-11 · Track: launch wave 3, api
workstream · Builds on:
ADR-0168,
ADR-0173
Context
There were three defects. The first two were found in wave 2; the third was found while fixing them.
Setup turned 2FA on.
Setup2FAwrote the new secret tousers.twofa_secret, which sign-in reads. A person who started setup and never confirmed it was asked for a code they did not have, and had no recovery codes, because those are issued on confirmation.OAuth skipped the second factor. The OAuth callback issued a session to any account whose provider login succeeded, 2FA or not.
Password sign-in never asked either.
Loginasks whenuser.TwoFASecretis set, butUserRepository.GetByEmail, the read it uses, never selectedtwofa_secret. The field was always empty, so 2FA has never been enforced at password sign-in. The handler's unit tests passed because their mock filled the field.The 2FA management routes answered 404. Setup, verify, disable and the recovery-code routes were registered in the
/api/v1group as/auth/2fa/…. chi sends every/api/v1/auth/*request to the/api/v1/authsub-router, so none of them was reachable: 2FA could never be set up through the API. A live run found it. No handler test could, because handler tests call the handler directly.
Decision
- A pending secret is not the secret. Migration 000049 adds
users.twofa_pending_secret.Setup2FAwrites there and refuses (409) while 2FA is already on.Verify2FAchecks the code against the pending secret, thenConfirmTwoFAmoves it intotwofa_secretin one statement, and only if the pending secret is still the one that was verified (a restarted setup gives 409).- Turning 2FA off clears both columns.
GetByEmailselectstwofa_secret. The field isjson:"-"and is never serialised. Both sign-in paths now see a confirmed second factor.- OAuth gets a pending token, not a session. For an account with 2FA on,
the callback stores the hash of a random 256-bit token
(
twofa_pending_logins) and answers withtwofa_required,twofa_pending_token,twofa_pending_expires(300 s) andtwofa_complete_at.POST /api/v1/auth/login/2fa {pending_token, twofa_token | recovery_code}exchanges it for a session. The route is behind the same lockout as the password sign-in.- Each token allows 5 attempts, counted in the statement that checks it is live. It is spent on success and is useless after 5 minutes.
- Every refusal is the same 401.
- Nothing is recorded as a sign-in until the exchange succeeds.
- With no pending store wired, the callback refuses (403) rather than letting the account through.
- The management routes live in the
/api/v1/authsub-router, in an authenticated group (Bearer, token-scope gate, the API rate limit). A live smoke run against the built server covers them, because a router-level regression is invisible to handler tests. - One check.
checkSecondFactorverifies a TOTP code or spends a recovery code for both sign-in paths, and audits rejected recovery codes.
The data rule for existing secrets
An existing twofa_secret stays on. Nothing ever recorded confirmation, so
the data cannot tell a confirmed secret from an abandoned setup. The two
possible mistakes are not equal:
- Switching off a second factor somebody relies on is a silent security downgrade for the accounts that most wanted protection.
- Leaving an abandoned one on keeps those accounts as they already were. In practice they were never asked for a code (defect 3), and from now on they will be.
So the rule is the safe direction, and the cost is stated. An account that only ever started setup will now be asked for a code it may not have. Such an account recovers through an operator, who clears the factor after verifying identity:
-- Operator, per account, after verifying the person:
UPDATE users SET twofa_secret = NULL, twofa_pending_secret = NULL WHERE user_id = $1;
DELETE FROM twofa_recovery_codes WHERE user_id = $1;
To size the population before deploying, count twofa_secret IS NOT NULL
rows that have no recovery codes. Any secret confirmed since wave 2 has codes.
SELECT COUNT(*) FROM users u
WHERE u.twofa_secret IS NOT NULL
AND NOT EXISTS (SELECT 1 FROM twofa_recovery_codes c WHERE c.user_id = u.user_id);
Consequences
- Because of defect 3, deploying this turns on 2FA prompts for every account that has a stored secret. Run the count above and tell those users before deploying (an owner decision).
- The web sign-in needs a second step after OAuth. The contract is in the wave-3 hand-off.
Source: docs/decisions/0174-a-second-factor-is-on-once-confirmed-and-asked-for-everywhere.md