Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 12 additions & 0 deletions .changeset/totp-code-accepted-once.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
---
'@objectstack/plugin-auth': patch
---

fix(auth): a TOTP code is accepted once

Clause-②: no
Metadata-protocol: none

A TOTP code that `/api/v1/auth/two-factor/verify-totp` accepts is remembered for its user and secret for as long as the verifier's window could still accept it. Any later presentation of that code is refused, whether it arrives on another sign-in challenge, from another client, on another server node or on the session lane (RFC 6238 §5.2). A code from a time step at or before the last accepted step for that user and secret is refused too. A refused code gets the answer a wrong code gets (`401 INVALID_CODE`) and counts toward the challenge's attempt budget and the account lockout in the same way. The same rule covers the code that confirms an enrolment, and every later code. A code from a later time step is accepted as before.

The spent steps are kept as short-lived `sys_verification` rows, one per step, reserved under a primary key derived from the user, the secret and the step. Two presentations of one code that arrive together therefore admit exactly one, across processes and nodes. A store error while recording a step fails that verification. A client that signs in with TOTP more than once inside the same 30-second step needs the code of a later step for its second sign-in.
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,7 @@ import { createBetterAuthLogSink } from './sso-oidc-refusal-cause.js';
import { withSystemReadContext } from './objectql-adapter.js';
import { refuseSsoAccountOutsideProvenDomain, refuseSsoAccountWithoutAddress } from './sso-account-at-proven-domain.js';
import { refuseSamlAccountKeyContradiction } from './sso-saml-account-key.js';
import { acceptEachTotpCodeOnce } from './totp-code-single-use.js';

/** The path better-auth declares for the email sign-up endpoint. */
const SIGN_UP_EMAIL_PATH = '/sign-up/email';
Expand Down Expand Up @@ -196,7 +197,10 @@ export interface AuthInstanceHost {
export async function createAuthInstance(this: AuthInstanceHost): Promise<Auth<any>> {
const { betterAuth } = await import('better-auth');
const { createAuthMiddleware, setShouldSkipSessionRefresh } = await import('better-auth/api');
const plugins = await this.buildPluginList();
// A TOTP code the two-factor plugin accepts is accepted once (RFC 6238
// §5.2): a before-hook on its `/two-factor/verify-totp`, appended to the
// plugin object in place. See `totp-code-single-use.ts`.
const plugins = await acceptEachTotpCodeOnce(await this.buildPluginList());
// ADR-0069 D2: the verifier refuses a locked account by better-auth's own
// wrong-password path; the WebContainer hasher, when there is one, is the
// pair it wraps (`sign-in-lockout-refusal.ts`).
Expand Down
Loading
Loading