Siti opens School Portal on her phone, taps "Sign in with a passkey", touches the sensor, and she's in. No password typed, no SMS code to wait for.
What she just used is a passkey: a way to log in without a password, built on the WebAuthn standard. It is a pair of keys: a private key that is never sent to the server, and a public key the server stores. Logging in means your device signs a fresh challenge with the private key, and the server checks the signature with the public key. Picture a personal stamp and a sample of its print. You keep the stamp yourself, the school keeps only a sample print. To prove it's you, the school hands you a new sheet with a random number of the day, you stamp it, and the school compares it with the sample. Nobody can make the stamp from the sample print.
Two ceremonies: register and log in
WebAuthn calls its two flows ceremonies: registration (registering a passkey) and authentication (logging in with it). There are three parties: the School Portal server (called the relying party, with the rpId portal.school.test), the browser, and the authenticator, the part of the device that holds the private key and asks for a fingerprint or PIN.
Both start with a challenge: 32 random bytes from the server, kept in the session, valid for a short time (for example 60 seconds), and usable only once. A challenge that is always new means an old signature can't be used again.
Registering, step by step
Siti registers a passkey from her phone. This is what happens, exactly as in the Lab:
- The server makes a challenge and sends the registration options.
- The browser calls
navigator.credentials.create. The phone's authenticator asks for Siti's fingerprint, creates a new ECDSA P-256 key pair, and gives it a random credential ID. - The browser sends back the credential ID, the public key,
clientDataJSON, andauthenticatorData. - The server checks them, then stores the credential.
const credential = await navigator.credentials.create({
publicKey: {
challenge, // a 32-byte Uint8Array from the server
rp: { id: "portal.school.test", name: "School Portal" },
user: { id: userIdBytes, name: "[email protected]", displayName: "Siti" },
pubKeyCredParams: [{ type: "public-key", alg: -7 }], // -7 = ES256 (ECDSA P-256)
authenticatorSelection: { residentKey: "required", userVerification: "required" },
timeout: 60000, // milliseconds
},
});
The pubKeyCredParams list in this example holds only -7. web.dev advises two, -7 (ES256) and -257 (RS256, an RSA key), because the two together cover every authenticator: [{ type: "public-key", alg: -7 }, { type: "public-key", alg: -257 }]. Your server must then be able to check both kinds of key. SimpleWebAuthn (see the end of this lesson) accepts both by default.
clientDataJSON is written by the browser, not by the page: {"type":"webauthn.create","challenge":"...","origin":"https://portal.school.test","crossOrigin":false}. The server checks four things before storing anything:
| Check | Must equal |
|---|---|
type |
"webauthn.create" (when logging in: "webauthn.get") |
challenge |
the challenge kept in the session |
origin |
an allowed origin: https://portal.school.test |
the first 32 bytes of authenticatorData (the rpIdHash) |
SHA-256 of portal.school.test, dee9eac9... |
It also checks the flags in authenticatorData: UP (user present, a touch happened) and UV (user verified, a fingerprint or PIN).
Logging in, step by step
- The server makes a new challenge.
- The browser calls
navigator.credentials.get({ publicKey: { challenge, rpId: "portal.school.test", userVerification: "required" } }). Siti touches the sensor. - The authenticator signs
authenticatorDatajoined with the SHA-256 ofclientDataJSON, using the private key. - The server looks the credential up by its credential ID, checks
type, the challenge, the origin, the rpIdHash, and the flags, then verifies the signature with the stored public key. A match means it's Siti, and the server creates an ordinary session.
No secret is sent. All that crosses the network is a signature for one challenge, which is useless for the next one.
Other options to start the login:
| Way | When to pick it |
|---|---|
| A "Sign in with a passkey" button (Siti's example above) | navigator.credentials.get is called when the button is pressed, then the browser shows a window to choose a passkey. The simplest. |
| Autofill (conditional UI) | while some users still use passwords. The passkey appears among the suggestions of the email field, next to saved passwords. Give the input autocomplete="username webauthn", then call navigator.credentials.get with mediation: "conditional" as soon as the login page opens. Check (await PublicKeyCredential.getClientCapabilities()).conditionalGet first. |
What the server stores
One row per passkey:
| Column | Holds |
|---|---|
credential_id |
the random ID from the authenticator |
public_key |
the P-256 public key |
sign_count |
the last counter value |
backed_up |
whether this passkey is synced |
user_id |
its owner, Siti (1) |
No password, no password hash, no private key. A public key can only check a signature, it can't make one. So if School Portal's database leaks, Siti's passkey table included, what's in it isn't enough to log in as Siti.
Origin and rpId
A passkey is bound to its rpId. The browser offers the portal.school.test passkey only to pages on that domain or its subdomains, and writes the calling page's origin into clientDataJSON. A page on another domain can't even ask for the signature, which is why passkeys resist phishing.
The server still checks the origin against an allowed list. In the Lab, the page https://staging.portal.school.test may use the rpId portal.school.test (it's a subdomain), so the browser agrees. But the production server only allows https://portal.school.test, so the request is rejected at the origin check.
Compared with a password plus an OTP code by SMS, a passkey closes three holes at once: there is no code to type into a fake site, no SMS to redirect through a SIM swap (the phone number moved to another SIM card), and the server stores no secret that could be stolen.
Synced passkeys and the counter
Modern passkeys are synced: Google Password Manager, iCloud Keychain, or 1Password copy the private key, encrypted, to the other devices on the same account. Siti registers from her phone, and her laptop can log in straight away with the same credential ID and public key, without registering again. The BE (backup eligible) and BS (backed up) flags in authenticatorData tell the server the passkey is synced.
The signature counter (signCount) is sent with every login. A USB security key raises it on each use: 1, 2, 3. If the number that arrives isn't bigger than the stored one, the server suspects the key was cloned. Synced passkeys generally always send 0, because several devices share the same key. The rule: if the old and the new number are both 0, accept. Otherwise the new number must be bigger.
The exercise: three functions
The exercise imitates the server's part with simple objects:
createChallenge(userId, operation)returns{ challenge, userId, expiresIn: 60, type }.typeis the realclientDataJSONvalue:"webauthn.create"for"register","webauthn.get"for"authenticate". The challenge is 32 random bytes fromcrypto.getRandomValues(new Uint8Array(32)), written as hex (64 characters), so it can't be guessed and differs every time. A real server also keeps it in the session and deletes it once used.isValidPublicKey(publicKey): the public key is imitated as a string. It is valid when it is a string (check this first), at least 10 long, and starts with"pk_". Sopk_abc123xyzis valid, whilenot_a_keyandpk_short(8 characters) are not.matchCredential(credentialId, allowedCredentials)looks for a credential with the sameidandtype === "public-key"(the real type value in WebAuthn), then returns that object, ornull.
📌 The libraries
Don't parse authenticatorData and the COSE format yourself. Laravel has the Laragear WebAuthn package, plain PHP has web-auth/webauthn-lib, and Node has SimpleWebAuthn (@simplewebauthn/server and @simplewebauthn/browser). You still decide the rpId, the allowed origins, and the counter rule.
⚠️ Passkeys that turn away their owner, or let someone else in
- A challenge that can be guessed or reused. Make 32 random bytes per ceremony, keep them in the session, delete them once used.
- No origin check, or the wrong rpId. The list of allowed origins must be exact, including
https://and with no trailing slash. - Rejecting a counter of 0. Synced passkeys always send 0. Reject only when the new number doesn't rise although it was once above 0.
- A single passkey and no backup. Allow several passkeys per user and offer account recovery (the Account Recovery & Lockout lesson).
TL;DR: A passkey: the private key stays on the device, the public key on the server. Registering: the server gives a random challenge, the authenticator creates an ES256 key pair, the server checks type webauthn.create, the challenge, the origin, and the rpIdHash, then stores the credential ID, public key, counter, and backup status, with no secret at all. Logging in: a new challenge, the authenticator signs it, the server verifies with the public key and checks webauthn.get, the origin, the rpIdHash, and the counter (0 and 0 is fine). A passkey is bound to its rpId, so it resists phishing, and it can sync to other devices through Google Password Manager or iCloud Keychain. In the exercise: a challenge of 32 random bytes written as hex (64 characters), expiresIn 60, a pk_ key of at least 10 characters, and the credential whose type is public-key, or null.
⚡ The private key on the device, the public key on the server
Meaning: A passkey is a pair of keys. The private key is never sent to the server, the public key is stored by the server. Logging in means your device signs a fresh challenge with the private key, and the server checks the signature with the public key. Like a personal stamp: you keep the stamp, the school keeps only a sample print.
⚡ Registering a passkey
Write:
const credential = await navigator.credentials.create({
publicKey: {
challenge, // a 32-byte Uint8Array from the server
rp: { id: "portal.school.test", name: "School Portal" },
user: { id: userIdBytes, name: "[email protected]", displayName: "Siti" },
pubKeyCredParams: [{ type: "public-key", alg: -7 }], // -7 = ES256 (ECDSA P-256)
authenticatorSelection: { residentKey: "required", userVerification: "required" },
timeout: 60000, // milliseconds
},
});
Meaning: The challenge is 32 random bytes from the server, valid for a short time, and usable only once. Before storing anything, the server checks type (webauthn.create), the challenge, the origin, and the rpIdHash.
⚡ If the database leaks
Guess first: the Portal's database leaks, Siti's passkey table included. Is what's in it enough to log in as Siti?
No. All that's stored is the credential ID, the public key, the counter and the backup status. A public key can only check a signature, it can't make one.
Meaning: A passkey is bound to its rpId. The browser offers it only to pages on that domain, so a fake site can't even ask for the signature. That's why passkeys resist phishing.