Second factor recovery
For: an operator who answers a person that cannot pass the second factor.
What it does: this guide checks who the person is, then turns their factor off in process. The person then turns a new method on.
Before you start
Section titled “Before you start”- A way to run code in the API’s process, for the in-process API.
- A written process of identity proofing, at the level that the person passed when they turned the factor on. The threat model tells why the level matters.
- Ask the person to try each method of the challenge first. A backup code passes when the authenticator is lost.
- Ask the person to reset the password if they lost it. A reset does not turn the factor off.
- Check who the person is with your identity proofing process. ASVS 5.0 6.4.4 asks for the level of the enrollment.
- Record the check: the user id, the time, the operator, and the evidence.
- Call the function below with the person’s address. It turns every method of the user off.
import { type Auth, normalizeEmail, type StorageAdapter } from "@authtempo/core";
/** * Turns every second factor method of one user off. Call it only after a * check of who the person is, at the level of the enrollment (ASVS 5.0 * 6.4.4). Core has no delayed self-recovery, and a reset never turns the * factor off, so this call is the only way back for a person who lost every * method and every backup code. */export async function recoverSecondFactor( auth: Auth, storage: StorageAdapter, email: string,): Promise<"off" | "was_off" | "no_user"> { // Core stores each address trimmed and in lower case: read it the same way. const address = normalizeEmail(email); if (!address) return "no_user"; // The app holds the storage adapter that it gave to `createAuth`. const user = await storage.findOne("user", [{ field: "email", op: "eq", value: address }]); if (!user) return "no_user"; // `api.totp` is absent with `totp: false` or with no `password`: no factor exists. if (!auth.api.totp) return "was_off"; const r = await auth.api.totp.disable({ userId: user.id }); // The one failure of the disable is `totp_not_enabled`: the factor was off. return r.ok ? "off" : "was_off";}- Read the answer.
offmeans the factor is off.was_offmeans it was off already.no_usermeans no account has the address. - Tell the person to sign in and turn a method on again. The first method answers new backup codes.
The test below turns the factor off and signs in with the password alone.
import { base32Decode, totpCode } from "@authtempo/core/testing";import { describe, expect, it } from "vitest";import { createSecondFactorAuth } from "../guides/second-factor.ts";import { call, harness, signIn } from "../support/harness.ts";import { recoverSecondFactor } from "./second-factor-recovery.ts";
const EMAIL = "ada@example.com";const PASSWORD = "correct horse battery";
describe("second factor recovery", () => { it("turns the factor off, and the next sign-in opens a session with the password alone", async () => { const h = harness(); const auth = createSecondFactorAuth(h); const ada = await signIn(auth, h, EMAIL); await call(auth, "POST", "/auth/password/change", { newPassword: PASSWORD }, { cookie: ada }); const setup = await call( auth, "POST", "/auth/totp/setup", { password: PASSWORD }, { cookie: ada }, ); const { secret } = (await setup.json()) as { secret: string }; await call( auth, "POST", "/auth/totp/enable", { code: await totpCode(base32Decode(secret), new Date()) }, { cookie: ada }, ); const gated = await call(auth, "POST", "/auth/password/sign-in", { email: EMAIL, password: PASSWORD, }); expect("twoFactor" in ((await gated.json()) as object)).toBe(true);
// The operator types the address as the person gives it: case and spaces do not matter. expect(await recoverSecondFactor(auth, h.storage, " ADA@example.com ")).toBe("off"); const open = await call(auth, "POST", "/auth/password/sign-in", { email: EMAIL, password: PASSWORD, }); expect(((await open.json()) as { token?: string }).token?.startsWith("ts_")).toBe(true); expect(await recoverSecondFactor(auth, h.storage, EMAIL)).toBe("was_off"); expect(await recoverSecondFactor(auth, h.storage, "bob@example.com")).toBe("no_user"); });});How it behaves
Section titled “How it behaves”- The disable deletes the TOTP secret, the factor row, the backup codes, the trusted devices, and the live challenges, in one transaction.
- The disable keeps the sessions of the user. A person who lost the factor has no session to end.
- Core has no delayed self-recovery. A recovery always passes through an operator.
- A later release adds operator tools for this step.
- Migration
0008_schema_v8deletes each TOTP setup that no enable confirmed. A person who began a setup before the deploy getstotp_not_enabledat the enable after it. That person sets up again. This case is not a lost factor.
If it fails
Section titled “If it fails”totp_not_enabled: the user has no factor on. The function answerswas_off. Tell the person to sign in.
Update 2026-10-21 by plan 12f
Section titled “Update 2026-10-21 by plan 12f”Update 2026-10-21 by plan 12f: the operator tools do step 5 with a record. The text above stays as it was published.
- Run
authtempo users search <base url> --email <address>. Copy the user id from theIDcolumn. - Run this command for step 5 above. The command needs the reason and the three proof flags.
authtempo users second-factor-off <base url> <user id> --reason "<reason>" --proof-method <method> --proof-reference <case id> --proof-checked-at <time> --yesThe rules of the flags:
-
--proof-method:document,in_person,video,knowledge, orother. -
--proof-reference: the record of step 4 above in your own system, never the evidence. -
--proof-checked-at: the time of the check of step 3 above, at most 7 days old. -
The secret needs the scope
users. Run the operator tools gives the rules of section 26 of the design. -
The method
knowledgealone is below the level of an enrollment. Usedocument,in_person, orvideo. -
Core checks no proof. The audit row
second_factor_disablekeeps the three values, andhooks.operator.beforeActioncan check them. -
Before step 5 above, look for a recent change of the address. A person who took the account can change the address, and then ask for a recovery.
-
The command turns every method of the user off, and keeps the sessions. The user gets the notice
factor_disabledunlessaccount.noticesis false. -
The deployment refuses a check time after its own clock with
invalid_body. -
A user with the factor off stops the command with
totp_not_enabled. Tell the person to sign in. -
A secret with no scope
usersgetsoperator_required. -
A bad reason, method, reference, or time gets
invalid_body. A user id that names no user getsnot_found. -
A wrong secret gets
unauthorized. The 600 calls of an hour giverate_limited. -
Another change of the user at the same time gets
conflict. Run the command again. -
A before hook of your app gets
hook_refusedorhook_failed. -
authtempo users second-factor-offlists each flag and each error.