better-auth 1.6.16 → 1.6.17

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.
Files changed (84) hide show
  1. package/dist/api/index.d.mts +2 -2
  2. package/dist/api/index.mjs +3 -4
  3. package/dist/api/middlewares/origin-check.mjs +5 -1
  4. package/dist/api/rate-limiter/index.mjs +259 -73
  5. package/dist/api/routes/account.mjs +22 -7
  6. package/dist/api/routes/callback.mjs +2 -2
  7. package/dist/api/routes/index.d.mts +1 -1
  8. package/dist/api/routes/password.mjs +3 -4
  9. package/dist/api/routes/session.d.mts +12 -1
  10. package/dist/api/routes/session.mjs +13 -1
  11. package/dist/api/routes/sign-in.mjs +5 -5
  12. package/dist/api/routes/sign-up.mjs +2 -2
  13. package/dist/api/routes/update-session.mjs +2 -3
  14. package/dist/api/routes/update-user.mjs +10 -12
  15. package/dist/auth/base.mjs +11 -7
  16. package/dist/client/equality.d.mts +19 -0
  17. package/dist/client/equality.mjs +42 -0
  18. package/dist/client/index.d.mts +5 -4
  19. package/dist/client/index.mjs +2 -1
  20. package/dist/client/path-to-object.d.mts +5 -2
  21. package/dist/client/plugins/index.d.mts +4 -1
  22. package/dist/client/plugins/index.mjs +4 -1
  23. package/dist/client/query.d.mts +4 -3
  24. package/dist/client/query.mjs +27 -17
  25. package/dist/client/session-atom.mjs +129 -4
  26. package/dist/client/session-refresh.d.mts +3 -18
  27. package/dist/client/session-refresh.mjs +38 -49
  28. package/dist/client/types.d.mts +2 -2
  29. package/dist/context/create-context.mjs +2 -1
  30. package/dist/context/store-capabilities.mjs +12 -0
  31. package/dist/cookies/index.mjs +25 -2
  32. package/dist/db/internal-adapter.mjs +51 -0
  33. package/dist/package.mjs +1 -1
  34. package/dist/plugins/access/access.mjs +49 -19
  35. package/dist/plugins/admin/routes.mjs +10 -3
  36. package/dist/plugins/captcha/constants.mjs +8 -1
  37. package/dist/plugins/captcha/index.mjs +8 -2
  38. package/dist/plugins/captcha/types.d.mts +21 -0
  39. package/dist/plugins/captcha/verify-handlers/captchafox.mjs +2 -0
  40. package/dist/plugins/captcha/verify-handlers/cloudflare-turnstile.mjs +7 -2
  41. package/dist/plugins/captcha/verify-handlers/google-recaptcha.mjs +7 -2
  42. package/dist/plugins/captcha/verify-handlers/h-captcha.mjs +2 -0
  43. package/dist/plugins/device-authorization/routes.mjs +16 -9
  44. package/dist/plugins/email-otp/routes.mjs +22 -52
  45. package/dist/plugins/generic-oauth/index.mjs +7 -2
  46. package/dist/plugins/generic-oauth/routes.mjs +16 -12
  47. package/dist/plugins/haveibeenpwned/index.d.mts +1 -1
  48. package/dist/plugins/haveibeenpwned/index.mjs +5 -1
  49. package/dist/plugins/index.d.mts +5 -1
  50. package/dist/plugins/index.mjs +4 -1
  51. package/dist/plugins/jwt/index.mjs +2 -2
  52. package/dist/plugins/mcp/client/index.mjs +1 -0
  53. package/dist/plugins/mcp/index.mjs +8 -0
  54. package/dist/plugins/multi-session/index.mjs +7 -5
  55. package/dist/plugins/oauth-popup/client.d.mts +82 -0
  56. package/dist/plugins/oauth-popup/client.mjs +203 -0
  57. package/dist/plugins/oauth-popup/constants.d.mts +11 -0
  58. package/dist/plugins/oauth-popup/constants.mjs +11 -0
  59. package/dist/plugins/oauth-popup/error-codes.d.mts +11 -0
  60. package/dist/plugins/oauth-popup/error-codes.mjs +10 -0
  61. package/dist/plugins/oauth-popup/index.d.mts +67 -0
  62. package/dist/plugins/oauth-popup/index.mjs +227 -0
  63. package/dist/plugins/oauth-popup/types.d.mts +30 -0
  64. package/dist/plugins/oauth-proxy/index.mjs +2 -2
  65. package/dist/plugins/oauth-proxy/utils.mjs +16 -2
  66. package/dist/plugins/oidc-provider/index.mjs +10 -0
  67. package/dist/plugins/one-tap/client.mjs +12 -6
  68. package/dist/plugins/one-tap/index.d.mts +1 -0
  69. package/dist/plugins/one-tap/index.mjs +9 -5
  70. package/dist/plugins/one-time-token/index.mjs +1 -3
  71. package/dist/plugins/open-api/generator.mjs +7 -4
  72. package/dist/plugins/organization/adapter.d.mts +29 -1
  73. package/dist/plugins/organization/adapter.mjs +66 -6
  74. package/dist/plugins/organization/routes/crud-invites.mjs +49 -34
  75. package/dist/plugins/organization/routes/crud-members.mjs +42 -6
  76. package/dist/plugins/organization/routes/crud-team.mjs +36 -3
  77. package/dist/plugins/phone-number/routes.mjs +41 -36
  78. package/dist/plugins/siwe/index.mjs +2 -3
  79. package/dist/plugins/two-factor/backup-codes/index.mjs +1 -1
  80. package/dist/plugins/two-factor/otp/index.mjs +11 -13
  81. package/dist/plugins/two-factor/totp/index.mjs +1 -1
  82. package/dist/plugins/two-factor/verify-two-factor.mjs +6 -2
  83. package/dist/plugins/username/index.mjs +6 -6
  84. package/package.json +9 -9
@@ -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 };
@@ -0,0 +1,30 @@
1
+ import { OAUTH_POPUP_MESSAGE_TYPE } from "./constants.mjs";
2
+
3
+ //#region src/plugins/oauth-popup/types.d.ts
4
+ /** OAuth error relayed to the opener when the flow fails. */
5
+ interface OAuthPopupError {
6
+ code: string;
7
+ description?: string;
8
+ }
9
+ /**
10
+ * Message the completion page posts to its opener — success carries the token,
11
+ * failure carries the error.
12
+ */
13
+ interface OAuthPopupMessage {
14
+ type: typeof OAUTH_POPUP_MESSAGE_TYPE;
15
+ /** Echoes the request nonce so the opener can correlate the handoff. */
16
+ nonce: string;
17
+ /** The session token, sent as `Authorization: Bearer <token>` (on success). */
18
+ token?: string;
19
+ /** Where the flow would have redirected (callbackURL / newUserURL). */
20
+ redirectTo?: string;
21
+ /** The OAuth error (on failure). */
22
+ error?: OAuthPopupError;
23
+ }
24
+ /** Payload embedded in the completion page's data block. */
25
+ interface OAuthPopupData extends OAuthPopupMessage {
26
+ /** Exact origin the message is posted to (the trusted popup opener). */
27
+ targetOrigin: string;
28
+ }
29
+ //#endregion
30
+ export { OAuthPopupData, OAuthPopupMessage };
@@ -183,13 +183,13 @@ const oAuthProxy = (opts) => {
183
183
  }
184
184
  if (error) throw redirectOnError(ctx, errorURL, error);
185
185
  if (!code) {
186
- ctx.context.logger.error("OAuth callback missing authorization code");
186
+ ctx.context.logger.warn("OAuth callback missing authorization code");
187
187
  throw redirectOnError(ctx, errorURL, "no_code");
188
188
  }
189
189
  const providerId = ctx.params?.id;
190
190
  const provider = ctx.context.socialProviders.find((p) => p.id === providerId);
191
191
  if (!provider) {
192
- ctx.context.logger.error("OAuth provider not found", providerId);
192
+ ctx.context.logger.warn("OAuth provider not found", { providerId });
193
193
  throw redirectOnError(ctx, errorURL, "oauth_provider_not_found");
194
194
  }
195
195
  let tokens;
@@ -21,10 +21,24 @@ function getVendorBaseURL() {
21
21
  return vercel || netlify || render || aws || google || azure;
22
22
  }
23
23
  /**
24
- * Resolve the current URL from various sources
24
+ * Resolve the current URL from various sources.
25
+ *
26
+ * The request URL host can come from an untrusted source (`Host` / forwarded host),
27
+ * and this origin becomes the receiver for the encrypted OAuth profile replay.
28
+ * So a request-derived origin is only honored when it is an explicitly trusted
29
+ * origin; otherwise resolution falls back to the configured platform/base URL,
30
+ * never the raw request host. An explicit `opts.currentURL` and the vendor/base
31
+ * URLs are configured by the developer and trusted as-is.
25
32
  */
26
33
  function resolveCurrentURL(ctx, opts) {
27
- return new URL(opts?.currentURL || ctx.request?.url || getVendorBaseURL() || ctx.context.baseURL);
34
+ if (opts?.currentURL) return new URL(opts.currentURL);
35
+ const requestURL = ctx.request?.url;
36
+ if (requestURL) {
37
+ const origin = getOrigin(requestURL);
38
+ if (origin && ctx.context.isTrustedOrigin(origin)) return new URL(requestURL);
39
+ }
40
+ const vendorBaseURL = getVendorBaseURL();
41
+ return new URL(vendorBaseURL && getOrigin(vendorBaseURL) ? vendorBaseURL : ctx.context.baseURL);
28
42
  }
29
43
  /**
30
44
  * Check if the proxy should be skipped for this request
@@ -1082,6 +1082,16 @@ const oidcProvider = (options) => {
1082
1082
  });
1083
1083
  }
1084
1084
  const session = await getSessionFromCtx(ctx);
1085
+ if (ctx.request && (validatedUserId || session)) {
1086
+ const fetchSite = ctx.request.headers.get("Sec-Fetch-Site");
1087
+ const originHeader = ctx.request.headers.get("origin") || ctx.request.headers.get("referer");
1088
+ const isSameSiteRequest = fetchSite === "same-origin" || fetchSite === "same-site" || fetchSite === "none" || !!originHeader && ctx.context.isTrustedOrigin(originHeader, { allowRelativePaths: false });
1089
+ const hintMatchesSession = !!validatedUserId && validatedUserId === session?.user.id;
1090
+ if (!isSameSiteRequest && !hintMatchesSession) throw new APIError("FORBIDDEN", {
1091
+ error: "invalid_request",
1092
+ error_description: "Logout must be same-site or carry an id_token_hint for the current session"
1093
+ });
1094
+ }
1085
1095
  if (validatedUserId || session) {
1086
1096
  const userId = validatedUserId || session?.user.id;
1087
1097
  if (userId) await ctx.context.adapter.deleteMany({
@@ -44,12 +44,15 @@ const oneTapClient = (options) => {
44
44
  return;
45
45
  }
46
46
  async function callback(idToken) {
47
- await $fetch("/one-tap/callback", {
47
+ if ((await $fetch("/one-tap/callback", {
48
48
  method: "POST",
49
- body: { idToken },
49
+ body: {
50
+ idToken,
51
+ callbackURL: opts?.callbackURL
52
+ },
50
53
  ...opts?.fetchOptions,
51
54
  ...fetchOptions
52
- });
55
+ }))?.error) return;
53
56
  if (!opts?.fetchOptions && !fetchOptions || opts?.callbackURL) {
54
57
  const target = opts?.callbackURL ?? "/";
55
58
  if (isSafeUrlScheme(target)) window.location.href = target;
@@ -80,12 +83,15 @@ const oneTapClient = (options) => {
80
83
  return;
81
84
  }
82
85
  async function callback(idToken) {
83
- await $fetch("/one-tap/callback", {
86
+ if ((await $fetch("/one-tap/callback", {
84
87
  method: "POST",
85
- body: { idToken },
88
+ body: {
89
+ idToken,
90
+ callbackURL: opts?.callbackURL
91
+ },
86
92
  ...opts?.fetchOptions,
87
93
  ...fetchOptions
88
- });
94
+ }))?.error) return;
89
95
  if (!opts?.fetchOptions && !fetchOptions || opts?.callbackURL) {
90
96
  const target = opts?.callbackURL ?? "/";
91
97
  if (isSafeUrlScheme(target)) window.location.href = target;