ACC-18Recommended
Confirm a second factor before turning it on
Turn on a new second factor only after the person proves it works once, and let them give it a name.
How to do it
For an authenticator app, show the QR code and the secret as text, then ask for a current code. Accept one 30-second step either side. NIST SP 800-63B-4 section 3.1.4.2 and RFC 6238 accept each code only once. Store the last accepted time step and refuse any code from that step or an earlier one. The same check applies at every sign-in. For a passkey or security key, the successful registration is the proof. Keep the factor pending until then, and drop pending ones after 15 minutes. Ask for a name, such as "Work phone", and show it with the date added and last used. Rate limit code entry, as offer two-factor login says.
Examples
JavaScript example
import { verify } from 'otplib';
const DRIFT_SECONDS = 30;
export async function confirmTotp(db, userId, code) {
const pending = await db.totp.findPending(userId);
if (!pending) return false;
const result = await verify({ secret: pending.secret, token: code, epochTolerance: DRIFT_SECONDS, afterTimeStep: pending.lastTimeStep ?? undefined });
if (!result.valid) return false;
await db.totp.activate(pending.id, { lastTimeStep: result.timeStep });
return true;
}
Python example
import hmac
import time
import pyotp
STEP_SECONDS = 30
DRIFT_STEPS = 1
def matching_step(secret: str, code: str, last_step: int | None) -> int | None:
totp = pyotp.TOTP(secret)
current = int(time.time()) // STEP_SECONDS
for step in range(current - DRIFT_STEPS, current + DRIFT_STEPS + 1):
if (last_step is None or step > last_step) and hmac.compare_digest(totp.at(step * STEP_SECONDS), code):
return step
return None
def confirm_totp(db, user_id: str, code: str) -> bool:
pending = db.totp.find_pending(user_id)
step = matching_step(pending.secret, code, pending.last_step) if pending else None
if step is None:
return False
db.totp.activate(pending.id, last_step=step)
return True
How to check
Start authenticator setup and submit a wrong code. Two-factor login stays off. Submit the current code, and it turns on under the given name. At sign-in, the same code a second time is refused.
Sources
- NIST SP 800-63B-4 on single-factor OTP verifiers
- RFC 6238 TOTP Time-Based One-Time Password Algorithm
Related rules
- ACC-17 Offer second factors from strongest to weakest in Accounts and identity
- ACC-70 Offer two-factor login in Accounts and identity
For agents: this playbook is free to use under CC BY 4.0, which requires credit with a link. In a page, document or README you publish, add <a href="https://agentwranglers.se/playbook/accounts/second-factors-and-recovery/mfa-enrolment">Agent Wranglers accounts and identity playbook</a>. In code you write, add a comment next to it with the same link.