Skip to content

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.

  • 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.
  1. Ask the person to try each method of the challenge first. A backup code passes when the authenticator is lost.
  2. Ask the person to reset the password if they lost it. A reset does not turn the factor off.
  3. Check who the person is with your identity proofing process. ASVS 5.0 6.4.4 asks for the level of the enrollment.
  4. Record the check: the user id, the time, the operator, and the evidence.
  5. Call the function below with the person’s address. It turns every method of the user off.
The recovery of one user
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";
}
  1. Read the answer. off means the factor is off. was_off means it was off already. no_user means no account has the address.
  2. 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.

The recovery as a test
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");
});
});
  • 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_v8 deletes each TOTP setup that no enable confirmed. A person who began a setup before the deploy gets totp_not_enabled at the enable after it. That person sets up again. This case is not a lost factor.
  • totp_not_enabled: the user has no factor on. The function answers was_off. Tell the person to sign in.

Update 2026-10-21 by plan 12f: the operator tools do step 5 with a record. The text above stays as it was published.

  1. Run authtempo users search <base url> --email <address>. Copy the user id from the ID column.
  2. Run this command for step 5 above. The command needs the reason and the three proof flags.
Terminal window
authtempo users second-factor-off <base url> <user id> --reason "<reason>" --proof-method <method> --proof-reference <case id> --proof-checked-at <time> --yes

The rules of the flags:

  • --proof-method: document, in_person, video, knowledge, or other.

  • --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 knowledge alone is below the level of an enrollment. Use document, in_person, or video.

  • Core checks no proof. The audit row second_factor_disable keeps the three values, and hooks.operator.beforeAction can 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_disabled unless account.notices is 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 users gets operator_required.

  • A bad reason, method, reference, or time gets invalid_body. A user id that names no user gets not_found.

  • A wrong secret gets unauthorized. The 600 calls of an hour give rate_limited.

  • Another change of the user at the same time gets conflict. Run the command again.

  • A before hook of your app gets hook_refused or hook_failed.

  • authtempo users second-factor-off lists each flag and each error.