-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathApps.tsx
More file actions
435 lines (405 loc) · 13.3 KB
/
Apps.tsx
File metadata and controls
435 lines (405 loc) · 13.3 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
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
import { useState, type FormEvent } from "react";
import { useNavigate } from "react-router-dom";
import { useQuery, useMutation } from "urql";
import { toast } from "sonner";
import { DataTable, type Column } from "@/components/DataTable";
import { Badge } from "@/components/ui/badge";
import { Button } from "@/components/ui/button";
import { Input } from "@/components/ui/input";
import { Label } from "@/components/ui/label";
import {
Dialog,
DialogContent,
DialogDescription,
DialogFooter,
DialogHeader,
DialogTitle,
} from "@/components/ui/dialog";
import { CopyIcon, CheckIcon, PlusIcon } from "lucide-react";
const LIST_APPS_QUERY = `
query ListOauthApplications($first: Int, $after: Cursor) {
allOauthApplications(first: $first, after: $after, orderBy: [CREATED_AT_DESC]) {
totalCount
pageInfo { hasNextPage hasPreviousPage startCursor endCursor }
nodes {
id
name
clientId
type
disabled
scope
createdAt
}
}
}
`;
const CREATE_APP_MUTATION = `
mutation CreateOauthApplication($input: CreateOauthApplicationInput!) {
createOauthApplication(input: $input) {
oauthApplication {
id
name
clientId
clientSecret
type
scope
createdAt
}
}
}
`;
interface OAuthApp {
id: string;
name: string;
clientId: string;
type: string;
disabled: boolean;
scope: string | null;
createdAt: string;
}
interface CreatedApp {
clientId: string;
clientSecret: string;
name: string;
}
function CopyButton({ text }: { text: string }) {
const [copied, setCopied] = useState(false);
function handleCopy() {
navigator.clipboard.writeText(text).then(() => {
setCopied(true);
setTimeout(() => setCopied(false), 2000);
});
}
return (
<Button variant="ghost" size="icon-xs" onClick={handleCopy}>
{copied ? (
<CheckIcon className="size-3" />
) : (
<CopyIcon className="size-3" />
)}
</Button>
);
}
function truncateId(id: string, length = 12): string {
if (id.length <= length) return id;
return id.slice(0, length) + "...";
}
export function Apps() {
const navigate = useNavigate();
const [cursor, setCursor] = useState<string | null>(null);
const [prevCursors, setPrevCursors] = useState<string[]>([]);
const [search, setSearch] = useState("");
const [registerOpen, setRegisterOpen] = useState(false);
const [credentialsDialog, setCredentialsDialog] = useState<CreatedApp | null>(
null
);
const [formName, setFormName] = useState("");
const [formRedirectUris, setFormRedirectUris] = useState("");
const [formType, setFormType] = useState<"confidential" | "public">(
"confidential"
);
const [formScopes, setFormScopes] = useState("");
const [submitting, setSubmitting] = useState(false);
const [result, reexecuteQuery] = useQuery({
query: LIST_APPS_QUERY,
variables: { first: 25, after: cursor },
});
const [, createApp] = useMutation(CREATE_APP_MUTATION);
const apps: OAuthApp[] = result.data?.allOauthApplications?.nodes || [];
const pageInfo = result.data?.allOauthApplications?.pageInfo;
const totalCount = result.data?.allOauthApplications?.totalCount ?? 0;
const filteredApps = search
? apps.filter(
(app) =>
app.name.toLowerCase().includes(search.toLowerCase()) ||
app.clientId.toLowerCase().includes(search.toLowerCase())
)
: apps;
function resetForm() {
setFormName("");
setFormRedirectUris("");
setFormType("confidential");
setFormScopes("");
}
async function handleCreate(e: FormEvent) {
e.preventDefault();
setSubmitting(true);
const redirectUris = formRedirectUris
.split("\n")
.map((uri) => uri.trim())
.filter(Boolean);
if (redirectUris.length === 0) {
toast.error("At least one redirect URI is required.");
setSubmitting(false);
return;
}
try {
const res = await createApp({
input: {
oauthApplication: {
name: formName,
redirectUris: JSON.stringify(redirectUris),
type: formType,
scope: formScopes || null,
},
},
});
if (res.error) {
toast.error(res.error.message);
return;
}
const created = res.data?.createOauthApplication?.oauthApplication;
if (created) {
setRegisterOpen(false);
resetForm();
setCredentialsDialog({
clientId: created.clientId,
clientSecret: created.clientSecret,
name: created.name,
});
reexecuteQuery({ requestPolicy: "network-only" });
toast.success("Application registered.");
}
} catch {
toast.error("Failed to register application.");
} finally {
setSubmitting(false);
}
}
const columns: Column<OAuthApp>[] = [
{
header: "Name",
accessorKey: "name",
},
{
header: "Client ID",
cell: (row) => (
<div className="flex items-center gap-1">
<code className="text-xs font-mono">
{truncateId(row.clientId)}
</code>
<CopyButton text={row.clientId} />
</div>
),
},
{
header: "Type",
cell: (row) => (
<Badge variant={row.type === "confidential" ? "default" : "secondary"}>
{row.type}
</Badge>
),
},
{
header: "Status",
cell: (row) => (
<Badge variant={row.disabled ? "secondary" : "default"}>
{row.disabled ? "Disabled" : "Active"}
</Badge>
),
},
{
header: "Created",
cell: (row) => new Date(row.createdAt).toLocaleDateString(),
},
];
return (
<div className="space-y-4">
<div>
<h2 className="text-2xl font-bold">Applications</h2>
<p className="text-muted-foreground">
OAuth applications ({totalCount} total)
</p>
</div>
<DataTable
columns={columns}
data={filteredApps}
loading={result.fetching}
searchPlaceholder="Search applications..."
searchValue={search}
onSearchChange={setSearch}
emptyMessage="No applications found."
onRowClick={(row) => navigate(`/admin/apps/${row.id}`)}
hasNextPage={pageInfo?.hasNextPage}
hasPreviousPage={prevCursors.length > 0}
onNextPage={() => {
if (pageInfo?.endCursor) {
setPrevCursors((p) => [...p, cursor || ""]);
setCursor(pageInfo.endCursor);
}
}}
onPreviousPage={() => {
const prev = prevCursors[prevCursors.length - 1];
setPrevCursors((p) => p.slice(0, -1));
setCursor(prev || null);
}}
actions={
<Button onClick={() => setRegisterOpen(true)}>
<PlusIcon />
Register Application
</Button>
}
/>
{/* Register Application Dialog */}
<Dialog
open={registerOpen}
onOpenChange={(open) => {
setRegisterOpen(open);
if (!open) resetForm();
}}
>
<DialogContent>
<DialogHeader>
<DialogTitle>Register Application</DialogTitle>
<DialogDescription>
Create a new OAuth application. You will receive a client ID and
secret after registration.
</DialogDescription>
</DialogHeader>
<form onSubmit={handleCreate} className="flex flex-col gap-4">
<div className="flex flex-col gap-2">
<Label htmlFor="app-name">Application Name</Label>
<Input
id="app-name"
placeholder="My Application"
value={formName}
onChange={(e) => setFormName(e.target.value)}
required
/>
</div>
<div className="flex flex-col gap-2">
<Label htmlFor="redirect-uris">Redirect URIs</Label>
<textarea
id="redirect-uris"
className="border-input bg-transparent placeholder:text-muted-foreground flex min-h-[80px] w-full rounded-md border px-3 py-2 text-sm focus-visible:border-ring focus-visible:ring-ring/50 focus-visible:ring-[3px] outline-none disabled:cursor-not-allowed disabled:opacity-50"
placeholder={"https://example.com/callback\nhttps://example.com/auth/callback"}
value={formRedirectUris}
onChange={(e) => setFormRedirectUris(e.target.value)}
required
/>
<p className="text-xs text-muted-foreground">
One URI per line
</p>
</div>
<div className="flex flex-col gap-2">
<Label>Application Type</Label>
<div className="flex gap-4">
<label className="flex items-center gap-2 text-sm cursor-pointer">
<input
type="radio"
name="app-type"
value="confidential"
checked={formType === "confidential"}
onChange={() => setFormType("confidential")}
className="accent-primary"
/>
Confidential
</label>
<label className="flex items-center gap-2 text-sm cursor-pointer">
<input
type="radio"
name="app-type"
value="public"
checked={formType === "public"}
onChange={() => setFormType("public")}
className="accent-primary"
/>
Public
</label>
</div>
<p className="text-xs text-muted-foreground">
Confidential clients can securely store a client secret. Public
clients (SPAs, mobile apps) cannot.
</p>
</div>
<div className="flex flex-col gap-2">
<Label htmlFor="scopes">Scopes</Label>
<Input
id="scopes"
placeholder="openid profile email"
value={formScopes}
onChange={(e) => setFormScopes(e.target.value)}
/>
<p className="text-xs text-muted-foreground">
Space-separated list of allowed scopes
</p>
</div>
<DialogFooter>
<Button
type="button"
variant="outline"
onClick={() => {
setRegisterOpen(false);
resetForm();
}}
>
Cancel
</Button>
<Button type="submit" disabled={submitting}>
{submitting ? "Registering..." : "Register"}
</Button>
</DialogFooter>
</form>
</DialogContent>
</Dialog>
{/* Credentials One-Time View Dialog */}
<Dialog
open={credentialsDialog !== null}
onOpenChange={(open) => {
if (!open) setCredentialsDialog(null);
}}
>
<DialogContent showCloseButton={false}>
<DialogHeader>
<DialogTitle>Application Credentials</DialogTitle>
<DialogDescription>
Save these credentials now. The client secret will not be shown
again.
</DialogDescription>
</DialogHeader>
{credentialsDialog && (
<div className="flex flex-col gap-4">
<div className="flex flex-col gap-2">
<Label className="text-muted-foreground">
Application Name
</Label>
<p className="text-sm font-medium">
{credentialsDialog.name}
</p>
</div>
<div className="flex flex-col gap-2">
<Label className="text-muted-foreground">Client ID</Label>
<div className="flex items-center gap-2 rounded-md bg-muted px-3 py-2">
<code className="text-sm font-mono flex-1 break-all">
{credentialsDialog.clientId}
</code>
<CopyButton text={credentialsDialog.clientId} />
</div>
</div>
<div className="flex flex-col gap-2">
<Label className="text-muted-foreground">Client Secret</Label>
<div className="flex items-center gap-2 rounded-md bg-muted px-3 py-2">
<code className="text-sm font-mono flex-1 break-all">
{credentialsDialog.clientSecret}
</code>
<CopyButton text={credentialsDialog.clientSecret} />
</div>
</div>
<div className="rounded-md border border-destructive/50 bg-destructive/10 p-3">
<p className="text-sm text-destructive">
Make sure to copy the client secret. You will not be able to
see it again.
</p>
</div>
</div>
)}
<DialogFooter>
<Button onClick={() => setCredentialsDialog(null)}>
I have saved the credentials
</Button>
</DialogFooter>
</DialogContent>
</Dialog>
</div>
);
}