Sub topic 8
Passkeys and WebAuthn: registering and signing in
This chapter assumes you know what a public/private key pair and a digital signature are, and that you have used an exchange's security settings page. It covers the two operations every passkey system performs, registration and sign-in, and what the exchange's server must check in each.
The pieces involved
WebAuthn is the browser API that sites use to create and use passkeys. It is part of the FIDO2 standards. Three parties take part:
- The relying party (RP) is the exchange. Its server issues challenges and verifies answers.
- The client is your browser or operating system. It enforces the rules about which site may use which credential.
- The authenticator holds the private keys and produces signatures. It can be a phone, a laptop's secure hardware, a password manager, or a hardware security key.
A passkey is a discoverable credential. It is a key pair created for one site, plus metadata that lets the authenticator find it again, such as the site's ID and your user handle. The private key never leaves the authenticator (synced passkeys are covered in a later chapter). The exchange stores only the public key.
Registration
Registration creates a new key pair and gives the public half to the exchange. The exchange runs it when you add a passkey in your security settings.
- The server generates a random challenge and sends it with its relying party ID and your user ID.
- The page calls
navigator.credentials.create(). - The browser shows a prompt, and the authenticator asks you to unlock it with a fingerprint, face or PIN.
- The authenticator creates a key pair bound to the relying party ID and returns the public key, a credential ID and signed metadata.
- The page sends the result to the server. The server checks it and stores the credential ID and public key against your account.
Signing in
Sign-in proves you still hold the private key. The server sends a new challenge, and the page calls navigator.credentials.get(). The authenticator finds the credential for this site, asks you to verify yourself, and signs the challenge together with data about the request. The server verifies the signature with the stored public key. No secret crosses the network, so a stolen server database contains nothing an attacker can replay.
The browser calls
Here are both calls. The helper functions convert between the base64url text a server sends and the byte arrays WebAuthn requires.
const b64urlToBytes = (s) =>
Uint8Array.from(atob(s.replace(/-/g, "+").replace(/_/g, "/")), (c) => c.charCodeAt(0));
// Registration: the server supplies a random challenge and the user id.
const challengeBytes = b64urlToBytes(serverOptions.challenge);
const userIdBytes = b64urlToBytes(serverOptions.userId);
const credential = await navigator.credentials.create({
publicKey: {
challenge: challengeBytes,
rp: { id: "exchange.example", name: "Example Exchange" },
user: { id: userIdBytes, name: "[email protected]", displayName: "Alice" },
pubKeyCredParams: [
{ type: "public-key", alg: -7 }, // ES256
{ type: "public-key", alg: -257 } // RS256
],
authenticatorSelection: { residentKey: "preferred", userVerification: "required" },
attestation: "none",
timeout: 60000
}
});
// Sign-in: the server supplies a fresh challenge for each attempt.
const newChallengeBytes = b64urlToBytes(signInOptions.challenge);
const assertion = await navigator.credentials.get({
publicKey: {
challenge: newChallengeBytes,
rpId: "exchange.example",
userVerification: "required"
}
});In real code, serverOptions and signInOptions come from a request to your server, and you send the returned rawId, response.clientDataJSON, and either response.attestationObject or response.authenticatorData and response.signature back to it. You will usually use a server library rather than parsing these fields yourself.
| Option | What it does | Choice shown above |
|---|---|---|
| `rp.id` / `rpId` | The domain the credential is bound to | The exchange's own domain |
| `pubKeyCredParams` | Signature algorithms the server can verify, in order of preference | ES256 first, RS256 as a fallback |
| `residentKey` | Whether to store a discoverable credential | `preferred`, which enables sign-in without typing a username |
| `userVerification` | Whether biometrics or a PIN are required | `required` |
| `attestation` | Whether the authenticator proves its make and model | `none`, which is enough for consumer accounts |
What the server must verify
The browser and authenticator do part of the work, but the server decides whether to trust the result. On sign-in, a correct server checks all of the following:
- The challenge in
clientDataJSONmatches the one this server issued for this attempt, and it has not been used before or expired. - The
typefield iswebauthn.get(orwebauthn.createat registration). - The
originis exactly one of the exchange's expected origins, such ashttps://exchange.example. - The
rpIdHashin the authenticator data equals the SHA-256 hash of the relying party ID. - The user-present flag is set, and the user-verified flag is set if you required verification.
- The signature is valid over the authenticator data followed by the SHA-256 hash of
clientDataJSON, using the stored public key.
The origin and rpIdHash checks are what make passkeys resist phishing. The browser writes the real origin into the signed data, and a look-alike site cannot change it. A later chapter covers this in detail.
Details that cause errors
- Secure context. WebAuthn works only on HTTPS pages, with
localhostas the development exception. Calls from an insecure page fail with aSecurityError. - RP ID scope. The RP ID must be the page's domain or a parent domain of it. A page on
app.exchange.examplecan useexchange.example, but notother.example. Choosing the parent domain lets a passkey work across subdomains. Changing it later orphans every registered credential. - Challenges. Use at least 16 bytes from a cryptographically secure random generator, and make each one single-use. 32 bytes is a common choice.
- Signature counter. The authenticator data includes a counter that should rise with each use. Many synced passkeys always report 0, so a server should treat a counter that goes backward as a warning sign, not reject all zero values.
- Duplicate registration. Pass
excludeCredentialswith a user's existing credential IDs so the same authenticator is not registered twice. - User cancellation. If the user dismisses the prompt, the call rejects with a
NotAllowedError. The timeout is only a hint to the browser. Handle both cases without treating them as attacks.
How this differs from TOTP
A TOTP code is derived from a secret that both you and the exchange hold, so whoever sees the secret or a fresh code can use it. A passkey signature is made with a private key that the exchange never holds, and it covers a challenge that exists only for one attempt. The exchange can therefore verify you without being able to impersonate you.
Exercise
A developer's sign-in endpoint verifies the signature with the stored public key and checks the user-verified flag, but it never compares the challenge in clientDataJSON to a stored value, and it never checks the origin. Name one attack each missing check allows.
Show answer
Without the challenge check, an attacker who captured one valid assertion, for example from a compromised network device or malware, can replay it later, because nothing ties the signature to this sign-in attempt. Without the origin check, the server cannot tell that the signed request came from a look-alike site, so it loses the origin binding that blocks phishing relays. Note that the rpIdHash check still stops a response created for a different relying party ID.
Exercise
A site's pages are served from https://app.exchange.example. Which of these relying party IDs can the page use: exchange.example, app.exchange.example, or other.example? Which choice would let the same passkey work on https://pro.exchange.example?
Show answer
The page can use exchange.example and app.exchange.example, because each is the page's domain or a parent of it. It cannot use other.example. Only exchange.example also works for pro.exchange.example, so choosing it from the start lets a passkey work across subdomains.