Passkeys panel on a person's account page (#334) #247
No files matched your search
@@ -66,6 +66,10 @@ const AccountLockBanner = onDemand(
|
||||
() => import('@/features/lock/AccountLockBanner'),
|
||||
(m) => m.AccountLockBanner,
|
||||
);
|
||||
const PasskeysPanel = onDemand(
|
||||
() => import('@/features/passkeys/PasskeysPanel'),
|
||||
(m) => m.PasskeysPanel,
|
||||
);
|
||||
const SharingSwitches = onDemand(
|
||||
() => import('@/features/sharing/SharingSwitches'),
|
||||
(m) => m.SharingSwitches,
|
||||
@@ -272,9 +276,14 @@ function recordPage(viewName: string, objectName: string, permissionPrefix: stri
|
||||
if (objectName === 'x:Tenant') {
|
||||
return stacked(<TenantLegacyProtocols tenantId={id} />, <SharingSwitches tenantId={id} />, page);
|
||||
}
|
||||
// A person's page says whether they're on hold or locked.
|
||||
// A person's page says whether they're on hold or locked, and lists their passkeys below.
|
||||
if (viewName === 'x:Account/User') {
|
||||
return stacked(<HeldNotice accountId={id} />, <AccountLockBanner accountId={id} />, page);
|
||||
return stacked(
|
||||
<HeldNotice accountId={id} />,
|
||||
<AccountLockBanner accountId={id} />,
|
||||
page,
|
||||
<PasskeysPanel accountId={id} />,
|
||||
);
|
||||
}
|
||||
return page;
|
||||
}
|
||||
|
||||
@@ -0,0 +1,466 @@
|
||||
/*
|
||||
* SPDX-FileCopyrightText: 2026 Coffey Labs LLC
|
||||
*
|
||||
* SPDX-License-Identifier: AGPL-3.0-only
|
||||
*/
|
||||
|
||||
/**
|
||||
* INBUXA: a person's passkeys on their account page (passkeys spec, the
|
||||
* inbuxa Admin interface). An administrator sees another account's passkeys
|
||||
* and can rename or remove them, which is how someone who lost theirs gets
|
||||
* back in. Only the account's owner can add one: the browser part runs on
|
||||
* the server's own registration page, opened in a new window.
|
||||
*/
|
||||
|
||||
import { useCallback, useEffect, useRef, useState } from 'react';
|
||||
import { useTranslation } from 'react-i18next';
|
||||
import type { TFunction } from 'i18next';
|
||||
import { KeyRound, Loader2, Pencil, Plus, Trash2 } from 'lucide-react';
|
||||
import { Badge } from '@/components/ui/badge';
|
||||
import { Button } from '@/components/ui/button';
|
||||
import { Card, CardContent, CardDescription, CardHeader, CardTitle } from '@/components/ui/card';
|
||||
import { Input } from '@/components/ui/input';
|
||||
import {
|
||||
AlertDialog,
|
||||
AlertDialogAction,
|
||||
AlertDialogCancel,
|
||||
AlertDialogContent,
|
||||
AlertDialogDescription,
|
||||
AlertDialogFooter,
|
||||
AlertDialogHeader,
|
||||
AlertDialogTitle,
|
||||
} from '@/components/ui/alert-dialog';
|
||||
import { toast } from '@/hooks/use-toast';
|
||||
import { useAccountStore } from '@/stores/accountStore';
|
||||
import { useAuthStore } from '@/stores/authStore';
|
||||
import {
|
||||
createPasskey,
|
||||
fetchPasskeys,
|
||||
MAX_NAME,
|
||||
nameProblem,
|
||||
PasskeyRefused,
|
||||
removePasskeys,
|
||||
renamePasskey,
|
||||
type Passkey,
|
||||
} from './passkeys';
|
||||
|
||||
function when(value: string, locale: string): string {
|
||||
if (!value) return '';
|
||||
const date = new Date(value);
|
||||
return Number.isNaN(date.getTime())
|
||||
? value
|
||||
: date.toLocaleString(locale, { dateStyle: 'medium', timeStyle: 'short' });
|
||||
}
|
||||
|
||||
function nameMessage(t: TFunction, name: string): string | null {
|
||||
const problem = nameProblem(name);
|
||||
if (problem === 'empty') return t('passkeys.nameEmpty', 'Give the passkey a name.');
|
||||
if (problem === 'tooLong') return t('passkeys.nameTooLong', 'At most {{max}} characters.', { max: MAX_NAME });
|
||||
return null;
|
||||
}
|
||||
|
||||
/** What a removal does, said before it happens. */
|
||||
function removalWarning(t: TFunction, own: boolean): string {
|
||||
return own
|
||||
? t(
|
||||
'passkeys.removeOwnWarning',
|
||||
'Removing a passkey signs you out everywhere, this console included. Your password still works.',
|
||||
)
|
||||
: t('passkeys.removeWarning', 'Removing a passkey signs this account out everywhere. Its password still works.');
|
||||
}
|
||||
|
||||
export function PasskeysPanel({ accountId }: { accountId: string }) {
|
||||
const { t, i18n } = useTranslation();
|
||||
const own = useAuthStore((s) => s.primaryAccountId === accountId);
|
||||
const has = useAccountStore((s) => s.hasPermission);
|
||||
const canGet = has('sysPasskeyGet') && (own || has('sysAccountUpdate'));
|
||||
const canRename = has('sysPasskeyUpdate');
|
||||
const canRemove = has('sysPasskeyDestroy');
|
||||
const canAdd = own && has('sysPasskeyCreate');
|
||||
|
||||
// undefined until known; null when the server has no passkeys for us to show
|
||||
const [passkeys, setPasskeys] = useState<Passkey[] | null | undefined>(undefined);
|
||||
const [fetches, setFetches] = useState(0);
|
||||
const refresh = useCallback(() => setFetches((n) => n + 1), []);
|
||||
|
||||
useEffect(() => {
|
||||
if (!canGet) return;
|
||||
const controller = new AbortController();
|
||||
fetchPasskeys(accountId, controller.signal)
|
||||
.then((list) => {
|
||||
if (!controller.signal.aborted) setPasskeys(list);
|
||||
})
|
||||
// An older server, or an account out of reach: show nothing
|
||||
.catch(() => {
|
||||
if (!controller.signal.aborted) setPasskeys(null);
|
||||
});
|
||||
return () => controller.abort();
|
||||
}, [accountId, canGet, fetches]);
|
||||
|
||||
if (!canGet || passkeys === null) return null;
|
||||
|
||||
return (
|
||||
<Card className="mx-auto max-w-4xl">
|
||||
<CardHeader>
|
||||
<CardTitle className="flex items-center gap-2 text-base">
|
||||
<KeyRound className="h-4 w-4" />
|
||||
{t('passkeys.title', 'Passkeys')}
|
||||
</CardTitle>
|
||||
<CardDescription>
|
||||
{t(
|
||||
'passkeys.description',
|
||||
'Passkeys sign in on the server’s sign-in page in place of the password. Mail apps that use IMAP or SMTP still use the password or an app password.',
|
||||
)}
|
||||
</CardDescription>
|
||||
</CardHeader>
|
||||
<CardContent className="space-y-4">
|
||||
{passkeys === undefined ? (
|
||||
<Loader2 className="h-4 w-4 animate-spin text-muted-foreground" />
|
||||
) : passkeys.length === 0 ? (
|
||||
<p className="text-sm text-muted-foreground">
|
||||
{own ? t('passkeys.noneOwn', 'You have no passkeys yet.') : t('passkeys.none', 'No passkeys.')}
|
||||
</p>
|
||||
) : (
|
||||
<ul className="divide-y rounded-lg border">
|
||||
{passkeys.map((p) => (
|
||||
<PasskeyRow
|
||||
key={p.id}
|
||||
passkey={p}
|
||||
accountId={accountId}
|
||||
own={own}
|
||||
canRename={canRename}
|
||||
canRemove={canRemove}
|
||||
locale={i18n.language}
|
||||
onChanged={refresh}
|
||||
/>
|
||||
))}
|
||||
</ul>
|
||||
)}
|
||||
<div className="flex flex-wrap items-start justify-between gap-3">
|
||||
{canAdd ? <AddPasskey accountId={accountId} onChanged={refresh} /> : <span />}
|
||||
{canRemove && passkeys && passkeys.length > 1 && (
|
||||
<RemoveAll accountId={accountId} ids={passkeys.map((p) => p.id)} own={own} onChanged={refresh} />
|
||||
)}
|
||||
</div>
|
||||
</CardContent>
|
||||
</Card>
|
||||
);
|
||||
}
|
||||
|
||||
function PasskeyRow({
|
||||
passkey,
|
||||
accountId,
|
||||
own,
|
||||
canRename,
|
||||
canRemove,
|
||||
locale,
|
||||
onChanged,
|
||||
}: {
|
||||
passkey: Passkey;
|
||||
accountId: string;
|
||||
own: boolean;
|
||||
canRename: boolean;
|
||||
canRemove: boolean;
|
||||
locale: string;
|
||||
onChanged: () => void;
|
||||
}) {
|
||||
const { t } = useTranslation();
|
||||
const [editing, setEditing] = useState(false);
|
||||
const [name, setName] = useState(passkey.name);
|
||||
const [busy, setBusy] = useState(false);
|
||||
const [confirming, setConfirming] = useState(false);
|
||||
const problem = nameMessage(t, name);
|
||||
|
||||
const rename = async () => {
|
||||
setBusy(true);
|
||||
try {
|
||||
await renamePasskey(accountId, passkey.id, name);
|
||||
setEditing(false);
|
||||
onChanged();
|
||||
} catch (e) {
|
||||
toast({
|
||||
variant: 'destructive',
|
||||
title: t('passkeys.renameFailed', 'The passkey wasn’t renamed'),
|
||||
description: e instanceof Error ? e.message : String(e),
|
||||
});
|
||||
} finally {
|
||||
setBusy(false);
|
||||
}
|
||||
};
|
||||
|
||||
const remove = async () => {
|
||||
setBusy(true);
|
||||
try {
|
||||
await removePasskeys(accountId, [passkey.id]);
|
||||
toast({ title: t('passkeys.removed', 'Passkey removed') });
|
||||
onChanged();
|
||||
} catch (e) {
|
||||
toast({
|
||||
variant: 'destructive',
|
||||
title: t('passkeys.removeFailed', 'The passkey wasn’t removed'),
|
||||
description: e instanceof Error ? e.message : String(e),
|
||||
});
|
||||
} finally {
|
||||
setBusy(false);
|
||||
setConfirming(false);
|
||||
}
|
||||
};
|
||||
|
||||
const pending = passkey.state === 'pending';
|
||||
return (
|
||||
<li className="flex flex-wrap items-center justify-between gap-3 p-3">
|
||||
<div className="min-w-0 space-y-1">
|
||||
{editing ? (
|
||||
<div className="flex items-center gap-2">
|
||||
<Input
|
||||
value={name}
|
||||
maxLength={MAX_NAME * 2}
|
||||
onChange={(e) => setName(e.target.value)}
|
||||
aria-label={t('passkeys.name', 'Name')}
|
||||
className="h-8 w-56"
|
||||
autoFocus
|
||||
/>
|
||||
<Button size="sm" onClick={rename} disabled={busy || !!problem}>
|
||||
{t('common.save', 'Save')}
|
||||
</Button>
|
||||
<Button
|
||||
size="sm"
|
||||
variant="outline"
|
||||
onClick={() => {
|
||||
setEditing(false);
|
||||
setName(passkey.name);
|
||||
}}
|
||||
disabled={busy}
|
||||
>
|
||||
{t('common.cancel', 'Cancel')}
|
||||
</Button>
|
||||
</div>
|
||||
) : (
|
||||
<p className="flex flex-wrap items-center gap-2 text-sm font-medium">
|
||||
<span className="truncate">{passkey.name}</span>
|
||||
{pending && <Badge variant="outline">{t('passkeys.pending', 'Not finished')}</Badge>}
|
||||
{!pending && !passkey.usable && (
|
||||
<Badge variant="destructive">{t('passkeys.needsReAdding', 'Needs re-adding')}</Badge>
|
||||
)}
|
||||
{passkey.backedUp && <Badge variant="secondary">{t('passkeys.synced', 'Synced')}</Badge>}
|
||||
</p>
|
||||
)}
|
||||
{editing && problem && <p className="text-xs text-destructive">{problem}</p>}
|
||||
<p className="text-xs text-muted-foreground">
|
||||
{pending
|
||||
? t('passkeys.pendingHint', 'Registration wasn’t finished. Remove it and add the passkey again.')
|
||||
: [
|
||||
t('passkeys.added', 'Added {{when}}', { when: when(passkey.createdAt, locale) }),
|
||||
passkey.lastUsedAt
|
||||
? t('passkeys.lastUsed', 'last used {{when}}', { when: when(passkey.lastUsedAt, locale) })
|
||||
: t('passkeys.neverUsed', 'never used'),
|
||||
].join(' · ')}
|
||||
</p>
|
||||
{!pending && !passkey.usable && (
|
||||
<p className="text-xs text-muted-foreground">
|
||||
{t(
|
||||
'passkeys.needsReAddingHint',
|
||||
'It was added for the server’s earlier name and can’t sign in here any more. Remove it and add a new one.',
|
||||
)}
|
||||
</p>
|
||||
)}
|
||||
</div>
|
||||
{!editing && (
|
||||
<div className="flex items-center gap-2">
|
||||
{canRename && !pending && (
|
||||
<Button size="sm" variant="outline" onClick={() => setEditing(true)} disabled={busy}>
|
||||
<Pencil className="mr-1 h-3.5 w-3.5" />
|
||||
{t('passkeys.rename', 'Rename')}
|
||||
</Button>
|
||||
)}
|
||||
{canRemove && (
|
||||
<Button size="sm" variant="outline" onClick={() => setConfirming(true)} disabled={busy}>
|
||||
<Trash2 className="mr-1 h-3.5 w-3.5" />
|
||||
{t('passkeys.remove', 'Remove')}
|
||||
</Button>
|
||||
)}
|
||||
</div>
|
||||
)}
|
||||
<AlertDialog open={confirming} onOpenChange={(open) => !busy && setConfirming(open)}>
|
||||
<AlertDialogContent>
|
||||
<AlertDialogHeader>
|
||||
<AlertDialogTitle>
|
||||
{t('passkeys.removeTitle', 'Remove “{{name}}”?', { name: passkey.name })}
|
||||
</AlertDialogTitle>
|
||||
<AlertDialogDescription>
|
||||
{pending
|
||||
? t('passkeys.removePendingWarning', 'It was never finished, so nothing else changes.')
|
||||
: removalWarning(t, own)}
|
||||
</AlertDialogDescription>
|
||||
</AlertDialogHeader>
|
||||
<AlertDialogFooter>
|
||||
<AlertDialogCancel disabled={busy}>{t('common.cancel', 'Cancel')}</AlertDialogCancel>
|
||||
<AlertDialogAction
|
||||
disabled={busy}
|
||||
onClick={(e) => {
|
||||
e.preventDefault();
|
||||
void remove();
|
||||
}}
|
||||
>
|
||||
{t('passkeys.remove', 'Remove')}
|
||||
</AlertDialogAction>
|
||||
</AlertDialogFooter>
|
||||
</AlertDialogContent>
|
||||
</AlertDialog>
|
||||
</li>
|
||||
);
|
||||
}
|
||||
|
||||
function RemoveAll({
|
||||
accountId,
|
||||
ids,
|
||||
own,
|
||||
onChanged,
|
||||
}: {
|
||||
accountId: string;
|
||||
ids: string[];
|
||||
own: boolean;
|
||||
onChanged: () => void;
|
||||
}) {
|
||||
const { t } = useTranslation();
|
||||
const [confirming, setConfirming] = useState(false);
|
||||
const [busy, setBusy] = useState(false);
|
||||
|
||||
const removeAll = async () => {
|
||||
setBusy(true);
|
||||
try {
|
||||
await removePasskeys(accountId, ids);
|
||||
toast({ title: t('passkeys.allRemoved', 'All passkeys removed') });
|
||||
onChanged();
|
||||
} catch (e) {
|
||||
toast({
|
||||
variant: 'destructive',
|
||||
title: t('passkeys.removeFailed', 'The passkey wasn’t removed'),
|
||||
description: e instanceof Error ? e.message : String(e),
|
||||
});
|
||||
} finally {
|
||||
setBusy(false);
|
||||
setConfirming(false);
|
||||
}
|
||||
};
|
||||
|
||||
return (
|
||||
<>
|
||||
<Button size="sm" variant="outline" onClick={() => setConfirming(true)}>
|
||||
<Trash2 className="mr-1 h-3.5 w-3.5" />
|
||||
{t('passkeys.removeAll', 'Remove all')}
|
||||
</Button>
|
||||
<AlertDialog open={confirming} onOpenChange={(open) => !busy && setConfirming(open)}>
|
||||
<AlertDialogContent>
|
||||
<AlertDialogHeader>
|
||||
<AlertDialogTitle>
|
||||
{t('passkeys.removeAllTitle', 'Remove all {{count}} passkeys?', { count: ids.length })}
|
||||
</AlertDialogTitle>
|
||||
<AlertDialogDescription>{removalWarning(t, own)}</AlertDialogDescription>
|
||||
</AlertDialogHeader>
|
||||
<AlertDialogFooter>
|
||||
<AlertDialogCancel disabled={busy}>{t('common.cancel', 'Cancel')}</AlertDialogCancel>
|
||||
<AlertDialogAction
|
||||
disabled={busy}
|
||||
onClick={(e) => {
|
||||
e.preventDefault();
|
||||
void removeAll();
|
||||
}}
|
||||
>
|
||||
{t('passkeys.removeAll', 'Remove all')}
|
||||
</AlertDialogAction>
|
||||
</AlertDialogFooter>
|
||||
</AlertDialogContent>
|
||||
</AlertDialog>
|
||||
</>
|
||||
);
|
||||
}
|
||||
|
||||
function AddPasskey({ accountId, onChanged }: { accountId: string; onChanged: () => void }) {
|
||||
const { t } = useTranslation();
|
||||
const [name, setName] = useState('');
|
||||
const [busy, setBusy] = useState(false);
|
||||
// The registration link, shown when the browser blocked the new window
|
||||
const [link, setLink] = useState<string | null>(null);
|
||||
const watcher = useRef<number | null>(null);
|
||||
const problem = name ? nameMessage(t, name) : null;
|
||||
|
||||
useEffect(
|
||||
() => () => {
|
||||
if (watcher.current !== null) window.clearInterval(watcher.current);
|
||||
},
|
||||
[],
|
||||
);
|
||||
|
||||
const add = async () => {
|
||||
setBusy(true);
|
||||
setLink(null);
|
||||
try {
|
||||
const url = await createPasskey(accountId, name);
|
||||
setName('');
|
||||
onChanged();
|
||||
const opened = window.open(url, '_blank');
|
||||
if (!opened) {
|
||||
setLink(url);
|
||||
return;
|
||||
}
|
||||
// Refresh once the registration window closes
|
||||
if (watcher.current !== null) window.clearInterval(watcher.current);
|
||||
watcher.current = window.setInterval(() => {
|
||||
if (opened.closed) {
|
||||
if (watcher.current !== null) window.clearInterval(watcher.current);
|
||||
watcher.current = null;
|
||||
onChanged();
|
||||
}
|
||||
}, 1000);
|
||||
} catch (e) {
|
||||
const description =
|
||||
e instanceof PasskeyRefused && e.kind === 'forbidden'
|
||||
? t('passkeys.forbidden', 'Passkeys are turned off for this account.')
|
||||
: e instanceof PasskeyRefused && e.kind === 'overQuota'
|
||||
? t('passkeys.overQuota', 'This account has as many passkeys as it may. Remove one first.')
|
||||
: e instanceof Error
|
||||
? e.message
|
||||
: String(e);
|
||||
toast({ variant: 'destructive', title: t('passkeys.addFailed', 'The passkey wasn’t added'), description });
|
||||
} finally {
|
||||
setBusy(false);
|
||||
}
|
||||
};
|
||||
|
||||
return (
|
||||
<div className="space-y-2">
|
||||
<div className="flex items-center gap-2">
|
||||
<Input
|
||||
value={name}
|
||||
onChange={(e) => setName(e.target.value)}
|
||||
placeholder={t('passkeys.namePlaceholder', 'Name, e.g. Phone or YubiKey')}
|
||||
aria-label={t('passkeys.name', 'Name')}
|
||||
className="h-8 w-64"
|
||||
/>
|
||||
<Button size="sm" onClick={add} disabled={busy || !name.trim() || !!problem}>
|
||||
{busy ? <Loader2 className="mr-1 h-3.5 w-3.5 animate-spin" /> : <Plus className="mr-1 h-3.5 w-3.5" />}
|
||||
{t('passkeys.add', 'Add a passkey')}
|
||||
</Button>
|
||||
</div>
|
||||
{problem && <p className="text-xs text-destructive">{problem}</p>}
|
||||
<p className="text-xs text-muted-foreground">
|
||||
{t(
|
||||
'passkeys.addHint',
|
||||
'Opens the server’s registration page in a new window, where you confirm with your password and then your passkey.',
|
||||
)}
|
||||
</p>
|
||||
{link && (
|
||||
<p className="text-xs">
|
||||
{t(
|
||||
'passkeys.blocked',
|
||||
'The browser blocked the new window. Open the registration page here; the link works for 10 minutes and only once:',
|
||||
)}{' '}
|
||||
<a href={link} target="_blank" rel="noreferrer" className="underline" onClick={() => setLink(null)}>
|
||||
{t('passkeys.openRegistration', 'Open the registration page')}
|
||||
</a>
|
||||
</p>
|
||||
)}
|
||||
</div>
|
||||
);
|
||||
}
|
||||
@@ -0,0 +1,123 @@
|
||||
/*
|
||||
* SPDX-FileCopyrightText: 2026 Coffey Labs LLC
|
||||
*
|
||||
* SPDX-License-Identifier: AGPL-3.0-only
|
||||
*/
|
||||
|
||||
import { beforeEach, describe, expect, it, vi } from 'vitest';
|
||||
|
||||
const jmapRequest = vi.fn();
|
||||
vi.mock('@/services/jmap/client', () => ({ jmapRequest: (...args: unknown[]) => jmapRequest(...args) }));
|
||||
|
||||
import {
|
||||
createPasskey,
|
||||
fetchPasskeys,
|
||||
fromServer,
|
||||
nameProblem,
|
||||
PasskeyRefused,
|
||||
PasskeysUnavailable,
|
||||
removePasskeys,
|
||||
renamePasskey,
|
||||
sortPasskeys,
|
||||
} from './passkeys';
|
||||
|
||||
beforeEach(() => jmapRequest.mockReset());
|
||||
|
||||
describe('fromServer', () => {
|
||||
it('reads a record, and treats missing flags as false', () => {
|
||||
expect(
|
||||
fromServer({ id: 'p1', name: 'Phone', state: 'active', usable: true, createdAt: '2026-11-18T10:00:00Z' }),
|
||||
).toEqual({
|
||||
id: 'p1',
|
||||
name: 'Phone',
|
||||
state: 'active',
|
||||
usable: true,
|
||||
backedUp: false,
|
||||
createdAt: '2026-11-18T10:00:00Z',
|
||||
lastUsedAt: '',
|
||||
});
|
||||
expect(fromServer({ id: 'p2', state: 'pending', lastUsedAt: null }).state).toBe('pending');
|
||||
});
|
||||
});
|
||||
|
||||
describe('sortPasskeys', () => {
|
||||
it('lists active ones newest first, pending last', () => {
|
||||
const list = [
|
||||
fromServer({ id: 'pending', state: 'pending' }),
|
||||
fromServer({ id: 'old', state: 'active', createdAt: '2026-01-01T00:00:00Z' }),
|
||||
fromServer({ id: 'new', state: 'active', createdAt: '2026-11-01T00:00:00Z' }),
|
||||
];
|
||||
expect(sortPasskeys(list).map((p) => p.id)).toEqual(['new', 'old', 'pending']);
|
||||
});
|
||||
});
|
||||
|
||||
describe('nameProblem', () => {
|
||||
it('needs a name of at most 64 characters', () => {
|
||||
expect(nameProblem(' ')).toBe('empty');
|
||||
expect(nameProblem('x'.repeat(65))).toBe('tooLong');
|
||||
expect(nameProblem(' YubiKey ')).toBeNull();
|
||||
});
|
||||
});
|
||||
|
||||
describe('requests', () => {
|
||||
it('lists an account’s passkeys with its own accountId', async () => {
|
||||
jmapRequest.mockResolvedValue([
|
||||
['inbuxa:Passkey/get', { list: [{ id: 'p1', name: 'Phone', state: 'active' }] }, '0'],
|
||||
]);
|
||||
const list = await fetchPasskeys('acc');
|
||||
expect(list.map((p) => p.name)).toEqual(['Phone']);
|
||||
expect(jmapRequest.mock.calls[0][0]).toEqual([['inbuxa:Passkey/get', { accountId: 'acc', ids: null }, '0']]);
|
||||
expect(jmapRequest.mock.calls[0][2]).toEqual(['urn:inbuxa:jmap']);
|
||||
});
|
||||
|
||||
it('says passkeys are unavailable on an older server or an account out of reach', async () => {
|
||||
jmapRequest.mockResolvedValue([['error', { type: 'unknownMethod' }, '0']]);
|
||||
await expect(fetchPasskeys('acc')).rejects.toBeInstanceOf(PasskeysUnavailable);
|
||||
jmapRequest.mockResolvedValue([['error', { type: 'forbidden' }, '0']]);
|
||||
await expect(fetchPasskeys('acc')).rejects.toBeInstanceOf(PasskeysUnavailable);
|
||||
});
|
||||
|
||||
it('returns the registration page of a new passkey', async () => {
|
||||
jmapRequest.mockResolvedValue([
|
||||
[
|
||||
'inbuxa:Passkey/set',
|
||||
{ created: { p: { id: 'p9', registrationUrl: 'https://mail.example/passkey/register?t=x' } } },
|
||||
'0',
|
||||
],
|
||||
]);
|
||||
await expect(createPasskey('acc', ' Phone ')).resolves.toBe('https://mail.example/passkey/register?t=x');
|
||||
expect(jmapRequest.mock.calls[0][0][0][1]).toEqual({
|
||||
accountId: 'acc',
|
||||
create: { p: { accountId: 'acc', name: 'Phone' } },
|
||||
});
|
||||
});
|
||||
|
||||
it('tells passkeys turned off apart from a full account', async () => {
|
||||
for (const type of ['forbidden', 'overQuota'] as const) {
|
||||
jmapRequest.mockResolvedValue([['inbuxa:Passkey/set', { notCreated: { p: { type } } }, '0']]);
|
||||
const error = await createPasskey('acc', 'Phone').catch((e: unknown) => e);
|
||||
expect(error).toBeInstanceOf(PasskeyRefused);
|
||||
expect((error as PasskeyRefused).kind).toBe(type);
|
||||
}
|
||||
});
|
||||
|
||||
it('renames and removes, and reports a refusal', async () => {
|
||||
jmapRequest.mockResolvedValue([['inbuxa:Passkey/set', { updated: { p1: null } }, '0']]);
|
||||
await renamePasskey('acc', 'p1', 'Laptop');
|
||||
expect(jmapRequest.mock.calls[0][0][0][1]).toEqual({ accountId: 'acc', update: { p1: { name: 'Laptop' } } });
|
||||
|
||||
jmapRequest.mockResolvedValue([['inbuxa:Passkey/set', { destroyed: ['p1', 'p2'] }, '0']]);
|
||||
await removePasskeys('acc', ['p1', 'p2']);
|
||||
expect(jmapRequest.mock.calls[1][0][0][1]).toEqual({ accountId: 'acc', destroy: ['p1', 'p2'] });
|
||||
|
||||
jmapRequest.mockResolvedValue([
|
||||
['inbuxa:Passkey/set', { notDestroyed: { p1: { type: 'forbidden', description: 'No.' } } }, '0'],
|
||||
]);
|
||||
await expect(removePasskeys('acc', ['p1'])).rejects.toThrow('No.');
|
||||
});
|
||||
|
||||
it('sends nothing when there is nothing to remove', async () => {
|
||||
await removePasskeys('acc', []);
|
||||
expect(jmapRequest).not.toHaveBeenCalled();
|
||||
});
|
||||
});
|
||||
@@ -0,0 +1,132 @@
|
||||
/*
|
||||
* SPDX-FileCopyrightText: 2026 Coffey Labs LLC
|
||||
*
|
||||
* SPDX-License-Identifier: AGPL-3.0-only
|
||||
*/
|
||||
|
||||
/**
|
||||
* INBUXA: `inbuxa:Passkey` over JMAP (passkeys spec, PK-5 and PK-14 to
|
||||
* PK-16). The request's `accountId` names the account whose passkeys these
|
||||
* are. Renaming changes only the name; removing one ends every session of
|
||||
* the account, the way a password change does.
|
||||
*/
|
||||
|
||||
import { jmapRequest } from '@/services/jmap/client';
|
||||
|
||||
const CAPABILITY = 'urn:inbuxa:jmap';
|
||||
const OBJECT = 'inbuxa:Passkey';
|
||||
|
||||
/** Longest name the server takes. */
|
||||
export const MAX_NAME = 64;
|
||||
|
||||
export interface Passkey {
|
||||
id: string;
|
||||
name: string;
|
||||
/** `pending` until the browser part of registration completes. */
|
||||
state: 'pending' | 'active';
|
||||
/** Registered for the server's current relying-party id. */
|
||||
usable: boolean;
|
||||
/** The authenticator said the key is synced. */
|
||||
backedUp: boolean;
|
||||
/** RFC 3339, or empty while pending. */
|
||||
createdAt: string;
|
||||
/** RFC 3339, or empty when never used. */
|
||||
lastUsedAt: string;
|
||||
}
|
||||
|
||||
/** The server doesn't offer passkeys (an older server), or this account's aren't in reach. */
|
||||
export class PasskeysUnavailable extends Error {
|
||||
constructor(reason: string) {
|
||||
super(`Passkeys unavailable: ${reason}`);
|
||||
}
|
||||
}
|
||||
|
||||
/** A create the server refused: passkeys are off for the account, or it holds as many as it may. */
|
||||
export class PasskeyRefused extends Error {
|
||||
readonly kind: 'forbidden' | 'overQuota' | 'other';
|
||||
constructor(kind: 'forbidden' | 'overQuota' | 'other', message: string) {
|
||||
super(message);
|
||||
this.kind = kind;
|
||||
}
|
||||
}
|
||||
|
||||
function methodError(name: string | undefined, result: unknown): Error {
|
||||
const r = result as { type?: string; description?: string } | undefined;
|
||||
if (r?.type === 'unknownMethod' || r?.type === 'unknownCapability' || r?.type === 'forbidden')
|
||||
return new PasskeysUnavailable(r.type);
|
||||
return new Error(r?.description ?? r?.type ?? `${name ?? 'Request'} failed`);
|
||||
}
|
||||
|
||||
function str(value: unknown): string {
|
||||
return typeof value === 'string' ? value : '';
|
||||
}
|
||||
|
||||
export function fromServer(raw: Record<string, unknown>): Passkey {
|
||||
return {
|
||||
id: String(raw.id ?? ''),
|
||||
name: str(raw.name),
|
||||
state: raw.state === 'pending' ? 'pending' : 'active',
|
||||
usable: raw.usable === true,
|
||||
backedUp: raw.backedUp === true,
|
||||
createdAt: str(raw.createdAt),
|
||||
lastUsedAt: str(raw.lastUsedAt),
|
||||
};
|
||||
}
|
||||
|
||||
/** Active ones first, newest first; pending ones last. */
|
||||
export function sortPasskeys(list: Passkey[]): Passkey[] {
|
||||
return [...list].sort((a, b) => {
|
||||
if (a.state !== b.state) return a.state === 'active' ? -1 : 1;
|
||||
return b.createdAt.localeCompare(a.createdAt);
|
||||
});
|
||||
}
|
||||
|
||||
async function call(method: string, args: Record<string, unknown>, signal?: AbortSignal) {
|
||||
const responses = await jmapRequest([[`${OBJECT}/${method}`, args, '0']], signal, [CAPABILITY]);
|
||||
const [name, result] = responses[0] ?? [];
|
||||
if (name !== `${OBJECT}/${method}`) throw methodError(name, result);
|
||||
return result as Record<string, unknown>;
|
||||
}
|
||||
|
||||
/** Every passkey of `accountId`. */
|
||||
export async function fetchPasskeys(accountId: string, signal?: AbortSignal): Promise<Passkey[]> {
|
||||
const result = await call('get', { accountId, ids: null }, signal);
|
||||
return sortPasskeys(((result.list as Record<string, unknown>[] | undefined) ?? []).map(fromServer));
|
||||
}
|
||||
|
||||
/** Starts registering a passkey on the signed-in person's own account; returns the page that finishes it. */
|
||||
export async function createPasskey(accountId: string, name: string): Promise<string> {
|
||||
const result = await call('set', { accountId, create: { p: { accountId, name: name.trim() } } });
|
||||
const failed = (result.notCreated as Record<string, { type?: string; description?: string }> | undefined)?.p;
|
||||
if (failed) {
|
||||
const kind = failed.type === 'forbidden' || failed.type === 'overQuota' ? failed.type : 'other';
|
||||
throw new PasskeyRefused(kind, failed.description ?? failed.type ?? 'Not added');
|
||||
}
|
||||
const created = (result.created as Record<string, Record<string, unknown>> | undefined)?.p;
|
||||
const url = str(created?.registrationUrl);
|
||||
if (!url) throw new Error('The server returned no registration page');
|
||||
return url;
|
||||
}
|
||||
|
||||
export async function renamePasskey(accountId: string, id: string, name: string): Promise<void> {
|
||||
const result = await call('set', { accountId, update: { [id]: { name: name.trim() } } });
|
||||
const failed = (result.notUpdated as Record<string, { type?: string; description?: string }> | undefined)?.[id];
|
||||
if (failed) throw new Error(failed.description ?? failed.type ?? 'Not renamed');
|
||||
}
|
||||
|
||||
/** Removes the given passkeys; the account's sessions end when any active one goes. */
|
||||
export async function removePasskeys(accountId: string, ids: string[]): Promise<void> {
|
||||
if (ids.length === 0) return;
|
||||
const result = await call('set', { accountId, destroy: ids });
|
||||
const failed = (result.notDestroyed as Record<string, { type?: string; description?: string }> | undefined) ?? {};
|
||||
const first = Object.values(failed)[0];
|
||||
if (first) throw new Error(first.description ?? first.type ?? 'Not removed');
|
||||
}
|
||||
|
||||
/** What's wrong with a name before it's sent, or null. */
|
||||
export function nameProblem(name: string): 'empty' | 'tooLong' | null {
|
||||
const trimmed = name.trim();
|
||||
if (!trimmed) return 'empty';
|
||||
if ([...trimmed].length > MAX_NAME) return 'tooLong';
|
||||
return null;
|
||||
}
|
||||
@@ -3107,5 +3107,42 @@
|
||||
"noFields": "This entry has no fields.",
|
||||
"onlyComponent": "Only {{component}}",
|
||||
"older": "Show older entries"
|
||||
},
|
||||
"passkeys": {
|
||||
"nameEmpty": "Give the passkey a name.",
|
||||
"nameTooLong": "At most {{max}} characters.",
|
||||
"removeOwnWarning": "Removing a passkey signs you out everywhere, this console included. Your password still works.",
|
||||
"removeWarning": "Removing a passkey signs this account out everywhere. Its password still works.",
|
||||
"title": "Passkeys",
|
||||
"description": "Passkeys sign in on the server’s sign-in page in place of the password. Mail apps that use IMAP or SMTP still use the password or an app password.",
|
||||
"noneOwn": "You have no passkeys yet.",
|
||||
"none": "No passkeys.",
|
||||
"renameFailed": "The passkey wasn’t renamed",
|
||||
"removed": "Passkey removed",
|
||||
"removeFailed": "The passkey wasn’t removed",
|
||||
"name": "Name",
|
||||
"pending": "Not finished",
|
||||
"needsReAdding": "Needs re-adding",
|
||||
"synced": "Synced",
|
||||
"pendingHint": "Registration wasn’t finished. Remove it and add the passkey again.",
|
||||
"added": "Added {{when}}",
|
||||
"lastUsed": "last used {{when}}",
|
||||
"neverUsed": "never used",
|
||||
"needsReAddingHint": "It was added for the server’s earlier name and can’t sign in here any more. Remove it and add a new one.",
|
||||
"rename": "Rename",
|
||||
"remove": "Remove",
|
||||
"removeTitle": "Remove “{{name}}”?",
|
||||
"removePendingWarning": "It was never finished, so nothing else changes.",
|
||||
"allRemoved": "All passkeys removed",
|
||||
"removeAll": "Remove all",
|
||||
"removeAllTitle": "Remove all {{count}} passkeys?",
|
||||
"forbidden": "Passkeys are turned off for this account.",
|
||||
"overQuota": "This account has as many passkeys as it may. Remove one first.",
|
||||
"addFailed": "The passkey wasn’t added",
|
||||
"namePlaceholder": "Name, e.g. Phone or YubiKey",
|
||||
"add": "Add a passkey",
|
||||
"addHint": "Opens the server’s registration page in a new window, where you confirm with your password and then your passkey.",
|
||||
"blocked": "The browser blocked the new window. Open the registration page here; the link works for 10 minutes and only once:",
|
||||
"openRegistration": "Open the registration page"
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user