@orangecheck/auth-client 2.1.0 → 2.2.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/dist/index.d.mts CHANGED
@@ -123,12 +123,19 @@ type WebAuthnListStatus = 'loading' | 'ready' | 'error';
123
123
  interface UseHostOptions {
124
124
  authOrigin?: string;
125
125
  }
126
+ type WebAuthnRegisterResult = {
127
+ ok: true;
128
+ credential: WebAuthnCredentialPublic;
129
+ } | {
130
+ ok: false;
131
+ reason: string;
132
+ };
126
133
  interface UseWebAuthnRegisterReturn {
127
134
  status: WebAuthnRegisterStatus;
128
135
  error: Error | null;
129
136
  register: (args?: {
130
137
  label?: string;
131
- }) => Promise<WebAuthnCredentialPublic | null>;
138
+ }) => Promise<WebAuthnRegisterResult>;
132
139
  reset: () => void;
133
140
  }
134
141
  declare function useWebAuthnRegister(opts?: UseHostOptions): UseWebAuthnRegisterReturn;
@@ -141,16 +148,33 @@ interface UseWebAuthnListReturn {
141
148
  remove: (id: string) => Promise<boolean>;
142
149
  }
143
150
  declare function useWebAuthnList(opts?: UseHostOptions): UseWebAuthnListReturn;
151
+ type WebAuthnStepUpResult = {
152
+ ok: true;
153
+ step_up_at: number;
154
+ } | {
155
+ ok: false;
156
+ reason: string;
157
+ };
144
158
  interface UseStepUpAuthReturn {
145
159
  status: WebAuthnAssertionStatus;
146
160
  error: Error | null;
147
161
  stepUp: (args: {
148
162
  purpose: string;
149
- }) => Promise<{
150
- step_up_at: number;
151
- } | null>;
163
+ }) => Promise<WebAuthnStepUpResult>;
152
164
  reset: () => void;
153
165
  }
154
166
  declare function useStepUpAuth(opts?: UseHostOptions): UseStepUpAuthReturn;
155
167
 
156
- export { DEFAULT_CONFIG, type OcAccount, OcAccountChip, type OcAccountChipProps, OcAccountPill, type OcAccountPillProps, OcAddressInput, type OcAddressInputProps, type OcAuthConfig, OcSessionProvider, type OcSessionState, type OcSessionStatus, OcSignIn, OcSignInButton, type OcSignInButtonProps, type OcSignInProps, type UseOcAddressSuggestionOptions, type UseOcAddressSuggestionReturn, type UseStepUpAuthReturn, type UseWebAuthnListReturn, type UseWebAuthnRegisterReturn, type WebAuthnAssertionStatus, type WebAuthnCredentialPublic, type WebAuthnListStatus, type WebAuthnRegisterStatus, buildSignInUrl, useOcAddressSuggestion, useOcSession, useOptionalOcSession, useStepUpAuth, useWebAuthnList, useWebAuthnRegister };
168
+ interface RedirectToSudoArgs {
169
+ returnTo?: string;
170
+ purpose?: string;
171
+ config?: OcAuthConfig;
172
+ }
173
+ declare function redirectToSudo(args?: RedirectToSudoArgs): void;
174
+ declare function handleSudoRequired(body: {
175
+ reason?: string;
176
+ } | null | undefined, args?: Omit<RedirectToSudoArgs, 'returnTo'> & {
177
+ returnTo?: string;
178
+ }): boolean;
179
+
180
+ export { DEFAULT_CONFIG, type OcAccount, OcAccountChip, type OcAccountChipProps, OcAccountPill, type OcAccountPillProps, OcAddressInput, type OcAddressInputProps, type OcAuthConfig, OcSessionProvider, type OcSessionState, type OcSessionStatus, OcSignIn, OcSignInButton, type OcSignInButtonProps, type OcSignInProps, type RedirectToSudoArgs, type UseOcAddressSuggestionOptions, type UseOcAddressSuggestionReturn, type UseStepUpAuthReturn, type UseWebAuthnListReturn, type UseWebAuthnRegisterReturn, type WebAuthnAssertionStatus, type WebAuthnCredentialPublic, type WebAuthnListStatus, type WebAuthnRegisterResult, type WebAuthnRegisterStatus, type WebAuthnStepUpResult, buildSignInUrl, handleSudoRequired, redirectToSudo, useOcAddressSuggestion, useOcSession, useOptionalOcSession, useStepUpAuth, useWebAuthnList, useWebAuthnRegister };
package/dist/index.d.ts CHANGED
@@ -123,12 +123,19 @@ type WebAuthnListStatus = 'loading' | 'ready' | 'error';
123
123
  interface UseHostOptions {
124
124
  authOrigin?: string;
125
125
  }
126
+ type WebAuthnRegisterResult = {
127
+ ok: true;
128
+ credential: WebAuthnCredentialPublic;
129
+ } | {
130
+ ok: false;
131
+ reason: string;
132
+ };
126
133
  interface UseWebAuthnRegisterReturn {
127
134
  status: WebAuthnRegisterStatus;
128
135
  error: Error | null;
129
136
  register: (args?: {
130
137
  label?: string;
131
- }) => Promise<WebAuthnCredentialPublic | null>;
138
+ }) => Promise<WebAuthnRegisterResult>;
132
139
  reset: () => void;
133
140
  }
134
141
  declare function useWebAuthnRegister(opts?: UseHostOptions): UseWebAuthnRegisterReturn;
@@ -141,16 +148,33 @@ interface UseWebAuthnListReturn {
141
148
  remove: (id: string) => Promise<boolean>;
142
149
  }
143
150
  declare function useWebAuthnList(opts?: UseHostOptions): UseWebAuthnListReturn;
151
+ type WebAuthnStepUpResult = {
152
+ ok: true;
153
+ step_up_at: number;
154
+ } | {
155
+ ok: false;
156
+ reason: string;
157
+ };
144
158
  interface UseStepUpAuthReturn {
145
159
  status: WebAuthnAssertionStatus;
146
160
  error: Error | null;
147
161
  stepUp: (args: {
148
162
  purpose: string;
149
- }) => Promise<{
150
- step_up_at: number;
151
- } | null>;
163
+ }) => Promise<WebAuthnStepUpResult>;
152
164
  reset: () => void;
153
165
  }
154
166
  declare function useStepUpAuth(opts?: UseHostOptions): UseStepUpAuthReturn;
155
167
 
156
- export { DEFAULT_CONFIG, type OcAccount, OcAccountChip, type OcAccountChipProps, OcAccountPill, type OcAccountPillProps, OcAddressInput, type OcAddressInputProps, type OcAuthConfig, OcSessionProvider, type OcSessionState, type OcSessionStatus, OcSignIn, OcSignInButton, type OcSignInButtonProps, type OcSignInProps, type UseOcAddressSuggestionOptions, type UseOcAddressSuggestionReturn, type UseStepUpAuthReturn, type UseWebAuthnListReturn, type UseWebAuthnRegisterReturn, type WebAuthnAssertionStatus, type WebAuthnCredentialPublic, type WebAuthnListStatus, type WebAuthnRegisterStatus, buildSignInUrl, useOcAddressSuggestion, useOcSession, useOptionalOcSession, useStepUpAuth, useWebAuthnList, useWebAuthnRegister };
168
+ interface RedirectToSudoArgs {
169
+ returnTo?: string;
170
+ purpose?: string;
171
+ config?: OcAuthConfig;
172
+ }
173
+ declare function redirectToSudo(args?: RedirectToSudoArgs): void;
174
+ declare function handleSudoRequired(body: {
175
+ reason?: string;
176
+ } | null | undefined, args?: Omit<RedirectToSudoArgs, 'returnTo'> & {
177
+ returnTo?: string;
178
+ }): boolean;
179
+
180
+ export { DEFAULT_CONFIG, type OcAccount, OcAccountChip, type OcAccountChipProps, OcAccountPill, type OcAccountPillProps, OcAddressInput, type OcAddressInputProps, type OcAuthConfig, OcSessionProvider, type OcSessionState, type OcSessionStatus, OcSignIn, OcSignInButton, type OcSignInButtonProps, type OcSignInProps, type RedirectToSudoArgs, type UseOcAddressSuggestionOptions, type UseOcAddressSuggestionReturn, type UseStepUpAuthReturn, type UseWebAuthnListReturn, type UseWebAuthnRegisterReturn, type WebAuthnAssertionStatus, type WebAuthnCredentialPublic, type WebAuthnListStatus, type WebAuthnRegisterResult, type WebAuthnRegisterStatus, type WebAuthnStepUpResult, buildSignInUrl, handleSudoRequired, redirectToSudo, useOcAddressSuggestion, useOcSession, useOptionalOcSession, useStepUpAuth, useWebAuthnList, useWebAuthnRegister };
package/dist/index.js CHANGED
@@ -1301,12 +1301,21 @@ function useWebAuthnRegister(opts) {
1301
1301
  });
1302
1302
  const optsBody = await optsRes.json();
1303
1303
  if (!optsRes.ok || !optsBody.ok || !optsBody.options || !optsBody.challenge_token) {
1304
- throw new Error(optsBody.reason ?? `options_failed_${optsRes.status}`);
1304
+ const reason = optsBody.reason ?? `options_failed_${optsRes.status}`;
1305
+ setError(new Error(reason));
1306
+ setStatus("error");
1307
+ return { ok: false, reason };
1305
1308
  }
1306
1309
  setStatus("authenticating");
1307
- const attestation = await browser.startRegistration({
1308
- optionsJSON: optsBody.options
1309
- });
1310
+ let attestation;
1311
+ try {
1312
+ attestation = await browser.startRegistration({ optionsJSON: optsBody.options });
1313
+ } catch (e) {
1314
+ const reason = isAbortError(e) ? "cancelled" : e.message;
1315
+ setError(new Error(reason));
1316
+ setStatus("error");
1317
+ return { ok: false, reason };
1318
+ }
1310
1319
  setStatus("verifying");
1311
1320
  const verifyRes = await fetch(`${authOrigin}/api/auth/webauthn/register/verify`, {
1312
1321
  method: "POST",
@@ -1320,18 +1329,18 @@ function useWebAuthnRegister(opts) {
1320
1329
  });
1321
1330
  const verifyBody = await verifyRes.json();
1322
1331
  if (!verifyRes.ok || !verifyBody.ok || !verifyBody.credential) {
1323
- throw new Error(verifyBody.reason ?? `verify_failed_${verifyRes.status}`);
1332
+ const reason = verifyBody.reason ?? `verify_failed_${verifyRes.status}`;
1333
+ setError(new Error(reason));
1334
+ setStatus("error");
1335
+ return { ok: false, reason };
1324
1336
  }
1325
1337
  setStatus("success");
1326
- return verifyBody.credential;
1338
+ return { ok: true, credential: verifyBody.credential };
1327
1339
  } catch (e) {
1328
- const err = e instanceof Error ? e : new Error(String(e));
1329
- setError(err);
1340
+ const reason = e instanceof Error ? e.message : String(e);
1341
+ setError(new Error(reason));
1330
1342
  setStatus("error");
1331
- if (isAbortError(err)) {
1332
- setError(new Error("cancelled"));
1333
- }
1334
- return null;
1343
+ return { ok: false, reason };
1335
1344
  }
1336
1345
  },
1337
1346
  [authOrigin]
@@ -1443,12 +1452,21 @@ function useStepUpAuth(opts) {
1443
1452
  });
1444
1453
  const optsBody = await optsRes.json();
1445
1454
  if (!optsRes.ok || !optsBody.ok || !optsBody.options || !optsBody.challenge_token) {
1446
- throw new Error(optsBody.reason ?? `options_failed_${optsRes.status}`);
1455
+ const reason = optsBody.reason ?? `options_failed_${optsRes.status}`;
1456
+ setError(new Error(reason));
1457
+ setStatus("error");
1458
+ return { ok: false, reason };
1447
1459
  }
1448
1460
  setStatus("authenticating");
1449
- const assertion = await browser.startAuthentication({
1450
- optionsJSON: optsBody.options
1451
- });
1461
+ let assertion;
1462
+ try {
1463
+ assertion = await browser.startAuthentication({ optionsJSON: optsBody.options });
1464
+ } catch (e) {
1465
+ const reason = isAbortError(e) ? "cancelled" : e.message;
1466
+ setError(new Error(reason));
1467
+ setStatus("error");
1468
+ return { ok: false, reason };
1469
+ }
1452
1470
  setStatus("verifying");
1453
1471
  const verifyRes = await fetch(
1454
1472
  `${authOrigin}/api/auth/webauthn/assertion/verify`,
@@ -1464,16 +1482,19 @@ function useStepUpAuth(opts) {
1464
1482
  );
1465
1483
  const verifyBody = await verifyRes.json();
1466
1484
  if (!verifyRes.ok || !verifyBody.ok || typeof verifyBody.step_up_at !== "number") {
1467
- throw new Error(verifyBody.reason ?? `verify_failed_${verifyRes.status}`);
1485
+ const reason = verifyBody.reason ?? `verify_failed_${verifyRes.status}`;
1486
+ setError(new Error(reason));
1487
+ setStatus("error");
1488
+ return { ok: false, reason };
1468
1489
  }
1469
1490
  await refresh();
1470
1491
  setStatus("success");
1471
- return { step_up_at: verifyBody.step_up_at };
1492
+ return { ok: true, step_up_at: verifyBody.step_up_at };
1472
1493
  } catch (e) {
1473
- const err = isAbortError(e) ? new Error("cancelled") : e instanceof Error ? e : new Error(String(e));
1474
- setError(err);
1494
+ const reason = e instanceof Error ? e.message : String(e);
1495
+ setError(new Error(reason));
1475
1496
  setStatus("error");
1476
- return null;
1497
+ return { ok: false, reason };
1477
1498
  }
1478
1499
  },
1479
1500
  [authOrigin, refresh]
@@ -1481,6 +1502,28 @@ function useStepUpAuth(opts) {
1481
1502
  return { status, error, stepUp, reset };
1482
1503
  }
1483
1504
 
1505
+ // src/sudo.tsx
1506
+ function redirectToSudo(args = {}) {
1507
+ if (typeof window === "undefined") {
1508
+ throw new Error(
1509
+ "[@orangecheck/auth-client] redirectToSudo() requires a browser environment"
1510
+ );
1511
+ }
1512
+ const cfg = resolveConfig(args.config);
1513
+ const url = new URL("/sudo", cfg.authOrigin);
1514
+ const returnTo = args.returnTo ?? window.location.href;
1515
+ if (returnTo) url.searchParams.set("return_to", returnTo);
1516
+ if (args.purpose) url.searchParams.set("purpose", args.purpose);
1517
+ window.location.assign(url.toString());
1518
+ }
1519
+ function handleSudoRequired(body, args = {}) {
1520
+ if (body && body.reason === "sudo_required") {
1521
+ redirectToSudo(args);
1522
+ return true;
1523
+ }
1524
+ return false;
1525
+ }
1526
+
1484
1527
  exports.DEFAULT_CONFIG = DEFAULT_CONFIG;
1485
1528
  exports.OcAccountChip = OcAccountChip;
1486
1529
  exports.OcAccountPill = OcAccountPill;
@@ -1489,6 +1532,8 @@ exports.OcSessionProvider = OcSessionProvider;
1489
1532
  exports.OcSignIn = OcSignIn;
1490
1533
  exports.OcSignInButton = OcSignInButton;
1491
1534
  exports.buildSignInUrl = buildSignInUrl;
1535
+ exports.handleSudoRequired = handleSudoRequired;
1536
+ exports.redirectToSudo = redirectToSudo;
1492
1537
  exports.useOcAddressSuggestion = useOcAddressSuggestion;
1493
1538
  exports.useOcSession = useOcSession;
1494
1539
  exports.useOptionalOcSession = useOptionalOcSession;