MFA, covering TOTP, email codes, recovery codes, step-up & passkeys (WebAuthn)
How TOTP works (RFC 6238), encrypting TOTP secrets, recovery codes, trusted devices, step-up authentication for sensitive actions, and phishing-resistant passkeys.
Factors
Something you know (a password), something you have (a phone or security key), something you are (biometrics). MFA combines at least two.
TOTP (authenticator apps)
Enrolment: server generates a random secret K, shown to the user as a QR code
otpauth://totp/SperoTek:jane@acme.com?secret=BASE32K&issuer=SperoTek
Login: code = Truncate( HMAC-SHA1(K, floor(unixTime / 30)) ) mod 10^6
Phone and server compute the same 6 digits from the shared secret and the current 30-second window. Accept ±1 window to allow for clock drift, and reject reuse of a code within its window.
The secret is as sensitive as a password, but you must be able to read it to verify codes. So you encrypt it (AES-GCM) rather than hash it.
Recovery codes
These are single-use backup codes, shown once. Store them hashed, like passwords, and mark each one as used.
Step-up authentication
Already logged in? You can still require fresh MFA for dangerous actions: changing a password, adding a payout account, transferring company ownership, creating API tokens. The step-up proof is short-lived (minutes) and scoped.
Passkeys / WebAuthn
Register: browser -> authenticator (Touch ID, Windows Hello, YubiKey) creates a key pair
for YOUR domain only; the server stores the PUBLIC key + credential id
Login: server sends a random challenge; the authenticator signs it with the private key;
the server verifies with the stored public key
- Phishing-resistant. The key is bound to the origin, so
sperotek-login.evil.comcan’t use it. - No shared secret on the server: a DB leak exposes only public keys.
- It can replace the password entirely (passwordless).
In your projects
mfa/* · TotpSecretCipher · passkey/* · WebAuthnManagerConfig · StaffMfaSetupFilter · @RequireStepUpMfa- TOTP via
dev.samstevens totp, with secrets encrypted byTotpSecretCipher(AES/GCM/NoPadding, a rotatable key ring). - Backup codes, trusted devices (skip MFA on a known device for N days), and OTP for phone or email.
- Passkeys via
webauthn4j. - Staff must enrol MFA.
StaffMfaSetupFilterblocks everything else until they do (StaffMfaPolicyIntegrationTest). @RequireStepUpMfaon sensitive endpoints, with Authsignal available for risk-based challenges.
TotpCodes · MfaChallengeService · MfaRecoveryCode · MfaStepUpGrant · StepUpMfaAspect · PasskeyService- TOTP (java-otp), email codes (
mfa-codetemplate) and recovery codes. - Step-up:
@RequireStepUpMfaandStepUpMfaAspectrequire anX-Step-Up-Tokenheader carrying a short-livedMfaStepUpGrant. It’s used for things like transfer ownership and API-token creation. - Role-required 2FA:
app.access.enforce-role-2faforces MFA for privileged roles. - Passkeys through Yubico
webauthn-server-core.
templates/email/two-factor-verification.htmlRM has an email-based 2FA template. Email codes are weaker than TOTP (the email account itself can be compromised) but easy to adopt.
Strength ranking
| Method | Phishing-resistant? | Notes |
|---|---|---|
| SMS codes | ❌ | SIM-swap attacks; NIST “restricted” |
| Email codes | ❌ | Only as strong as the email account |
| TOTP | ❌ (codes can be relayed by phishing proxies) | Good baseline |
| Push approval | ❌ (MFA fatigue) | Use number matching |
| Passkeys / FIDO2 | ✅ | Best |
Related concepts you must know
- Rate-limit MFA verification. A 6-digit code has only a million possibilities.
- Recovery is the weak link. Attackers target “lost my phone” flows, so make recovery strict.
- Device binding vs synced passkeys. iCloud and Google sync passkeys across devices, while hardware keys are device-bound.
- Adaptive / risk-based MFA. Challenge only when signals look unusual (new device, new country).