Multi-factor authentication and passkeys
Passwords alone fail all the time: they're reused, phished, guessed and leaked. Multi-factor authentication (MFA) requires a second, different kind of proof, which blocks the vast majority of account takeovers. And passkeys go further, replacing passwords with cryptography that can't be phished at all.
Factors, ranked
| Method | Phishing-resistant? | Notes |
|---|---|---|
| SMS codes | ✖ | Better than nothing; vulnerable to SIM swapping and phishing |
| Email codes / magic links | ✖ | Only as safe as the email account |
| Authenticator apps (TOTP) | ✖ | Codes can still be typed into a fake site, but far better than SMS |
| Push approval | ✖ (prone to "MFA fatigue") | Add number matching |
| Passkeys / security keys (WebAuthn) | ✔ | Bound to the real website's domain |
How authenticator apps work: TOTP
Google Authenticator, Microsoft Authenticator, 1Password and friends implement TOTP (Time-based One-Time Password, RFC 6238). It's elegant and small:
- At setup, the server creates a random secret and shows it as a QR code; the app stores it.
- Both sides compute
HMAC-SHA1(secret, current 30-second time step)and turn the result into a 6-digit number. - At login, the user types the app's code; the server computes its own and compares.
No network connection is needed: both sides just share a secret and a clock. Here's a complete implementation, checked against the official test values from the RFC:
Setting up and verifying
Secrets are shared as base32 text inside an otpauth:// URL, which is what the QR code contains:
In production, use a library (otplib in Node, pyotp in Python), encrypt the stored secrets, rate-limit attempts (a 6-digit code can be brute-forced otherwise), and give users recovery codes.
Recovery codes
If the phone is lost, the user needs a way back in. Generate about 10 single-use codes at setup, show them once, and store them hashed:
Passkeys: the end of passwords
A passkey is a key pair created by the user's device for one specific website. The private key never leaves the device (it's protected by the fingerprint, face or PIN prompt and usually synced via iCloud Keychain, Google Password Manager or a password manager). The website stores only the public key.
At login:
- The server sends a random challenge.
- The browser asks the device to sign it; the user approves with a fingerprint or face.
- The device signs the challenge together with the website's origin, and the server verifies the signature with the stored public key.
Why that's a big deal:
- Unphishable: the signature includes the real domain. On
myshop-login.examplethe browser simply won't use themyshop.examplepasskey. - Nothing to steal from the server: a breach leaks public keys, which are useless to attackers.
- Nothing to remember or reuse, and it counts as two factors (the device you have + the biometric or PIN).
This is the same cryptography as signing JWTs with a private key and verifying with the public one:
In practice the browser does this through the WebAuthn API (navigator.credentials.create() and .get()), and the server uses a library such as SimpleWebAuthn (Node), py_webauthn or webauthn4j, which checks the challenge, origin, signature counter and attestation for you.
Rolling out MFA
- Offer passkeys first, authenticator apps second, SMS as a last resort.
- Require MFA for admins and anyone with access to money or personal data.
- Ask for MFA again for sensitive actions (changing the email or password, adding a payee): step-up authentication.
- Make recovery secure: account recovery is where attackers go once MFA blocks the front door.
Practice
- Add HOTP test values from RFC 4226 (secret
12345678901234567890, counters 0–2 give755224,287082,359152) to the first example. - Change
verifyto also reject codes if there were five wrong attempts in the last minute. - Explain why a passkey is safe even if the website's database is stolen.
1.What do the server and the authenticator app share in TOTP?
2.Why are passkeys resistant to phishing?
3.How should recovery codes be stored?