Repository Public
1// Devices: certified device list, revocation, approval of a new device, account rekey (danger zone).2import { useCallback, useEffect, useState } from "react";3import type { DeviceView } from "../core/engine.ts";4import { confirmAction, dateTime, engine, onLive, relTime, reportError, toast } from "../state.ts";5import { Badge, Btn, Callout, Dialog, ErrorText, Icon, PageHead, Section, Skeleton, useAction } from "./common.tsx";6import { ApproveDeviceDialog } from "./Pairing.tsx";7import { RecoveryKit } from "./RecoveryKit.tsx";89export function DevicesPage({ onBack }: { onBack: () => void }) {10 const [devices, setDevices] = useState<DeviceView[] | null>(null);11 const [approving, setApproving] = useState(false);12 const [kit, setKit] = useState<string | null>(null);13 const [error, setError] = useState("");14 const act = useAction();15 const load = useCallback(async () => { try { setDevices(await engine.devices()); setError(""); } catch (e) { reportError(e); setError("The device list could not be loaded."); } }, []);16 useEffect(() => { void load(); return onLive(["devices", "pairing"], () => void load()); }, [load]);1718 const revoke = async (d: DeviceView) => {19 if (!(await confirmAction({ title: d.current ? "Revoke this browser?" : `Revoke “${d.platform}”?`, danger: true, confirmLabel: "Revoke device", body: [20 "The server deletes this device's key envelope, certificate and sessions. It can no longer sign in or sync.",21 "Revocation cannot erase what the device already holds: if it was compromised while unlocked, run an account rekey afterwards and change the passwords it could read.",22 d.current ? "This is the browser you are using: it will be signed out and its local data erased." : "Your passkey is requested if it was not used in the last few minutes."] }))) return;23 await act.run(async () => {24 await engine.revokeDevice(d.deviceId);25 if (d.current) { await engine.forget(); location.reload(); return; }26 toast("Device revoked", "ok"); await load();27 });28 };29 const rekey = async () => {30 if (!(await confirmAction({ title: "Rekey the account?", danger: true, confirmLabel: "Rekey account", typeToConfirm: "REKEY", body: [31 "Use this after a device was lost or compromised while it was unlocked. Revoke that device first.",32 "A new account root key is created and wrapped for each remaining certified device; every vault you own gets a new key epoch and is re-encrypted; a NEW recovery key is issued and the old one stops working. Other devices are signed out and must unlock again.",33 "It does not change the passwords stored in the vault: rotate those on their websites as well. Keep this tab open until it finishes."] }))) return;34 await act.run(async () => { await engine.refreshAccount(); setKit(await engine.rekeyAccount()); await load(); });35 };3637 return (38 <div className="page" data-testid="devices-page">39 <PageHead title="Devices" sub="Every device holds its own key pair and a certificate signed by your account key. The server cannot add a device on its own." onBack={onBack}40 actions={<Btn variant="primary" icon="plus" onClick={() => setApproving(true)} data-testid="approve-device">Approve a new device</Btn>} />41 <ErrorText text={error || act.error} />42 {act.busy && <p className="muted row"><span className="spinner" aria-hidden="true" /> Working — keep this tab open…</p>}43 {!devices && !error ? <Skeleton rows={2} /> : devices && (44 <ul className="rows" data-testid="device-list">45 {devices.map((d) => (46 <li key={d.deviceId} className={`row-card device${d.verified ? "" : " bad"}`} data-testid="device-row" data-current={d.current}>47 <span className="device-icon"><Icon name="device" size={22} /></span>48 <div className="row-main">49 <strong>{d.platform} {d.current && <Badge kind="accent">this device</Badge>}</strong>50 <span className="muted">Added {dateTime(d.addedAt)} · last seen {d.current ? "now" : relTime(d.lastSeenAt)}</span>51 <span className="row wrap badges">52 <Badge icon={d.hardware ? "shield" : "key"}>{d.hardware ? "hardware-bound key" : "software key"}</Badge>53 {d.verified ? <Badge kind="ok" icon="check">certificate verified</Badge> : <Badge kind="danger" icon="alert">certificate NOT valid</Badge>}54 <span className="mono fp-short" title="Short fingerprint of this device's public keys">{d.fingerprint.match(/.{4}/g)?.join(" ")}</span>55 </span>56 {!d.verified && <Callout kind="security" title="This device is not certified by your account key"><p>It may have been inserted by the server. Revoke it now and do not approve anything from it.</p></Callout>}57 </div>58 <Btn small variant="danger" onClick={() => revoke(d)} disabled={act.busy} data-testid="device-revoke">Revoke</Btn>59 </li>60 ))}61 </ul>62 )}63 <p className="fine">Web browsers use software keys kept non-extractable in the browser's storage. Native apps keep theirs in hardware (Secure Enclave).</p>6465 <Section title="Danger zone" danger>66 <div className="danger-row">67 <div><strong>Account rekey</strong><p className="muted">For when a device was compromised while unlocked. Creates a new root key, new vault key epochs and a new recovery key, and signs other devices out.</p></div>68 <Btn variant="danger" onClick={rekey} disabled={act.busy} data-testid="rekey">Rekey account…</Btn>69 </div>70 </Section>7172 {approving && <ApproveDeviceDialog onClose={() => setApproving(false)} onApproved={load} />}73 {kit && <KitOverlay recoveryKey={kit} reason="rekeyed" onDone={() => { setKit(null); toast("Account rekeyed. The new recovery key is now the only valid one.", "ok"); }} />}74 </div>75 );76}7778/** Recovery kit in a modal that can only be left by completing the verification. */79export function KitOverlay({ recoveryKey, reason, onDone }: { recoveryKey: string; reason: "rotated" | "rekeyed"; onDone: () => void }) {80 const leave = async () => { if (await confirmAction({ title: "Leave without verifying?", danger: true, confirmLabel: "Leave anyway", body: "The new recovery key will not be shown again. If you did not save it, you will have to issue another one." })) onDone(); };81 return <Dialog title="Recovery kit" wide onClose={leave}><RecoveryKit recoveryKey={recoveryKey} handle={engine.identity?.handle ?? ""} reason={reason} onDone={onDone} /></Dialog>;82}83