Open-source document signature platform. Not affiliated with DocuSign.
TypeScript
Show an API key as stopped while its owner's account is closing
Claude795f8db0167b
diff --git a/src/components/settings/api-keys-panel.tsx b/src/components/settings/api-keys-panel.tsxindex ef089b7..11fc6df 100644--- a/src/components/settings/api-keys-panel.tsx+++ b/src/components/settings/api-keys-panel.tsx@@ -23,16 +23,23 @@ function formatDate(seconds: number | null) {: dateFormatter.format(new Date(seconds * 1000));}+// The same checks the server makes when a key is used, so that a key the+// API turns away never shows as active. Only a key that could still be+// used, or one stopped only by its owner's closing account, can be revoked.function keyState(key: ApiKeySummary, now: number) {if (key.revokedAt !== null) {- return { label: "Revoked", tone: "pill-neutral" };+ return { label: "Revoked", revocable: false, tone: "pill-neutral" };}if (key.expiresAt !== null && key.expiresAt <= now) {- return { label: "Expired", tone: "pill-warning" };+ return { label: "Expired", revocable: false, tone: "pill-warning" };}- return { label: "Active", tone: "pill-success" };+ if (key.ownerClosing) {+ return { label: "Stopped", revocable: true, tone: "pill-warning" };+ }++ return { label: "Active", revocable: true, tone: "pill-success" };}export function ApiKeysPanel() {@@ -367,12 +374,20 @@ export function ApiKeysPanel() {? ` Expires ${expires}.`: ""}</p>- {state.label === "Active" ? (+ {state.label === "Stopped" ? (+ <p className="text-sm text-(--secondary)">+ {key.ownerName} is closing their account, so apps using this+ key are turned away. It is deleted with the account.+ </p>+ ) : null}+ {state.revocable ? (<div className="flex flex-wrap items-center gap-2">{revokingId === key.id ? (<><span className="text-sm text-(--secondary)">- Revoke this key? Apps using it stop working at once.+ {state.label === "Stopped"+ ? "Revoke this key? It can never be used again."+ : "Revoke this key? Apps using it stop working at once."}</span><buttonclassName="btn-quiet btn-quiet-danger"diff --git a/src/lib/api-keys.dbtest.ts b/src/lib/api-keys.dbtest.tsindex 29e6843..fa4c88f 100644--- a/src/lib/api-keys.dbtest.ts+++ b/src/lib/api-keys.dbtest.ts@@ -202,13 +202,15 @@ test("an administrator who loses the role loses their keys for good", async () =test("a key stops working while its owner's account is being closed", async () => {const admin = await createUser();- const { key } = await createApiKey(+ const { key, summary } = await createApiKey(admin,{ expiresInDays: null, name: "Closing", scope: "write" },database,);const now = Math.floor(Date.now() / 1000);+ assert.equal(summary.ownerClosing, false);+await database.query(`insert into account_closure_requests (@@ -219,6 +221,13 @@ test("a key stops working while its owner's account is being closed", async () =);assert.equal(await authenticateApiKey(key, database), null);+ // Settings shows the key as stopped rather than active, so nobody is+ // told a key works while every request with it is turned away.+ const listed = (await listApiKeys(database)).find(+ (row) => row.id === summary.id,+ );+ assert.equal(listed?.ownerClosing, true);+ assert.equal(listed?.revokedAt, null);});test("only an administrator can create or revoke keys", async () => {diff --git a/src/lib/api-keys.ts b/src/lib/api-keys.tsindex 13fbe47..614e260 100644--- a/src/lib/api-keys.ts+++ b/src/lib/api-keys.ts@@ -48,6 +48,11 @@ export type ApiKeySummary = {id: string;lastUsedAt: number | null;name: string;+ /**+ * Whether the owner has asked to close their account. Their keys are+ * turned away from then on and are deleted with the account.+ */+ ownerClosing: boolean;ownerEmail: string;ownerName: string;prefix: string;@@ -87,7 +92,10 @@ const SUMMARY_COLUMNS = `k.expires_at AS "expiresAt",k.revoked_at AS "revokedAt",u.email AS "ownerEmail",- u.name AS "ownerName"+ u.name AS "ownerName",+ EXISTS (+ SELECT 1 FROM account_closure_requests c WHERE c.user_id = k.user_id+ ) AS "ownerClosing"`;function nowInSeconds() {