Learn to Code
All tracks
Learn to Code

Free lessons from scratch to pro — every language, databases and DevOps.

AboutContactPrivacy PolicyTerms of Use

Your progress is saved in this browser. © 2026 Learn to Code

SEC
Authentication & Security
0/10 completed
Beginner
Authentication vs authorizationHashing passwords
Intermediate
Sessions and cookiesJSON Web TokensOAuth 2.0 and OpenID ConnectMulti-factor authentication and passkeys
Advanced
The OWASP Top 10XSS, CSRF and CORSSecrets management and encryption
Pro
Security in practice
Tracks / Authentication & Security / Intermediate

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:

  1. At setup, the server creates a random secret and shows it as a QR code; the app stores it.
  2. Both sides compute HMAC-SHA1(secret, current 30-second time step) and turn the result into a 6-digit number.
  3. 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:

Node.js · editable

Setting up and verifying

Secrets are shared as base32 text inside an otpauth:// URL, which is what the QR code contains:

Node.js · editable

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:

Node.js · editable

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:

  1. The server sends a random challenge.
  2. The browser asks the device to sign it; the user approves with a fingerprint or face.
  3. 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.example the browser simply won't use the myshop.example passkey.
  • 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:

Node.js · editable

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

  1. Add HOTP test values from RFC 4226 (secret 12345678901234567890, counters 0–2 give 755224, 287082, 359152) to the first example.
  2. Change verify to also reject codes if there were five wrong attempts in the last minute.
  3. Explain why a passkey is safe even if the website's database is stolen.
✎ Check your understanding

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?

← OAuth 2.0 and OpenID Connect