@useauthio/react 0.2.4 → 0.3.1
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/CHANGELOG.md +79 -0
- package/README.md +88 -19
- package/dist/index.cjs +496 -63
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.cts +358 -27
- package/dist/index.d.ts +358 -27
- package/dist/index.js +485 -65
- package/dist/index.js.map +1 -1
- package/package.json +2 -2
- package/src/PasskeyManager.tsx +4 -3
- package/src/activity.ts +95 -0
- package/src/device-signals.ts +86 -0
- package/src/errors.ts +239 -17
- package/src/fetch.ts +23 -12
- package/src/index.ts +37 -3
- package/src/lobby-context.ts +16 -0
- package/src/locate.ts +42 -0
- package/src/passkeys.ts +18 -8
- package/src/provider.tsx +111 -15
- package/src/refresh.ts +41 -0
- package/src/sign-in.ts +263 -29
- package/src/telemetry.ts +11 -0
- package/src/types.ts +71 -0
- package/src/usePasskeys.ts +13 -4
- package/src/version.ts +1 -1
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@useauthio/react",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.3.1",
|
|
4
4
|
"description": "React SDK for Authio: hooks, gates, and sign-in helpers for pure SPAs.",
|
|
5
5
|
"author": "Authio",
|
|
6
6
|
"license": "MIT",
|
|
@@ -51,7 +51,7 @@
|
|
|
51
51
|
"tsup": "^8.3.0",
|
|
52
52
|
"typescript": "^5.6.0",
|
|
53
53
|
"vitest": "^4.1.0",
|
|
54
|
-
"vite": "^6.
|
|
54
|
+
"vite": "^6.4.4"
|
|
55
55
|
},
|
|
56
56
|
"scripts": {
|
|
57
57
|
"build": "tsup",
|
package/src/PasskeyManager.tsx
CHANGED
|
@@ -1,5 +1,6 @@
|
|
|
1
1
|
import React, { useState } from "react";
|
|
2
2
|
import { usePasskeys } from "./usePasskeys";
|
|
3
|
+
import { AuthioError } from "./errors";
|
|
3
4
|
|
|
4
5
|
export interface PasskeyManagerProps {
|
|
5
6
|
className?: string;
|
|
@@ -30,7 +31,7 @@ export function PasskeyManager({
|
|
|
30
31
|
try {
|
|
31
32
|
await rename(id, nickname);
|
|
32
33
|
} catch (err) {
|
|
33
|
-
setActionError(err instanceof
|
|
34
|
+
setActionError(err instanceof AuthioError ? err.message : "We couldn't rename that passkey. Please try again.");
|
|
34
35
|
} finally {
|
|
35
36
|
setBusyId(null);
|
|
36
37
|
}
|
|
@@ -42,7 +43,7 @@ export function PasskeyManager({
|
|
|
42
43
|
try {
|
|
43
44
|
await revoke(id);
|
|
44
45
|
} catch (err) {
|
|
45
|
-
setActionError(err instanceof
|
|
46
|
+
setActionError(err instanceof AuthioError ? err.message : "We couldn't remove that passkey. Please try again.");
|
|
46
47
|
} finally {
|
|
47
48
|
setBusyId(null);
|
|
48
49
|
}
|
|
@@ -55,7 +56,7 @@ export function PasskeyManager({
|
|
|
55
56
|
await enroll({ returnUrl: enrollReturnUrl, next: enrollNext });
|
|
56
57
|
} catch (err) {
|
|
57
58
|
setEnrolling(false);
|
|
58
|
-
setActionError(err instanceof
|
|
59
|
+
setActionError(err instanceof AuthioError ? err.message : "We couldn't start passkey setup. Please try again.");
|
|
59
60
|
}
|
|
60
61
|
}
|
|
61
62
|
|
package/src/activity.ts
ADDED
|
@@ -0,0 +1,95 @@
|
|
|
1
|
+
import { isBrowser, isTabHidden } from "./ssr";
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* DOM events that count as "the user is here". All are registered
|
|
5
|
+
* passive + capture so we never delay the page's own handlers, and
|
|
6
|
+
* `scroll` is the only high-frequency one — the throttle below keeps
|
|
7
|
+
* it from churning.
|
|
8
|
+
*/
|
|
9
|
+
const ACTIVITY_EVENTS = [
|
|
10
|
+
"pointerdown",
|
|
11
|
+
"keydown",
|
|
12
|
+
"touchstart",
|
|
13
|
+
"scroll",
|
|
14
|
+
"focus",
|
|
15
|
+
] as const;
|
|
16
|
+
|
|
17
|
+
/** Minimum gap between two recorded activity timestamps. */
|
|
18
|
+
const THROTTLE_MS = 1000;
|
|
19
|
+
|
|
20
|
+
export type ActivityListener = (at: number) => void;
|
|
21
|
+
|
|
22
|
+
/**
|
|
23
|
+
* Tracks the last moment the user interacted with the page. Used by the
|
|
24
|
+
* provider to decide whether a scheduled silent refresh should run or
|
|
25
|
+
* be deferred: when the project/org has an inactivity (idle) timeout,
|
|
26
|
+
* refreshing on a timer while nobody is at the keyboard would bump the
|
|
27
|
+
* server's `last_active_at` and defeat that policy. See `RefreshScheduler`.
|
|
28
|
+
*
|
|
29
|
+
* SSR-safe — under SSR `lastActivityAt()` is `0` and listeners are never
|
|
30
|
+
* attached.
|
|
31
|
+
*/
|
|
32
|
+
export class ActivityTracker {
|
|
33
|
+
private last = 0;
|
|
34
|
+
private listeners = new Set<ActivityListener>();
|
|
35
|
+
private handler: (() => void) | null = null;
|
|
36
|
+
private visibilityHandler: (() => void) | null = null;
|
|
37
|
+
private destroyed = false;
|
|
38
|
+
|
|
39
|
+
constructor(now: () => number = Date.now) {
|
|
40
|
+
this.now = now;
|
|
41
|
+
if (!isBrowser()) return;
|
|
42
|
+
this.handler = () => this.mark();
|
|
43
|
+
for (const ev of ACTIVITY_EVENTS) {
|
|
44
|
+
window.addEventListener(ev, this.handler, { passive: true, capture: true });
|
|
45
|
+
}
|
|
46
|
+
this.visibilityHandler = () => {
|
|
47
|
+
if (!isTabHidden()) this.mark();
|
|
48
|
+
};
|
|
49
|
+
document.addEventListener("visibilitychange", this.visibilityHandler);
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
private readonly now: () => number;
|
|
53
|
+
|
|
54
|
+
/** Unix ms of the most recent recorded interaction, `0` if none yet. */
|
|
55
|
+
lastActivityAt(): number {
|
|
56
|
+
return this.last;
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
/**
|
|
60
|
+
* Record activity now. Throttled to one write per second; listeners
|
|
61
|
+
* fire only when a write actually happens. Exposed so tests (and
|
|
62
|
+
* embedders with their own activity signal) can poke it directly.
|
|
63
|
+
*/
|
|
64
|
+
mark(): void {
|
|
65
|
+
if (this.destroyed) return;
|
|
66
|
+
const t = this.now();
|
|
67
|
+
if (t - this.last < THROTTLE_MS) return;
|
|
68
|
+
this.last = t;
|
|
69
|
+
for (const l of this.listeners) l(t);
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
/** Subscribe to recorded activity. Returns an unsubscribe function. */
|
|
73
|
+
onActivity(listener: ActivityListener): () => void {
|
|
74
|
+
this.listeners.add(listener);
|
|
75
|
+
return () => {
|
|
76
|
+
this.listeners.delete(listener);
|
|
77
|
+
};
|
|
78
|
+
}
|
|
79
|
+
|
|
80
|
+
destroy(): void {
|
|
81
|
+
this.destroyed = true;
|
|
82
|
+
this.listeners.clear();
|
|
83
|
+
if (!isBrowser()) return;
|
|
84
|
+
if (this.handler) {
|
|
85
|
+
for (const ev of ACTIVITY_EVENTS) {
|
|
86
|
+
window.removeEventListener(ev, this.handler, { capture: true });
|
|
87
|
+
}
|
|
88
|
+
}
|
|
89
|
+
if (this.visibilityHandler) {
|
|
90
|
+
document.removeEventListener("visibilitychange", this.visibilityHandler);
|
|
91
|
+
}
|
|
92
|
+
this.handler = null;
|
|
93
|
+
this.visibilityHandler = null;
|
|
94
|
+
}
|
|
95
|
+
}
|
|
@@ -0,0 +1,86 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Optional browser device signals for Authio risk / device fingerprinting.
|
|
3
|
+
*
|
|
4
|
+
* Web browsers do not expose a hardware ID (IMEI, serial, MAC). These
|
|
5
|
+
* coarse, non-PII signals improve device recognition alongside the
|
|
6
|
+
* server-parsed user agent. auth-core hashes them server-side — never
|
|
7
|
+
* send a precomputed fingerprint from the client.
|
|
8
|
+
*/
|
|
9
|
+
|
|
10
|
+
export interface DeviceSignalsCapture {
|
|
11
|
+
timezone?: string;
|
|
12
|
+
screen_resolution?: string;
|
|
13
|
+
language?: string;
|
|
14
|
+
platform?: string;
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
/** Collect coarse device signals available in a browser context. */
|
|
18
|
+
export function collectDeviceSignals(): DeviceSignalsCapture {
|
|
19
|
+
if (typeof window === "undefined") {
|
|
20
|
+
return {};
|
|
21
|
+
}
|
|
22
|
+
const out: DeviceSignalsCapture = {};
|
|
23
|
+
try {
|
|
24
|
+
out.timezone = Intl.DateTimeFormat().resolvedOptions().timeZone;
|
|
25
|
+
} catch {
|
|
26
|
+
/* ignore */
|
|
27
|
+
}
|
|
28
|
+
if (typeof screen !== "undefined") {
|
|
29
|
+
out.screen_resolution = `${screen.width}x${screen.height}`;
|
|
30
|
+
}
|
|
31
|
+
if (typeof navigator !== "undefined") {
|
|
32
|
+
if (navigator.language) {
|
|
33
|
+
out.language = navigator.language;
|
|
34
|
+
}
|
|
35
|
+
if (navigator.platform) {
|
|
36
|
+
out.platform = navigator.platform;
|
|
37
|
+
}
|
|
38
|
+
}
|
|
39
|
+
return out;
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
/** Encode signals for the `X-Authio-Device-Signals` request header. */
|
|
43
|
+
export function encodeDeviceSignalsHeader(
|
|
44
|
+
signals: DeviceSignalsCapture,
|
|
45
|
+
): string {
|
|
46
|
+
const json = JSON.stringify(signals);
|
|
47
|
+
if (typeof btoa === "function") {
|
|
48
|
+
return btoa(json).replace(/\+/g, "-").replace(/\//g, "_").replace(/=+$/, "");
|
|
49
|
+
}
|
|
50
|
+
// Node / test fallback. Typed structurally off globalThis so the browser
|
|
51
|
+
// SDK doesn't need @types/node just for this branch.
|
|
52
|
+
const nodeBuffer = (
|
|
53
|
+
globalThis as {
|
|
54
|
+
Buffer?: {
|
|
55
|
+
from(input: string, encoding: string): { toString(encoding: string): string };
|
|
56
|
+
};
|
|
57
|
+
}
|
|
58
|
+
).Buffer;
|
|
59
|
+
if (!nodeBuffer) {
|
|
60
|
+
throw new Error("No base64 encoder available in this environment.");
|
|
61
|
+
}
|
|
62
|
+
return nodeBuffer
|
|
63
|
+
.from(json, "utf8")
|
|
64
|
+
.toString("base64")
|
|
65
|
+
.replace(/\+/g, "-")
|
|
66
|
+
.replace(/\//g, "_")
|
|
67
|
+
.replace(/=+$/, "");
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
/** Header name auth-core reads for optional client device signals. */
|
|
71
|
+
export const DEVICE_SIGNALS_HEADER = "X-Authio-Device-Signals";
|
|
72
|
+
|
|
73
|
+
export function deviceSignalsExtraHeaders(
|
|
74
|
+
signals?: DeviceSignalsCapture,
|
|
75
|
+
): Record<string, string> {
|
|
76
|
+
const s = signals ?? collectDeviceSignals();
|
|
77
|
+
if (
|
|
78
|
+
!s.timezone &&
|
|
79
|
+
!s.screen_resolution &&
|
|
80
|
+
!s.language &&
|
|
81
|
+
!s.platform
|
|
82
|
+
) {
|
|
83
|
+
return {};
|
|
84
|
+
}
|
|
85
|
+
return { [DEVICE_SIGNALS_HEADER]: encodeDeviceSignalsHeader(s) };
|
|
86
|
+
}
|
package/src/errors.ts
CHANGED
|
@@ -2,34 +2,256 @@ import { AuthioError } from "@useauthio/node";
|
|
|
2
2
|
|
|
3
3
|
export { AuthioError };
|
|
4
4
|
|
|
5
|
+
// ------------------------------------------------------------
|
|
6
|
+
// User-facing copy. Every message the SDK produces on its own must be
|
|
7
|
+
// plain English and tell the user what to do next. Raw JS / browser
|
|
8
|
+
// error text (e.g. "Failed to fetch", "The operation either timed out
|
|
9
|
+
// or was not allowed") is kept on `cause` for debugging, never shown.
|
|
10
|
+
// ------------------------------------------------------------
|
|
11
|
+
|
|
12
|
+
export const AUTHIO_MESSAGES = {
|
|
13
|
+
network_error:
|
|
14
|
+
"We couldn't reach the sign-in service. Check your internet connection and try again.",
|
|
15
|
+
timeout:
|
|
16
|
+
"The sign-in service took too long to respond. Please try again.",
|
|
17
|
+
aborted: "The request was cancelled before it finished. Please try again.",
|
|
18
|
+
invalid_response:
|
|
19
|
+
"We couldn't finish signing you in because the sign-in service sent an unexpected response. Please try again.",
|
|
20
|
+
step_up_required:
|
|
21
|
+
"For your security, we need one more check before signing you in.",
|
|
22
|
+
sign_in_denied:
|
|
23
|
+
"We couldn't sign you in. If you think this is a mistake, contact support or your administrator.",
|
|
24
|
+
rate_limited: "Too many attempts. Please wait a moment and try again.",
|
|
25
|
+
server_error:
|
|
26
|
+
"Something went wrong on our side. Please try again in a moment.",
|
|
27
|
+
request_failed: "We couldn't complete that request. Please try again.",
|
|
28
|
+
webauthn_cancelled:
|
|
29
|
+
"Passkey sign-in was cancelled or timed out. Please try again.",
|
|
30
|
+
webauthn_registration_cancelled:
|
|
31
|
+
"Passkey setup was cancelled or timed out. Please try again.",
|
|
32
|
+
webauthn_already_registered:
|
|
33
|
+
"This device already has a passkey for your account.",
|
|
34
|
+
webauthn_unsupported:
|
|
35
|
+
"This browser doesn't support passkeys. Try a different browser, or sign in another way.",
|
|
36
|
+
not_signed_in: "You need to be signed in to do that. Please sign in and try again.",
|
|
37
|
+
token_rejected:
|
|
38
|
+
"We couldn't verify your sign-in. Please sign in again.",
|
|
39
|
+
} as const;
|
|
40
|
+
|
|
41
|
+
/** Attach the underlying error for debugging without exposing its text. */
|
|
42
|
+
function withCause<E extends Error>(err: E, cause: unknown): E {
|
|
43
|
+
if (cause !== undefined) {
|
|
44
|
+
Object.defineProperty(err, "cause", {
|
|
45
|
+
value: cause,
|
|
46
|
+
enumerable: false,
|
|
47
|
+
configurable: true,
|
|
48
|
+
writable: true,
|
|
49
|
+
});
|
|
50
|
+
}
|
|
51
|
+
return err;
|
|
52
|
+
}
|
|
53
|
+
|
|
5
54
|
/**
|
|
6
55
|
* Wrap a native fetch rejection (network error, abort, timeout) in
|
|
7
56
|
* an `AuthioError` so consumers always get a typed `code` to switch
|
|
8
|
-
* on
|
|
57
|
+
* on and a readable `message`. Already-typed `AuthioError` instances
|
|
58
|
+
* pass through unchanged. The original error is kept on `cause`.
|
|
9
59
|
*/
|
|
10
60
|
export function wrapFetchError(
|
|
11
61
|
err: unknown,
|
|
12
62
|
fallbackCode = "network_error",
|
|
13
63
|
): AuthioError {
|
|
14
64
|
if (err instanceof AuthioError) return err;
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
65
|
+
// DOMException is not `instanceof Error` in every runtime, so read
|
|
66
|
+
// name/message structurally.
|
|
67
|
+
const e = (err && typeof err === "object" ? err : {}) as {
|
|
68
|
+
name?: unknown;
|
|
69
|
+
message?: unknown;
|
|
70
|
+
};
|
|
71
|
+
const name = typeof e.name === "string" ? e.name : "";
|
|
72
|
+
const rawMessage = typeof e.message === "string" ? e.message : "";
|
|
73
|
+
const isTimeout = name === "TimeoutError" || /timed? ?out/i.test(rawMessage);
|
|
74
|
+
if (isTimeout) {
|
|
75
|
+
return withCause(
|
|
76
|
+
new AuthioError({
|
|
77
|
+
code: "timeout",
|
|
78
|
+
message: AUTHIO_MESSAGES.timeout,
|
|
79
|
+
status: 0,
|
|
80
|
+
}),
|
|
81
|
+
err,
|
|
82
|
+
);
|
|
83
|
+
}
|
|
84
|
+
if (name === "AbortError") {
|
|
85
|
+
return withCause(
|
|
86
|
+
new AuthioError({
|
|
87
|
+
code: "aborted",
|
|
88
|
+
message: AUTHIO_MESSAGES.aborted,
|
|
89
|
+
status: 0,
|
|
90
|
+
}),
|
|
91
|
+
err,
|
|
92
|
+
);
|
|
93
|
+
}
|
|
94
|
+
const message =
|
|
95
|
+
fallbackCode === "network_error"
|
|
96
|
+
? AUTHIO_MESSAGES.network_error
|
|
97
|
+
: AUTHIO_MESSAGES.request_failed;
|
|
98
|
+
return withCause(
|
|
99
|
+
new AuthioError({ code: fallbackCode, message, status: 0 }),
|
|
100
|
+
err,
|
|
101
|
+
);
|
|
102
|
+
}
|
|
103
|
+
|
|
104
|
+
/** Readable fallback when auth-core returned an error without a message. */
|
|
105
|
+
export function messageForStatus(status: number): string {
|
|
106
|
+
if (status === 429) return AUTHIO_MESSAGES.rate_limited;
|
|
107
|
+
if (status >= 500) return AUTHIO_MESSAGES.server_error;
|
|
108
|
+
return AUTHIO_MESSAGES.request_failed;
|
|
109
|
+
}
|
|
110
|
+
|
|
111
|
+
// ------------------------------------------------------------
|
|
112
|
+
// Risk-engine outcomes
|
|
113
|
+
// ------------------------------------------------------------
|
|
114
|
+
|
|
115
|
+
/**
|
|
116
|
+
* Thrown (code `step_up_required`) when auth-core's risk engine wants
|
|
117
|
+
* one more verification step before it will issue a session. No session
|
|
118
|
+
* exists yet. Send the user to `stepUpUrl` (the hosted step-up page) to
|
|
119
|
+
* finish signing in:
|
|
120
|
+
*
|
|
121
|
+
* ```ts
|
|
122
|
+
* catch (err) {
|
|
123
|
+
* if (err instanceof AuthioStepUpRequiredError && err.stepUpUrl) {
|
|
124
|
+
* window.location.assign(err.stepUpUrl);
|
|
125
|
+
* }
|
|
126
|
+
* }
|
|
127
|
+
* ```
|
|
128
|
+
*
|
|
129
|
+
* `stepUpUrl` is `undefined` on auth-core versions that predate the
|
|
130
|
+
* field or when no hosted UI is configured for the project.
|
|
131
|
+
*/
|
|
132
|
+
export class AuthioStepUpRequiredError extends AuthioError {
|
|
133
|
+
readonly stepUpUrl?: string;
|
|
134
|
+
readonly challengeId?: string;
|
|
135
|
+
readonly requiredMethod?: string;
|
|
136
|
+
readonly reasons: string[];
|
|
137
|
+
readonly enrollmentRequired: boolean;
|
|
138
|
+
/** auth-core's own explanation of the step, when it sent one. */
|
|
139
|
+
readonly serverMessage?: string;
|
|
140
|
+
|
|
141
|
+
constructor(opts: {
|
|
142
|
+
status: number;
|
|
143
|
+
stepUpUrl?: string;
|
|
144
|
+
challengeId?: string;
|
|
145
|
+
requiredMethod?: string;
|
|
146
|
+
reasons?: string[];
|
|
147
|
+
enrollmentRequired?: boolean;
|
|
148
|
+
serverMessage?: string;
|
|
149
|
+
requestId?: string;
|
|
150
|
+
}) {
|
|
151
|
+
super({
|
|
152
|
+
code: "step_up_required",
|
|
153
|
+
message: AUTHIO_MESSAGES.step_up_required,
|
|
154
|
+
status: opts.status,
|
|
155
|
+
requestId: opts.requestId,
|
|
156
|
+
});
|
|
157
|
+
this.name = "AuthioStepUpRequiredError";
|
|
158
|
+
this.stepUpUrl = opts.stepUpUrl;
|
|
159
|
+
this.challengeId = opts.challengeId;
|
|
160
|
+
this.requiredMethod = opts.requiredMethod;
|
|
161
|
+
this.reasons = opts.reasons ?? [];
|
|
162
|
+
this.enrollmentRequired = Boolean(opts.enrollmentRequired);
|
|
163
|
+
this.serverMessage = opts.serverMessage;
|
|
164
|
+
}
|
|
165
|
+
}
|
|
166
|
+
|
|
167
|
+
/**
|
|
168
|
+
* Thrown (code `sign_in_denied`) when auth-core's risk engine or a
|
|
169
|
+
* project policy (country, network type, risk score) refuses the
|
|
170
|
+
* sign-in outright. `message` is auth-core's human-readable reason;
|
|
171
|
+
* `denyCode` is its stable machine code (e.g. `blocked_by_geo_policy`).
|
|
172
|
+
*/
|
|
173
|
+
export class AuthioSignInDeniedError extends AuthioError {
|
|
174
|
+
readonly denyCode?: string;
|
|
175
|
+
readonly reasons: string[];
|
|
176
|
+
|
|
177
|
+
constructor(opts: {
|
|
178
|
+
status: number;
|
|
179
|
+
message?: string;
|
|
180
|
+
denyCode?: string;
|
|
181
|
+
reasons?: string[];
|
|
182
|
+
requestId?: string;
|
|
183
|
+
}) {
|
|
184
|
+
super({
|
|
185
|
+
code: "sign_in_denied",
|
|
186
|
+
message: nonEmpty(opts.message) ?? AUTHIO_MESSAGES.sign_in_denied,
|
|
187
|
+
status: opts.status,
|
|
188
|
+
requestId: opts.requestId,
|
|
189
|
+
});
|
|
190
|
+
this.name = "AuthioSignInDeniedError";
|
|
191
|
+
this.denyCode = opts.denyCode;
|
|
192
|
+
this.reasons = opts.reasons ?? [];
|
|
193
|
+
}
|
|
194
|
+
}
|
|
195
|
+
|
|
196
|
+
/** True when `err` is a step-up-required error from any SDK helper. */
|
|
197
|
+
export function isStepUpRequired(
|
|
198
|
+
err: unknown,
|
|
199
|
+
): err is AuthioStepUpRequiredError {
|
|
200
|
+
return err instanceof AuthioStepUpRequiredError;
|
|
201
|
+
}
|
|
202
|
+
|
|
203
|
+
/** Wire shape of auth-core's risk fields (step_up / block bodies). */
|
|
204
|
+
export interface RiskDecisionBody {
|
|
205
|
+
decision?: string;
|
|
206
|
+
challenge_id?: string;
|
|
207
|
+
required_method?: string;
|
|
208
|
+
reasons?: unknown;
|
|
209
|
+
step_up_url?: string;
|
|
210
|
+
enrollment_required?: boolean;
|
|
211
|
+
message?: string;
|
|
212
|
+
code?: string;
|
|
213
|
+
error?: string;
|
|
214
|
+
request_id?: string;
|
|
215
|
+
}
|
|
216
|
+
|
|
217
|
+
/**
|
|
218
|
+
* Turn a risk-engine body into a typed error, or `null` when the body
|
|
219
|
+
* is not a step_up / deny decision. Used for both 2xx (step_up is a
|
|
220
|
+
* 200) and non-2xx (deny is a 403) responses.
|
|
221
|
+
*/
|
|
222
|
+
export function riskDecisionError(
|
|
223
|
+
body: RiskDecisionBody | null | undefined,
|
|
224
|
+
status: number,
|
|
225
|
+
): AuthioError | null {
|
|
226
|
+
if (!body || typeof body !== "object") return null;
|
|
227
|
+
const decision = typeof body.decision === "string" ? body.decision : "";
|
|
228
|
+
const reasons = Array.isArray(body.reasons)
|
|
229
|
+
? body.reasons.filter((r): r is string => typeof r === "string")
|
|
230
|
+
: [];
|
|
231
|
+
if (decision === "step_up" || body.code === "step_up_required") {
|
|
232
|
+
return new AuthioStepUpRequiredError({
|
|
233
|
+
status,
|
|
234
|
+
stepUpUrl: nonEmpty(body.step_up_url),
|
|
235
|
+
challengeId: nonEmpty(body.challenge_id),
|
|
236
|
+
requiredMethod: nonEmpty(body.required_method),
|
|
237
|
+
reasons,
|
|
238
|
+
enrollmentRequired: body.enrollment_required === true,
|
|
239
|
+
serverMessage: nonEmpty(body.message),
|
|
240
|
+
requestId: body.request_id,
|
|
20
241
|
});
|
|
21
242
|
}
|
|
22
|
-
if (
|
|
23
|
-
return new
|
|
24
|
-
|
|
25
|
-
message:
|
|
26
|
-
|
|
243
|
+
if (decision === "block" || decision === "deny") {
|
|
244
|
+
return new AuthioSignInDeniedError({
|
|
245
|
+
status,
|
|
246
|
+
message: body.message,
|
|
247
|
+
denyCode: nonEmpty(body.code) ?? nonEmpty(body.error),
|
|
248
|
+
reasons,
|
|
249
|
+
requestId: body.request_id,
|
|
27
250
|
});
|
|
28
251
|
}
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
});
|
|
252
|
+
return null;
|
|
253
|
+
}
|
|
254
|
+
|
|
255
|
+
function nonEmpty(v: unknown): string | undefined {
|
|
256
|
+
return typeof v === "string" && v.trim() !== "" ? v : undefined;
|
|
35
257
|
}
|
package/src/fetch.ts
CHANGED
|
@@ -1,5 +1,11 @@
|
|
|
1
1
|
import { AuthioError } from "@useauthio/node";
|
|
2
|
-
import {
|
|
2
|
+
import {
|
|
3
|
+
AUTHIO_MESSAGES,
|
|
4
|
+
messageForStatus,
|
|
5
|
+
riskDecisionError,
|
|
6
|
+
wrapFetchError,
|
|
7
|
+
type RiskDecisionBody,
|
|
8
|
+
} from "./errors";
|
|
3
9
|
import { SDK_USER_AGENT } from "./version";
|
|
4
10
|
|
|
5
11
|
const DEFAULT_TIMEOUT_MS = 10_000;
|
|
@@ -28,7 +34,10 @@ export interface AuthioFetchOptions {
|
|
|
28
34
|
* - `application/json` content type; body is `JSON.stringify`'d.
|
|
29
35
|
* - 2xx → parses JSON body (or returns undefined on 204).
|
|
30
36
|
* - !2xx → reads the auth-core error envelope (`{ code, message }`)
|
|
31
|
-
* and throws a typed `AuthioError`.
|
|
37
|
+
* and throws a typed `AuthioError`. Risk-engine denies (403
|
|
38
|
+
* `decision: "block"`) throw `AuthioSignInDeniedError`
|
|
39
|
+
* (`sign_in_denied`). When auth-core sends no message, a readable
|
|
40
|
+
* status-based message is used instead.
|
|
32
41
|
* - Network / abort / timeout failures throw `AuthioError` via
|
|
33
42
|
* `wrapFetchError`. Callers never see a raw `TypeError: fetch failed`.
|
|
34
43
|
*/
|
|
@@ -78,17 +87,19 @@ export async function authioFetch<T = unknown>(
|
|
|
78
87
|
}
|
|
79
88
|
|
|
80
89
|
if (!res.ok) {
|
|
81
|
-
const
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
90
|
+
const raw: unknown = await res.json().catch(() => ({}));
|
|
91
|
+
const body = (raw && typeof raw === "object" ? raw : {}) as RiskDecisionBody;
|
|
92
|
+
// Risk-engine deny (403 decision:"block") and 403 step-up variants
|
|
93
|
+
// become typed sign_in_denied / step_up_required errors.
|
|
94
|
+
const risk = riskDecisionError(body, res.status);
|
|
95
|
+
if (risk) throw risk;
|
|
96
|
+
const message =
|
|
97
|
+
typeof body.message === "string" && body.message.trim() !== ""
|
|
98
|
+
? body.message
|
|
99
|
+
: messageForStatus(res.status);
|
|
87
100
|
throw new AuthioError({
|
|
88
101
|
code: body.code ?? body.error ?? "request_failed",
|
|
89
|
-
message
|
|
90
|
-
body.message ??
|
|
91
|
-
`Request to ${opts.path} failed with status ${res.status}`,
|
|
102
|
+
message,
|
|
92
103
|
status: res.status,
|
|
93
104
|
requestId: body.request_id,
|
|
94
105
|
});
|
|
@@ -102,7 +113,7 @@ export async function authioFetch<T = unknown>(
|
|
|
102
113
|
} catch {
|
|
103
114
|
throw new AuthioError({
|
|
104
115
|
code: "invalid_response",
|
|
105
|
-
message:
|
|
116
|
+
message: AUTHIO_MESSAGES.invalid_response,
|
|
106
117
|
status: res.status,
|
|
107
118
|
});
|
|
108
119
|
}
|
package/src/index.ts
CHANGED
|
@@ -17,7 +17,12 @@ export type {
|
|
|
17
17
|
RedirectToSignInProps,
|
|
18
18
|
} from "./gates";
|
|
19
19
|
|
|
20
|
-
export {
|
|
20
|
+
export {
|
|
21
|
+
signInWithMagicLink,
|
|
22
|
+
signInWithPasskey,
|
|
23
|
+
sendEmailOtp,
|
|
24
|
+
verifyEmailOtp,
|
|
25
|
+
} from "./sign-in";
|
|
21
26
|
|
|
22
27
|
export {
|
|
23
28
|
addPasskey,
|
|
@@ -46,20 +51,44 @@ export type {
|
|
|
46
51
|
SignInWithMagicLinkOptions,
|
|
47
52
|
SignInWithPasskeyOptions,
|
|
48
53
|
SignInWithPasskeyResult,
|
|
54
|
+
SendEmailOtpOptions,
|
|
55
|
+
VerifyEmailOtpOptions,
|
|
56
|
+
VerifyEmailOtpResult,
|
|
49
57
|
} from "./sign-in";
|
|
50
58
|
|
|
51
|
-
export { captureClientLocation } from "./locate";
|
|
59
|
+
export { captureClientLocation, verifyLocate } from "./locate";
|
|
52
60
|
export type {
|
|
53
61
|
ClientLocationCapture,
|
|
54
62
|
CaptureClientLocationOptions,
|
|
63
|
+
LocateAction,
|
|
64
|
+
VerifyLocateOptions,
|
|
65
|
+
LocateVerifyResult,
|
|
55
66
|
} from "./locate";
|
|
56
67
|
|
|
57
|
-
export {
|
|
68
|
+
export {
|
|
69
|
+
collectDeviceSignals,
|
|
70
|
+
encodeDeviceSignalsHeader,
|
|
71
|
+
deviceSignalsExtraHeaders,
|
|
72
|
+
DEVICE_SIGNALS_HEADER,
|
|
73
|
+
} from "./device-signals";
|
|
74
|
+
export type { DeviceSignalsCapture } from "./device-signals";
|
|
75
|
+
|
|
76
|
+
export {
|
|
77
|
+
AuthioError,
|
|
78
|
+
AuthioStepUpRequiredError,
|
|
79
|
+
AuthioSignInDeniedError,
|
|
80
|
+
isStepUpRequired,
|
|
81
|
+
wrapFetchError,
|
|
82
|
+
AUTHIO_MESSAGES,
|
|
83
|
+
} from "./errors";
|
|
58
84
|
|
|
59
85
|
export type {
|
|
60
86
|
AuthioUser,
|
|
61
87
|
AuthioStatus,
|
|
62
88
|
AuthioStorageMode,
|
|
89
|
+
AuthioIdleRefreshMode,
|
|
90
|
+
SessionPolicy,
|
|
91
|
+
RawSessionPolicy,
|
|
63
92
|
AuthioContextValue,
|
|
64
93
|
AuthioProviderProps,
|
|
65
94
|
AuthioTokenVerification,
|
|
@@ -67,3 +96,8 @@ export type {
|
|
|
67
96
|
} from "./types";
|
|
68
97
|
|
|
69
98
|
export type { AuthioTelemetryEvent } from "./telemetry";
|
|
99
|
+
|
|
100
|
+
export { coerceSessionPolicy } from "./types";
|
|
101
|
+
export { ActivityTracker } from "./activity";
|
|
102
|
+
export type { ActivityListener } from "./activity";
|
|
103
|
+
export type { RefreshDeferReason } from "./refresh";
|
package/src/lobby-context.ts
CHANGED
|
@@ -7,6 +7,10 @@ export interface MintLobbySignInUrlOptions {
|
|
|
7
7
|
redirectUri: string;
|
|
8
8
|
/** Post-login path threaded separately from redirect_uri (BFF ?next=). */
|
|
9
9
|
next?: string;
|
|
10
|
+
/** Org scope for org_policies.disabled_methods on the Lobby surface. */
|
|
11
|
+
organizationId?: string;
|
|
12
|
+
/** Opaque, consent-gated proof minted by @useauthio/xray. */
|
|
13
|
+
xrayVisitorProof?: string;
|
|
10
14
|
fetchImpl?: typeof fetch;
|
|
11
15
|
}
|
|
12
16
|
|
|
@@ -37,6 +41,12 @@ export async function mintLobbySignInUrl(
|
|
|
37
41
|
project_id: opts.projectId,
|
|
38
42
|
redirect_uri: opts.redirectUri,
|
|
39
43
|
...(opts.next ? { next: opts.next } : {}),
|
|
44
|
+
...(opts.organizationId
|
|
45
|
+
? { organization_id: opts.organizationId }
|
|
46
|
+
: {}),
|
|
47
|
+
...(opts.xrayVisitorProof
|
|
48
|
+
? { xray_visitor_proof: opts.xrayVisitorProof }
|
|
49
|
+
: {}),
|
|
40
50
|
}),
|
|
41
51
|
},
|
|
42
52
|
);
|
|
@@ -59,5 +69,11 @@ export async function mintLobbySignInUrl(
|
|
|
59
69
|
if (opts.next) {
|
|
60
70
|
target.searchParams.set("next", opts.next);
|
|
61
71
|
}
|
|
72
|
+
if (opts.organizationId) {
|
|
73
|
+
target.searchParams.set("organization_id", opts.organizationId);
|
|
74
|
+
}
|
|
75
|
+
if (opts.xrayVisitorProof) {
|
|
76
|
+
target.searchParams.set("xray_visitor_proof", opts.xrayVisitorProof);
|
|
77
|
+
}
|
|
62
78
|
return target.toString();
|
|
63
79
|
}
|