diff --git a/docs/specs/hosted.md b/docs/specs/hosted.md index ab1973548..daa903002 100644 --- a/docs/specs/hosted.md +++ b/docs/specs/hosted.md @@ -222,7 +222,7 @@ The relay Worker serves the self-host Relay's HTTP API to many accounts: the pat | Route | On Hosted | |---|---| -| `POST /api/setup/begin`, `/finish` | The passkey joins the account owning the token's Burrow, recorded as that Burrow's: `accountId` is its user ID, `existingCredentialIds` its passkeys; 409 at `MAX_PASSKEYS_PER_ACCOUNT`, naming the account page; `finish`'s session counts toward `MAX_SESSIONS_PER_ACCOUNT` as sign-in's does | +| `POST /api/setup/begin`, `/finish` | The passkey joins the account owning the token's Burrow, recorded as that Burrow's: `accountId` is its user ID, `existingCredentialIds` its passkeys, `accountEmail` its email (which names the new passkey, so it reaches whoever holds a live code); 409 at `MAX_PASSKEYS_PER_ACCOUNT`, naming the account page; `finish`'s session counts toward `MAX_SESSIONS_PER_ACCOUNT` as sign-in's does | | `POST /api/setup/retire` | Spends only a token one of the session's account's Burrows minted | | `POST /api/signin/finish` | The asserted credential's account; `accountId` is its user ID. 401 `NOT_ENTITLED_ERROR`, and no session, for an account not entitled | | `POST /api/reauth/begin`, `/finish` | Only the session's account's credentials and nonces | diff --git a/hosted/server/relay-api.ts b/hosted/server/relay-api.ts index 6c39bcab6..5b3d8b339 100644 --- a/hosted/server/relay-api.ts +++ b/hosted/server/relay-api.ts @@ -210,8 +210,8 @@ export function relayApiRoutes(app: Hono<{ Bindings: RelayEnv }>) { return database(c, async (db) => { const { rows: [minter], - } = await db.query( - `SELECT t."burrowId", b."userId", ${OWNER_COLUMNS}, + } = await db.query( + `SELECT t."burrowId", b."userId", u.email, ${OWNER_COLUMNS}, ARRAY( SELECT p."credentialId" FROM dormouse_relay_passkeys p WHERE p."userId" = b."userId" ORDER BY p."createdAt", p."credentialId" @@ -236,6 +236,7 @@ export function relayApiRoutes(app: Hono<{ Bindings: RelayEnv }>) { rpId: rpIdOf(c.env), accountId: minter.userId, existingCredentialIds: minter.credentialIds, + ...(minter.email ? { accountEmail: minter.email } : {}), }; return c.json(res); }); diff --git a/hosted/server/tests/relay.test.ts b/hosted/server/tests/relay.test.ts index 6df091d7d..15ce6fdd3 100644 --- a/hosted/server/tests/relay.test.ts +++ b/hosted/server/tests/relay.test.ts @@ -687,8 +687,12 @@ test("another account's session sees, proves with, and retires none of this acco await f.entitle(a); expect((await finish(sessionA.sessionToken)).status).toBe(200); const begun = await f.call("POST", API_ROUTES.setupBegin, { body: { setupToken: code } }); - // Only A's own credentials are excluded, never B's. - expect(begun.json).toMatchObject({ accountId: a, existingCredentialIds: [phoneA.credentialId] }); + // Only A's own credentials are excluded, never B's, and the passkey is named by A's email. + expect(begun.json).toMatchObject({ + accountId: a, + existingCredentialIds: [phoneA.credentialId], + accountEmail: ADMIN_EMAIL, + }); // B's Burrow mints nothing for an account that is not entitled. expect(await f.mint(laptopB.token)).toMatchObject({ status: 403, json: { error: NOT_ENTITLED_ERROR } }); diff --git a/lib/src/remote/client/pocket-client.test.ts b/lib/src/remote/client/pocket-client.test.ts index 1e0503f6b..43140ca9d 100644 --- a/lib/src/remote/client/pocket-client.test.ts +++ b/lib/src/remote/client/pocket-client.test.ts @@ -1540,6 +1540,29 @@ describe('setup + signin', () => { expect(harness.client.holdsAnyCredential(['someone-else'])).toBe(false); }); + it('names the passkey by the account email the Relay sends, and as before without one', async () => { + const emails: (string | undefined)[] = []; + const webauthn: WebAuthnClient = { + ...fakeWebAuthn, + registerPasskey: (...args) => { + emails.push(args[4]); + return fakeWebAuthn.registerPasskey(...args); + }, + }; + const withEmail = makeClient( + { + ...AUTH_ROUTES, + '/api/setup/begin': () => ({ + json: { challenge: secret(), rpId: 'relay.test', accountId: ACCOUNT_ID, existingCredentialIds: [], accountEmail: 'a@example.test' }, + }), + }, + { webauthn }, + ); + await withEmail.client.setup({ setupToken: secret() }, 'Phone'); + await makeClient({ ...AUTH_ROUTES }, { webauthn }).client.setup({ setupToken: secret() }, 'Phone'); + expect(emails).toEqual(['a@example.test', undefined]); + }); + /** * Its own error class, because Pocket has to react rather than report: a * spent code means "show a new one on the computer", where the shared diff --git a/lib/src/remote/client/pocket-client.ts b/lib/src/remote/client/pocket-client.ts index 7eceed3a2..47b03306a 100644 --- a/lib/src/remote/client/pocket-client.ts +++ b/lib/src/remote/client/pocket-client.ts @@ -573,6 +573,7 @@ export class PocketClient { begin.rpId, begin.accountId, begin.existingCredentialIds, + begin.accountEmail, ); } catch (err) { // Translated at this seam, not inside the browser wrapper, so every diff --git a/lib/src/remote/client/webauthn.test.ts b/lib/src/remote/client/webauthn.test.ts index 14b0594fe..6c856a2bd 100644 --- a/lib/src/remote/client/webauthn.test.ts +++ b/lib/src/remote/client/webauthn.test.ts @@ -26,6 +26,25 @@ describe('browserWebAuthn.registerPasskey', () => { expect(user.displayName).toBe('Dormouse Pocket (relay.dormouse.sh)'); expect(utf8Decode(new Uint8Array(user.id as ArrayBuffer))).toBe('user-123'); }); + + it('names the passkey by its account email when the Relay sent one', async () => { + const create = vi.fn(async () => ({ + id: 'cred', + response: { + getPublicKey: () => new Uint8Array([1, 2, 3]).buffer, + clientDataJSON: new Uint8Array([4]).buffer, + }, + })); + vi.stubGlobal('navigator', { credentials: { create } }); + + await browserWebAuthn.registerPasskey(toBase64Url(new Uint8Array(32)), 'relay.dormouse.sh', 'user-123', [], 'a@example.test'); + + const { user } = (create.mock.calls[0] as unknown as [{ publicKey: PublicKeyCredentialCreationOptions }])[0] + .publicKey; + expect(user.name).toBe('a@example.test (Dormouse.sh)'); + expect(user.displayName).toBe('a@example.test (Dormouse.sh)'); + expect(utf8Decode(new Uint8Array(user.id as ArrayBuffer))).toBe('user-123'); + }); }); describe('a dismissed or timed-out passkey prompt', () => { diff --git a/lib/src/remote/client/webauthn.ts b/lib/src/remote/client/webauthn.ts index dfe2ca541..dc56325d8 100644 --- a/lib/src/remote/client/webauthn.ts +++ b/lib/src/remote/client/webauthn.ts @@ -57,11 +57,12 @@ export function isPasskeyAlreadyRegistered(err: unknown): boolean { /** * The name a registered passkey shows in the platform's passkey manager: the - * app and the Relay it signs in to, never the account id, which is the - * credential's opaque `user.id` alone. + * account's email where the Relay sent one, so one phone's passkeys for two + * accounts read apart, else the app and the Relay it signs in to. Never the + * account id, which is the credential's opaque `user.id` alone. */ -export function passkeyUserName(rpId: string): string { - return `Dormouse Pocket (${rpId})`; +export function passkeyUserName(rpId: string, accountEmail?: string): string { + return accountEmail ? `${accountEmail} (Dormouse.sh)` : `Dormouse Pocket (${rpId})`; } /** The result of a passkey registration, ready for `POST /api/setup/finish`. */ @@ -79,13 +80,15 @@ export interface WebAuthnClient { /** * Create a passkey for `accountId`. Pass `excludeCredentialIds` (base64url, * from `SetupBeginResponse`) to stop the authenticator minting a duplicate of - * a credential the Relay already holds. + * a credential the Relay already holds, and `accountEmail` (also from it) + * to name the passkey by its account. */ registerPasskey( challenge: string, rpId: string, accountId: string, excludeCredentialIds?: readonly string[], + accountEmail?: string, ): Promise; /** * Get an assertion bound to `challenge`. Pass `allowCredentials` (base64url @@ -132,6 +135,7 @@ async function registerPasskey( rpId: string, accountId: string, excludeCredentialIds: readonly string[] = [], + accountEmail?: string, ): Promise { const credential = (await navigator.credentials.create({ publicKey: { @@ -139,8 +143,8 @@ async function registerPasskey( rp: { id: rpId, name: 'Dormouse' }, user: { id: toBufferSource(utf8Encode(accountId)), - name: passkeyUserName(rpId), - displayName: passkeyUserName(rpId), + name: passkeyUserName(rpId, accountEmail), + displayName: passkeyUserName(rpId, accountEmail), }, pubKeyCredParams: [{ type: 'public-key', alg: -7 }], excludeCredentials: excludeCredentialIds.map((id) => ({ diff --git a/remote-lib-common/src/remote/wire.ts b/remote-lib-common/src/remote/wire.ts index 577d3e042..8cf050b2c 100644 --- a/remote-lib-common/src/remote/wire.ts +++ b/remote-lib-common/src/remote/wire.ts @@ -230,6 +230,13 @@ export interface SetupBeginResponse { * cache is empty on a fresh install and cleared again by a refused `finish`. */ existingCredentialIds: string[]; + /** + * The account's email, where the Relay keeps one (Hosted does, selfhost does + * not), so a phone holding passkeys for two accounts can tell them apart in + * its passkey manager. Optional both ways: a Pocket that predates it ignores + * it, and one reading a Relay that sends none names the passkey as before. + */ + accountEmail?: string; } export type SetupFinishRequest = SetupCredential & {