Skip to main content
← CONCEPTS / SECURITY & IDENTITY
An end-to-end domain learning journey

Passkeys, WebAuthn &
FIDO2 Passwordless Auth.

WebAuthn/FIDO2 replaces passwords with per-site asymmetric keypairs generated inside a user's hardware authenticator, where private keys never leave the device and signatures are cryptographically bound to the exact Relying Party origin.
Start the journey7 sections
FIRST PRINCIPLES. PRODUCTION SCALE.
The idea to keep with you

WebAuthn/FIDO2 replaces passwords with per-site asymmetric keypairs generated inside a user's hardware authenticator, where private keys never leave the device and signatures are cryptographically bound to the exact Relying Party origin.

TL;DR30-second simplified summary
▾

Traditional passwords and SMS/TOTP one-time codes are shared secrets: if a user is tricked into typing them into an adversary-in-the-middle (AiTM) phishing proxy, the attacker replays the credentials against the real site in real time. WebAuthn and FIDO2 eliminate shared secrets by using per-site asymmetric keypairs (typically ES256 / P-256 or Ed25519) anchored in a hardware Secure Enclave, TPM, or roaming security key.

During registration (navigator.credentials.create()), the Relying Party (RP) server issues a fresh cryptographic challenge and its rpId (e.g., app.example.com). The authenticator verifies user presence/biometrics locally, generates a unique keypair scoped to that rpId, retains the private key inside hardware, and returns the public key and credentialId to the server.

During authentication (navigator.credentials.get()), the server sends a one-time random challenge; the browser constructs clientDataJSON containing the exact active TLS origin, and the authenticator signs sha256(authenticatorData) || sha256(clientDataJSON). Because the signature binds both the server challenge and the browser-verified origin, a phishing domain like app-example.com produces an invalid signature that the real RP immediately rejects.

Key trade-offs and failure modes: synced passkeys (iCloud Keychain, Google Password Manager) solve device-loss lockouts via end-to-end encrypted key sync, but often report signCount = 0 rather than monotonically incrementing counters—so servers must not blindly reject signCount == 0 when backupEligible is set. Servers must also enforce single-use challenge TTLs and strict rpIdHash checks.

From patterns to decisions.

Understand how WebAuthn registration and assertion ceremonies work, why origin binding defeats phishing proxies, and how to handle signCount cloning and multi-device passkey recovery.

Full walkthrough · 7 stages
01
STAGE 01 · THREAT MODELfoundation

Why Passwords and TOTP Fail Against Reverse-Proxy Phishing.

Even with 6-digit TOTP or SMS codes, an Adversary-in-the-Middle (AiTM) reverse proxy sits between the victim and the legitimate login page, forwarding the username, password, and OTP in real time and stealing the resulting session cookie.

FIDO2/WebAuthn defeats AiTM phishing because the browser enforces origin binding: the authenticator signs the TLS origin observed by the browser (https://evil-login.com), which never matches the legitimate Relying Party's expected origin (https://example.com).

02
STAGE 02 · REGISTRATIONfoundation

The WebAuthn Registration Ceremony (Attestation).

  1. Server Challenge: RP generates a 32-byte CSPRNG challenge, stores it in short-lived state (e.g., Redis with 5-minute TTL), and specifies rp.id, user.id, and allowed algorithms (-7 ES256, -8 EdDSA).
  2. Browser & Authenticator: navigator.credentials.create() invokes the platform authenticator (Touch ID, Windows Hello, Android Biometrics) or roaming key.
  3. Server Verification: RP verifies the challenge matches and has not expired, validates rpIdHash == sha256(rp.id), checks UP (User Present) and UV (User Verified) flags, and stores (credentialId, publicKey, signCount).

WebAuthn Registration Data Flow

RP ServerBrowser APISecure EnclaveRP Credential DB
03
STAGE 03 · AUTHENTICATIONintermediate

Assertion Verification, Origin Binding & Replay Protection.

When signing in, navigator.credentials.get() returns authenticatorData, clientDataJSON, and signature.

The RP server must verify all of the following in order:

  • Challenge: clientDataJSON.challenge matches the stored single-use challenge (and delete it immediately to prevent replay).
  • Origin & Type: clientDataJSON.type === 'webauthn.get' and clientDataJSON.origin matches your exact allowlisted HTTPS origin.
  • RP ID Hash: First 32 bytes of authenticatorData equal sha256(expectedRpId).
  • Signature: Verify signature over authenticatorData || sha256(clientDataJSON) using the stored COSE public key.
04
STAGE 04 · PRODUCTION CODEintermediate

Safe Challenge Lifecycle & Assertion Verification with Atomic Consumption.

Always consume challenges atomically (GETDEL in Redis) so concurrent replay attempts fail immediately, and handle synced passkeys (signCount == 0) properly:

import hashlib, json, base64

def verify_assertion(redis_client, session_id, assertion, stored_cred, expected_origin, expected_rp_id):
    # 1. Atomically consume single-use challenge (prevents replay race conditions)
    expected_challenge = redis_client.getdel(f'webauthn:chal:{session_id}')
    if not expected_challenge:
        raise AuthError('Challenge missing, expired, or already consumed')

    client_data = json.loads(base64.urlsafe_b64decode(assertion['clientDataJSON'] + '=='))
    if client_data.get('type') != 'webauthn.get' or client_data.get('origin') != expected_origin:
        raise AuthError('Origin or ceremony type mismatch (possible phishing proxy)')
    if client_data.get('challenge') != expected_challenge:
        raise AuthError('Challenge mismatch')

    auth_data = base64.urlsafe_b64decode(assertion['authenticatorData'] + '==')
    if auth_data[:32] != hashlib.sha256(expected_rp_id.encode()).digest():
        raise AuthError('RP ID hash mismatch')

    # 2. Verify cryptographic signature over auth_data || sha256(clientDataJSON_raw)
    if not verify_cose_signature(stored_cred.public_key, auth_data, assertion['clientDataJSON'], assertion['signature']):
        raise AuthError('Invalid WebAuthn signature')

    # 3. Check signCount (only flag cloning when both stored & new counters are > 0)
    new_count = int.from_bytes(auth_data[33:37], 'big')
    if stored_cred.sign_count > 0 and new_count > 0 and new_count <= stored_cred.sign_count:
        raise SecurityAlert('Potential cloned hardware authenticator detected')
    stored_cred.update_sign_count(new_count)
05
STAGE 05 · DISCOVERABLE CREDENTIALSintermediate

Resident (Discoverable) Keys vs. Enterprise Hardware Keys.

  • Synced Passkeys (Multi-Device): Stored in iCloud Keychain, Google Password Manager, or 1Password; BE (Backup Eligible) and BS (Backed Up) flags are 1. Great for consumer UX because losing a phone does not lock out the account.
  • Device-Bound Keys (Single-Device): YubiKeys or enterprise TPMs where BE=0. Required for high-assurance government/fintech compliance where keys must never leave physical hardware.
06
STAGE 06 · RECOVERYadvanced

Preventing Lockout Without Weakening Security.

If a user registers a single hardware key and loses it, falling back to an unverified SMS reset reintroduces SIM-swap risk.

Best practices:

  1. Encourage 2+ passkeys (e.g., platform passkey + backup security key or synced password manager).
  2. Use WebAuthn Conditional UI (mediation: 'conditional') for one-tap autofill login.
  3. Gate recovery with high-entropy recovery codes or identity-verified re-enrollment delays.
07
STAGE 07 · GOTCHASadvanced

Subdomain rpId Scope, signCount=0 False Positives & Browser Quirks.

  • rpId Domain Mismatch: Setting rpId to login.example.com prevents credentials from working on app.example.com. Use the registrable domain (example.com) if passkeys span subdomains—but never a public suffix.
  • False Cloned-Key Lockouts: Multi-device synced passkeys intentionally emit signCount = 0. Always check stored_count > 0 && new_count > 0 before flagging a clone.
Practice · Explain the consequences

Practice: Make the trade-off yourself.

Question 1 of 3
Score: 0/3

An employee clicks a link to `https://corp-sso-login.com` (an AiTM reverse proxy for `https://sso.corp.com`) and taps their FIDO2 passkey. Why does the login fail?

Recommended Learning Flow