-
Notifications
You must be signed in to change notification settings - Fork 154
Expand file tree
/
Copy pathapi-keys.tsx
More file actions
281 lines (267 loc) · 10.7 KB
/
Copy pathapi-keys.tsx
File metadata and controls
281 lines (267 loc) · 10.7 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
import { useState } from "react";
import { Exit } from "effect";
import * as AsyncResult from "effect/unstable/reactivity/AsyncResult";
import { useAtomRefresh, useAtomSet, useAtomValue } from "@effect/atom-react";
import { toast } from "sonner";
import { apiKeyWriteKeys } from "../api/reactivity-keys";
import { trackEvent } from "../api/analytics";
import { apiKeysAtom, createApiKey, revokeApiKey } from "../api/account-atoms";
import { Button } from "../components/button";
import { PageContainer, PageHeader } from "../components/page";
import { CopyButton } from "../components/copy-button";
import {
Dialog,
DialogClose,
DialogContent,
DialogDescription,
DialogFooter,
DialogHeader,
DialogTitle,
} from "../components/dialog";
import { Input } from "../components/input";
import { Label } from "../components/label";
import { useExecutorDocumentTitle } from "../lib/document-title";
import { ErrorState } from "../components/error-state";
import { isAsyncResultLoading } from "../lib/async-result";
// ---------------------------------------------------------------------------
// Shared API-keys page. Reads/writes the provider-neutral `/account/api-keys`
// surface, so it works identically on cloud (WorkOS) and self-host (Better
// Auth). API keys are how a user authenticates the Executor API + MCP endpoint
// from scripts/agents (Authorization: Bearer <key>).
// ---------------------------------------------------------------------------
type ApiKeySummary = {
readonly id: string;
readonly name: string;
readonly obfuscatedValue: string;
readonly createdAt: string;
readonly lastUsedAt: string | null;
};
type CreatedKey = ApiKeySummary & { readonly value: string };
const formatDate = (value: string | null): string => {
if (!value) return "Never";
const date = new Date(value);
return Number.isNaN(date.getTime())
? value
: new Intl.DateTimeFormat(undefined, {
month: "short",
day: "numeric",
year: "numeric",
}).format(date);
};
const defaultApiKeyName = (): string =>
`API key ${new Intl.DateTimeFormat(undefined, {
month: "short",
day: "numeric",
year: "numeric",
}).format(new Date())}`;
export function ApiKeysPage() {
useExecutorDocumentTitle("API keys");
const result = useAtomValue(apiKeysAtom);
const refreshApiKeys = useAtomRefresh(apiKeysAtom);
const doCreate = useAtomSet(createApiKey, { mode: "promiseExit" });
const doRevoke = useAtomSet(revokeApiKey, { mode: "promiseExit" });
const [createOpen, setCreateOpen] = useState(false);
const [name, setName] = useState("");
const [createdKey, setCreatedKey] = useState<CreatedKey | null>(null);
const [creating, setCreating] = useState(false);
const [revokingId, setRevokingId] = useState<string | null>(null);
const handleCreate = async () => {
const trimmed = name.trim();
if (!trimmed) return;
setCreating(true);
const exit = await doCreate({ payload: { name: trimmed }, reactivityKeys: apiKeyWriteKeys });
setCreating(false);
trackEvent("api_key_created", { success: Exit.isSuccess(exit) });
if (Exit.isSuccess(exit)) {
setCreatedKey(exit.value);
setName("");
toast.success("API key created");
return;
}
toast.error("Failed to create API key");
};
const handleRevoke = async (key: ApiKeySummary) => {
setRevokingId(key.id);
const exit = await doRevoke({ params: { apiKeyId: key.id }, reactivityKeys: apiKeyWriteKeys });
setRevokingId(null);
trackEvent("api_key_revoked", { success: Exit.isSuccess(exit) });
if (Exit.isSuccess(exit)) {
toast.success(`Revoked ${key.name}`);
return;
}
toast.error("Failed to revoke API key");
};
const closeCreate = (open: boolean) => {
setCreateOpen(open);
if (!open) {
setName("");
setCreatedKey(null);
setCreating(false);
}
};
return (
<PageContainer>
<PageHeader
title="API keys"
description="User keys for accessing the Executor API and MCP endpoint from scripts and tools."
actions={
<Button
onClick={() => {
setName(defaultApiKeyName());
setCreateOpen(true);
}}
>
<span aria-hidden="true">+</span>
New key
</Button>
}
>
<div className="mt-4 flex max-w-2xl items-center gap-2 rounded-md border border-border bg-card px-3 py-2">
<code className="min-w-0 flex-1 truncate font-mono text-xs text-foreground">
Authorization: Bearer <api-key>
</code>
<CopyButton value="Authorization: Bearer <api-key>" />
</div>
<p className="mt-2 max-w-2xl text-xs leading-5 text-muted-foreground">
API keys work like personal access tokens and have full access to your account.
</p>
</PageHeader>
{isAsyncResultLoading(result) ? (
<div className="rounded-md border border-border bg-card p-6 text-sm text-muted-foreground">
Loading API keys...
</div>
) : (
AsyncResult.match(result, {
onInitial: () => (
<div className="rounded-md border border-border bg-card p-6 text-sm text-muted-foreground">
Loading API keys...
</div>
),
onFailure: () => (
<ErrorState message="Failed to load API keys" onRetry={refreshApiKeys} />
),
onSuccess: ({ value }) =>
value.apiKeys.length === 0 ? (
<div className="rounded-md border border-dashed border-border bg-card p-8">
<h2 className="text-base font-semibold text-foreground">No API keys</h2>
<p className="mt-2 max-w-xl text-sm leading-6 text-muted-foreground">
Create a key and send it in the Authorization Bearer header.
</p>
</div>
) : (
<div className="overflow-hidden rounded-md border border-border bg-card">
<div className="grid grid-cols-[1fr_auto] gap-4 border-b border-border px-4 py-3 text-xs font-medium uppercase tracking-wider text-muted-foreground md:grid-cols-[1.4fr_1fr_1fr_auto]">
<span>Name</span>
<span className="hidden md:block">Created</span>
<span className="hidden md:block">Last used</span>
<span className="text-right">Actions</span>
</div>
{value.apiKeys.map((key: ApiKeySummary) => (
<div
key={key.id}
className="grid grid-cols-[1fr_auto] items-center gap-4 border-b border-border px-4 py-4 last:border-b-0 md:grid-cols-[1.4fr_1fr_1fr_auto]"
>
<div className="min-w-0">
<p className="truncate text-sm font-medium text-foreground">{key.name}</p>
<p className="mt-1 font-mono text-xs text-muted-foreground">
{key.obfuscatedValue}
</p>
</div>
<p className="hidden text-sm text-muted-foreground md:block">
{formatDate(key.createdAt)}
</p>
<p className="hidden text-sm text-muted-foreground md:block">
{formatDate(key.lastUsedAt)}
</p>
<Button
variant="ghost"
size="icon-sm"
onClick={() => handleRevoke(key)}
disabled={revokingId === key.id}
title={`Revoke ${key.name}`}
className="text-muted-foreground hover:text-destructive"
>
<span aria-hidden="true">×</span>
</Button>
</div>
))}
</div>
),
})
)}
<Dialog open={createOpen} onOpenChange={closeCreate}>
<DialogContent className="sm:max-w-[480px]">
<DialogHeader>
<DialogTitle className="font-display text-xl">Create API key</DialogTitle>
<DialogDescription className="text-sm leading-relaxed">
The key will act as your user in the current organization.
</DialogDescription>
</DialogHeader>
{createdKey ? (
<div className="grid gap-4 py-3">
<div className="grid gap-1.5">
<Label className="text-sm font-medium text-foreground">New key</Label>
<div className="flex items-center gap-2">
<Input
value={createdKey.value}
readOnly
className="font-mono text-xs"
data-ph-mask
/>
<CopyButton
value={createdKey.value}
onCopy={() => trackEvent("api_key_copied", { kind: "value" })}
/>
</div>
</div>
<div className="grid gap-1.5">
<Label className="text-sm font-medium text-foreground">Bearer header</Label>
<div className="flex items-center gap-2">
<Input
value={`Authorization: Bearer ${createdKey.value}`}
readOnly
className="font-mono text-xs"
data-ph-mask
/>
<CopyButton
value={`Authorization: Bearer ${createdKey.value}`}
onCopy={() => trackEvent("api_key_copied", { kind: "bearer_header" })}
/>
</div>
</div>
<p className="text-sm leading-6 text-muted-foreground">
Send this value as a Bearer token. It is only shown once.
</p>
</div>
) : (
<div className="grid gap-4 py-3">
<div className="grid gap-1.5">
<Label htmlFor="api-key-name" className="text-sm font-medium text-foreground">
Name
</Label>
<Input
id="api-key-name"
value={name}
onChange={(event) => setName(event.target.value)}
placeholder="Local CLI"
maxLength={80}
autoFocus
/>
</div>
</div>
)}
<DialogFooter>
<DialogClose asChild>
<Button variant="ghost">Close</Button>
</DialogClose>
{!createdKey && (
<Button onClick={handleCreate} disabled={creating || !name.trim()}>
{creating ? "Creating..." : "Create key"}
</Button>
)}
</DialogFooter>
</DialogContent>
</Dialog>
</PageContainer>
);
}