Full Stack & Backend Engineer Guide: Passkeys (WebAuthn / FIDO2) & Phishing-Resistant Auth
SMS OTPs are vulnerable to SIM-swapping and toll-fraud attacks, while passwords and TOTP codes can be intercepted by real-time reverse-proxy phishing kits. Passkeys (WebAuthn / FIDO2) eliminate shared secrets entirely by binding an asymmetric public/private keypair to the exact browser origin (`rpId`) inside the user's Secure Enclave, TPM, or synced credential manager. As a Senior Full Stack Engineer and Backend Engineer, here is how I implement standards-compliant WebAuthn registration, login, and step-up transaction signing in Python and TypeScript.
1. Cryptographic Challenge Generation & Strict Origin Verification
A secure WebAuthn relying party (RP) must generate a cryptographically random 32-byte challenge with a short 5-minute TTL in Redis, consume it atomically upon verification to prevent replay attacks, and validate the `rp_id_hash`, `origin`, and signature counter (`sign_count`):
import secrets
from django.core.cache import cache
from webauthn import (
generate_authentication_options,
verify_authentication_response,
)
from webauthn.helpers.structs import PublicKeyCredentialRequestOptions
RP_ID = "aldianfazrihady.com"
EXPECTED_ORIGIN = "https://www.aldianfazrihady.com"
def create_passkey_login_challenge(session_id: str) -> PublicKeyCredentialRequestOptions:
challenge = secrets.token_bytes(32)
cache.set(f"webauthn:auth_challenge:{session_id}", challenge, timeout=300)
return generate_authentication_options(
rp_id=RP_ID,
challenge=challenge,
)
def verify_passkey_assertion(
session_id: str,
credential_json: str,
stored_public_key: bytes,
stored_sign_count: int,
) -> int:
expected_challenge = cache.get(f"webauthn:auth_challenge:{session_id}")
cache.delete(f"webauthn:auth_challenge:{session_id}")
if not expected_challenge:
raise ValueError("Expired or already-consumed WebAuthn challenge.")
verification = verify_authentication_response(
credential=credential_json,
expected_challenge=expected_challenge,
expected_rp_id=RP_ID,
expected_origin=EXPECTED_ORIGIN,
credential_public_key=stored_public_key,
credential_current_sign_count=stored_sign_count,
require_user_verification=True,
)
return verification.new_sign_count
2. Conditional UI (`autocomplete="webauthn"`) & Multi-Device Passkey Sync
On the React/Next.js frontend, pairing `navigator.credentials.get({ mediation: "conditional" })` with an `` field lets users sign in with Touch ID, Face ID, or Windows Hello directly from the browser autofill prompt in a single tap.
3. Hire a Senior Full Stack & Backend Engineer for Passkey Auth
Upgrading your authentication stack to Passkeys, OAuth 2.1, or enterprise SSO? Explore my Full Stack Engineer Pillar Guide, review my OAuth 2.1 & PKCE Architecture Guide, or book a 1-Day Security & Auth Sprint.