Skip to content

Open-source document signature platform. Not affiliated with DocuSign.

TypeScript

Keep a new API key whole when its listing fails

Claude3668288ad941
diff --git a/src/components/settings/api-keys-panel.tsx b/src/components/settings/api-keys-panel.tsx
index 4e1674e..ef089b7 100644
--- a/src/components/settings/api-keys-panel.tsx
+++ b/src/components/settings/api-keys-panel.tsx
@@ -38,6 +38,9 @@ function keyState(key: ApiKeySummary, now: number) {
export function ApiKeysPanel() {
const [keys, setKeys] = useState<ApiKeySummary[] | null>(null);
const [error, setError] = useState<string | null>(null);
+ // Why the list did not load, kept apart from the errors of a change so
+ // that making or revoking a key does not hide it, with a way to retry.
+ const [listError, setListError] = useState<string | null>(null);
const [name, setName] = useState("");
const [scope, setScope] = useState<"read" | "write">("write");
const [expiry, setExpiry] = useState("365");
@@ -70,15 +73,16 @@ export function ApiKeysPanel() {
}
if (!response.ok || !payload?.keys) {
- setError(payload?.error ?? GENERIC_ERROR_MESSAGE);
+ setListError(payload?.error ?? GENERIC_ERROR_MESSAGE);
return;
}
setNow(Math.floor(Date.now() / 1000));
setKeys(payload.keys);
+ setListError(null);
} catch {
if (!cancelled) {
- setError(GENERIC_ERROR_MESSAGE);
+ setListError(GENERIC_ERROR_MESSAGE);
}
}
})();
@@ -313,6 +317,26 @@ export function ApiKeysPanel() {
</p>
) : null}
+ {listError ? (
+ <div
+ className="notice notice-danger flex max-w-2xl flex-wrap items-center justify-between gap-2"
+ role="alert"
+ >
+ <span>
+ {keys === null
+ ? `The keys could not be loaded. ${listError}`
+ : `The list may be out of date. ${listError}`}
+ </span>
+ <button
+ className="btn-quiet"
+ onClick={() => setRevision((value) => value + 1)}
+ type="button"
+ >
+ Try again
+ </button>
+ </div>
+ ) : null}
+
{keys === null ? null : keys.length === 0 ? (
<p className="text-sm text-(--secondary)">No keys yet.</p>
) : (
diff --git a/src/lib/api-keys.dbtest.ts b/src/lib/api-keys.dbtest.ts
index 2af693e..19d441e 100644
--- a/src/lib/api-keys.dbtest.ts
+++ b/src/lib/api-keys.dbtest.ts
@@ -285,6 +285,7 @@ test("removing an administrator takes the key rows before the audit lock, as rev
database,
);
const holder = await database.connect();
+ let roleChange: Promise<unknown> | undefined;
try {
await holder.query("begin");
@@ -292,12 +293,10 @@ test("removing an administrator takes the key rows before the audit lock, as rev
summary.id,
]);
- const roleChange = setPersonRole(
- stays.userId,
- leaves.userId,
- "user",
- database,
- );
+ roleChange = setPersonRole(stays.userId, leaves.userId, "user", database);
+ // Handled at once, so a failure before it waits is reported below
+ // instead of as an unhandled rejection.
+ roleChange.catch(() => undefined);
await waitUntilBlockedBy(holder);
@@ -307,9 +306,13 @@ test("removing an administrator takes the key rows before the audit lock, as rev
);
assert.equal(free.rows[0]?.locked, true);
- await holder.query("rollback");
- await roleChange;
} finally {
+ // Let go of the key row whatever happened, and let the role change
+ // finish before the connection goes back to the pool.
+ await holder.query("rollback").catch(() => undefined);
+ await roleChange?.catch(() => undefined);
holder.release();
}
+
+ await roleChange;
});
diff --git a/src/lib/api-keys.ts b/src/lib/api-keys.ts
index d02d314..889d31d 100644
--- a/src/lib/api-keys.ts
+++ b/src/lib/api-keys.ts
@@ -179,7 +179,9 @@ export async function createApiKey(
? null
: createdAt + input.expiresInDays * DAY_IN_SECONDS;
- await withTransaction(async (tx) => {
+ // The summary is read in the same transaction, so a failed read undoes
+ // the key instead of leaving one live whose secret was never shown.
+ const summary = await withTransaction(async (tx) => {
// Held while counting, so two requests at once cannot both slip under
// the limit, and while the role is checked, so a removal that lands at
// the same moment is seen.
@@ -247,13 +249,15 @@ export async function createApiKey(
},
tx,
);
- }, database);
- const summary = await getApiKeySummary(id, database);
+ const created = await getApiKeySummary(id, tx);
- if (!summary) {
- throw new Error("A key that was just created could not be read back.");
- }
+ if (!created) {
+ throw new Error("A key that was just created could not be read back.");
+ }
+
+ return created;
+ }, database);
return { key, summary };
}