Passkeys: say up front when adding one would be refused #248
No files matched your search
@@ -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<Passkey[] | null | undefined>(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<string, unknown>, 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 }) {
|
||||
</ul>
|
||||
)}
|
||||
<div className="flex flex-wrap items-start justify-between gap-3">
|
||||
{canAdd ? <AddPasskey accountId={accountId} onChanged={refresh} /> : <span />}
|
||||
{!canAdd ? (
|
||||
<span />
|
||||
) : policy.allowed === false ? (
|
||||
<p className="text-xs text-muted-foreground">
|
||||
{t('passkeys.forbidden', 'Passkeys are turned off for this account.')}
|
||||
</p>
|
||||
) : policy.max !== undefined && passkeys !== undefined && passkeys.length >= policy.max ? (
|
||||
<p className="text-xs text-muted-foreground">
|
||||
{t('passkeys.overQuota', 'This account has as many passkeys as it may. Remove one first.')}
|
||||
</p>
|
||||
) : (
|
||||
<AddPasskey accountId={accountId} onChanged={refresh} />
|
||||
)}
|
||||
{canRemove && passkeys && passkeys.length > 1 && (
|
||||
<RemoveAll accountId={accountId} ids={passkeys.map((p) => p.id)} own={own} onChanged={refresh} />
|
||||
)}
|
||||
|
||||
@@ -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<string, unknown>) => ({ 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({});
|
||||
});
|
||||
});
|
||||
@@ -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<string, unknown> | null,
|
||||
accountId: string | null,
|
||||
): { allowed?: boolean; max?: number } {
|
||||
if (!session || !accountId) return {};
|
||||
const accounts = session.accounts as Record<string, { accountCapabilities?: Record<string, unknown> }> | 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;
|
||||
|
||||
|
||||
Reference in new issue
Block a user