better-auth 1.6.16 → 1.6.18
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/api/index.d.mts +2 -2
- package/dist/api/index.mjs +3 -4
- package/dist/api/middlewares/origin-check.mjs +5 -1
- package/dist/api/rate-limiter/index.mjs +259 -73
- package/dist/api/routes/account.mjs +22 -7
- package/dist/api/routes/callback.mjs +2 -2
- package/dist/api/routes/index.d.mts +1 -1
- package/dist/api/routes/password.mjs +3 -4
- package/dist/api/routes/session.d.mts +12 -1
- package/dist/api/routes/session.mjs +13 -1
- package/dist/api/routes/sign-in.mjs +5 -5
- package/dist/api/routes/sign-up.mjs +2 -2
- package/dist/api/routes/update-session.mjs +2 -3
- package/dist/api/routes/update-user.mjs +10 -12
- package/dist/auth/base.mjs +11 -7
- package/dist/client/equality.d.mts +19 -0
- package/dist/client/equality.mjs +42 -0
- package/dist/client/index.d.mts +5 -4
- package/dist/client/index.mjs +2 -1
- package/dist/client/lynx/index.d.mts +4 -2
- package/dist/client/path-to-object.d.mts +5 -2
- package/dist/client/plugins/index.d.mts +4 -1
- package/dist/client/plugins/index.mjs +4 -1
- package/dist/client/query.d.mts +4 -3
- package/dist/client/query.mjs +27 -17
- package/dist/client/react/index.d.mts +4 -2
- package/dist/client/session-atom.mjs +129 -4
- package/dist/client/session-refresh.d.mts +3 -18
- package/dist/client/session-refresh.mjs +38 -49
- package/dist/client/solid/index.d.mts +4 -2
- package/dist/client/svelte/index.d.mts +4 -2
- package/dist/client/types.d.mts +27 -16
- package/dist/client/vanilla.d.mts +4 -2
- package/dist/client/vue/index.d.mts +4 -2
- package/dist/context/create-context.mjs +2 -1
- package/dist/context/store-capabilities.mjs +12 -0
- package/dist/cookies/index.mjs +25 -2
- package/dist/db/internal-adapter.mjs +51 -0
- package/dist/package.mjs +1 -1
- package/dist/plugins/access/access.mjs +49 -19
- package/dist/plugins/admin/routes.mjs +10 -3
- package/dist/plugins/captcha/constants.mjs +8 -1
- package/dist/plugins/captcha/index.mjs +8 -2
- package/dist/plugins/captcha/types.d.mts +21 -0
- package/dist/plugins/captcha/verify-handlers/captchafox.mjs +2 -0
- package/dist/plugins/captcha/verify-handlers/cloudflare-turnstile.mjs +7 -2
- package/dist/plugins/captcha/verify-handlers/google-recaptcha.mjs +7 -2
- package/dist/plugins/captcha/verify-handlers/h-captcha.mjs +2 -0
- package/dist/plugins/device-authorization/routes.mjs +16 -9
- package/dist/plugins/email-otp/routes.mjs +22 -52
- package/dist/plugins/generic-oauth/index.mjs +7 -2
- package/dist/plugins/generic-oauth/routes.mjs +16 -12
- package/dist/plugins/haveibeenpwned/index.d.mts +1 -1
- package/dist/plugins/haveibeenpwned/index.mjs +5 -1
- package/dist/plugins/index.d.mts +6 -2
- package/dist/plugins/index.mjs +4 -1
- package/dist/plugins/jwt/index.mjs +2 -2
- package/dist/plugins/mcp/client/index.mjs +1 -0
- package/dist/plugins/mcp/index.mjs +8 -0
- package/dist/plugins/multi-session/index.mjs +7 -5
- package/dist/plugins/oauth-popup/client.d.mts +82 -0
- package/dist/plugins/oauth-popup/client.mjs +203 -0
- package/dist/plugins/oauth-popup/constants.d.mts +11 -0
- package/dist/plugins/oauth-popup/constants.mjs +11 -0
- package/dist/plugins/oauth-popup/error-codes.d.mts +11 -0
- package/dist/plugins/oauth-popup/error-codes.mjs +10 -0
- package/dist/plugins/oauth-popup/index.d.mts +67 -0
- package/dist/plugins/oauth-popup/index.mjs +227 -0
- package/dist/plugins/oauth-popup/types.d.mts +30 -0
- package/dist/plugins/oauth-proxy/index.mjs +2 -2
- package/dist/plugins/oauth-proxy/utils.mjs +16 -2
- package/dist/plugins/oidc-provider/index.mjs +10 -0
- package/dist/plugins/one-tap/client.mjs +12 -6
- package/dist/plugins/one-tap/index.d.mts +1 -0
- package/dist/plugins/one-tap/index.mjs +9 -5
- package/dist/plugins/one-time-token/index.mjs +1 -3
- package/dist/plugins/open-api/generator.d.mts +66 -57
- package/dist/plugins/open-api/generator.mjs +185 -67
- package/dist/plugins/open-api/index.d.mts +2 -2
- package/dist/plugins/organization/adapter.d.mts +29 -1
- package/dist/plugins/organization/adapter.mjs +66 -6
- package/dist/plugins/organization/routes/crud-invites.mjs +49 -34
- package/dist/plugins/organization/routes/crud-members.mjs +42 -6
- package/dist/plugins/organization/routes/crud-team.mjs +36 -3
- package/dist/plugins/phone-number/routes.mjs +41 -36
- package/dist/plugins/siwe/index.mjs +2 -3
- package/dist/plugins/two-factor/backup-codes/index.mjs +1 -1
- package/dist/plugins/two-factor/otp/index.mjs +11 -13
- package/dist/plugins/two-factor/totp/index.mjs +1 -1
- package/dist/plugins/two-factor/verify-two-factor.mjs +6 -2
- package/dist/plugins/username/index.mjs +6 -6
- package/dist/test-utils/test-instance.d.mts +26 -23
- package/package.json +9 -9
|
@@ -302,6 +302,10 @@ const mcp = (options) => {
|
|
|
302
302
|
error_description: "refresh token expired",
|
|
303
303
|
error: "invalid_grant"
|
|
304
304
|
});
|
|
305
|
+
if (!token.scopes?.split(" ").includes("offline_access")) throw new APIError("UNAUTHORIZED", {
|
|
306
|
+
error_description: "refresh token was not issued for the offline_access scope",
|
|
307
|
+
error: "invalid_grant"
|
|
308
|
+
});
|
|
305
309
|
const refreshClient = await ctx.context.adapter.findOne({
|
|
306
310
|
model: modelName.oauthClient,
|
|
307
311
|
where: [{
|
|
@@ -693,6 +697,10 @@ const mcp = (options) => {
|
|
|
693
697
|
}]
|
|
694
698
|
});
|
|
695
699
|
if (!accessTokenData) return c.json(null);
|
|
700
|
+
if (accessTokenData.accessTokenExpiresAt < /* @__PURE__ */ new Date()) {
|
|
701
|
+
c.headers?.set("WWW-Authenticate", "Bearer");
|
|
702
|
+
return c.json(null);
|
|
703
|
+
}
|
|
696
704
|
return c.json(accessTokenData);
|
|
697
705
|
})
|
|
698
706
|
},
|
|
@@ -55,8 +55,9 @@ const multiSession = (options) => {
|
|
|
55
55
|
}, async (ctx) => {
|
|
56
56
|
const sessionToken = ctx.body.sessionToken;
|
|
57
57
|
const multiSessionCookieName = `${ctx.context.authCookies.sessionToken.name}_multi-${sessionToken.toLowerCase()}`;
|
|
58
|
-
|
|
59
|
-
|
|
58
|
+
const sessionCookie = await ctx.getSignedCookie(multiSessionCookieName, ctx.context.secret);
|
|
59
|
+
if (!sessionCookie) throw APIError.from("UNAUTHORIZED", MULTI_SESSION_ERROR_CODES.INVALID_SESSION_TOKEN);
|
|
60
|
+
const session = await ctx.context.internalAdapter.findSession(sessionCookie);
|
|
60
61
|
if (!session || session.session.expiresAt < /* @__PURE__ */ new Date()) {
|
|
61
62
|
expireCookie(ctx, {
|
|
62
63
|
name: multiSessionCookieName,
|
|
@@ -88,13 +89,14 @@ const multiSession = (options) => {
|
|
|
88
89
|
}, async (ctx) => {
|
|
89
90
|
const sessionToken = ctx.body.sessionToken;
|
|
90
91
|
const multiSessionCookieName = `${ctx.context.authCookies.sessionToken.name}_multi-${sessionToken.toLowerCase()}`;
|
|
91
|
-
|
|
92
|
-
|
|
92
|
+
const sessionCookie = await ctx.getSignedCookie(multiSessionCookieName, ctx.context.secret);
|
|
93
|
+
if (!sessionCookie) throw APIError.from("UNAUTHORIZED", MULTI_SESSION_ERROR_CODES.INVALID_SESSION_TOKEN);
|
|
94
|
+
await ctx.context.internalAdapter.deleteSession(sessionCookie);
|
|
93
95
|
expireCookie(ctx, {
|
|
94
96
|
name: multiSessionCookieName,
|
|
95
97
|
attributes: ctx.context.authCookies.sessionToken.attributes
|
|
96
98
|
});
|
|
97
|
-
if (!(ctx.context.session?.session.token ===
|
|
99
|
+
if (!(ctx.context.session?.session.token === sessionCookie)) return ctx.json({ status: true });
|
|
98
100
|
const cookieHeader = ctx.headers?.get("cookie");
|
|
99
101
|
if (cookieHeader) {
|
|
100
102
|
const cookies = Object.fromEntries(parseCookies(cookieHeader));
|
|
@@ -0,0 +1,82 @@
|
|
|
1
|
+
import { POPUP_TOKEN_STORAGE_KEY } from "./constants.mjs";
|
|
2
|
+
import { OAUTH_POPUP_ERROR_CODES } from "./error-codes.mjs";
|
|
3
|
+
import { oauthPopup } from "./index.mjs";
|
|
4
|
+
import { BetterAuthClientOptions, ClientStore } from "@better-auth/core";
|
|
5
|
+
import * as _better_auth_core_utils_error_codes0 from "@better-auth/core/utils/error-codes";
|
|
6
|
+
import { BetterFetch, BetterFetchPlugin } from "@better-fetch/fetch";
|
|
7
|
+
|
|
8
|
+
//#region src/plugins/oauth-popup/client.d.ts
|
|
9
|
+
/** Inputs for `authClient.signIn.popup`; mirror the redirect sign-in. */
|
|
10
|
+
interface SignInPopupOptions {
|
|
11
|
+
/** Built-in social provider id (e.g. `"google"`). */
|
|
12
|
+
provider?: string;
|
|
13
|
+
/** Generic OAuth provider id (registered via `genericOAuth`). */
|
|
14
|
+
providerId?: string;
|
|
15
|
+
callbackURL?: string;
|
|
16
|
+
errorCallbackURL?: string;
|
|
17
|
+
newUserCallbackURL?: string;
|
|
18
|
+
requestSignUp?: boolean;
|
|
19
|
+
scopes?: string[];
|
|
20
|
+
additionalData?: Record<string, unknown>;
|
|
21
|
+
/** `window.open` feature string; defaults to a centered 500x600 window. */
|
|
22
|
+
windowFeatures?: string;
|
|
23
|
+
/** How long (ms) to wait for the popup to complete. Default 5 minutes. */
|
|
24
|
+
timeoutMs?: number;
|
|
25
|
+
}
|
|
26
|
+
interface SignInPopupResult {
|
|
27
|
+
data: {
|
|
28
|
+
success: boolean;
|
|
29
|
+
} | null;
|
|
30
|
+
error: {
|
|
31
|
+
code: string;
|
|
32
|
+
message: string;
|
|
33
|
+
status?: number;
|
|
34
|
+
} | null;
|
|
35
|
+
}
|
|
36
|
+
/** Reads the stored popup token (browser-only; null otherwise). */
|
|
37
|
+
declare function getStoredPopupToken(): string | null;
|
|
38
|
+
/**
|
|
39
|
+
* Attaches the popup token as a bearer header when embedded (where the cookie is
|
|
40
|
+
* partitioned), and clears it once the session ends so it can't be reused.
|
|
41
|
+
*/
|
|
42
|
+
declare const popupBearerFetchPlugin: BetterFetchPlugin;
|
|
43
|
+
interface SignInPopupDeps {
|
|
44
|
+
$fetch: BetterFetch;
|
|
45
|
+
options?: BetterAuthClientOptions | undefined;
|
|
46
|
+
/** Refreshes the reactive session, as the redirect flow's atom listeners do. */
|
|
47
|
+
notifySessionSignal: () => void;
|
|
48
|
+
}
|
|
49
|
+
/**
|
|
50
|
+
* Builds `signIn.popup`. Runs the sign-in in the popup's own first-party
|
|
51
|
+
* context (so the OAuth state cookie lands there), waits for the completion
|
|
52
|
+
* page to post the session token back, stores it for the bearer fetch plugin,
|
|
53
|
+
* and refreshes the reactive session.
|
|
54
|
+
*/
|
|
55
|
+
declare function createSignInPopup({
|
|
56
|
+
$fetch,
|
|
57
|
+
options,
|
|
58
|
+
notifySessionSignal
|
|
59
|
+
}: SignInPopupDeps): (opts: SignInPopupOptions) => Promise<SignInPopupResult>;
|
|
60
|
+
/**
|
|
61
|
+
* Client plugin for popup OAuth sign-in. Adds `authClient.signIn.popup`. Pair
|
|
62
|
+
* with the server `oauthPopup` and `bearer` plugins.
|
|
63
|
+
*/
|
|
64
|
+
declare const oauthPopupClient: () => {
|
|
65
|
+
id: "oauth-popup";
|
|
66
|
+
version: string;
|
|
67
|
+
$InferServerPlugin: ReturnType<typeof oauthPopup>;
|
|
68
|
+
$ERROR_CODES: {
|
|
69
|
+
POPUP_SIGN_IN_FAILED: _better_auth_core_utils_error_codes0.RawError<"POPUP_SIGN_IN_FAILED">;
|
|
70
|
+
POPUP_BLOCKED: _better_auth_core_utils_error_codes0.RawError<"POPUP_BLOCKED">;
|
|
71
|
+
POPUP_CLOSED: _better_auth_core_utils_error_codes0.RawError<"POPUP_CLOSED">;
|
|
72
|
+
POPUP_TIMEOUT: _better_auth_core_utils_error_codes0.RawError<"POPUP_TIMEOUT">;
|
|
73
|
+
};
|
|
74
|
+
fetchPlugins: BetterFetchPlugin[];
|
|
75
|
+
getActions: ($fetch: BetterFetch, $store: ClientStore, options: BetterAuthClientOptions | undefined) => {
|
|
76
|
+
signIn: {
|
|
77
|
+
popup: (opts: SignInPopupOptions) => Promise<SignInPopupResult>;
|
|
78
|
+
};
|
|
79
|
+
};
|
|
80
|
+
};
|
|
81
|
+
//#endregion
|
|
82
|
+
export { SignInPopupOptions, SignInPopupResult, createSignInPopup, getStoredPopupToken, oauthPopupClient, popupBearerFetchPlugin };
|
|
@@ -0,0 +1,203 @@
|
|
|
1
|
+
import { getBaseURL } from "../../utils/url.mjs";
|
|
2
|
+
import { PACKAGE_VERSION } from "../../version.mjs";
|
|
3
|
+
import { POPUP_TOKEN_STORAGE_KEY } from "./constants.mjs";
|
|
4
|
+
import { OAUTH_POPUP_ERROR_CODES } from "./error-codes.mjs";
|
|
5
|
+
//#region src/plugins/oauth-popup/client.ts
|
|
6
|
+
const POPUP_NAME = "better-auth-oauth";
|
|
7
|
+
const POPUP_WIDTH = 500;
|
|
8
|
+
const POPUP_HEIGHT = 600;
|
|
9
|
+
const CLOSED_POLL_MS = 500;
|
|
10
|
+
const DEFAULT_TIMEOUT_MS = 300 * 1e3;
|
|
11
|
+
/** True when embedded cross-origin, where the cookie may be partitioned. */
|
|
12
|
+
function isEmbedded() {
|
|
13
|
+
if (typeof window === "undefined" || window.self === window.top) return false;
|
|
14
|
+
try {
|
|
15
|
+
window.top?.location.href;
|
|
16
|
+
return false;
|
|
17
|
+
} catch {
|
|
18
|
+
return true;
|
|
19
|
+
}
|
|
20
|
+
}
|
|
21
|
+
/** Reads the stored popup token (browser-only; null otherwise). */
|
|
22
|
+
function getStoredPopupToken() {
|
|
23
|
+
if (typeof window === "undefined") return null;
|
|
24
|
+
try {
|
|
25
|
+
return window.localStorage.getItem(POPUP_TOKEN_STORAGE_KEY);
|
|
26
|
+
} catch {
|
|
27
|
+
return null;
|
|
28
|
+
}
|
|
29
|
+
}
|
|
30
|
+
function storePopupToken(token) {
|
|
31
|
+
try {
|
|
32
|
+
window.localStorage?.setItem(POPUP_TOKEN_STORAGE_KEY, token);
|
|
33
|
+
} catch {}
|
|
34
|
+
}
|
|
35
|
+
function clearPopupToken() {
|
|
36
|
+
try {
|
|
37
|
+
window.localStorage?.removeItem(POPUP_TOKEN_STORAGE_KEY);
|
|
38
|
+
} catch {}
|
|
39
|
+
}
|
|
40
|
+
/**
|
|
41
|
+
* Attaches the popup token as a bearer header when embedded (where the cookie is
|
|
42
|
+
* partitioned), and clears it once the session ends so it can't be reused.
|
|
43
|
+
*/
|
|
44
|
+
const popupBearerFetchPlugin = {
|
|
45
|
+
id: "better-auth-popup-bearer",
|
|
46
|
+
name: "Popup Bearer",
|
|
47
|
+
hooks: {
|
|
48
|
+
onRequest(context) {
|
|
49
|
+
if (!isEmbedded()) return context;
|
|
50
|
+
const token = getStoredPopupToken();
|
|
51
|
+
if (!token) return context;
|
|
52
|
+
const headers = new Headers(context.headers);
|
|
53
|
+
if (!headers.has("authorization")) headers.set("authorization", `Bearer ${token}`);
|
|
54
|
+
return {
|
|
55
|
+
...context,
|
|
56
|
+
headers
|
|
57
|
+
};
|
|
58
|
+
},
|
|
59
|
+
onSuccess(context) {
|
|
60
|
+
const path = new URL(context.request.url).pathname;
|
|
61
|
+
if (path.endsWith("/sign-out") || path.endsWith("/revoke-session") || path.endsWith("/revoke-sessions") || path.endsWith("/revoke-other-sessions") || path.endsWith("/delete-user")) clearPopupToken();
|
|
62
|
+
}
|
|
63
|
+
}
|
|
64
|
+
};
|
|
65
|
+
let activePopup = null;
|
|
66
|
+
function popupError(code, status) {
|
|
67
|
+
return {
|
|
68
|
+
data: null,
|
|
69
|
+
error: {
|
|
70
|
+
code,
|
|
71
|
+
message: String(OAUTH_POPUP_ERROR_CODES[code]),
|
|
72
|
+
...status ? { status } : {}
|
|
73
|
+
}
|
|
74
|
+
};
|
|
75
|
+
}
|
|
76
|
+
function centeredFeatures() {
|
|
77
|
+
return `width=${POPUP_WIDTH},height=${POPUP_HEIGHT},left=${window.screenX + (window.outerWidth - POPUP_WIDTH) / 2},top=${window.screenY + (window.outerHeight - POPUP_HEIGHT) / 2},menubar=no,toolbar=no`;
|
|
78
|
+
}
|
|
79
|
+
function randomNonce() {
|
|
80
|
+
const bytes = new Uint8Array(16);
|
|
81
|
+
crypto.getRandomValues(bytes);
|
|
82
|
+
return Array.from(bytes, (b) => b.toString(16).padStart(2, "0")).join("");
|
|
83
|
+
}
|
|
84
|
+
/**
|
|
85
|
+
* Resolves with the token (or relayed error) once the completion page posts
|
|
86
|
+
* back, gating on origin, type, and nonce.
|
|
87
|
+
*/
|
|
88
|
+
function waitForPopupResult(popup, expectedOrigin, nonce, timeoutMs) {
|
|
89
|
+
return new Promise((resolve) => {
|
|
90
|
+
let settled = false;
|
|
91
|
+
const settle = (outcome) => {
|
|
92
|
+
if (settled) return;
|
|
93
|
+
settled = true;
|
|
94
|
+
window.removeEventListener("message", onMessage);
|
|
95
|
+
clearInterval(closedPoll);
|
|
96
|
+
clearTimeout(timeout);
|
|
97
|
+
try {
|
|
98
|
+
if (!popup.closed) popup.close();
|
|
99
|
+
} catch {}
|
|
100
|
+
resolve(outcome);
|
|
101
|
+
};
|
|
102
|
+
const onMessage = (event) => {
|
|
103
|
+
if (event.origin !== expectedOrigin) return;
|
|
104
|
+
const data = event.data;
|
|
105
|
+
if (data?.type !== "better-auth:oauth-popup") return;
|
|
106
|
+
if (data.nonce !== nonce) return;
|
|
107
|
+
if (data.error) {
|
|
108
|
+
settle({
|
|
109
|
+
status: "error",
|
|
110
|
+
error: data.error
|
|
111
|
+
});
|
|
112
|
+
return;
|
|
113
|
+
}
|
|
114
|
+
if (typeof data.token !== "string" || !data.token) return;
|
|
115
|
+
settle({
|
|
116
|
+
status: "success",
|
|
117
|
+
token: data.token
|
|
118
|
+
});
|
|
119
|
+
};
|
|
120
|
+
const closedPoll = setInterval(() => {
|
|
121
|
+
if (popup.closed) settle({ status: "cancelled" });
|
|
122
|
+
}, CLOSED_POLL_MS);
|
|
123
|
+
const timeout = setTimeout(() => settle({ status: "timeout" }), timeoutMs);
|
|
124
|
+
window.addEventListener("message", onMessage);
|
|
125
|
+
});
|
|
126
|
+
}
|
|
127
|
+
function resolveAuthURL(options) {
|
|
128
|
+
const configured = getBaseURL(options?.baseURL, options?.basePath) ?? options?.basePath ?? "/api/auth";
|
|
129
|
+
return new URL(configured, window.location.origin);
|
|
130
|
+
}
|
|
131
|
+
/**
|
|
132
|
+
* Builds `signIn.popup`. Runs the sign-in in the popup's own first-party
|
|
133
|
+
* context (so the OAuth state cookie lands there), waits for the completion
|
|
134
|
+
* page to post the session token back, stores it for the bearer fetch plugin,
|
|
135
|
+
* and refreshes the reactive session.
|
|
136
|
+
*/
|
|
137
|
+
function createSignInPopup({ $fetch, options, notifySessionSignal }) {
|
|
138
|
+
return async function signInPopup(opts) {
|
|
139
|
+
if (typeof window === "undefined") return popupError("POPUP_SIGN_IN_FAILED");
|
|
140
|
+
const { provider, providerId, additionalData, windowFeatures, timeoutMs = DEFAULT_TIMEOUT_MS, callbackURL, errorCallbackURL, newUserCallbackURL, scopes, requestSignUp } = opts;
|
|
141
|
+
if (!provider && !providerId) return popupError("POPUP_SIGN_IN_FAILED");
|
|
142
|
+
if (activePopup && !activePopup.closed) {
|
|
143
|
+
activePopup.focus();
|
|
144
|
+
return popupError("POPUP_SIGN_IN_FAILED");
|
|
145
|
+
}
|
|
146
|
+
const nonce = randomNonce();
|
|
147
|
+
const authUrl = resolveAuthURL(options);
|
|
148
|
+
const authOrigin = authUrl.origin;
|
|
149
|
+
const startUrl = new URL(`${authUrl.href.replace(/\/$/, "")}/oauth-popup/start`);
|
|
150
|
+
startUrl.searchParams.set("provider", provider ?? providerId);
|
|
151
|
+
startUrl.searchParams.set("popupOrigin", window.location.origin);
|
|
152
|
+
startUrl.searchParams.set("popupNonce", nonce);
|
|
153
|
+
if (callbackURL) startUrl.searchParams.set("callbackURL", callbackURL);
|
|
154
|
+
if (errorCallbackURL) startUrl.searchParams.set("errorCallbackURL", errorCallbackURL);
|
|
155
|
+
if (newUserCallbackURL) startUrl.searchParams.set("newUserCallbackURL", newUserCallbackURL);
|
|
156
|
+
if (scopes?.length) startUrl.searchParams.set("scopes", scopes.join(","));
|
|
157
|
+
if (requestSignUp) startUrl.searchParams.set("requestSignUp", "true");
|
|
158
|
+
if (additionalData) startUrl.searchParams.set("additionalData", JSON.stringify(additionalData));
|
|
159
|
+
const popup = window.open(startUrl.toString(), POPUP_NAME, windowFeatures ?? centeredFeatures());
|
|
160
|
+
if (!popup) return popupError("POPUP_BLOCKED");
|
|
161
|
+
activePopup = popup;
|
|
162
|
+
const outcome = await waitForPopupResult(popup, authOrigin, nonce, timeoutMs);
|
|
163
|
+
activePopup = null;
|
|
164
|
+
if (outcome.status === "timeout") return popupError("POPUP_TIMEOUT");
|
|
165
|
+
if (outcome.status === "cancelled") return popupError("POPUP_CLOSED");
|
|
166
|
+
if (outcome.status === "error") return {
|
|
167
|
+
data: null,
|
|
168
|
+
error: {
|
|
169
|
+
code: outcome.error.code,
|
|
170
|
+
message: outcome.error.description || outcome.error.code
|
|
171
|
+
}
|
|
172
|
+
};
|
|
173
|
+
if (isEmbedded()) storePopupToken(outcome.token);
|
|
174
|
+
else clearPopupToken();
|
|
175
|
+
const session = await $fetch("/get-session");
|
|
176
|
+
if (session.error || !session.data) return popupError("POPUP_SIGN_IN_FAILED", session.error?.status);
|
|
177
|
+
notifySessionSignal();
|
|
178
|
+
return {
|
|
179
|
+
data: { success: true },
|
|
180
|
+
error: null
|
|
181
|
+
};
|
|
182
|
+
};
|
|
183
|
+
}
|
|
184
|
+
/**
|
|
185
|
+
* Client plugin for popup OAuth sign-in. Adds `authClient.signIn.popup`. Pair
|
|
186
|
+
* with the server `oauthPopup` and `bearer` plugins.
|
|
187
|
+
*/
|
|
188
|
+
const oauthPopupClient = () => {
|
|
189
|
+
return {
|
|
190
|
+
id: "oauth-popup",
|
|
191
|
+
version: PACKAGE_VERSION,
|
|
192
|
+
$InferServerPlugin: {},
|
|
193
|
+
$ERROR_CODES: OAUTH_POPUP_ERROR_CODES,
|
|
194
|
+
fetchPlugins: [popupBearerFetchPlugin],
|
|
195
|
+
getActions: ($fetch, $store, options) => ({ signIn: { popup: createSignInPopup({
|
|
196
|
+
$fetch,
|
|
197
|
+
options,
|
|
198
|
+
notifySessionSignal: () => $store.notify("$sessionSignal")
|
|
199
|
+
}) } })
|
|
200
|
+
};
|
|
201
|
+
};
|
|
202
|
+
//#endregion
|
|
203
|
+
export { createSignInPopup, getStoredPopupToken, oauthPopupClient, popupBearerFetchPlugin };
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
//#region src/plugins/oauth-popup/constants.d.ts
|
|
2
|
+
/** postMessage `type` the completion page posts to its opener. */
|
|
3
|
+
declare const OAUTH_POPUP_MESSAGE_TYPE = "better-auth:oauth-popup";
|
|
4
|
+
/** DOM id of the inert JSON data block the completion page reads. */
|
|
5
|
+
declare const OAUTH_POPUP_DATA_ELEMENT_ID = "better-auth-oauth-popup";
|
|
6
|
+
/** Signed cookie carrying the opener origin/nonce from sign-in to callback. */
|
|
7
|
+
declare const POPUP_MARKER_COOKIE = "oauth_popup";
|
|
8
|
+
/** localStorage key the popup session token is persisted under. */
|
|
9
|
+
declare const POPUP_TOKEN_STORAGE_KEY = "better-auth.popup_token";
|
|
10
|
+
//#endregion
|
|
11
|
+
export { OAUTH_POPUP_DATA_ELEMENT_ID, OAUTH_POPUP_MESSAGE_TYPE, POPUP_MARKER_COOKIE, POPUP_TOKEN_STORAGE_KEY };
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
//#region src/plugins/oauth-popup/constants.ts
|
|
2
|
+
/** postMessage `type` the completion page posts to its opener. */
|
|
3
|
+
const OAUTH_POPUP_MESSAGE_TYPE = "better-auth:oauth-popup";
|
|
4
|
+
/** DOM id of the inert JSON data block the completion page reads. */
|
|
5
|
+
const OAUTH_POPUP_DATA_ELEMENT_ID = "better-auth-oauth-popup";
|
|
6
|
+
/** Signed cookie carrying the opener origin/nonce from sign-in to callback. */
|
|
7
|
+
const POPUP_MARKER_COOKIE = "oauth_popup";
|
|
8
|
+
/** localStorage key the popup session token is persisted under. */
|
|
9
|
+
const POPUP_TOKEN_STORAGE_KEY = "better-auth.popup_token";
|
|
10
|
+
//#endregion
|
|
11
|
+
export { OAUTH_POPUP_DATA_ELEMENT_ID, OAUTH_POPUP_MESSAGE_TYPE, POPUP_MARKER_COOKIE, POPUP_TOKEN_STORAGE_KEY };
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
import * as _better_auth_core_utils_error_codes0 from "@better-auth/core/utils/error-codes";
|
|
2
|
+
|
|
3
|
+
//#region src/plugins/oauth-popup/error-codes.d.ts
|
|
4
|
+
declare const OAUTH_POPUP_ERROR_CODES: {
|
|
5
|
+
POPUP_SIGN_IN_FAILED: _better_auth_core_utils_error_codes0.RawError<"POPUP_SIGN_IN_FAILED">;
|
|
6
|
+
POPUP_BLOCKED: _better_auth_core_utils_error_codes0.RawError<"POPUP_BLOCKED">;
|
|
7
|
+
POPUP_CLOSED: _better_auth_core_utils_error_codes0.RawError<"POPUP_CLOSED">;
|
|
8
|
+
POPUP_TIMEOUT: _better_auth_core_utils_error_codes0.RawError<"POPUP_TIMEOUT">;
|
|
9
|
+
};
|
|
10
|
+
//#endregion
|
|
11
|
+
export { OAUTH_POPUP_ERROR_CODES };
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
import { defineErrorCodes } from "@better-auth/core/utils/error-codes";
|
|
2
|
+
//#region src/plugins/oauth-popup/error-codes.ts
|
|
3
|
+
const OAUTH_POPUP_ERROR_CODES = defineErrorCodes({
|
|
4
|
+
POPUP_SIGN_IN_FAILED: "Popup sign-in failed",
|
|
5
|
+
POPUP_BLOCKED: "Sign-in popup was blocked by the browser",
|
|
6
|
+
POPUP_CLOSED: "Sign-in popup was closed before completing",
|
|
7
|
+
POPUP_TIMEOUT: "Sign-in popup timed out"
|
|
8
|
+
});
|
|
9
|
+
//#endregion
|
|
10
|
+
export { OAUTH_POPUP_ERROR_CODES };
|
|
@@ -0,0 +1,67 @@
|
|
|
1
|
+
import { OAUTH_POPUP_DATA_ELEMENT_ID, OAUTH_POPUP_MESSAGE_TYPE, POPUP_MARKER_COOKIE } from "./constants.mjs";
|
|
2
|
+
import { OAUTH_POPUP_ERROR_CODES } from "./error-codes.mjs";
|
|
3
|
+
import { OAuthPopupData, OAuthPopupMessage } from "./types.mjs";
|
|
4
|
+
import * as _better_auth_core0 from "@better-auth/core";
|
|
5
|
+
import * as _better_auth_core_utils_error_codes0 from "@better-auth/core/utils/error-codes";
|
|
6
|
+
import * as better_call0 from "better-call";
|
|
7
|
+
import * as z from "zod";
|
|
8
|
+
|
|
9
|
+
//#region src/plugins/oauth-popup/index.d.ts
|
|
10
|
+
declare module "@better-auth/core" {
|
|
11
|
+
interface BetterAuthPluginRegistry<AuthOptions, Options> {
|
|
12
|
+
"oauth-popup": {
|
|
13
|
+
creator: typeof oauthPopup;
|
|
14
|
+
};
|
|
15
|
+
}
|
|
16
|
+
}
|
|
17
|
+
/**
|
|
18
|
+
* The completion-page script.
|
|
19
|
+
*/
|
|
20
|
+
declare const OAUTH_POPUP_COMPLETE_SCRIPT: string;
|
|
21
|
+
/**
|
|
22
|
+
* sha256 of `OAUTH_POPUP_COMPLETE_SCRIPT`, pinned in the completion CSP.
|
|
23
|
+
*/
|
|
24
|
+
declare const OAUTH_POPUP_SCRIPT_CSP_HASH = "sha256-tIo2K8VBC9SnhvdZ+9GsGkQoZm+jm/JcxL+d+i8b8KQ=";
|
|
25
|
+
/**
|
|
26
|
+
* Server plugin for popup-based OAuth. `signIn.popup` navigates the popup to
|
|
27
|
+
* `/oauth-popup/start`; on the OAuth callback this plugin swaps the redirect for
|
|
28
|
+
* a page that posts the session token (or error) back to the opener. Pair with
|
|
29
|
+
* the `bearer` plugin and `oauthPopupClient`.
|
|
30
|
+
*/
|
|
31
|
+
declare const oauthPopup: () => {
|
|
32
|
+
id: "oauth-popup";
|
|
33
|
+
version: string;
|
|
34
|
+
$ERROR_CODES: {
|
|
35
|
+
POPUP_SIGN_IN_FAILED: _better_auth_core_utils_error_codes0.RawError<"POPUP_SIGN_IN_FAILED">;
|
|
36
|
+
POPUP_BLOCKED: _better_auth_core_utils_error_codes0.RawError<"POPUP_BLOCKED">;
|
|
37
|
+
POPUP_CLOSED: _better_auth_core_utils_error_codes0.RawError<"POPUP_CLOSED">;
|
|
38
|
+
POPUP_TIMEOUT: _better_auth_core_utils_error_codes0.RawError<"POPUP_TIMEOUT">;
|
|
39
|
+
};
|
|
40
|
+
endpoints: {
|
|
41
|
+
oauthPopupStart: better_call0.StrictEndpoint<"/oauth-popup/start", {
|
|
42
|
+
method: "GET";
|
|
43
|
+
query: z.ZodObject<{
|
|
44
|
+
provider: z.ZodString;
|
|
45
|
+
popupOrigin: z.ZodString;
|
|
46
|
+
popupNonce: z.ZodOptional<z.ZodString>;
|
|
47
|
+
callbackURL: z.ZodOptional<z.ZodString>;
|
|
48
|
+
errorCallbackURL: z.ZodOptional<z.ZodString>;
|
|
49
|
+
newUserCallbackURL: z.ZodOptional<z.ZodString>;
|
|
50
|
+
scopes: z.ZodOptional<z.ZodString>;
|
|
51
|
+
requestSignUp: z.ZodOptional<z.ZodString>;
|
|
52
|
+
additionalData: z.ZodOptional<z.ZodString>;
|
|
53
|
+
}, z.core.$strip>;
|
|
54
|
+
metadata: {
|
|
55
|
+
readonly scope: "server";
|
|
56
|
+
};
|
|
57
|
+
}, Response>;
|
|
58
|
+
};
|
|
59
|
+
hooks: {
|
|
60
|
+
after: {
|
|
61
|
+
matcher(context: _better_auth_core0.HookEndpointContext): boolean;
|
|
62
|
+
handler: (inputContext: better_call0.MiddlewareInputContext<better_call0.MiddlewareOptions>) => Promise<void>;
|
|
63
|
+
}[];
|
|
64
|
+
};
|
|
65
|
+
};
|
|
66
|
+
//#endregion
|
|
67
|
+
export { OAUTH_POPUP_COMPLETE_SCRIPT, OAUTH_POPUP_SCRIPT_CSP_HASH, oauthPopup };
|
|
@@ -0,0 +1,227 @@
|
|
|
1
|
+
import { parseSetCookieHeader, splitSetCookieHeader } from "../../cookies/cookie-utils.mjs";
|
|
2
|
+
import { generateRandomString } from "../../crypto/random.mjs";
|
|
3
|
+
import { expireCookie } from "../../cookies/index.mjs";
|
|
4
|
+
import { getAwaitableValue } from "../../context/helpers.mjs";
|
|
5
|
+
import { setOAuthState } from "../../api/state/oauth.mjs";
|
|
6
|
+
import { generateGenericState } from "../../state.mjs";
|
|
7
|
+
import { HIDE_METADATA } from "../../utils/hide-metadata.mjs";
|
|
8
|
+
import { PACKAGE_VERSION } from "../../version.mjs";
|
|
9
|
+
import { OAUTH_POPUP_DATA_ELEMENT_ID, OAUTH_POPUP_MESSAGE_TYPE, POPUP_MARKER_COOKIE } from "./constants.mjs";
|
|
10
|
+
import { OAUTH_POPUP_ERROR_CODES } from "./error-codes.mjs";
|
|
11
|
+
import { APIError, BASE_ERROR_CODES } from "@better-auth/core/error";
|
|
12
|
+
import { safeJSONParse } from "@better-auth/core/utils/json";
|
|
13
|
+
import { createAuthEndpoint, createAuthMiddleware } from "@better-auth/core/api";
|
|
14
|
+
import * as z from "zod";
|
|
15
|
+
//#region src/plugins/oauth-popup/index.ts
|
|
16
|
+
let warnedMissingBearer = false;
|
|
17
|
+
/**
|
|
18
|
+
* Escapes `<\/script>` and JS line separators for embedding in a script element.
|
|
19
|
+
*/
|
|
20
|
+
function inlineJSON(value) {
|
|
21
|
+
return JSON.stringify(value).replace(/[<\u2028\u2029]/g, (c) => `\\u${c.charCodeAt(0).toString(16).padStart(4, "0")}`);
|
|
22
|
+
}
|
|
23
|
+
/**
|
|
24
|
+
* The completion-page script.
|
|
25
|
+
*/
|
|
26
|
+
const OAUTH_POPUP_COMPLETE_SCRIPT = `(function () {
|
|
27
|
+
var el = document.getElementById(${JSON.stringify(OAUTH_POPUP_DATA_ELEMENT_ID)});
|
|
28
|
+
if (!el) return;
|
|
29
|
+
var payload;
|
|
30
|
+
try {
|
|
31
|
+
payload = JSON.parse(el.textContent || "");
|
|
32
|
+
} catch (e) {
|
|
33
|
+
return;
|
|
34
|
+
}
|
|
35
|
+
var target = window.opener || window.parent;
|
|
36
|
+
if (target && target !== window) {
|
|
37
|
+
try {
|
|
38
|
+
target.postMessage(
|
|
39
|
+
{
|
|
40
|
+
type: payload.type,
|
|
41
|
+
nonce: payload.nonce,
|
|
42
|
+
token: payload.token,
|
|
43
|
+
redirectTo: payload.redirectTo,
|
|
44
|
+
error: payload.error,
|
|
45
|
+
},
|
|
46
|
+
payload.targetOrigin,
|
|
47
|
+
);
|
|
48
|
+
} catch (e) {}
|
|
49
|
+
}
|
|
50
|
+
window.close();
|
|
51
|
+
})();
|
|
52
|
+
`;
|
|
53
|
+
/**
|
|
54
|
+
* sha256 of `OAUTH_POPUP_COMPLETE_SCRIPT`, pinned in the completion CSP.
|
|
55
|
+
*/
|
|
56
|
+
const OAUTH_POPUP_SCRIPT_CSP_HASH = "sha256-tIo2K8VBC9SnhvdZ+9GsGkQoZm+jm/JcxL+d+i8b8KQ=";
|
|
57
|
+
/**
|
|
58
|
+
* Renders the page that posts the outcome (token or error) to the opener. The
|
|
59
|
+
* caller must pass a trusted `popupOrigin` — validated at `/oauth-popup/start`
|
|
60
|
+
* and preserved in the signed marker cookie the callback reads.
|
|
61
|
+
*/
|
|
62
|
+
function renderCompletion(c, popupOrigin, message) {
|
|
63
|
+
if (message.token && !warnedMissingBearer && !c.context.hasPlugin("bearer")) {
|
|
64
|
+
warnedMissingBearer = true;
|
|
65
|
+
c.context.logger.warn("OAuth popup hands the session token back via postMessage, but the `bearer` plugin is not registered, so an embedded (cross-site iframe) app cannot authenticate with it. Add bearer() to your auth `plugins`.");
|
|
66
|
+
}
|
|
67
|
+
const html = `<!doctype html>
|
|
68
|
+
<html>
|
|
69
|
+
<head><meta charset="utf-8"><title>Completing sign-in</title></head>
|
|
70
|
+
<body>
|
|
71
|
+
<script type="application/json" id="${OAUTH_POPUP_DATA_ELEMENT_ID}">${inlineJSON({
|
|
72
|
+
type: OAUTH_POPUP_MESSAGE_TYPE,
|
|
73
|
+
targetOrigin: popupOrigin,
|
|
74
|
+
...message
|
|
75
|
+
})}<\/script>
|
|
76
|
+
<script>${OAUTH_POPUP_COMPLETE_SCRIPT}<\/script>
|
|
77
|
+
</body>
|
|
78
|
+
</html>`;
|
|
79
|
+
return new Response(html, {
|
|
80
|
+
status: 200,
|
|
81
|
+
headers: {
|
|
82
|
+
"content-type": "text/html; charset=utf-8",
|
|
83
|
+
"content-security-policy": `default-src 'none'; script-src '${OAUTH_POPUP_SCRIPT_CSP_HASH}'; base-uri 'none'`,
|
|
84
|
+
"cache-control": "no-store",
|
|
85
|
+
pragma: "no-cache"
|
|
86
|
+
}
|
|
87
|
+
});
|
|
88
|
+
}
|
|
89
|
+
/**
|
|
90
|
+
* Starts the OAuth flow for a popup. The popup navigates here (top-level, so it
|
|
91
|
+
* is first-party to the auth origin even when the app is on another origin),
|
|
92
|
+
* the server sets the state + opener-marker cookies in the right partition, then
|
|
93
|
+
* redirects to the provider. The callback then renders the completion page.
|
|
94
|
+
*/
|
|
95
|
+
const oauthPopupStart = createAuthEndpoint("/oauth-popup/start", {
|
|
96
|
+
method: "GET",
|
|
97
|
+
query: z.object({
|
|
98
|
+
provider: z.string(),
|
|
99
|
+
popupOrigin: z.string(),
|
|
100
|
+
popupNonce: z.string().optional(),
|
|
101
|
+
callbackURL: z.string().optional(),
|
|
102
|
+
errorCallbackURL: z.string().optional(),
|
|
103
|
+
newUserCallbackURL: z.string().optional(),
|
|
104
|
+
scopes: z.string().optional(),
|
|
105
|
+
requestSignUp: z.string().optional(),
|
|
106
|
+
additionalData: z.string().optional()
|
|
107
|
+
}),
|
|
108
|
+
metadata: HIDE_METADATA
|
|
109
|
+
}, async (c) => {
|
|
110
|
+
const { popupOrigin } = c.query;
|
|
111
|
+
if (!c.context.isTrustedOrigin(popupOrigin, { allowRelativePaths: false })) {
|
|
112
|
+
c.context.logger.error(`OAuth popup origin is not a trusted origin. Add ${popupOrigin} to trustedOrigins.`);
|
|
113
|
+
throw APIError.from("FORBIDDEN", BASE_ERROR_CODES.INVALID_ORIGIN);
|
|
114
|
+
}
|
|
115
|
+
const fail = (code, description) => renderCompletion(c, popupOrigin, {
|
|
116
|
+
nonce: c.query.popupNonce ?? "",
|
|
117
|
+
error: {
|
|
118
|
+
code,
|
|
119
|
+
description
|
|
120
|
+
}
|
|
121
|
+
});
|
|
122
|
+
const validateRedirect = (url, code) => {
|
|
123
|
+
if (!url || c.context.isTrustedOrigin(url, { allowRelativePaths: true })) return;
|
|
124
|
+
c.context.logger.error(`Invalid redirect URL: ${url}`);
|
|
125
|
+
return fail(code, `Untrusted URL: ${url}`);
|
|
126
|
+
};
|
|
127
|
+
const invalidRedirect = validateRedirect(c.query.callbackURL, "invalid_callback_url") ?? validateRedirect(c.query.errorCallbackURL, "invalid_error_callback_url") ?? validateRedirect(c.query.newUserCallbackURL, "invalid_new_user_callback_url");
|
|
128
|
+
if (invalidRedirect) return invalidRedirect;
|
|
129
|
+
const provider = await getAwaitableValue(c.context.socialProviders, { value: c.query.provider });
|
|
130
|
+
if (!provider) return fail("provider_not_found", `Unknown provider: ${c.query.provider}`);
|
|
131
|
+
const callbackURL = c.query.callbackURL || c.context.baseURL;
|
|
132
|
+
let url;
|
|
133
|
+
try {
|
|
134
|
+
const codeVerifier = generateRandomString(128);
|
|
135
|
+
const stateData = {
|
|
136
|
+
...c.query.additionalData ? safeJSONParse(c.query.additionalData) ?? {} : {},
|
|
137
|
+
callbackURL,
|
|
138
|
+
codeVerifier,
|
|
139
|
+
errorURL: c.query.errorCallbackURL,
|
|
140
|
+
newUserURL: c.query.newUserCallbackURL,
|
|
141
|
+
requestSignUp: c.query.requestSignUp === "true" ? true : void 0,
|
|
142
|
+
expiresAt: Date.now() + 600 * 1e3
|
|
143
|
+
};
|
|
144
|
+
await setOAuthState(stateData);
|
|
145
|
+
const { state } = await generateGenericState(c, stateData);
|
|
146
|
+
const marker = c.context.createAuthCookie(POPUP_MARKER_COOKIE, { maxAge: 600 });
|
|
147
|
+
await c.setSignedCookie(marker.name, JSON.stringify({
|
|
148
|
+
popupOrigin,
|
|
149
|
+
popupNonce: c.query.popupNonce ?? ""
|
|
150
|
+
}), c.context.secret, marker.attributes);
|
|
151
|
+
url = await provider.createAuthorizationURL({
|
|
152
|
+
state,
|
|
153
|
+
codeVerifier,
|
|
154
|
+
redirectURI: `${c.context.baseURL}/callback/${provider.id}`,
|
|
155
|
+
scopes: c.query.scopes ? c.query.scopes.split(",") : void 0
|
|
156
|
+
});
|
|
157
|
+
} catch (error) {
|
|
158
|
+
c.context.logger.error("OAuth popup failed to start", error);
|
|
159
|
+
return fail("popup_sign_in_failed", "Failed to start the OAuth flow.");
|
|
160
|
+
}
|
|
161
|
+
throw c.redirect(url.toString());
|
|
162
|
+
});
|
|
163
|
+
/**
|
|
164
|
+
* Server plugin for popup-based OAuth. `signIn.popup` navigates the popup to
|
|
165
|
+
* `/oauth-popup/start`; on the OAuth callback this plugin swaps the redirect for
|
|
166
|
+
* a page that posts the session token (or error) back to the opener. Pair with
|
|
167
|
+
* the `bearer` plugin and `oauthPopupClient`.
|
|
168
|
+
*/
|
|
169
|
+
const oauthPopup = () => {
|
|
170
|
+
return {
|
|
171
|
+
id: "oauth-popup",
|
|
172
|
+
version: PACKAGE_VERSION,
|
|
173
|
+
$ERROR_CODES: OAUTH_POPUP_ERROR_CODES,
|
|
174
|
+
endpoints: { oauthPopupStart },
|
|
175
|
+
hooks: { after: [{
|
|
176
|
+
matcher(context) {
|
|
177
|
+
return !!(context.path?.startsWith("/callback/") || context.path?.startsWith("/oauth2/callback/"));
|
|
178
|
+
},
|
|
179
|
+
handler: createAuthMiddleware(async (c) => {
|
|
180
|
+
const redirectTo = c.context.responseHeaders?.get("location");
|
|
181
|
+
if (!redirectTo) return;
|
|
182
|
+
const cookie = c.context.createAuthCookie(POPUP_MARKER_COOKIE);
|
|
183
|
+
const marker = await c.getSignedCookie(cookie.name, c.context.secret);
|
|
184
|
+
if (!marker) return;
|
|
185
|
+
expireCookie(c, cookie);
|
|
186
|
+
let popupOrigin;
|
|
187
|
+
let popupNonce;
|
|
188
|
+
try {
|
|
189
|
+
const parsed = JSON.parse(marker);
|
|
190
|
+
popupOrigin = parsed.popupOrigin;
|
|
191
|
+
popupNonce = parsed.popupNonce ?? "";
|
|
192
|
+
} catch {
|
|
193
|
+
return;
|
|
194
|
+
}
|
|
195
|
+
const setCookies = c.context.responseHeaders?.getSetCookie?.() ?? splitSetCookieHeader(c.context.responseHeaders?.get("set-cookie") ?? "");
|
|
196
|
+
let token;
|
|
197
|
+
for (const raw of setCookies) {
|
|
198
|
+
const value = parseSetCookieHeader(raw).get(c.context.authCookies.sessionToken.name)?.value;
|
|
199
|
+
if (value !== void 0) {
|
|
200
|
+
token = value;
|
|
201
|
+
break;
|
|
202
|
+
}
|
|
203
|
+
}
|
|
204
|
+
let response;
|
|
205
|
+
if (token) response = renderCompletion(c, popupOrigin, {
|
|
206
|
+
nonce: popupNonce,
|
|
207
|
+
token,
|
|
208
|
+
redirectTo
|
|
209
|
+
});
|
|
210
|
+
else {
|
|
211
|
+
const error = new URL(redirectTo, c.context.baseURL).searchParams.get("error");
|
|
212
|
+
if (!error) return;
|
|
213
|
+
response = renderCompletion(c, popupOrigin, {
|
|
214
|
+
nonce: popupNonce,
|
|
215
|
+
error: {
|
|
216
|
+
code: error,
|
|
217
|
+
description: new URL(redirectTo, c.context.baseURL).searchParams.get("error_description") ?? void 0
|
|
218
|
+
}
|
|
219
|
+
});
|
|
220
|
+
}
|
|
221
|
+
c.context.returned = response;
|
|
222
|
+
})
|
|
223
|
+
}] }
|
|
224
|
+
};
|
|
225
|
+
};
|
|
226
|
+
//#endregion
|
|
227
|
+
export { OAUTH_POPUP_COMPLETE_SCRIPT, OAUTH_POPUP_SCRIPT_CSP_HASH, oauthPopup };
|