Sign up with a passkey
For: an app developer who wants a person to make an account with a passkey only.
What it does: passkey.signUp adds POST /passkey/sign-up/options and POST /passkey/sign-up/verify. The verify makes the user, the passkey, and the session. The user has no address.
Before you start
Section titled “Before you start”- Passkey sign-in, as in Sign in with a passkey.
- A limiter that counts for the whole deployment: the D1, Postgres, or MySQL limiter. Limits names each one.
signUp: "open", the default of Top level.
- Set
passkey.signUptotrue, or to an object withdailyCap. - Pass the limiter of your adapter as
limiter. - Ask the person for a name. Post
{ name }toPOST /passkey/sign-up/options. - Give the answer to
navigator.credentials.create. - Post
{ response }toPOST /passkey/sign-up/verify. A browser gets the session cookie. - In a browser app, call
client.passkey.signUpof the client package in place of steps 3 to 5. - On a public site, add the captcha flow
passkey-sign-uptocaptcha.flows.
import { createAuth, type LimiterPort, type MailPort, type StorageAdapter } from "@authtempo/core";
/** * Sign-up with a passkey and a name. The new user has no address. Pass the * limiter of your D1, Postgres, or MySQL adapter: the daily cap counts per * deployment. Only a test passes "isolate-for-tests", and its cap then * counts per isolate. */export function createPasskeySignUpAuth(deps: { storage: StorageAdapter; mail: MailPort; secret: string; limiter: LimiterPort | "isolate-for-tests";}) { const forTests = deps.limiter === "isolate-for-tests"; return createAuth({ baseURL: "https://api.example.com", appURL: "https://app.example.com", trustedOrigins: ["https://app.example.com"], secret: deps.secret, storage: deps.storage, mail: deps.mail, ...(deps.limiter === "isolate-for-tests" ? {} : { limiter: deps.limiter }), passkey: { rpID: "example.com", rpName: "Example", origins: ["https://app.example.com"], signUp: { // The sign-ups of the whole deployment per 24 hours. dailyCap: 1000, // Tests only. With a limiter that counts per isolate, createAuth refuses this. ...(forTests ? { isolateLimiter: "allow" as const } : {}), }, }, });}import { createMemoryLimiter, systemClock } from "@authtempo/core";import { SoftAuthenticator } from "@authtempo/core/testing";import { describe, expect, it } from "vitest";import { call, harness, sessionCookieOf } from "../support/harness.ts";import { createPasskeySignUpAuth } from "./passkey-sign-up.ts";
/** The bytes of a base64url string. */const bytesOf = (s: string) => Uint8Array.from(Buffer.from(s, "base64url"));
describe("sign up with a passkey", () => { it("makes a user with a passkey, a name, and no address", async () => { // A test passes the test value. An app passes the limiter of its adapter. const auth = createPasskeySignUpAuth({ ...harness(), limiter: "isolate-for-tests" }); const res = await call(auth, "POST", "/auth/passkey/sign-up/options", { name: "Ada" }); expect(res.status).toBe(200); const options = (await res.json()) as { challenge: string; user: { id: string; name: string } }; expect(options.user.name).toBe("Ada"); // In the browser: navigator.credentials.create. A test uses a software authenticator. const device = await SoftAuthenticator.create({ rpID: "example.com", origin: "https://app.example.com", }); const response = await device.register(options.challenge, bytesOf(options.user.id)); const done = await call(auth, "POST", "/auth/passkey/sign-up/verify", { response }); expect(done.status).toBe(200); const { user } = (await done.json()) as { user: { name: string; email: string | null } }; expect(user.email).toBeNull(); expect(user.name).toBe("Ada"); expect(sessionCookieOf(done)).not.toBe(""); });
it("refuses a memory limiter outside tests: the cap is per deployment", () => { const limiter = createMemoryLimiter(systemClock); expect(() => createPasskeySignUpAuth({ ...harness(), limiter })).toThrow(/shared limiter/); });
it("refuses a call with no limiter, as from plain JavaScript", () => { // The type needs a limiter. A call that skips the type check gets the refusal. const deps = harness() as never; expect(() => createPasskeySignUpAuth(deps)).toThrow(/shared limiter/); });});How it behaves
Section titled “How it behaves”- The options make no user. Core mints the user id at the options and keeps it with the challenge. The verify makes the user.
- The name has 1 to 80 characters, with no control, format, or bidi character. It is the name of the user, and the device shows it with the passkey. The stored passkey has no name until a rename.
- A challenge makes one user. A second verify of the same challenge answers
invalid_token. - The verify runs
user.beforeCreatewithvia: "passkey", and the session hooks seevia: "passkey". - No second factor gates the sign-up. A new user has no factor, and a passkey with user verification is two factors.
- Only
signUp: "open"lets the user in."invited"andfalseanswersign_up_closed, because an invitation names an address. - A credential id that another user holds answers
passkey_failedand makes nothing. - A live anonymous session of the browser is replaced, as at each sign-in. See Anonymous sessions.
- The limit
passkey-sign-up:ipallows 10 options per IP per hour. The cappasskey-sign-up:all-dayallowsdailyCapoptions per deployment per 24 hours, 1000 by default. - With the captcha flow on, the captcha runs after the per-IP limit and before the cap. A failed captcha does not count against the cap.
createAuthrefusespasskey.signUpwith a limiter that counts per isolate. For tests only, setpasskey.signUp.isolateLimiter: "allow". The cap is then per isolate.- The user has a proved handle: its passkey. So it can register a second passkey, set a username, and add an address. See The account with no address.
Residuals
Section titled “Residuals”- A script with a software authenticator can make a user at each verify. The attestation is
none, so core cannot tell a script from a device. The per-IP limit and the daily cap bound it. - A script that spends the cap stops real sign-ups until the window ends. Turn on the captcha flow on a public site.
- The user has no address, so no mail can recover the account. Ask the person to register a second passkey or to add an address.
If it fails
Section titled “If it fails”invalid_body: the name is empty, too long, or holds a control character.sign_up_closed:signUpis not"open".rate_limited: the IP made 10 option requests this hour, or the deployment reached the daily cap.captcha_required,captcha_failed, andcaptcha_unavailable: the captcha flowpasskey-sign-uprefused the request.passkey_failed: the response did not verify, or another user holds the credential.invalid_tokenandtoken_expired: the challenge is spent or older than five minutes. Start again.hook_refusedandhook_failed: a hook of your app refused the user or threw.