diff --git a/src/features/passkeys/PasskeysPanel.tsx b/src/features/passkeys/PasskeysPanel.tsx index 7ab3901..66f7f30 100644 --- a/src/features/passkeys/PasskeysPanel.tsx +++ b/src/features/passkeys/PasskeysPanel.tsx @@ -33,11 +33,13 @@ import { import { toast } from '@/hooks/use-toast'; import { useAccountStore } from '@/stores/accountStore'; import { useAuthStore } from '@/stores/authStore'; +import { fetchSession } from '@/services/jmap/client'; import { createPasskey, fetchPasskeys, MAX_NAME, nameProblem, + passkeyPolicy, PasskeyRefused, removePasskeys, renamePasskey, @@ -80,6 +82,8 @@ export function PasskeysPanel({ accountId }: { accountId: string }) { // undefined until known; null when the server has no passkeys for us to show const [passkeys, setPasskeys] = useState(undefined); + // PK-19: whether the server would refuse an add, read from the session + const [policy, setPolicy] = useState<{ allowed?: boolean; max?: number }>({}); const [fetches, setFetches] = useState(0); const refresh = useCallback(() => setFetches((n) => n + 1), []); @@ -97,6 +101,19 @@ export function PasskeysPanel({ accountId }: { accountId: string }) { return () => controller.abort(); }, [accountId, canGet, fetches]); + useEffect(() => { + if (!canAdd) return; + let live = true; + fetchSession() + .then((session) => { + if (live) setPolicy(passkeyPolicy(session as Record, accountId)); + }) + .catch(() => {}); + return () => { + live = false; + }; + }, [accountId, canAdd, fetches]); + if (!canGet || passkeys === null) return null; return ( @@ -137,7 +154,19 @@ export function PasskeysPanel({ accountId }: { accountId: string }) { )}
- {canAdd ? : } + {!canAdd ? ( + + ) : policy.allowed === false ? ( +

+ {t('passkeys.forbidden', 'Passkeys are turned off for this account.')} +

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

+ {t('passkeys.overQuota', 'This account has as many passkeys as it may. Remove one first.')} +

+ ) : ( + + )} {canRemove && passkeys && passkeys.length > 1 && ( p.id)} own={own} onChanged={refresh} /> )} diff --git a/src/features/passkeys/passkeys.test.ts b/src/features/passkeys/passkeys.test.ts index ae8b2d8..ab46d76 100644 --- a/src/features/passkeys/passkeys.test.ts +++ b/src/features/passkeys/passkeys.test.ts @@ -11,6 +11,7 @@ vi.mock('@/services/jmap/client', () => ({ jmapRequest: (...args: unknown[]) => import { createPasskey, + passkeyPolicy, fetchPasskeys, fromServer, nameProblem, @@ -120,4 +121,13 @@ describe('requests', () => { await removePasskeys('acc', []); expect(jmapRequest).not.toHaveBeenCalled(); }); + + it('reads up front whether adding is allowed (PK-19)', () => { + const session = (cap: Record) => ({ accounts: { a: { accountCapabilities: { 'urn:inbuxa:jmap': cap } } } }); + expect(passkeyPolicy(session({ passkeysAllowed: false, maxPasskeys: 10 }), 'a')).toEqual({ allowed: false, max: 10 }); + expect(passkeyPolicy(session({ passkeysAllowed: true, maxPasskeys: 3 }), 'a')).toEqual({ allowed: true, max: 3 }); + // an older server says nothing: the add stays and the server refuses it + expect(passkeyPolicy(session({ aiExplain: false }), 'a')).toEqual({ allowed: undefined, max: undefined }); + expect(passkeyPolicy(null, 'a')).toEqual({}); + }); }); diff --git a/src/features/passkeys/passkeys.ts b/src/features/passkeys/passkeys.ts index 2103199..55f2ea7 100644 --- a/src/features/passkeys/passkeys.ts +++ b/src/features/passkeys/passkeys.ts @@ -16,6 +16,27 @@ import { jmapRequest } from '@/services/jmap/client'; const CAPABILITY = 'urn:inbuxa:jmap'; const OBJECT = 'inbuxa:Passkey'; +/** + * What the session says up front about adding a passkey to the signed-in + * account (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; + * that server refuses the add itself. + */ +export function passkeyPolicy( + session: Record | null, + accountId: string | null, +): { allowed?: boolean; max?: number } { + if (!session || !accountId) return {}; + const accounts = session.accounts as Record }> | undefined; + const cap = accounts?.[accountId]?.accountCapabilities?.[CAPABILITY] as + | { passkeysAllowed?: unknown; maxPasskeys?: unknown } + | undefined; + return { + allowed: typeof cap?.passkeysAllowed === 'boolean' ? cap.passkeysAllowed : undefined, + max: typeof cap?.maxPasskeys === 'number' ? cap.maxPasskeys : undefined, + }; +} + /** Longest name the server takes. */ export const MAX_NAME = 64;