bwh-auth 0.3.2 → 0.4.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +30 -0
- package/dist/index.cjs +538 -61
- package/dist/index.d.cts +72 -2
- package/dist/index.d.ts +72 -2
- package/dist/index.js +536 -61
- package/package.json +1 -1
package/dist/index.js
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
// src/forms.tsx
|
|
2
|
-
import * as
|
|
2
|
+
import * as React3 from "react";
|
|
3
3
|
|
|
4
4
|
// src/components.ts
|
|
5
5
|
var requiredComponentKeys = [
|
|
@@ -26,10 +26,17 @@ function resolveAuthButtonComponent(components) {
|
|
|
26
26
|
return { Button: components.Button };
|
|
27
27
|
}
|
|
28
28
|
|
|
29
|
-
// src/passkey-
|
|
30
|
-
import { KeyRound } from "lucide-react";
|
|
29
|
+
// src/passkey-authentication.ts
|
|
31
30
|
import * as React from "react";
|
|
32
31
|
|
|
32
|
+
// src/abort-utils.ts
|
|
33
|
+
function throwIfAborted(signal) {
|
|
34
|
+
if (!signal?.aborted) return;
|
|
35
|
+
const error = new Error("Passkey authentication was cancelled");
|
|
36
|
+
error.name = "AbortError";
|
|
37
|
+
throw error;
|
|
38
|
+
}
|
|
39
|
+
|
|
33
40
|
// src/webauthn-utils.ts
|
|
34
41
|
function getCsrfToken(explicitToken) {
|
|
35
42
|
if (explicitToken) {
|
|
@@ -56,7 +63,7 @@ function arrayBufferToBase64url(buffer) {
|
|
|
56
63
|
return window.btoa(binary).replace(/\+/g, "-").replace(/\//g, "_").replace(/=/g, "");
|
|
57
64
|
}
|
|
58
65
|
function isAbortError(error) {
|
|
59
|
-
return error
|
|
66
|
+
return error !== null && typeof error === "object" && "name" in error && error.name === "AbortError";
|
|
60
67
|
}
|
|
61
68
|
function getDefaultPasskeyName() {
|
|
62
69
|
if (typeof window === "undefined") return "Passkey";
|
|
@@ -99,6 +106,7 @@ async function authenticateWithPasskey({ endpoints = {}, mediation, signal } = {
|
|
|
99
106
|
throw new Error("Failed to get authentication options");
|
|
100
107
|
}
|
|
101
108
|
const options = await optRes.json();
|
|
109
|
+
throwIfAborted(signal);
|
|
102
110
|
const publicKey = {
|
|
103
111
|
...options,
|
|
104
112
|
challenge: base64urlToArrayBuffer(options.challenge),
|
|
@@ -108,6 +116,7 @@ async function authenticateWithPasskey({ endpoints = {}, mediation, signal } = {
|
|
|
108
116
|
}))
|
|
109
117
|
};
|
|
110
118
|
const credential = await navigator.credentials.get({ publicKey, mediation, signal });
|
|
119
|
+
throwIfAborted(signal);
|
|
111
120
|
if (!credential || credential.type !== "public-key") {
|
|
112
121
|
throw new Error("No passkey selected");
|
|
113
122
|
}
|
|
@@ -202,31 +211,87 @@ async function registerPasskey({ endpoints = {}, name, signal } = {}) {
|
|
|
202
211
|
return { result };
|
|
203
212
|
}
|
|
204
213
|
|
|
214
|
+
// src/passkey-authentication.ts
|
|
215
|
+
var PasskeyAuthenticationContext = React.createContext(null);
|
|
216
|
+
function usePasskeyAuthentication() {
|
|
217
|
+
const pending = React.useRef(null);
|
|
218
|
+
const cancel = React.useCallback(async (conditionalOnly = false) => {
|
|
219
|
+
const request = pending.current;
|
|
220
|
+
if (!request || conditionalOnly && !request.conditional) return;
|
|
221
|
+
request.controller.abort();
|
|
222
|
+
await request.promise.catch(() => void 0);
|
|
223
|
+
}, []);
|
|
224
|
+
const authenticate = React.useCallback(async (options = {}) => {
|
|
225
|
+
const previous = pending.current;
|
|
226
|
+
const controller = new AbortController();
|
|
227
|
+
previous?.controller.abort();
|
|
228
|
+
const promise = (async () => {
|
|
229
|
+
if (previous) await previous.promise.catch(() => void 0);
|
|
230
|
+
throwIfAborted(controller.signal);
|
|
231
|
+
const result = await authenticateWithPasskey({ ...options, signal: controller.signal });
|
|
232
|
+
throwIfAborted(controller.signal);
|
|
233
|
+
return result;
|
|
234
|
+
})();
|
|
235
|
+
const request = { controller, promise, conditional: options.mediation === "conditional" };
|
|
236
|
+
pending.current = request;
|
|
237
|
+
try {
|
|
238
|
+
return await promise;
|
|
239
|
+
} finally {
|
|
240
|
+
if (pending.current === request) pending.current = null;
|
|
241
|
+
}
|
|
242
|
+
}, []);
|
|
243
|
+
React.useEffect(() => () => {
|
|
244
|
+
void cancel();
|
|
245
|
+
}, [cancel]);
|
|
246
|
+
return React.useMemo(() => ({ authenticate, cancel }), [authenticate, cancel]);
|
|
247
|
+
}
|
|
248
|
+
|
|
205
249
|
// src/passkey-login-button.tsx
|
|
250
|
+
import { KeyRound } from "lucide-react";
|
|
251
|
+
import * as React2 from "react";
|
|
206
252
|
import { jsx, jsxs } from "react/jsx-runtime";
|
|
207
|
-
function PasskeyLoginButton({ endpoints = {}, components, className, onSuccess, onError }) {
|
|
253
|
+
function PasskeyLoginButton({ endpoints = {}, components, className, disabled = false, onSuccess, onError }) {
|
|
208
254
|
const { Button } = resolveAuthButtonComponent(components);
|
|
209
|
-
const [loading, setLoading] =
|
|
210
|
-
const [error, setError] =
|
|
255
|
+
const [loading, setLoading] = React2.useState(false);
|
|
256
|
+
const [error, setError] = React2.useState(null);
|
|
257
|
+
const localAuthentication = usePasskeyAuthentication();
|
|
258
|
+
const authentication = React2.useContext(PasskeyAuthenticationContext) ?? localAuthentication;
|
|
259
|
+
const inFlight = React2.useRef(false);
|
|
260
|
+
const mounted = React2.useRef(true);
|
|
261
|
+
const callbacks = React2.useRef({ onSuccess, onError });
|
|
262
|
+
React2.useLayoutEffect(() => {
|
|
263
|
+
callbacks.current = { onSuccess, onError };
|
|
264
|
+
});
|
|
265
|
+
React2.useEffect(() => {
|
|
266
|
+
mounted.current = true;
|
|
267
|
+
return () => {
|
|
268
|
+
mounted.current = false;
|
|
269
|
+
if (inFlight.current) void authentication.cancel();
|
|
270
|
+
};
|
|
271
|
+
}, [authentication]);
|
|
211
272
|
async function handlePasskeyLogin() {
|
|
273
|
+
if (disabled || inFlight.current) return;
|
|
274
|
+
inFlight.current = true;
|
|
212
275
|
setError(null);
|
|
213
276
|
setLoading(true);
|
|
214
277
|
try {
|
|
215
|
-
const { redirectUrl, result } = await
|
|
216
|
-
if (
|
|
217
|
-
|
|
278
|
+
const { redirectUrl, result } = await authentication.authenticate({ endpoints });
|
|
279
|
+
if (!mounted.current) return;
|
|
280
|
+
if (callbacks.current.onSuccess) {
|
|
281
|
+
callbacks.current.onSuccess(redirectUrl, result);
|
|
218
282
|
} else {
|
|
219
283
|
window.location.href = redirectUrl;
|
|
220
284
|
}
|
|
221
285
|
} catch (caughtError) {
|
|
222
|
-
if (isAbortError(caughtError)) {
|
|
286
|
+
if (!mounted.current || isAbortError(caughtError)) {
|
|
223
287
|
return;
|
|
224
288
|
}
|
|
225
289
|
const message = caughtError instanceof Error ? caughtError.message : "Passkey login failed";
|
|
226
|
-
|
|
227
|
-
|
|
290
|
+
if (callbacks.current.onError) callbacks.current.onError(message);
|
|
291
|
+
else setError(message);
|
|
228
292
|
} finally {
|
|
229
|
-
|
|
293
|
+
inFlight.current = false;
|
|
294
|
+
if (mounted.current) setLoading(false);
|
|
230
295
|
}
|
|
231
296
|
}
|
|
232
297
|
if (typeof window === "undefined" || !window.PublicKeyCredential) {
|
|
@@ -234,7 +299,7 @@ function PasskeyLoginButton({ endpoints = {}, components, className, onSuccess,
|
|
|
234
299
|
}
|
|
235
300
|
return /* @__PURE__ */ jsxs("div", { className, children: [
|
|
236
301
|
error ? /* @__PURE__ */ jsx("p", { className: "mb-2 text-sm text-destructive", children: error }) : null,
|
|
237
|
-
/* @__PURE__ */ jsxs(Button, { type: "button", variant: "outline", className: "w-full", disabled: loading, onClick: handlePasskeyLogin, children: [
|
|
302
|
+
/* @__PURE__ */ jsxs(Button, { type: "button", variant: "outline", className: "w-full", disabled: disabled || loading, onClick: handlePasskeyLogin, children: [
|
|
238
303
|
/* @__PURE__ */ jsx(KeyRound, { "aria-hidden": "true" }),
|
|
239
304
|
loading ? "Verifying..." : "Sign in with Passkey"
|
|
240
305
|
] })
|
|
@@ -304,53 +369,70 @@ function LoginForm({
|
|
|
304
369
|
enablePasskeyAutofill = enablePasskeys
|
|
305
370
|
}) {
|
|
306
371
|
const { Button, Card, CardContent, CardHeader, CardTitle, Input, Label } = resolveAuthComponents(components);
|
|
307
|
-
const [email, setEmail] =
|
|
308
|
-
const [password, setPassword] =
|
|
309
|
-
const [remember, setRemember] =
|
|
310
|
-
const [loading, setLoading] =
|
|
311
|
-
const [conditionalPasskeyAvailable, setConditionalPasskeyAvailable] =
|
|
312
|
-
const
|
|
372
|
+
const [email, setEmail] = React3.useState(initialEmail);
|
|
373
|
+
const [password, setPassword] = React3.useState("");
|
|
374
|
+
const [remember, setRemember] = React3.useState(false);
|
|
375
|
+
const [loading, setLoading] = React3.useState(false);
|
|
376
|
+
const [conditionalPasskeyAvailable, setConditionalPasskeyAvailable] = React3.useState(false);
|
|
377
|
+
const passkeyAuthentication = usePasskeyAuthentication();
|
|
378
|
+
const conditionalInterrupted = React3.useRef(false);
|
|
379
|
+
const submitting = React3.useRef(false);
|
|
380
|
+
const callbacks = React3.useRef({ onError, onPasskeySuccess });
|
|
381
|
+
React3.useLayoutEffect(() => {
|
|
382
|
+
callbacks.current = { onError, onPasskeySuccess };
|
|
383
|
+
});
|
|
384
|
+
const coordinatedAuthentication = React3.useMemo(() => ({
|
|
385
|
+
...passkeyAuthentication,
|
|
386
|
+
authenticate: (options) => {
|
|
387
|
+
if (options?.mediation !== "conditional") conditionalInterrupted.current = true;
|
|
388
|
+
return passkeyAuthentication.authenticate(options);
|
|
389
|
+
}
|
|
390
|
+
}), [passkeyAuthentication]);
|
|
391
|
+
const passkeyEndpoints = React3.useMemo(() => ({
|
|
313
392
|
csrfToken: endpoints.csrfToken,
|
|
314
393
|
passkeyAuth: endpoints.passkeyAuth,
|
|
315
394
|
passkeyAuthOptions: endpoints.passkeyAuthOptions
|
|
316
395
|
}), [endpoints.csrfToken, endpoints.passkeyAuth, endpoints.passkeyAuthOptions]);
|
|
317
|
-
|
|
318
|
-
|
|
319
|
-
|
|
396
|
+
React3.useEffect(() => {
|
|
397
|
+
setConditionalPasskeyAvailable(false);
|
|
398
|
+
if (!enablePasskeyAutofill || conditionalInterrupted.current) return;
|
|
320
399
|
let active = true;
|
|
321
400
|
async function startConditionalPasskeyLogin() {
|
|
322
401
|
const available = await isConditionalMediationAvailable();
|
|
323
|
-
if (!available || !active) return;
|
|
402
|
+
if (!available || !active || conditionalInterrupted.current) return;
|
|
324
403
|
setConditionalPasskeyAvailable(true);
|
|
325
404
|
try {
|
|
326
|
-
const { redirectUrl, result } = await
|
|
405
|
+
const { redirectUrl, result } = await coordinatedAuthentication.authenticate({
|
|
327
406
|
endpoints: passkeyEndpoints,
|
|
328
|
-
mediation: "conditional"
|
|
329
|
-
signal: abortController.signal
|
|
407
|
+
mediation: "conditional"
|
|
330
408
|
});
|
|
331
409
|
if (!active) return;
|
|
332
|
-
if (onPasskeySuccess) {
|
|
333
|
-
onPasskeySuccess(redirectUrl, result);
|
|
410
|
+
if (callbacks.current.onPasskeySuccess) {
|
|
411
|
+
callbacks.current.onPasskeySuccess(redirectUrl, result);
|
|
334
412
|
} else {
|
|
335
413
|
window.location.href = redirectUrl;
|
|
336
414
|
}
|
|
337
415
|
} catch (error) {
|
|
338
416
|
if (!isAbortError(error) && active) {
|
|
339
|
-
onError?.(error instanceof Error ? error.message : "Passkey login failed");
|
|
417
|
+
callbacks.current.onError?.(error instanceof Error ? error.message : "Passkey login failed");
|
|
340
418
|
}
|
|
341
419
|
}
|
|
342
420
|
}
|
|
343
421
|
void startConditionalPasskeyLogin();
|
|
344
422
|
return () => {
|
|
345
423
|
active = false;
|
|
346
|
-
|
|
424
|
+
void coordinatedAuthentication.cancel(true);
|
|
347
425
|
};
|
|
348
|
-
}, [enablePasskeyAutofill,
|
|
426
|
+
}, [enablePasskeyAutofill, coordinatedAuthentication, passkeyEndpoints]);
|
|
349
427
|
async function onSubmit(event) {
|
|
350
428
|
event.preventDefault();
|
|
429
|
+
if (submitting.current) return;
|
|
430
|
+
submitting.current = true;
|
|
431
|
+
conditionalInterrupted.current = true;
|
|
351
432
|
onSubmitStart?.();
|
|
352
433
|
setLoading(true);
|
|
353
434
|
try {
|
|
435
|
+
await passkeyAuthentication.cancel();
|
|
354
436
|
const result = await postForm(endpoints.login ?? "/login", { email, password, remember }, endpoints.csrfToken);
|
|
355
437
|
if (result.requires_2fa) {
|
|
356
438
|
onTwoFactorRequired?.(result);
|
|
@@ -364,6 +446,7 @@ function LoginForm({
|
|
|
364
446
|
} catch (error) {
|
|
365
447
|
onError?.(error instanceof Error ? error.message : "Login failed");
|
|
366
448
|
} finally {
|
|
449
|
+
submitting.current = false;
|
|
367
450
|
setLoading(false);
|
|
368
451
|
}
|
|
369
452
|
}
|
|
@@ -407,11 +490,12 @@ function LoginForm({
|
|
|
407
490
|
] }),
|
|
408
491
|
/* @__PURE__ */ jsx2(Button, { type: "submit", className: "w-full", disabled: loading, children: loading ? "Signing in..." : "Sign In" })
|
|
409
492
|
] }),
|
|
410
|
-
enablePasskeys ? /* @__PURE__ */ jsx2("div", { className: "mt-4", children: /* @__PURE__ */ jsx2(
|
|
493
|
+
enablePasskeys ? /* @__PURE__ */ jsx2("div", { className: "mt-4", children: /* @__PURE__ */ jsx2(PasskeyAuthenticationContext.Provider, { value: coordinatedAuthentication, children: /* @__PURE__ */ jsx2(
|
|
411
494
|
PasskeyLoginButton,
|
|
412
495
|
{
|
|
413
496
|
components: { Button },
|
|
414
497
|
endpoints,
|
|
498
|
+
disabled: loading,
|
|
415
499
|
onSuccess: (redirectUrl, result) => {
|
|
416
500
|
if (onPasskeySuccess) {
|
|
417
501
|
onPasskeySuccess(redirectUrl, result);
|
|
@@ -421,7 +505,7 @@ function LoginForm({
|
|
|
421
505
|
},
|
|
422
506
|
onError
|
|
423
507
|
}
|
|
424
|
-
) }) : null
|
|
508
|
+
) }) }) : null
|
|
425
509
|
] })
|
|
426
510
|
] });
|
|
427
511
|
}
|
|
@@ -453,11 +537,11 @@ function SignupForm({
|
|
|
453
537
|
submittingLabel = "Creating account..."
|
|
454
538
|
}) {
|
|
455
539
|
const { Button, Card, CardContent, CardDescription, CardHeader, CardTitle, Input, Label } = resolveAuthComponents(components);
|
|
456
|
-
const [values, setValues] =
|
|
540
|
+
const [values, setValues] = React3.useState(() => Object.fromEntries(
|
|
457
541
|
fields.map((field) => [field.name, initialSignupValue(field, initialValues)])
|
|
458
542
|
));
|
|
459
|
-
const [fieldErrors, setFieldErrors] =
|
|
460
|
-
const [loading, setLoading] =
|
|
543
|
+
const [fieldErrors, setFieldErrors] = React3.useState(errors);
|
|
544
|
+
const [loading, setLoading] = React3.useState(false);
|
|
461
545
|
async function onSubmit(event) {
|
|
462
546
|
setLoading(true);
|
|
463
547
|
if (submitMode === "native") {
|
|
@@ -544,8 +628,8 @@ function SignupForm({
|
|
|
544
628
|
}
|
|
545
629
|
function PasswordResetRequestForm({ endpoints = {}, components, onSuccess, onError }) {
|
|
546
630
|
const { Button, Input, Label } = resolveAuthComponents(components);
|
|
547
|
-
const [email, setEmail] =
|
|
548
|
-
const [loading, setLoading] =
|
|
631
|
+
const [email, setEmail] = React3.useState("");
|
|
632
|
+
const [loading, setLoading] = React3.useState(false);
|
|
549
633
|
async function onSubmit(event) {
|
|
550
634
|
event.preventDefault();
|
|
551
635
|
setLoading(true);
|
|
@@ -568,10 +652,10 @@ function PasswordResetRequestForm({ endpoints = {}, components, onSuccess, onErr
|
|
|
568
652
|
}
|
|
569
653
|
function ResetPasswordForm({ endpoints = {}, components, onSuccess, onError, token: initialToken = "", email: initialEmail = "" }) {
|
|
570
654
|
const { Button, Input, Label } = resolveAuthComponents(components);
|
|
571
|
-
const [email, setEmail] =
|
|
572
|
-
const [token, setToken] =
|
|
573
|
-
const [password, setPassword] =
|
|
574
|
-
const [passwordConfirmation, setPasswordConfirmation] =
|
|
655
|
+
const [email, setEmail] = React3.useState(initialEmail);
|
|
656
|
+
const [token, setToken] = React3.useState(initialToken);
|
|
657
|
+
const [password, setPassword] = React3.useState("");
|
|
658
|
+
const [passwordConfirmation, setPasswordConfirmation] = React3.useState("");
|
|
575
659
|
async function onSubmit(event) {
|
|
576
660
|
event.preventDefault();
|
|
577
661
|
try {
|
|
@@ -605,10 +689,10 @@ function ResetPasswordForm({ endpoints = {}, components, onSuccess, onError, tok
|
|
|
605
689
|
}
|
|
606
690
|
function ChangePasswordForm({ endpoints = {}, components, onSuccess, onError }) {
|
|
607
691
|
const { Button, Input, Label } = resolveAuthComponents(components);
|
|
608
|
-
const [currentPassword, setCurrentPassword] =
|
|
609
|
-
const [password, setPassword] =
|
|
610
|
-
const [passwordConfirmation, setPasswordConfirmation] =
|
|
611
|
-
const [loading, setLoading] =
|
|
692
|
+
const [currentPassword, setCurrentPassword] = React3.useState("");
|
|
693
|
+
const [password, setPassword] = React3.useState("");
|
|
694
|
+
const [passwordConfirmation, setPasswordConfirmation] = React3.useState("");
|
|
695
|
+
const [loading, setLoading] = React3.useState(false);
|
|
612
696
|
async function onSubmit(event) {
|
|
613
697
|
event.preventDefault();
|
|
614
698
|
setLoading(true);
|
|
@@ -651,12 +735,12 @@ function ChangePasswordForm({ endpoints = {}, components, onSuccess, onError })
|
|
|
651
735
|
}
|
|
652
736
|
function TwoFactorForm({ endpoints = {}, components, attemptToken, appEnv, onSuccess, onError, onReportSuspicious }) {
|
|
653
737
|
const { Button, Card, CardContent, CardDescription, CardHeader, CardTitle, Input, Label } = resolveAuthComponents(components);
|
|
654
|
-
const [currentAttemptToken, setCurrentAttemptToken] =
|
|
655
|
-
const [code, setCode] =
|
|
656
|
-
const [loading, setLoading] =
|
|
657
|
-
const [resending, setResending] =
|
|
658
|
-
const [message, setMessage] =
|
|
659
|
-
|
|
738
|
+
const [currentAttemptToken, setCurrentAttemptToken] = React3.useState(attemptToken);
|
|
739
|
+
const [code, setCode] = React3.useState("");
|
|
740
|
+
const [loading, setLoading] = React3.useState(false);
|
|
741
|
+
const [resending, setResending] = React3.useState(false);
|
|
742
|
+
const [message, setMessage] = React3.useState("");
|
|
743
|
+
React3.useEffect(() => setCurrentAttemptToken(attemptToken), [attemptToken]);
|
|
660
744
|
async function onSubmit(event) {
|
|
661
745
|
event.preventDefault();
|
|
662
746
|
setLoading(true);
|
|
@@ -738,20 +822,20 @@ function TwoFactorForm({ endpoints = {}, components, attemptToken, appEnv, onSuc
|
|
|
738
822
|
|
|
739
823
|
// src/passkey-section.tsx
|
|
740
824
|
import { Key, Plus, Trash2 } from "lucide-react";
|
|
741
|
-
import * as
|
|
825
|
+
import * as React4 from "react";
|
|
742
826
|
import { flushSync } from "react-dom";
|
|
743
827
|
import { jsx as jsx3, jsxs as jsxs3 } from "react/jsx-runtime";
|
|
744
828
|
function PasskeySection({ endpoints = {}, components, onSuccess, onError }) {
|
|
745
829
|
const { Button, Card, CardContent, CardDescription, CardHeader, CardTitle, Input, Label } = resolveAuthComponents(components);
|
|
746
|
-
const [passkeys, setPasskeys] =
|
|
747
|
-
const [loading, setLoading] =
|
|
748
|
-
const [registering, setRegistering] =
|
|
749
|
-
const [pendingName, setPendingName] =
|
|
830
|
+
const [passkeys, setPasskeys] = React4.useState([]);
|
|
831
|
+
const [loading, setLoading] = React4.useState(true);
|
|
832
|
+
const [registering, setRegistering] = React4.useState(false);
|
|
833
|
+
const [pendingName, setPendingName] = React4.useState("");
|
|
750
834
|
const listUrl = endpoints.passkeyList ?? "/api/passkeys";
|
|
751
835
|
const registerOptionsUrl = endpoints.passkeyRegisterOptions ?? "/api/passkeys/register/options";
|
|
752
836
|
const registerUrl = endpoints.passkeyRegister ?? "/api/passkeys/register";
|
|
753
837
|
const deleteUrl = endpoints.passkeyDelete ?? ((id) => `/api/passkeys/${id}`);
|
|
754
|
-
const fetchPasskeys =
|
|
838
|
+
const fetchPasskeys = React4.useCallback(async () => {
|
|
755
839
|
try {
|
|
756
840
|
const res = await fetch(listUrl);
|
|
757
841
|
if (res.ok) {
|
|
@@ -763,7 +847,7 @@ function PasskeySection({ endpoints = {}, components, onSuccess, onError }) {
|
|
|
763
847
|
setLoading(false);
|
|
764
848
|
}
|
|
765
849
|
}, [listUrl, onError]);
|
|
766
|
-
|
|
850
|
+
React4.useEffect(() => {
|
|
767
851
|
void fetchPasskeys();
|
|
768
852
|
}, [fetchPasskeys]);
|
|
769
853
|
async function registerPasskey2() {
|
|
@@ -858,7 +942,397 @@ function relyingApplicationsFrom(value) {
|
|
|
858
942
|
return href === null ? [] : [{ key, name: name.trim(), url: href }];
|
|
859
943
|
});
|
|
860
944
|
}
|
|
945
|
+
|
|
946
|
+
// src/api-credentials-section.tsx
|
|
947
|
+
import * as React5 from "react";
|
|
948
|
+
import { Fragment, jsx as jsx4, jsxs as jsxs4 } from "react/jsx-runtime";
|
|
949
|
+
function ApiCredentialsSection({ indexUrl, components, links = [], csrfToken, onSuccess, onError }) {
|
|
950
|
+
const { Card, CardContent, CardDescription, CardHeader, CardTitle } = resolveAuthComponents(components);
|
|
951
|
+
const [index, setIndex] = React5.useState(null);
|
|
952
|
+
const [issued, setIssued] = React5.useState([]);
|
|
953
|
+
const issuedKey = React5.useRef(0);
|
|
954
|
+
const addIssued = React5.useCallback((credential) => {
|
|
955
|
+
issuedKey.current += 1;
|
|
956
|
+
const key = issuedKey.current;
|
|
957
|
+
setIssued((current) => [...current, { key, credential }]);
|
|
958
|
+
}, []);
|
|
959
|
+
const [loadError, setLoadError] = React5.useState(null);
|
|
960
|
+
const onErrorRef = React5.useRef(onError);
|
|
961
|
+
onErrorRef.current = onError;
|
|
962
|
+
const generation = React5.useRef(0);
|
|
963
|
+
const context = React5.useRef({ indexUrl, csrfToken });
|
|
964
|
+
context.current = { indexUrl, csrfToken };
|
|
965
|
+
const reload = React5.useCallback(async () => {
|
|
966
|
+
generation.current += 1;
|
|
967
|
+
const mine = generation.current;
|
|
968
|
+
const { indexUrl: url, csrfToken: token } = context.current;
|
|
969
|
+
const result = await credentialRequest("GET", url, void 0, token);
|
|
970
|
+
if (mine !== generation.current) {
|
|
971
|
+
return;
|
|
972
|
+
}
|
|
973
|
+
if (result.ok) {
|
|
974
|
+
setIndex(result.data);
|
|
975
|
+
setLoadError(null);
|
|
976
|
+
} else {
|
|
977
|
+
setLoadError(result.message);
|
|
978
|
+
onErrorRef.current?.("api-credentials", result.message);
|
|
979
|
+
}
|
|
980
|
+
}, []);
|
|
981
|
+
React5.useEffect(() => {
|
|
982
|
+
setIndex(null);
|
|
983
|
+
void reload();
|
|
984
|
+
}, [indexUrl, csrfToken, reload]);
|
|
985
|
+
const removeLocally = React5.useCallback((list, id) => {
|
|
986
|
+
setIndex(
|
|
987
|
+
(current) => current === null ? current : list === "tokens" ? { ...current, tokens: current.tokens.filter((token) => token.id !== id) } : { ...current, apps: current.apps.filter((app) => app.id !== id) }
|
|
988
|
+
);
|
|
989
|
+
}, []);
|
|
990
|
+
const shared = { components, csrfToken, onError, onSuccess, removeLocally };
|
|
991
|
+
return /* @__PURE__ */ jsxs4(Card, { children: [
|
|
992
|
+
/* @__PURE__ */ jsxs4(CardHeader, { children: [
|
|
993
|
+
/* @__PURE__ */ jsx4(CardTitle, { children: "API access" }),
|
|
994
|
+
/* @__PURE__ */ jsx4(CardDescription, { children: "Connect apps that use the API. Register an OAuth app for apps that sign you in, or create an API token for apps that ask for a key. Each carries only the permissions you choose, and never more than your own access." })
|
|
995
|
+
] }),
|
|
996
|
+
/* @__PURE__ */ jsx4(CardContent, { children: /* @__PURE__ */ jsxs4("div", { style: { display: "grid", gap: "1.5rem", minWidth: 0 }, children: [
|
|
997
|
+
links.length > 0 && /* @__PURE__ */ jsx4("dl", { style: { display: "grid", gap: "0.5rem", minWidth: 0 }, children: links.map((link) => /* @__PURE__ */ jsxs4("div", { style: { minWidth: 0 }, children: [
|
|
998
|
+
/* @__PURE__ */ jsx4("dt", { children: link.label }),
|
|
999
|
+
/* @__PURE__ */ jsxs4("dd", { style: { margin: 0, overflowWrap: "anywhere" }, children: [
|
|
1000
|
+
/* @__PURE__ */ jsx4("code", { children: link.url }),
|
|
1001
|
+
" ",
|
|
1002
|
+
/* @__PURE__ */ jsx4(CopyButton, { text: link.url, label: link.label, components })
|
|
1003
|
+
] })
|
|
1004
|
+
] }, link.label)) }),
|
|
1005
|
+
issued.map(({ key, credential }) => /* @__PURE__ */ jsx4(
|
|
1006
|
+
IssuedNotice,
|
|
1007
|
+
{
|
|
1008
|
+
issued: credential,
|
|
1009
|
+
components,
|
|
1010
|
+
onDismiss: () => setIssued((current) => current.filter((entry) => entry.key !== key))
|
|
1011
|
+
},
|
|
1012
|
+
key
|
|
1013
|
+
)),
|
|
1014
|
+
loadError !== null && /* @__PURE__ */ jsx4("p", { role: "alert", children: loadError }),
|
|
1015
|
+
index !== null && /* @__PURE__ */ jsxs4(Fragment, { children: [
|
|
1016
|
+
(index.issue_token_href === null || index.register_app_href === null) && /* @__PURE__ */ jsx4("p", { role: "status", children: "Creating new credentials is unavailable right now. You can still revoke existing ones." }),
|
|
1017
|
+
/* @__PURE__ */ jsx4(TokenSection, { index, ...shared, onIssued: addIssued, reload }),
|
|
1018
|
+
/* @__PURE__ */ jsx4(AppSection, { index, ...shared, onIssued: addIssued, reload })
|
|
1019
|
+
] })
|
|
1020
|
+
] }) })
|
|
1021
|
+
] });
|
|
1022
|
+
}
|
|
1023
|
+
function TokenSection({ index, components, csrfToken, onIssued, reload, onSuccess, onError, removeLocally }) {
|
|
1024
|
+
const { Button, Input, Label } = resolveAuthComponents(components);
|
|
1025
|
+
const [name, setName] = React5.useState("");
|
|
1026
|
+
const [scopes, setScopes] = React5.useState([]);
|
|
1027
|
+
const [lifetime, setLifetime] = React5.useState(index.token_lifetimes[0] ?? "");
|
|
1028
|
+
const [busy, setBusy] = React5.useState(false);
|
|
1029
|
+
const [error, setError] = React5.useState(null);
|
|
1030
|
+
async function create(event) {
|
|
1031
|
+
event.preventDefault();
|
|
1032
|
+
if (index.issue_token_href === null) {
|
|
1033
|
+
return;
|
|
1034
|
+
}
|
|
1035
|
+
setBusy(true);
|
|
1036
|
+
const result = await credentialRequest("POST", index.issue_token_href, { name, scopes, lifetime }, csrfToken);
|
|
1037
|
+
setBusy(false);
|
|
1038
|
+
if (!result.ok) {
|
|
1039
|
+
setError(result.message);
|
|
1040
|
+
onError?.("api-tokens", result.message);
|
|
1041
|
+
return;
|
|
1042
|
+
}
|
|
1043
|
+
setError(null);
|
|
1044
|
+
setName("");
|
|
1045
|
+
setScopes([]);
|
|
1046
|
+
onIssued(result.data);
|
|
1047
|
+
onSuccess?.("API token created.");
|
|
1048
|
+
await reload();
|
|
1049
|
+
}
|
|
1050
|
+
const [pending, setPending] = React5.useState([]);
|
|
1051
|
+
async function revoke(token) {
|
|
1052
|
+
if (pending.includes(token.id)) {
|
|
1053
|
+
return;
|
|
1054
|
+
}
|
|
1055
|
+
setPending((current) => [...current, token.id]);
|
|
1056
|
+
const result = await credentialRequest("DELETE", token.revoke_href, void 0, csrfToken);
|
|
1057
|
+
if (!result.ok) {
|
|
1058
|
+
setPending((current) => current.filter((id) => id !== token.id));
|
|
1059
|
+
setError(result.message);
|
|
1060
|
+
onError?.("api-tokens", result.message);
|
|
1061
|
+
return;
|
|
1062
|
+
}
|
|
1063
|
+
setError(null);
|
|
1064
|
+
removeLocally("tokens", token.id);
|
|
1065
|
+
onSuccess?.("API token revoked.");
|
|
1066
|
+
await reload();
|
|
1067
|
+
setPending((current) => current.filter((id) => id !== token.id));
|
|
1068
|
+
}
|
|
1069
|
+
return /* @__PURE__ */ jsxs4("section", { style: { display: "grid", gap: "0.75rem", minWidth: 0 }, children: [
|
|
1070
|
+
/* @__PURE__ */ jsx4("h3", { children: "API tokens" }),
|
|
1071
|
+
index.issue_token_href !== null && /* @__PURE__ */ jsxs4("form", { onSubmit: create, style: { display: "grid", gap: "0.75rem" }, children: [
|
|
1072
|
+
/* @__PURE__ */ jsxs4("div", { style: { display: "grid", gap: "0.25rem" }, children: [
|
|
1073
|
+
/* @__PURE__ */ jsx4(Label, { htmlFor: "api-token-name", children: "Token name" }),
|
|
1074
|
+
/* @__PURE__ */ jsx4(Input, { id: "api-token-name", value: name, maxLength: 120, onChange: (event) => setName(event.target.value) })
|
|
1075
|
+
] }),
|
|
1076
|
+
/* @__PURE__ */ jsx4(ScopePicker, { idPrefix: "api-token-scope", scopes: index.scopes, selected: scopes, onChange: setScopes }),
|
|
1077
|
+
/* @__PURE__ */ jsxs4("fieldset", { style: { display: "grid", gap: "0.25rem", border: 0, padding: 0 }, children: [
|
|
1078
|
+
/* @__PURE__ */ jsx4("legend", { children: "Expires after" }),
|
|
1079
|
+
index.token_lifetimes.map((spec) => /* @__PURE__ */ jsxs4("label", { children: [
|
|
1080
|
+
/* @__PURE__ */ jsx4("input", { type: "radio", name: "api-token-lifetime", checked: lifetime === spec, onChange: () => setLifetime(spec) }),
|
|
1081
|
+
" ",
|
|
1082
|
+
describeDuration(spec)
|
|
1083
|
+
] }, spec))
|
|
1084
|
+
] }),
|
|
1085
|
+
/* @__PURE__ */ jsx4("div", { children: /* @__PURE__ */ jsx4(Button, { type: "submit", disabled: busy || name.trim() === "" || scopes.length === 0 || lifetime === "", children: "Create API token" }) })
|
|
1086
|
+
] }),
|
|
1087
|
+
error !== null && /* @__PURE__ */ jsx4("p", { role: "alert", children: error }),
|
|
1088
|
+
/* @__PURE__ */ jsx4("ul", { style: { display: "grid", gap: "0.5rem", listStyle: "none", padding: 0, margin: 0, minWidth: 0 }, children: index.tokens.map((token) => /* @__PURE__ */ jsxs4("li", { style: { minWidth: 0, overflowWrap: "anywhere" }, children: [
|
|
1089
|
+
/* @__PURE__ */ jsx4("strong", { children: token.name }),
|
|
1090
|
+
" \xB7 ",
|
|
1091
|
+
token.scopes.join(", "),
|
|
1092
|
+
token.expires_at !== null && /* @__PURE__ */ jsxs4(Fragment, { children: [
|
|
1093
|
+
" \xB7 expires ",
|
|
1094
|
+
new Date(token.expires_at).toLocaleString()
|
|
1095
|
+
] }),
|
|
1096
|
+
" ",
|
|
1097
|
+
/* @__PURE__ */ jsx4(Button, { type: "button", disabled: pending.includes(token.id), onClick: () => void revoke(token), children: "Revoke" })
|
|
1098
|
+
] }, token.id)) })
|
|
1099
|
+
] });
|
|
1100
|
+
}
|
|
1101
|
+
function AppSection({ index, components, csrfToken, onIssued, reload, onSuccess, onError, removeLocally }) {
|
|
1102
|
+
const { Button, Input, Label } = resolveAuthComponents(components);
|
|
1103
|
+
const [name, setName] = React5.useState("");
|
|
1104
|
+
const [redirects, setRedirects] = React5.useState("");
|
|
1105
|
+
const [confidential, setConfidential] = React5.useState(true);
|
|
1106
|
+
const [scopes, setScopes] = React5.useState([]);
|
|
1107
|
+
const [busy, setBusy] = React5.useState(false);
|
|
1108
|
+
const [error, setError] = React5.useState(null);
|
|
1109
|
+
const redirectUris = redirects.split("\n").map((line) => line.trim()).filter((line) => line !== "");
|
|
1110
|
+
async function register(event) {
|
|
1111
|
+
event.preventDefault();
|
|
1112
|
+
if (index.register_app_href === null) {
|
|
1113
|
+
return;
|
|
1114
|
+
}
|
|
1115
|
+
setBusy(true);
|
|
1116
|
+
const result = await credentialRequest(
|
|
1117
|
+
"POST",
|
|
1118
|
+
index.register_app_href,
|
|
1119
|
+
{ name, redirect_uris: redirectUris, confidential, scopes },
|
|
1120
|
+
csrfToken
|
|
1121
|
+
);
|
|
1122
|
+
setBusy(false);
|
|
1123
|
+
if (!result.ok) {
|
|
1124
|
+
setError(result.message);
|
|
1125
|
+
onError?.("oauth-apps", result.message);
|
|
1126
|
+
return;
|
|
1127
|
+
}
|
|
1128
|
+
setError(null);
|
|
1129
|
+
setName("");
|
|
1130
|
+
setRedirects("");
|
|
1131
|
+
setScopes([]);
|
|
1132
|
+
onIssued(result.data);
|
|
1133
|
+
onSuccess?.("OAuth app registered.");
|
|
1134
|
+
await reload();
|
|
1135
|
+
}
|
|
1136
|
+
const [pending, setPending] = React5.useState([]);
|
|
1137
|
+
async function remove(app) {
|
|
1138
|
+
if (pending.includes(app.id)) {
|
|
1139
|
+
return;
|
|
1140
|
+
}
|
|
1141
|
+
setPending((current) => [...current, app.id]);
|
|
1142
|
+
const result = await credentialRequest("DELETE", app.delete_href, void 0, csrfToken);
|
|
1143
|
+
if (!result.ok) {
|
|
1144
|
+
setPending((current) => current.filter((id) => id !== app.id));
|
|
1145
|
+
setError(result.message);
|
|
1146
|
+
onError?.("oauth-apps", result.message);
|
|
1147
|
+
return;
|
|
1148
|
+
}
|
|
1149
|
+
setError(null);
|
|
1150
|
+
removeLocally("apps", app.id);
|
|
1151
|
+
onSuccess?.("OAuth app deleted and its tokens revoked.");
|
|
1152
|
+
await reload();
|
|
1153
|
+
setPending((current) => current.filter((id) => id !== app.id));
|
|
1154
|
+
}
|
|
1155
|
+
return /* @__PURE__ */ jsxs4("section", { style: { display: "grid", gap: "0.75rem", minWidth: 0 }, children: [
|
|
1156
|
+
/* @__PURE__ */ jsx4("h3", { children: "OAuth apps" }),
|
|
1157
|
+
index.register_app_href !== null && /* @__PURE__ */ jsxs4("form", { onSubmit: register, style: { display: "grid", gap: "0.75rem" }, children: [
|
|
1158
|
+
/* @__PURE__ */ jsxs4("div", { style: { display: "grid", gap: "0.25rem" }, children: [
|
|
1159
|
+
/* @__PURE__ */ jsx4(Label, { htmlFor: "oauth-app-name", children: "App name" }),
|
|
1160
|
+
/* @__PURE__ */ jsx4(Input, { id: "oauth-app-name", value: name, maxLength: 120, onChange: (event) => setName(event.target.value) })
|
|
1161
|
+
] }),
|
|
1162
|
+
/* @__PURE__ */ jsxs4("div", { style: { display: "grid", gap: "0.25rem" }, children: [
|
|
1163
|
+
/* @__PURE__ */ jsx4(Label, { htmlFor: "oauth-app-redirects", children: "Redirect URIs (one per line)" }),
|
|
1164
|
+
/* @__PURE__ */ jsx4("textarea", { id: "oauth-app-redirects", rows: 3, value: redirects, onChange: (event) => setRedirects(event.target.value) })
|
|
1165
|
+
] }),
|
|
1166
|
+
/* @__PURE__ */ jsxs4("fieldset", { style: { display: "grid", gap: "0.25rem", border: 0, padding: 0 }, children: [
|
|
1167
|
+
/* @__PURE__ */ jsx4("legend", { children: "Client type" }),
|
|
1168
|
+
/* @__PURE__ */ jsxs4("label", { children: [
|
|
1169
|
+
/* @__PURE__ */ jsx4("input", { type: "radio", name: "oauth-app-type", checked: confidential, onChange: () => setConfidential(true) }),
|
|
1170
|
+
" Confidential: the app keeps a client secret on its server"
|
|
1171
|
+
] }),
|
|
1172
|
+
/* @__PURE__ */ jsxs4("label", { children: [
|
|
1173
|
+
/* @__PURE__ */ jsx4("input", { type: "radio", name: "oauth-app-type", checked: !confidential, onChange: () => setConfidential(false) }),
|
|
1174
|
+
" Public: no secret, PKCE only"
|
|
1175
|
+
] })
|
|
1176
|
+
] }),
|
|
1177
|
+
/* @__PURE__ */ jsx4(ScopePicker, { idPrefix: "oauth-app-scope", scopes: index.scopes, selected: scopes, onChange: setScopes }),
|
|
1178
|
+
/* @__PURE__ */ jsx4("div", { children: /* @__PURE__ */ jsx4(Button, { type: "submit", disabled: busy || name.trim() === "" || redirectUris.length === 0 || scopes.length === 0, children: "Register OAuth app" }) })
|
|
1179
|
+
] }),
|
|
1180
|
+
error !== null && /* @__PURE__ */ jsx4("p", { role: "alert", children: error }),
|
|
1181
|
+
/* @__PURE__ */ jsx4("ul", { style: { display: "grid", gap: "0.5rem", listStyle: "none", padding: 0, margin: 0, minWidth: 0 }, children: index.apps.map((app) => /* @__PURE__ */ jsxs4("li", { style: { minWidth: 0, overflowWrap: "anywhere" }, children: [
|
|
1182
|
+
/* @__PURE__ */ jsx4("strong", { children: app.name }),
|
|
1183
|
+
" \xB7 client ID ",
|
|
1184
|
+
/* @__PURE__ */ jsx4("code", { children: app.id }),
|
|
1185
|
+
" \xB7 ",
|
|
1186
|
+
app.confidential ? "confidential" : "public",
|
|
1187
|
+
/* @__PURE__ */ jsx4("br", {}),
|
|
1188
|
+
app.redirect_uris.join(", "),
|
|
1189
|
+
/* @__PURE__ */ jsx4("br", {}),
|
|
1190
|
+
"May request: ",
|
|
1191
|
+
app.scopes.join(", "),
|
|
1192
|
+
" ",
|
|
1193
|
+
/* @__PURE__ */ jsx4(Button, { type: "button", disabled: pending.includes(app.id), onClick: () => void remove(app), children: "Delete" })
|
|
1194
|
+
] }, app.id)) })
|
|
1195
|
+
] });
|
|
1196
|
+
}
|
|
1197
|
+
function ScopePicker({
|
|
1198
|
+
idPrefix,
|
|
1199
|
+
scopes,
|
|
1200
|
+
selected,
|
|
1201
|
+
onChange
|
|
1202
|
+
}) {
|
|
1203
|
+
return /* @__PURE__ */ jsxs4("fieldset", { style: { display: "grid", gap: "0.25rem", border: 0, padding: 0, minWidth: 0 }, children: [
|
|
1204
|
+
/* @__PURE__ */ jsx4("legend", { children: "Permissions" }),
|
|
1205
|
+
scopes.map((scope) => {
|
|
1206
|
+
const id = `${idPrefix}-${scope.id}`;
|
|
1207
|
+
return /* @__PURE__ */ jsxs4("label", { htmlFor: id, style: { overflowWrap: "anywhere" }, children: [
|
|
1208
|
+
/* @__PURE__ */ jsx4(
|
|
1209
|
+
"input",
|
|
1210
|
+
{
|
|
1211
|
+
id,
|
|
1212
|
+
type: "checkbox",
|
|
1213
|
+
checked: selected.includes(scope.id),
|
|
1214
|
+
onChange: (event) => onChange(event.target.checked ? [...selected, scope.id] : selected.filter((value) => value !== scope.id))
|
|
1215
|
+
}
|
|
1216
|
+
),
|
|
1217
|
+
" ",
|
|
1218
|
+
/* @__PURE__ */ jsx4("code", { children: scope.id }),
|
|
1219
|
+
" ",
|
|
1220
|
+
scope.description
|
|
1221
|
+
] }, scope.id);
|
|
1222
|
+
})
|
|
1223
|
+
] });
|
|
1224
|
+
}
|
|
1225
|
+
function IssuedNotice({
|
|
1226
|
+
issued,
|
|
1227
|
+
components,
|
|
1228
|
+
onDismiss
|
|
1229
|
+
}) {
|
|
1230
|
+
const { Button } = resolveAuthComponents(components);
|
|
1231
|
+
const values = issued.kind === "token" ? [{ label: "API token", url: issued.token }] : [
|
|
1232
|
+
{ label: "Client ID", url: issued.client_id },
|
|
1233
|
+
...issued.client_secret !== null ? [{ label: "Client secret", url: issued.client_secret }] : []
|
|
1234
|
+
];
|
|
1235
|
+
return /* @__PURE__ */ jsxs4("div", { role: "status", style: { display: "grid", gap: "0.5rem", minWidth: 0 }, children: [
|
|
1236
|
+
/* @__PURE__ */ jsxs4("p", { style: { overflowWrap: "anywhere" }, children: [
|
|
1237
|
+
issued.kind === "token" ? `API token \u201C${issued.name}\u201D created.` : `OAuth app \u201C${issued.name}\u201D registered.`,
|
|
1238
|
+
" Copy it now: it will not be shown again."
|
|
1239
|
+
] }),
|
|
1240
|
+
values.map((value) => /* @__PURE__ */ jsxs4("div", { style: { minWidth: 0, overflowWrap: "anywhere" }, children: [
|
|
1241
|
+
value.label,
|
|
1242
|
+
": ",
|
|
1243
|
+
/* @__PURE__ */ jsx4("code", { children: value.url }),
|
|
1244
|
+
" ",
|
|
1245
|
+
/* @__PURE__ */ jsx4(CopyButton, { text: value.url, label: value.label, components })
|
|
1246
|
+
] }, value.label)),
|
|
1247
|
+
/* @__PURE__ */ jsx4("div", { children: /* @__PURE__ */ jsx4(Button, { type: "button", onClick: onDismiss, children: "Done" }) })
|
|
1248
|
+
] });
|
|
1249
|
+
}
|
|
1250
|
+
function CopyButton({ text, label, components }) {
|
|
1251
|
+
const { Button } = resolveAuthComponents(components);
|
|
1252
|
+
const [state, setState] = React5.useState("idle");
|
|
1253
|
+
return /* @__PURE__ */ jsxs4(Fragment, { children: [
|
|
1254
|
+
/* @__PURE__ */ jsx4(
|
|
1255
|
+
Button,
|
|
1256
|
+
{
|
|
1257
|
+
type: "button",
|
|
1258
|
+
"aria-label": `Copy ${label}`,
|
|
1259
|
+
onClick: async () => {
|
|
1260
|
+
if (typeof navigator === "undefined" || typeof navigator.clipboard?.writeText !== "function") {
|
|
1261
|
+
setState("failed");
|
|
1262
|
+
return;
|
|
1263
|
+
}
|
|
1264
|
+
try {
|
|
1265
|
+
await navigator.clipboard.writeText(text);
|
|
1266
|
+
setState("copied");
|
|
1267
|
+
} catch {
|
|
1268
|
+
setState("failed");
|
|
1269
|
+
}
|
|
1270
|
+
},
|
|
1271
|
+
children: state === "copied" ? "Copied" : "Copy"
|
|
1272
|
+
}
|
|
1273
|
+
),
|
|
1274
|
+
state === "failed" && /* @__PURE__ */ jsx4("span", { role: "status", children: " Copying is unavailable here; select the text and copy it manually." })
|
|
1275
|
+
] });
|
|
1276
|
+
}
|
|
1277
|
+
function describeDuration(spec) {
|
|
1278
|
+
const match = /^P(?:(\d+)Y)?(?:(\d+)M)?(?:(\d+)W)?(?:(\d+)D)?(?:T(?:(\d+)H)?(?:(\d+)M)?)?$/.exec(spec);
|
|
1279
|
+
if (!match) {
|
|
1280
|
+
return spec;
|
|
1281
|
+
}
|
|
1282
|
+
const units = ["year", "month", "week", "day", "hour", "minute"];
|
|
1283
|
+
const parts = match.slice(1).map((value, position) => value ? `${Number(value)} ${units[position]}${Number(value) === 1 ? "" : "s"}` : null).filter((part) => part !== null);
|
|
1284
|
+
return parts.length > 0 ? parts.join(" ") : spec;
|
|
1285
|
+
}
|
|
1286
|
+
async function credentialRequest(method, url, body, csrfToken) {
|
|
1287
|
+
let response;
|
|
1288
|
+
try {
|
|
1289
|
+
response = await fetch(url, {
|
|
1290
|
+
method,
|
|
1291
|
+
credentials: "same-origin",
|
|
1292
|
+
headers: {
|
|
1293
|
+
Accept: "application/json",
|
|
1294
|
+
...body !== void 0 ? { "Content-Type": "application/json" } : {},
|
|
1295
|
+
...method !== "GET" ? { "X-CSRF-TOKEN": getCsrfToken(csrfToken) } : {}
|
|
1296
|
+
},
|
|
1297
|
+
body: body !== void 0 ? JSON.stringify(body) : void 0
|
|
1298
|
+
});
|
|
1299
|
+
} catch {
|
|
1300
|
+
return { ok: false, status: 0, message: "The server could not be reached. Check the connection and try again." };
|
|
1301
|
+
}
|
|
1302
|
+
const payload = await response.json().catch(() => null);
|
|
1303
|
+
if (response.ok) {
|
|
1304
|
+
const data = typeof payload === "object" && payload !== null && "data" in payload ? payload.data : payload;
|
|
1305
|
+
return { ok: true, status: response.status, data };
|
|
1306
|
+
}
|
|
1307
|
+
return { ok: false, status: response.status, message: refusalMessage(response.status, payload) };
|
|
1308
|
+
}
|
|
1309
|
+
function refusalMessage(status, payload) {
|
|
1310
|
+
if (status === 401 || status === 419) {
|
|
1311
|
+
return "Your session has expired. Reload the page and sign in again.";
|
|
1312
|
+
}
|
|
1313
|
+
if (typeof payload === "object" && payload !== null) {
|
|
1314
|
+
const errors = payload.errors;
|
|
1315
|
+
if (errors && typeof errors === "object") {
|
|
1316
|
+
const first = Object.values(errors)[0];
|
|
1317
|
+
if (Array.isArray(first) && typeof first[0] === "string") {
|
|
1318
|
+
return first[0];
|
|
1319
|
+
}
|
|
1320
|
+
}
|
|
1321
|
+
const message = payload.message;
|
|
1322
|
+
if (typeof message === "string" && message !== "") {
|
|
1323
|
+
return message;
|
|
1324
|
+
}
|
|
1325
|
+
}
|
|
1326
|
+
if (status === 403) {
|
|
1327
|
+
return "You do not have permission to do that.";
|
|
1328
|
+
}
|
|
1329
|
+
if (status === 404) {
|
|
1330
|
+
return "That credential no longer exists, or creating credentials is unavailable right now.";
|
|
1331
|
+
}
|
|
1332
|
+
return "That action could not be completed.";
|
|
1333
|
+
}
|
|
861
1334
|
export {
|
|
1335
|
+
ApiCredentialsSection,
|
|
862
1336
|
ChangePasswordForm,
|
|
863
1337
|
LoginForm,
|
|
864
1338
|
PasskeyLoginButton,
|
|
@@ -870,6 +1344,7 @@ export {
|
|
|
870
1344
|
arrayBufferToBase64url,
|
|
871
1345
|
authenticateWithPasskey,
|
|
872
1346
|
base64urlToArrayBuffer,
|
|
1347
|
+
describeDuration,
|
|
873
1348
|
getCsrfToken,
|
|
874
1349
|
getDefaultPasskeyName,
|
|
875
1350
|
isAbortError,
|