Use one passkey on related sites
For: an app developer with one service on several domains, such as example.com and example.net.
What it does: with passkey.relatedOrigins: true, auth.wellKnown serves GET /.well-known/webauthn on the rpID host. A browser reads it, and lets a passkey of example.com sign in on example.net.
Before you start
Section titled “Before you start”- Passkey sign-in, as in Sign in with a passkey.
- The
rpIDhost served by your app, so that you can mountauth.wellKnownat its origin root.
- Set
passkey.relatedOriginstotrue. - Add each related origin to
passkey.originsand totrustedOrigins. - Mount
auth.wellKnownat the origin root of therpIDhost, for each path that starts with/.well-known/. - On the related site, run the ceremonies with the same
rpID.
import { createAuth, type MailPort, type StorageAdapter } from "@authtempo/core";
/** One passkey for example.com, its app host, and two related sites. */export function createRelatedOriginsAuth(deps: { storage: StorageAdapter; mail: MailPort; secret: string;}) { const origins = [ "https://app.example.com", // Outside the domain tree of rpID: the document lists these two. "https://example.net", "https://example.org", ]; return createAuth({ baseURL: "https://api.example.com", appURL: "https://app.example.com", // Each passkey origin must be a trusted origin, or the router refuses its posts. trustedOrigins: origins, secret: deps.secret, storage: deps.storage, mail: deps.mail, passkey: { rpID: "example.com", rpName: "Example", origins, // At most five origins outside example.com. auth.wellKnown serves the document. relatedOrigins: true, }, });}import { describe, expect, it } from "vitest";import { harness } from "../support/harness.ts";import { createRelatedOriginsAuth } from "./related-origins.ts";
describe("passkey related origins", () => { it("lists the origins outside the RP ID on the RP ID host", async () => { const auth = createRelatedOriginsAuth(harness()); // Mount auth.wellKnown at the origin root of example.com. const res = await auth.wellKnown(new Request("https://example.com/.well-known/webauthn")); expect(res.status).toBe(200); expect(res.headers.get("content-type")).toContain("application/json"); expect(await res.json()).toEqual({ origins: ["https://example.net", "https://example.org"] }); });
it("answers 404 on any other host", async () => { const auth = createRelatedOriginsAuth(harness()); const res = await auth.wellKnown(new Request("https://api.example.com/.well-known/webauthn")); expect(res.status).toBe(404); });});How it behaves
Section titled “How it behaves”- The document is
{ "origins": [...] }, withcontent-type: application/jsonandcache-control: public, max-age=300. - It lists only the origins outside the domain tree of
rpID, in config order. An origin on the tree needs no document. - Only a request to the
rpIDhost gets it. Any other host answers 404. - WebAuthn Level 3 says a browser must accept at least five labels, such as
exampleofexample.net. SocreateAuthrefuses more than five origins outside the tree. - Core has no public suffix list, so it counts each origin outside the tree as one label. Two origins of one label count twice. So core can refuse a config that a browser accepts.
- Each ceremony still checks the origin of the browser against
passkey.origins. An origin outside the list never passes. passkey.hints.registerandpasskey.hints.signIngive the browser a preference:security-key,client-device, orhybrid. Ahintsfield of an options body replaces the list for that ceremony.- A sign-in by a credential that the site does not store answers
passkey_unknown. CallPublicKeyCredential.signalUnknownCredentialwith it, so the device removes the passkey. The client’spasskey.signInmakes that call for you where the browser has it. GET /passkeysanswersrpIdanduserHandle. Pass them with the credential ids toPublicKeyCredential.signalAllAcceptedCredentials. The client’spasskeys.sync()does this.GET /passkeysalso answersuserNameanduserDisplayName, the names of a new registration. Pass them toPublicKeyCredential.signalCurrentUserDetailsafter a change of name. The client’spasskeys.signalDetails()does this.
Residuals
Section titled “Residuals”passkey_unknowntells the holder of a credential that the site stores no row for it. Only that holder can see the answer: the credential id is random, and the person’s own device gives it.- A browser that does not read the document refuses the passkey on the related site. The person then signs in by another method there.
If it fails
Section titled “If it fails”passkey_failed: the origin of the browser is not inpasskey.origins, or the signature is bad.passkey_unknown: the site stores no passkey with this credential id.not_found: the document was asked on a host that is not therpIDhost.invalid_body: an options body named a hint that is not known, or a hint twice.