Second factors
In short
- The threat
A second factor stops someone with the password only if every way into the account asks for it. Replayed and guessed codes, the mailbox, a flow that forgets and the recovery codes all go around it.
- The rule
Codes that work once, guesses counted per account before any comparison, recovery codes kept like passwords, and one seam that refuses every session the factor did not let through (an app’s own re-issues pass it on a best-effort check).
- In the package
The two-factor plugin,
@noctcore/auth/two-factor, with its hook onissueSession.
What goes wrong
Section titled “What goes wrong”A second factor is meant to stop someone who has the password. It only does if every way into the account asks for it. The usual failures are not in the code math but around it:
- Replayed. A six-digit code that stays valid for a minute can be used again by whoever watched it typed or read it from a log, and two requests carrying it at once can both pass a check-then-write guard.
- Guessed. A million codes, three of them valid at any moment, are guessed in hours by a botnet when the only limit is per IP.
- The mailbox beats the phone. A password reset that writes the new password on the link alone, or turns the factor off, hands the account to whoever controls the email.
- Another flow forgets it. Sign-up with auto-login, a magic link, a re-issued session after a password change: any path that mints a session without asking.
- A stolen session turns it off, or prints new recovery codes, because the settings page only asks for the password.
- The recovery codes are the weakest part. They skip the phone, they live on paper or in a password manager, and they are what an attacker with the password goes after.
Every session the library issues is minted by one function, issueSession, and the two-factor plugin's hook runs on every call. A user with a factor who signs in with the right password gets a challenge instead of a session; the challenge lives 5 min and takes 5 codes, and a session comes out of it only after a correct code. A re-issue by the core, after a password change, a re-authentication, an email verification or a confirmed email change, gets through only with the user's own live session, read from the store just before. Any other flow, a plain password login or sign-up with autoLogin, is refused at the seam for a user with a factor.
Text version
The rule
Section titled “The rule”Every way in asks for the factor, every code counts once, and every guess is counted before it is checked.
TOTP, by the RFC, on the server clock
Section titled “TOTP, by the RFC, on the server clock”Codes are RFC 6238 (HMAC-SHA-1, 30-second steps, six digits), so every authenticator app works; the test suite checks the RFC test vectors. One step of drift is accepted either side, never more.
The secret is 160 random bits from the CSPRNG, and it is stored only as the ciphertext your SecretCipher returns. The plugin never holds a key, and the user id is passed to the cipher so it can bind each ciphertext to its row.
A code works once
Section titled “A code works once”The store keeps the last accepted time step, and a code is accepted only if its step moves that value forward, by a compare-and-set in the store. Refused, every time (A036, A037):
- a replayed code;
- an older code after a newer one;
- the slower of two simultaneous requests with the same code.
The spent step is shared by every door: a code used at login cannot then step up a session or reset a password.
Time runs in steps of 30 seconds, and a code matches its own step or one step either side. The store keeps the last accepted step as a mark. A fresh code for the current step is past the mark, so it is accepted and the mark moves to just after it, in one compare-and-set. The same code sent again, or the slower of two requests carrying it at once, is not after the new mark and is refused. An older code, for the step before, sent after a newer one, is refused too.
Text version
Guesses are counted per account, before the comparison
Section titled “Guesses are counted per account, before the comparison”Every code check, TOTP or recovery code, first reserves an attempt in one budget per user, from every IP together: at login, step-up, enrollment, reset, password change or email change.
| Budget | Default | What happens when it runs out |
|---|---|---|
Per user (attempts) |
5 per 15 minutes, from every IP | The factor locks with the password lockout’s capped backoff (5 minutes, then 15, then 45, at most an hour). With throttle.lockout.permanent on, each lock also counts toward the permanent tier (A108). |
| One login challenge | 5 codes | The challenge dies, so every new batch of guesses costs a correct password first (A038). |
A locked factor refuses even the right code, and answers exactly like a wrong one.
The budget is the same wherever the code is typed, so wrong codes while enrolling (confirming a new authenticator) or at a step-up count toward the factor’s lock just as wrong codes at login do. With throttle.lockout.permanent on, those locks count toward the permanent tier too: a user who keeps mistyping the code of a new authenticator can lock their account for good, until an admin clears it. With the lockout’s own ladder (throttle.lockout.durations) set, factor locks climb that ladder instead of the multiplied one.
Enrollment is confirmed by a code
Section titled “Enrollment is confirmed by a code”Starting it needs a fresh password proof. The new secret stays pending until a code from it is typed, within the enrollment window (10 minutes by default, configurable up to 24 hours).
Confirming it is a compare-and-set on that very secret, so a code for a secret that was replaced meanwhile never activates the new one (A046). Turning the factor on mails the owner.
Recovery codes get the care of passwords
Section titled “Recovery codes get the care of passwords”They are the weak link, so:
- Ten codes of 50 bits each from the CSPRNG, shown once, stored only as an HMAC keyed by the server secret and bound to the user.
- Spent by one atomic delete. Of many simultaneous uses, exactly one gets it (A041).
- Replaced in one write. Generating a new set replaces the old one, and needs a step-up.
- Nothing to brute force. Every guess spends the factor budget, so at about 2^47 guesses per hit there is nothing to search (A042).
- The owner hears about it. Each use mails the owner how many codes are left.
Codes carried over are a bridge, not a second format
Section titled “Codes carried over are a bridge, not a second format”An app moving onto the plugin can import its users’ old recovery code hashes and tell the plugin how its old scheme hashed a code (importedRecoveryCodes). Those hashes are often weaker than the plugin’s (an unsalted SHA-256 is not bound to the user), so the bridge is short:
- an imported code is spent once by the same atomic delete and counted in the same budget;
- the first one that passes a login challenge replaces the whole set once the session is issued, with new codes shown once, so the old hashes leave the store (A095);
- two logins racing with two imported codes cannot leave a dead set: only the one that claims the rotation replaces it and shows it (A098).
A scheme with a per-code salt cannot be looked up at all; those users re-enroll.
The mailbox is not a second factor
Section titled “The mailbox is not a second factor”A password reset of an account with a factor demands a TOTP code or a recovery code before the new password is written, and counts it in the same budget. The new password is checked against the policy first, so a refused password spends no code.
The reset never touches the factor: the next login is still challenged (A044).
Every session goes through the check
Section titled “Every session goes through the check”The core mints sessions in one place, issueSession, and runs the plugin’s hook on every call. For any user with a confirmed factor (or one the app requires a factor of) the hook refuses the session unless it comes from:
- the plugin’s own challenge;
- one of the core’s re-issues: after a password change, a re-authentication, an email verification, a confirmed email change or “sign out other sessions”;
- a re-issue under your own
viawhoseinheritis a live session of that user that the plugin accepts (below).
A re-issue needs a live session. It goes through only with a session the core itself read from the store just before the flow revoked anything: live (not revoked, not expired or idle, on the current epoch) and the same user’s. A revoked, expired, hand-built or copied session object is refused.
Your own re-issues need a session the plugin accepts. An app that still runs some of these flows itself issues the replacement under its own via, with the caller’s session as inherit. The core checks that the session is live and the same user’s (or takes the object confirmLiveSession returned before the flow ended sessions), else the issue is refused as session_expired. The plugin then lets it through when that session was created after the factor’s enrollment window closed, or holds a step-up of its own that is still open; anything else is refused until it steps up. Without inherit your via is refused as before. This is a best effort, not a proof that the session descends from a proof of the factor: sessions carry no record of how they began. Two unproved sessions pass it. A session from before the enrollment that one of the core’s re-issues (sign out of other sessions, a re-authentication, an email verification) renews after the window gets a new creation time. A password login that read the factor as still pending while it was being turned on can mint its session, unchallenged, after the window. The core’s own re-issues already let the first through today. Turn the factor on with revokeOtherSessions: true to end both.
Everything else is refused. A plain password login and sign-up with auto-login are refused (A048), and so is any other via a plugin or app code names. A user the app requires a factor of who has none yet gets an enrollment challenge instead of a session.
Your refusals come before the challenge. After a correct password, the core runs the beforeSessionCreate hooks (with beforeChallenge: true) before any challenge is made. An account your hook refuses (a removed member, an account fenced from this surface) gets invalid_credentials, like a wrong password, with no challenge that would say the password was right, and the refusal counts as a wrong password in the lockout, so its next answer cannot tell them apart either (A111). What still differs is the work the hooks do: your own, and the core’s (the email revert lockout’s token read, and the permanent lockout’s read when trusted devices are off).
Your own doors use the same budget. A challenge your app starts for a sign-in it authenticated itself (twoFactor.challenge) is the login’s challenge, and a code it checks without a session (twoFactor.verifyProof) is counted on the factor’s own lock and the plugin’s per-IP limit, like the challenge. Spreading guesses across the doors gains nothing, with a code, a user with no factor (or no user) answers like a wrong code after the same work, and since such a code can never pass, it locks no account (A112).
A standing stamped at issue is the server’s own, and short-lived. With enforcementStanding: 'at-issue', a session issued unrestricted skips the per-request reads on a stamp that is a keyed hash of the user, the version, the revocation epoch and the issue time, so a copied stamp does not verify and every stamp is checked live after maxAge. A refresh or a factor reset ends the user’s sessions in either mode (so a stamp cannot come back after a rollback), a sign-in racing a refresh cannot keep its stamp, and a stamp another hook put on a session is refused (A113).
Trusted code is trusted. What the check cannot judge is code that claims the factor was checked: plugins and callers of auth.context are trusted code, and one that issues with the plugin’s own label, second-factor, gets a session.
Sensitive actions need a recent proof in this session
Section titled “Sensitive actions need a recent proof in this session”A step-up is a code proved in one session, stored under that session’s id with an expiry on the server clock (10 minutes by default). These need one, together with fresh authentication:
- turning the factor off;
- new recovery codes;
- a password change or an email change request without a code.
Without the email change rule, a password alone could move the account to a new address and reset the factor’s protection from there (A052). A session re-issued by a password re-entry starts without one, another session of the same user has none, and a password alone never makes one (A045, A049).
A trusted device skips the code, never the password
Section titled “A trusted device skips the code, never the password”“Remember this device” sets an opaque random token in a cookie and stores only its SHA-256, with an expiry (30 days by default) and the user’s revocation epoch. It counts only after a correct password, at the challenge.
A password change, a reset, an email change or its revert, and “sign out everywhere” bump the epoch, which voids every device trusted before, and also delete the rows (A043). A device never records a step-up.
A revert never removes a factor
Section titled “A revert never removes a factor”Reverting an email change cannot tell the owner’s factor from one an intruder enrolled. If it removed factors by when they were enrolled, whoever reads the old mailbox could revert a change the owner made right after enrolling, and reset the password with no factor in the way (A063).
So every factor stays. The cost falls on an owner whose account an intruder enrolled a factor on: the reset asks for a factor they cannot pass, and support has to verify them some other way and remove it.
Refusals look alike
Section titled “Refusals look alike”The factor is only looked at after a correct password, so a wrong password answers and works the same for accounts with and without one.
Behind a challenge, these all answer invalid_second_factor, and the reason goes to the audit row only (A050):
- an unknown, spent or foreign challenge;
- a wrong code or a replayed code;
- a locked factor.
An unknown challenge and a user without a factor still decrypt a stand-in secret and compute every code in the window, so they cost what a wrong code costs. The test suite checks this by counting store and cipher calls, not by timing (A047).
In code
Section titled “In code”The plugin’s limits written out, and a sign-in that turns a correct password into a challenge.
import type { TwoFactorOptions } from '@noctcore/auth/two-factor';
// The plugin's limits and windows, written out. Pass them to twoFactor()// beside store, cipher and issuer, or leave them out for the same values.export const secondFactor = { // Wrong codes per account, from every IP together, before the factor // locks with the password lockout's capped backoff. attempts: { limit: 5, window: '15m' }, // Code checks per client IP. perIp: { limit: 10, window: '1m' }, // How long a code proved in this session counts for sensitive actions. stepUpWindow: '10m', // "Remember this device": skips the code after a correct password, // never the password and never a step-up. `false` turns it off. trustedDevices: { ttl: '30d', maxPerUser: 5 },} satisfies Partial<TwoFactorOptions>;import type { RequestMeta } from '@noctcore/auth';import type { AppAuth } from '../build/two-factor/auth';
/** A correct password for a user with a factor gives a challenge, never a session. */export async function signIn(auth: AppAuth, email: string, password: string, meta: RequestMeta) { const result = await auth.login({ email, password, meta });
if (!result.ok) return { error: result.code }; if (result.value.kind === 'session') return { session: result.value.session };
// The challenge lives five minutes and takes five codes. return { challenge: result.value.challenge, methods: result.value.methods };}
/** The code from the authenticator app, typed on the next screen. */export async function answer(auth: AppAuth, challenge: string, code: string, meta: RequestMeta) { const result = await auth.twoFactor.verifyChallenge({ challenge, proof: { kind: 'totp', code }, rememberDevice: false, meta, });
// A wrong, replayed or locked code, and a spent or unknown challenge, // all answer `invalid_second_factor`. The reason goes to the audit row. if (!result.ok) return { error: result.code };
return { session: result.value.session };}What it does not do
Section titled “What it does not do”Real-time phishing. A page that relays the password and the code to the real site as the user types them gets a session. Only origin-bound factors (WebAuthn and passkeys) stop that, and they are not part of this plugin yet.
A session that was already stolen. The cookie is the credential once issued; the step-up only guards the actions that need it.
A copied challenge. A challenge is bound to the browser by its user agent, which an attacker who steals the challenge can copy. The binding stops a challenge from being used by accident elsewhere; the real limits are the 5-minute lifetime and the 5 codes.
A fully compromised server. Whoever runs code with your cipher’s key can decrypt the TOTP secret.
Someone with the password enrolling first. For a user you require a factor of who has not enrolled yet, the password is the only credential, so the enrollment challenge accepts it. The owner is mailed when the factor goes on. Enroll required users early, or require a factor only of users who have one. With enforcement and a grace window the same holds after the window: an enrollment-only session can enroll, and the password is all it took to get one (A091).
A stolen session locking the factor. Step-up checks a code from inside a signed-in session, and wrong codes spend the same per-account budget as the login challenge. Whoever holds a session can lock the factor, and with it the owner’s challenge, for up to an hour at a time (the password lockout’s cap), again and again while the session lives. Revoking that session ends it.
Two proofs at once on one challenge. A TOTP code and a recovery code, say, can both be checked before either spends the challenge. Only one gets the session; if the other was a recovery code, that code is spent anyway.
Where the package and the guide differ
Section titled “Where the package and the guide differ”The guide’s MFA page asks for RFC 6238 codes from a 160-bit random secret, a confirmation code before the secret is active, a new secret invalidating the old one, throttling after about 5 failures, and one or more single-use recovery codes. The plugin does all of that, and differs in a few places:
- Replay. The guide validates a code by generating one and comparing; it does not ask that a code be refused the second time. The plugin stores the last accepted step and refuses anything at or before it, atomically.
- How recovery codes are stored. The guide suggests a password hash such as Argon2id. The plugin uses an HMAC keyed by the server secret: the codes are 50 random bits, not something a person chose, so a slow hash adds little, and with the key outside the database a leaked table cannot be used to test guesses at all. The plugin issues ten codes, where the guide says one or more.
- Telling the owner. The guide suggests telling the user to change their password after the throttle trips. The plugin does not mail on a temporary factor lock (it writes an audit row); it mails when the factor is turned on or off and when a recovery code is used. With the password lockout’s permanent tier on, factor locks count toward it, and the permanent lock they bring about mails the owner like a password one.
- Resets. The guide leaves resets to the app. The plugin makes a reset of a two-factor account demand the factor, because otherwise the mailbox alone would beat it.
Attack tests
Section titled “Attack tests”| Test | Attack | Stopped by |
|---|---|---|
| A036 | Replaying a TOTP code in the same time step | plugins/two-factor/proofs.ts, ports/two-factor.ts |
| A037 | Submitting one TOTP code twice at the same moment | plugins/two-factor/proofs.ts, plugins/two-factor/challenge.ts, ports/two-factor.ts |
| A038 | Brute forcing the second factor from many IPs | throttle/throttle.ts, plugins/two-factor/proofs.ts, plugins/two-factor/challenge.ts |
| A039 | Reusing a login challenge | plugins/two-factor/challenge.ts, tokens/one-time-tokens.ts |
| A040 | Using a login challenge as a session or a reset ticket | plugins/two-factor/challenge.ts, tokens/one-time-tokens.ts |
| A041 | Reusing a recovery code | plugins/two-factor/proofs.ts, plugins/two-factor/management.ts, ports/two-factor.ts |
| A042 | Brute forcing a recovery code | plugins/two-factor/recovery-codes.ts, plugins/two-factor/proofs.ts, throttle/throttle.ts |
| A043 | A stolen trusted-device token outliving a password change | plugins/two-factor/trusted-devices.ts, plugins/two-factor/hooks.ts |
| A044 | Resetting the password of a two-factor account without the factor | plugins/two-factor/hooks.ts, flows/password-reset.ts |
| A045 | Turning two-factor off without a second-factor proof | plugins/two-factor/management.ts, plugins/two-factor/step-up.ts |
| A046 | Confirming an enrollment with a wrong or stale code | plugins/two-factor/enrollment.ts, core/auth-core.ts |
| A047 | Telling a wrong code from an unknown challenge or a missing factor by timing | plugins/two-factor/challenge.ts, plugins/two-factor/proofs.ts |
| A048 | Getting a session past the second factor through another flow, an app’s own re-issue included | plugins/two-factor/hooks.ts, plugins/two-factor/challenge.ts, core/auth-core.ts |
| A049 | Acting on a stale or borrowed second-factor proof | plugins/two-factor/step-up.ts, plugins/two-factor/challenge.ts, core/auth-core.ts |
| A050 | Learning account state from second-factor answers | flows/login.ts, plugins/two-factor/challenge.ts, plugins/two-factor/proofs.ts |
| A052 | Changing the email of a two-factor account without the factor | plugins/two-factor/hooks.ts, plugins/two-factor/step-up.ts, flows/email-change.ts |
| A063 | Stripping the owner’s second factor with an email revert | flows/email-change.ts, plugins/two-factor/hooks.ts |
| A073 | Getting a session past a second factor through an invitation | plugins/invitations/accept.ts, plugins/two-factor/hooks.ts |
| A091 | Outliving a two-factor grace window without ever setting a factor up | plugins/two-factor/enforcement.ts, flows/session-validation.ts, core/hooks.ts |
| A095 | Reusing an imported recovery code | plugins/two-factor/proofs.ts, plugins/two-factor/recovery-codes.ts, ports/two-factor.ts |
| A098 | Racing two logins with two imported recovery codes to leave a dead set | plugins/two-factor/proofs.ts, plugins/two-factor/challenge.ts |
| A108 | Never reaching the permanent lockout tier while guessing codes with the password in hand | plugins/two-factor/runtime.ts, core/auth-core.ts, throttle/throttle.ts |
| A111 | Learning that a refused account’s password is right from its second-factor challenge | flows/login.ts, core/auth-core.ts, plugins/two-factor/hooks.ts |
| A112 | Guessing second-factor codes, learning who has a factor, or locking an account with junk codes, through the doors an app calls itself | plugins/two-factor/step-up.ts, plugins/two-factor/challenge.ts, plugins/two-factor/proofs.ts |
| A113 | A session skipping enforcement on a standing stamped at issue after the user came to need it | plugins/two-factor/enforcement.ts, plugins/two-factor/management.ts, session/session-manager.ts |
Further reading
Section titled “Further reading”- Multi-factor authentication in The Copenhagen Book, also by Pilcrow (the auth guide has no page on it).
- RFC 6238: TOTP and RFC 4226: HOTP.
- OWASP Multifactor Authentication Cheat Sheet.