diff --git a/web/src/lib/__tests__/passkeys.test.ts b/web/src/lib/__tests__/passkeys.test.ts index 3f0e07e..a672ae1 100644 --- a/web/src/lib/__tests__/passkeys.test.ts +++ b/web/src/lib/__tests__/passkeys.test.ts @@ -3,7 +3,8 @@ import { beforeEach, describe, expect, it, vi } from "vitest"; const call = vi.fn(); vi.mock("@/jmap/client", () => ({ client: { call: (...a: unknown[]) => call(...a) }, INBUXA_CAP: "urn:inbuxa:jmap" })); -import { addErrorMessage, listPasskeys, removePasskey, renamePasskey, startPasskey } from "@/lib/passkeys"; +import { addErrorMessage, listPasskeys, passkeyPolicy, removePasskey, renamePasskey, startPasskey } from "@/lib/passkeys"; +import type { JmapSession } from "@/jmap/types"; describe("passkeys (#334)", () => { beforeEach(() => { call.mockReset(); }); @@ -48,4 +49,14 @@ describe("passkeys (#334)", () => { call.mockResolvedValue({ notDestroyed: { p: { type: "notFound" } } }); await expect(removePasskey("acc", "p")).rejects.toThrow(); }); + + it("reads up front whether adding is allowed (PK-19)", () => { + const session = (cap: Record) => + ({ accounts: { acc: { accountCapabilities: { "urn:inbuxa:jmap": cap } } } }) as unknown as JmapSession; + expect(passkeyPolicy(session({ passkeysAllowed: false, maxPasskeys: 10 }), "acc")).toEqual({ allowed: false, max: 10 }); + expect(passkeyPolicy(session({ passkeysAllowed: true, maxPasskeys: 3 }), "acc")).toEqual({ allowed: true, max: 3 }); + // an older server says nothing: the add is offered and refused there + expect(passkeyPolicy(session({ logo: null }), "acc")).toEqual({ allowed: undefined, max: undefined }); + expect(passkeyPolicy(null, "acc")).toEqual({}); + }); }); diff --git a/web/src/lib/passkeys.ts b/web/src/lib/passkeys.ts index 126df1b..fa95c12 100644 --- a/web/src/lib/passkeys.ts +++ b/web/src/lib/passkeys.ts @@ -1,4 +1,5 @@ import { client, INBUXA_CAP } from "@/jmap/client"; +import type { Id, JmapSession } from "@/jmap/types"; import { t } from "@/lib/i18n"; /** @@ -60,6 +61,26 @@ export async function listPasskeys(accountId: string): Promise } } +/** + * What the server says up front about adding a passkey to this account + * (passkeys PK-19): `allowed` false when a switch turns passkeys off for it, + * `max` the most it may have. Both undefined from a server that doesn't say, + * which then refuses the add itself. + */ +export function passkeyPolicy( + session: JmapSession | null, + accountId: Id | null, +): { allowed?: boolean; max?: number } { + if (!session || !accountId) return {}; + const cap = session.accounts[accountId]?.accountCapabilities?.[INBUXA_CAP] as + | { passkeysAllowed?: unknown; maxPasskeys?: unknown } + | undefined; + return { + allowed: typeof cap?.passkeysAllowed === "boolean" ? cap.passkeysAllowed : undefined, + max: typeof cap?.maxPasskeys === "number" ? cap.maxPasskeys : undefined, + }; +} + /** Words for a refused create (PK-5). */ export function addErrorMessage(err: SetError): string { switch (err.type) { diff --git a/web/src/views/settings/SecuritySettings.tsx b/web/src/views/settings/SecuritySettings.tsx index c304702..365d1d1 100644 --- a/web/src/views/settings/SecuritySettings.tsx +++ b/web/src/views/settings/SecuritySettings.tsx @@ -8,7 +8,7 @@ import { toast } from "@/ui/toast"; import { confirmDialog, Dialog } from "@/ui/dialog"; import { plural, t, tNode } from "@/lib/i18n"; import { SIGNIN_NOTICE_KEY } from "@/views/Login"; -import { listPasskeys, removePasskey, renamePasskey, startPasskey, type Passkey } from "@/lib/passkeys"; +import { addErrorMessage, listPasskeys, passkeyPolicy, removePasskey, renamePasskey, startPasskey, type Passkey } from "@/lib/passkeys"; interface SessionRow { id: string; @@ -86,7 +86,7 @@ export function SecuritySettings() { )} - {session?.ihasmail?.signIn === "oauth" && accountId && } + {session?.ihasmail?.signIn === "oauth" && accountId && }

{t("App passwords")}

{legacyProtocolsOff(session, accountId) && ( @@ -379,7 +379,7 @@ function AppPasswords({ state, reload }: { state: SecurityState | null; reload: * has no passkeys. Adding one runs on the server's own page in a new window, * because a passkey is bound to the server's name, not this webmail's. */ -export function Passkeys({ accountId }: { accountId: string }) { +export function Passkeys({ accountId, allowed, max }: { accountId: string; allowed?: boolean; max?: number }) { const [rows, setRows] = useState(undefined); const [name, setName] = useState(""); const [busy, setBusy] = useState(false); @@ -483,6 +483,12 @@ export function Passkeys({ accountId }: { accountId: string }) { {rows?.some((r) => r.state === "active" && !r.usable) && (

{t("A passkey marked “needs re-adding” was added for the mail server's old name. Add a new one, then remove the old one.")}

)} + {/* PK-19: say up front when the server would refuse the add */} + {allowed === false ? ( +

{addErrorMessage({ type: "forbidden" })}

+ ) : max !== undefined && rows !== undefined && rows.length >= max ? ( +

{addErrorMessage({ type: "overQuota" })}

+ ) : (
@@ -490,6 +496,7 @@ export function Passkeys({ accountId }: { accountId: string }) {
+ )} {waiting &&

{t("Finish adding the passkey in the window that opened. The page there works for 10 minutes.")}

} setRenaming(null)} title={t("Rename passkey")} size="sm"