@better-auth/core 1.3.28 → 1.4.0-beta.11

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 (157) hide show
  1. package/.turbo/turbo-build.log +81 -76
  2. package/dist/api/index.cjs +30 -0
  3. package/dist/api/index.d.cts +7 -0
  4. package/dist/api/index.d.ts +7 -0
  5. package/dist/api/index.js +26 -0
  6. package/dist/async_hooks/index.cjs +2 -26
  7. package/dist/async_hooks/index.d.cts +4 -7
  8. package/dist/async_hooks/index.d.ts +4 -7
  9. package/dist/async_hooks/index.js +3 -0
  10. package/dist/async_hooks-CaXONcb9.cjs +24 -0
  11. package/dist/async_hooks-CkLtIFa6.js +18 -0
  12. package/dist/chunk-CUT6urMc.cjs +30 -0
  13. package/dist/context/index.cjs +8 -0
  14. package/dist/context/index.d.cts +17 -0
  15. package/dist/context/index.d.ts +17 -0
  16. package/dist/context/index.js +4 -0
  17. package/dist/context-DAu9ytQl.cjs +85 -0
  18. package/dist/context-rhoB1CVr.js +56 -0
  19. package/dist/db/adapter/index.cjs +0 -2
  20. package/dist/db/adapter/index.d.cts +7 -14
  21. package/dist/db/adapter/index.d.ts +7 -14
  22. package/dist/db/adapter/index.js +1 -0
  23. package/dist/db/index.cjs +46 -69
  24. package/dist/db/index.d.cts +3 -39
  25. package/dist/db/index.d.ts +3 -39
  26. package/dist/db/index.js +61 -0
  27. package/dist/env/index.cjs +15 -311
  28. package/dist/env/index.d.cts +2 -36
  29. package/dist/env/index.d.ts +2 -36
  30. package/dist/env/index.js +3 -0
  31. package/dist/env-D1OdwBk9.cjs +330 -0
  32. package/dist/env-DzHiLVFb.js +246 -0
  33. package/dist/error/index.cjs +4 -43
  34. package/dist/error/index.d.cts +31 -29
  35. package/dist/error/index.d.ts +31 -29
  36. package/dist/error/index.js +4 -0
  37. package/dist/error-H6U5qGNP.js +45 -0
  38. package/dist/error-k9KM7GAb.cjs +56 -0
  39. package/dist/helper-DVgWo5zB.d.cts +6 -0
  40. package/dist/helper-Dm5AtPVS.d.ts +6 -0
  41. package/dist/index-BPsqLi68.d.ts +4410 -0
  42. package/dist/index-CgIE2ECh.d.cts +262 -0
  43. package/dist/index-ChlC5Vy3.d.ts +344 -0
  44. package/dist/index-Cmh0x42V.d.ts +262 -0
  45. package/dist/index-CsHMVqbX.d.ts +82 -0
  46. package/dist/index-DFwrZMma.d.cts +82 -0
  47. package/dist/index-OrTFOyAG.d.ts +1871 -0
  48. package/dist/index-_Oiv-VVr.d.cts +344 -0
  49. package/dist/index-ehsSkBxt.d.cts +4410 -0
  50. package/dist/index-yBChAjSS.d.cts +1871 -0
  51. package/dist/index.cjs +0 -2
  52. package/dist/index.d.cts +7 -180
  53. package/dist/index.d.ts +7 -180
  54. package/dist/index.js +1 -0
  55. package/dist/oauth2/index.cjs +12 -368
  56. package/dist/oauth2/index.d.cts +3 -100
  57. package/dist/oauth2/index.d.ts +3 -100
  58. package/dist/oauth2/index.js +3 -0
  59. package/dist/oauth2-D_FUKfC8.js +239 -0
  60. package/dist/oauth2-DqX_pPhp.cjs +302 -0
  61. package/dist/social-providers/index.cjs +2262 -2645
  62. package/dist/social-providers/index.d.cts +4 -3903
  63. package/dist/social-providers/index.d.ts +4 -3903
  64. package/dist/social-providers/index.js +2370 -0
  65. package/dist/utils/index.cjs +2 -6
  66. package/dist/utils/index.d.cts +4 -5
  67. package/dist/utils/index.d.ts +4 -5
  68. package/dist/utils/index.js +3 -0
  69. package/dist/utils-Bs-jKit4.cjs +13 -0
  70. package/dist/utils-Sn10glI8.js +7 -0
  71. package/package.json +35 -25
  72. package/src/api/index.ts +57 -0
  73. package/src/async_hooks/index.ts +1 -9
  74. package/src/context/endpoint-context.ts +32 -0
  75. package/src/context/index.ts +6 -0
  76. package/src/context/transaction.ts +63 -0
  77. package/src/db/index.ts +2 -0
  78. package/src/db/type.ts +1 -10
  79. package/src/env/color-depth.ts +4 -5
  80. package/src/error/codes.ts +1 -1
  81. package/src/social-providers/apple.ts +2 -2
  82. package/src/social-providers/atlassian.ts +4 -7
  83. package/src/social-providers/cognito.ts +4 -7
  84. package/src/social-providers/discord.ts +2 -5
  85. package/src/social-providers/dropbox.ts +2 -2
  86. package/src/social-providers/facebook.ts +3 -6
  87. package/src/social-providers/figma.ts +4 -7
  88. package/src/social-providers/github.ts +2 -2
  89. package/src/social-providers/gitlab.ts +2 -2
  90. package/src/social-providers/google.ts +4 -7
  91. package/src/social-providers/huggingface.ts +2 -2
  92. package/src/social-providers/kakao.ts +2 -2
  93. package/src/social-providers/kick.ts +2 -5
  94. package/src/social-providers/line.ts +2 -2
  95. package/src/social-providers/linear.ts +2 -2
  96. package/src/social-providers/linkedin.ts +2 -2
  97. package/src/social-providers/microsoft-entra-id.ts +3 -3
  98. package/src/social-providers/naver.ts +2 -2
  99. package/src/social-providers/notion.ts +2 -2
  100. package/src/social-providers/paypal.ts +3 -3
  101. package/src/social-providers/reddit.ts +2 -2
  102. package/src/social-providers/roblox.ts +2 -5
  103. package/src/social-providers/salesforce.ts +4 -7
  104. package/src/social-providers/slack.ts +2 -5
  105. package/src/social-providers/spotify.ts +2 -2
  106. package/src/social-providers/tiktok.ts +2 -5
  107. package/src/social-providers/twitch.ts +3 -3
  108. package/src/social-providers/twitter.ts +2 -2
  109. package/src/social-providers/vk.ts +2 -5
  110. package/src/social-providers/zoom.ts +2 -5
  111. package/src/types/context.ts +20 -99
  112. package/src/types/index.ts +1 -1
  113. package/src/types/init-options.ts +20 -22
  114. package/src/types/plugin.ts +3 -3
  115. package/{build.config.ts → tsdown.config.ts} +8 -11
  116. package/dist/async_hooks/index.d.mts +0 -10
  117. package/dist/async_hooks/index.mjs +0 -25
  118. package/dist/db/adapter/index.d.mts +0 -14
  119. package/dist/db/adapter/index.mjs +0 -1
  120. package/dist/db/index.d.mts +0 -39
  121. package/dist/db/index.mjs +0 -70
  122. package/dist/env/index.d.mts +0 -36
  123. package/dist/env/index.mjs +0 -297
  124. package/dist/error/index.d.mts +0 -33
  125. package/dist/error/index.mjs +0 -41
  126. package/dist/index.d.mts +0 -180
  127. package/dist/index.mjs +0 -1
  128. package/dist/middleware/index.cjs +0 -25
  129. package/dist/middleware/index.d.cts +0 -14
  130. package/dist/middleware/index.d.mts +0 -14
  131. package/dist/middleware/index.d.ts +0 -14
  132. package/dist/middleware/index.mjs +0 -21
  133. package/dist/oauth2/index.d.mts +0 -100
  134. package/dist/oauth2/index.mjs +0 -357
  135. package/dist/shared/core.BJPBStdk.d.ts +0 -1693
  136. package/dist/shared/core.Bl6TpxyD.d.mts +0 -181
  137. package/dist/shared/core.Bqe5IGAi.d.ts +0 -13
  138. package/dist/shared/core.BwoNUcJQ.d.cts +0 -53
  139. package/dist/shared/core.BwoNUcJQ.d.mts +0 -53
  140. package/dist/shared/core.BwoNUcJQ.d.ts +0 -53
  141. package/dist/shared/core.CajxAutx.d.cts +0 -143
  142. package/dist/shared/core.CajxAutx.d.mts +0 -143
  143. package/dist/shared/core.CajxAutx.d.ts +0 -143
  144. package/dist/shared/core.CkkLHQWc.d.mts +0 -1693
  145. package/dist/shared/core.DkdZ1o38.d.ts +0 -181
  146. package/dist/shared/core.Dl-70uns.d.cts +0 -84
  147. package/dist/shared/core.Dl-70uns.d.mts +0 -84
  148. package/dist/shared/core.Dl-70uns.d.ts +0 -84
  149. package/dist/shared/core.DyEdx0m7.d.cts +0 -181
  150. package/dist/shared/core.E9DfzGLz.d.mts +0 -13
  151. package/dist/shared/core.HqYn20Fi.d.cts +0 -13
  152. package/dist/shared/core.gYIBmdi1.d.cts +0 -1693
  153. package/dist/social-providers/index.d.mts +0 -3903
  154. package/dist/social-providers/index.mjs +0 -2743
  155. package/dist/utils/index.d.mts +0 -10
  156. package/dist/utils/index.mjs +0 -5
  157. package/src/middleware/index.ts +0 -33
@@ -0,0 +1,239 @@
1
+ import { base64, base64Url } from "@better-auth/utils/base64";
2
+ import { betterFetch } from "@better-fetch/fetch";
3
+ import { jwtVerify } from "jose";
4
+
5
+ //#region src/oauth2/utils.ts
6
+ function getOAuth2Tokens(data) {
7
+ const getDate = (seconds) => {
8
+ const now = /* @__PURE__ */ new Date();
9
+ return new Date(now.getTime() + seconds * 1e3);
10
+ };
11
+ return {
12
+ tokenType: data.token_type,
13
+ accessToken: data.access_token,
14
+ refreshToken: data.refresh_token,
15
+ accessTokenExpiresAt: data.expires_in ? getDate(data.expires_in) : void 0,
16
+ refreshTokenExpiresAt: data.refresh_token_expires_in ? getDate(data.refresh_token_expires_in) : void 0,
17
+ scopes: data?.scope ? typeof data.scope === "string" ? data.scope.split(" ") : data.scope : [],
18
+ idToken: data.id_token
19
+ };
20
+ }
21
+ async function generateCodeChallenge(codeVerifier) {
22
+ const data = new TextEncoder().encode(codeVerifier);
23
+ const hash = await crypto.subtle.digest("SHA-256", data);
24
+ return base64Url.encode(new Uint8Array(hash), { padding: false });
25
+ }
26
+
27
+ //#endregion
28
+ //#region src/oauth2/create-authorization-url.ts
29
+ async function createAuthorizationURL({ id, options, authorizationEndpoint, state, codeVerifier, scopes, claims, redirectURI, duration, prompt, accessType, responseType, display, loginHint, hd, responseMode, additionalParams, scopeJoiner }) {
30
+ const url = new URL(authorizationEndpoint);
31
+ url.searchParams.set("response_type", responseType || "code");
32
+ const primaryClientId = Array.isArray(options.clientId) ? options.clientId[0] : options.clientId;
33
+ url.searchParams.set("client_id", primaryClientId);
34
+ url.searchParams.set("state", state);
35
+ url.searchParams.set("scope", scopes.join(scopeJoiner || " "));
36
+ url.searchParams.set("redirect_uri", options.redirectURI || redirectURI);
37
+ duration && url.searchParams.set("duration", duration);
38
+ display && url.searchParams.set("display", display);
39
+ loginHint && url.searchParams.set("login_hint", loginHint);
40
+ prompt && url.searchParams.set("prompt", prompt);
41
+ hd && url.searchParams.set("hd", hd);
42
+ accessType && url.searchParams.set("access_type", accessType);
43
+ responseMode && url.searchParams.set("response_mode", responseMode);
44
+ if (codeVerifier) {
45
+ const codeChallenge = await generateCodeChallenge(codeVerifier);
46
+ url.searchParams.set("code_challenge_method", "S256");
47
+ url.searchParams.set("code_challenge", codeChallenge);
48
+ }
49
+ if (claims) {
50
+ const claimsObj = claims.reduce((acc, claim) => {
51
+ acc[claim] = null;
52
+ return acc;
53
+ }, {});
54
+ url.searchParams.set("claims", JSON.stringify({ id_token: {
55
+ email: null,
56
+ email_verified: null,
57
+ ...claimsObj
58
+ } }));
59
+ }
60
+ if (additionalParams) Object.entries(additionalParams).forEach(([key, value]) => {
61
+ url.searchParams.set(key, value);
62
+ });
63
+ return url;
64
+ }
65
+
66
+ //#endregion
67
+ //#region src/oauth2/validate-authorization-code.ts
68
+ function createAuthorizationCodeRequest({ code, codeVerifier, redirectURI, options, authentication, deviceId, headers, additionalParams = {}, resource }) {
69
+ const body = new URLSearchParams();
70
+ const requestHeaders = {
71
+ "content-type": "application/x-www-form-urlencoded",
72
+ accept: "application/json",
73
+ "user-agent": "better-auth",
74
+ ...headers
75
+ };
76
+ body.set("grant_type", "authorization_code");
77
+ body.set("code", code);
78
+ codeVerifier && body.set("code_verifier", codeVerifier);
79
+ options.clientKey && body.set("client_key", options.clientKey);
80
+ deviceId && body.set("device_id", deviceId);
81
+ body.set("redirect_uri", options.redirectURI || redirectURI);
82
+ if (resource) if (typeof resource === "string") body.append("resource", resource);
83
+ else for (const _resource of resource) body.append("resource", _resource);
84
+ if (authentication === "basic") {
85
+ const primaryClientId = Array.isArray(options.clientId) ? options.clientId[0] : options.clientId;
86
+ requestHeaders["authorization"] = `Basic ${base64.encode(`${primaryClientId}:${options.clientSecret ?? ""}`)}`;
87
+ } else {
88
+ const primaryClientId = Array.isArray(options.clientId) ? options.clientId[0] : options.clientId;
89
+ body.set("client_id", primaryClientId);
90
+ if (options.clientSecret) body.set("client_secret", options.clientSecret);
91
+ }
92
+ for (const [key, value] of Object.entries(additionalParams)) if (!body.has(key)) body.append(key, value);
93
+ return {
94
+ body,
95
+ headers: requestHeaders
96
+ };
97
+ }
98
+ async function validateAuthorizationCode({ code, codeVerifier, redirectURI, options, tokenEndpoint, authentication, deviceId, headers, additionalParams = {}, resource }) {
99
+ const { body, headers: requestHeaders } = createAuthorizationCodeRequest({
100
+ code,
101
+ codeVerifier,
102
+ redirectURI,
103
+ options,
104
+ authentication,
105
+ deviceId,
106
+ headers,
107
+ additionalParams,
108
+ resource
109
+ });
110
+ const { data, error } = await betterFetch(tokenEndpoint, {
111
+ method: "POST",
112
+ body,
113
+ headers: requestHeaders
114
+ });
115
+ if (error) throw error;
116
+ return getOAuth2Tokens(data);
117
+ }
118
+ async function validateToken(token, jwksEndpoint) {
119
+ const { data, error } = await betterFetch(jwksEndpoint, {
120
+ method: "GET",
121
+ headers: {
122
+ accept: "application/json",
123
+ "user-agent": "better-auth"
124
+ }
125
+ });
126
+ if (error) throw error;
127
+ const keys = data["keys"];
128
+ const header = JSON.parse(atob(token.split(".")[0]));
129
+ const key = keys.find((key$1) => key$1.kid === header.kid);
130
+ if (!key) throw new Error("Key not found");
131
+ return await jwtVerify(token, key);
132
+ }
133
+
134
+ //#endregion
135
+ //#region src/oauth2/refresh-access-token.ts
136
+ function createRefreshAccessTokenRequest({ refreshToken, options, authentication, extraParams, resource }) {
137
+ const body = new URLSearchParams();
138
+ const headers = {
139
+ "content-type": "application/x-www-form-urlencoded",
140
+ accept: "application/json"
141
+ };
142
+ body.set("grant_type", "refresh_token");
143
+ body.set("refresh_token", refreshToken);
144
+ if (authentication === "basic") {
145
+ const primaryClientId = Array.isArray(options.clientId) ? options.clientId[0] : options.clientId;
146
+ if (primaryClientId) headers["authorization"] = "Basic " + base64.encode(`${primaryClientId}:${options.clientSecret ?? ""}`);
147
+ else headers["authorization"] = "Basic " + base64.encode(`:${options.clientSecret ?? ""}`);
148
+ } else {
149
+ const primaryClientId = Array.isArray(options.clientId) ? options.clientId[0] : options.clientId;
150
+ body.set("client_id", primaryClientId);
151
+ if (options.clientSecret) body.set("client_secret", options.clientSecret);
152
+ }
153
+ if (resource) if (typeof resource === "string") body.append("resource", resource);
154
+ else for (const _resource of resource) body.append("resource", _resource);
155
+ if (extraParams) for (const [key, value] of Object.entries(extraParams)) body.set(key, value);
156
+ return {
157
+ body,
158
+ headers
159
+ };
160
+ }
161
+ async function refreshAccessToken({ refreshToken, options, tokenEndpoint, authentication, extraParams }) {
162
+ const { body, headers } = createRefreshAccessTokenRequest({
163
+ refreshToken,
164
+ options,
165
+ authentication,
166
+ extraParams
167
+ });
168
+ const { data, error } = await betterFetch(tokenEndpoint, {
169
+ method: "POST",
170
+ body,
171
+ headers
172
+ });
173
+ if (error) throw error;
174
+ const tokens = {
175
+ accessToken: data.access_token,
176
+ refreshToken: data.refresh_token,
177
+ tokenType: data.token_type,
178
+ scopes: data.scope?.split(" "),
179
+ idToken: data.id_token
180
+ };
181
+ if (data.expires_in) {
182
+ const now = /* @__PURE__ */ new Date();
183
+ tokens.accessTokenExpiresAt = new Date(now.getTime() + data.expires_in * 1e3);
184
+ }
185
+ return tokens;
186
+ }
187
+
188
+ //#endregion
189
+ //#region src/oauth2/client-credentials-token.ts
190
+ function createClientCredentialsTokenRequest({ options, scope, authentication, resource }) {
191
+ const body = new URLSearchParams();
192
+ const headers = {
193
+ "content-type": "application/x-www-form-urlencoded",
194
+ accept: "application/json"
195
+ };
196
+ body.set("grant_type", "client_credentials");
197
+ scope && body.set("scope", scope);
198
+ if (resource) if (typeof resource === "string") body.append("resource", resource);
199
+ else for (const _resource of resource) body.append("resource", _resource);
200
+ if (authentication === "basic") {
201
+ const primaryClientId = Array.isArray(options.clientId) ? options.clientId[0] : options.clientId;
202
+ headers["authorization"] = `Basic ${base64Url.encode(`${primaryClientId}:${options.clientSecret}`)}`;
203
+ } else {
204
+ const primaryClientId = Array.isArray(options.clientId) ? options.clientId[0] : options.clientId;
205
+ body.set("client_id", primaryClientId);
206
+ body.set("client_secret", options.clientSecret);
207
+ }
208
+ return {
209
+ body,
210
+ headers
211
+ };
212
+ }
213
+ async function clientCredentialsToken({ options, tokenEndpoint, scope, authentication, resource }) {
214
+ const { body, headers } = createClientCredentialsTokenRequest({
215
+ options,
216
+ scope,
217
+ authentication,
218
+ resource
219
+ });
220
+ const { data, error } = await betterFetch(tokenEndpoint, {
221
+ method: "POST",
222
+ body,
223
+ headers
224
+ });
225
+ if (error) throw error;
226
+ const tokens = {
227
+ accessToken: data.access_token,
228
+ tokenType: data.token_type,
229
+ scopes: data.scope?.split(" ")
230
+ };
231
+ if (data.expires_in) {
232
+ const now = /* @__PURE__ */ new Date();
233
+ tokens.accessTokenExpiresAt = new Date(now.getTime() + data.expires_in * 1e3);
234
+ }
235
+ return tokens;
236
+ }
237
+
238
+ //#endregion
239
+ export { clientCredentialsToken, createAuthorizationCodeRequest, createAuthorizationURL, createClientCredentialsTokenRequest, createRefreshAccessTokenRequest, generateCodeChallenge, getOAuth2Tokens, refreshAccessToken, validateAuthorizationCode, validateToken };
@@ -0,0 +1,302 @@
1
+ const require_chunk = require('./chunk-CUT6urMc.cjs');
2
+ let __better_auth_utils_base64 = require("@better-auth/utils/base64");
3
+ __better_auth_utils_base64 = require_chunk.__toESM(__better_auth_utils_base64);
4
+ let __better_fetch_fetch = require("@better-fetch/fetch");
5
+ __better_fetch_fetch = require_chunk.__toESM(__better_fetch_fetch);
6
+ let jose = require("jose");
7
+ jose = require_chunk.__toESM(jose);
8
+
9
+ //#region src/oauth2/utils.ts
10
+ function getOAuth2Tokens(data) {
11
+ const getDate = (seconds) => {
12
+ const now = /* @__PURE__ */ new Date();
13
+ return new Date(now.getTime() + seconds * 1e3);
14
+ };
15
+ return {
16
+ tokenType: data.token_type,
17
+ accessToken: data.access_token,
18
+ refreshToken: data.refresh_token,
19
+ accessTokenExpiresAt: data.expires_in ? getDate(data.expires_in) : void 0,
20
+ refreshTokenExpiresAt: data.refresh_token_expires_in ? getDate(data.refresh_token_expires_in) : void 0,
21
+ scopes: data?.scope ? typeof data.scope === "string" ? data.scope.split(" ") : data.scope : [],
22
+ idToken: data.id_token
23
+ };
24
+ }
25
+ async function generateCodeChallenge(codeVerifier) {
26
+ const data = new TextEncoder().encode(codeVerifier);
27
+ const hash = await crypto.subtle.digest("SHA-256", data);
28
+ return __better_auth_utils_base64.base64Url.encode(new Uint8Array(hash), { padding: false });
29
+ }
30
+
31
+ //#endregion
32
+ //#region src/oauth2/create-authorization-url.ts
33
+ async function createAuthorizationURL({ id, options, authorizationEndpoint, state, codeVerifier, scopes, claims, redirectURI, duration, prompt, accessType, responseType, display, loginHint, hd, responseMode, additionalParams, scopeJoiner }) {
34
+ const url = new URL(authorizationEndpoint);
35
+ url.searchParams.set("response_type", responseType || "code");
36
+ const primaryClientId = Array.isArray(options.clientId) ? options.clientId[0] : options.clientId;
37
+ url.searchParams.set("client_id", primaryClientId);
38
+ url.searchParams.set("state", state);
39
+ url.searchParams.set("scope", scopes.join(scopeJoiner || " "));
40
+ url.searchParams.set("redirect_uri", options.redirectURI || redirectURI);
41
+ duration && url.searchParams.set("duration", duration);
42
+ display && url.searchParams.set("display", display);
43
+ loginHint && url.searchParams.set("login_hint", loginHint);
44
+ prompt && url.searchParams.set("prompt", prompt);
45
+ hd && url.searchParams.set("hd", hd);
46
+ accessType && url.searchParams.set("access_type", accessType);
47
+ responseMode && url.searchParams.set("response_mode", responseMode);
48
+ if (codeVerifier) {
49
+ const codeChallenge = await generateCodeChallenge(codeVerifier);
50
+ url.searchParams.set("code_challenge_method", "S256");
51
+ url.searchParams.set("code_challenge", codeChallenge);
52
+ }
53
+ if (claims) {
54
+ const claimsObj = claims.reduce((acc, claim) => {
55
+ acc[claim] = null;
56
+ return acc;
57
+ }, {});
58
+ url.searchParams.set("claims", JSON.stringify({ id_token: {
59
+ email: null,
60
+ email_verified: null,
61
+ ...claimsObj
62
+ } }));
63
+ }
64
+ if (additionalParams) Object.entries(additionalParams).forEach(([key, value]) => {
65
+ url.searchParams.set(key, value);
66
+ });
67
+ return url;
68
+ }
69
+
70
+ //#endregion
71
+ //#region src/oauth2/validate-authorization-code.ts
72
+ function createAuthorizationCodeRequest({ code, codeVerifier, redirectURI, options, authentication, deviceId, headers, additionalParams = {}, resource }) {
73
+ const body = new URLSearchParams();
74
+ const requestHeaders = {
75
+ "content-type": "application/x-www-form-urlencoded",
76
+ accept: "application/json",
77
+ "user-agent": "better-auth",
78
+ ...headers
79
+ };
80
+ body.set("grant_type", "authorization_code");
81
+ body.set("code", code);
82
+ codeVerifier && body.set("code_verifier", codeVerifier);
83
+ options.clientKey && body.set("client_key", options.clientKey);
84
+ deviceId && body.set("device_id", deviceId);
85
+ body.set("redirect_uri", options.redirectURI || redirectURI);
86
+ if (resource) if (typeof resource === "string") body.append("resource", resource);
87
+ else for (const _resource of resource) body.append("resource", _resource);
88
+ if (authentication === "basic") {
89
+ const primaryClientId = Array.isArray(options.clientId) ? options.clientId[0] : options.clientId;
90
+ requestHeaders["authorization"] = `Basic ${__better_auth_utils_base64.base64.encode(`${primaryClientId}:${options.clientSecret ?? ""}`)}`;
91
+ } else {
92
+ const primaryClientId = Array.isArray(options.clientId) ? options.clientId[0] : options.clientId;
93
+ body.set("client_id", primaryClientId);
94
+ if (options.clientSecret) body.set("client_secret", options.clientSecret);
95
+ }
96
+ for (const [key, value] of Object.entries(additionalParams)) if (!body.has(key)) body.append(key, value);
97
+ return {
98
+ body,
99
+ headers: requestHeaders
100
+ };
101
+ }
102
+ async function validateAuthorizationCode({ code, codeVerifier, redirectURI, options, tokenEndpoint, authentication, deviceId, headers, additionalParams = {}, resource }) {
103
+ const { body, headers: requestHeaders } = createAuthorizationCodeRequest({
104
+ code,
105
+ codeVerifier,
106
+ redirectURI,
107
+ options,
108
+ authentication,
109
+ deviceId,
110
+ headers,
111
+ additionalParams,
112
+ resource
113
+ });
114
+ const { data, error } = await (0, __better_fetch_fetch.betterFetch)(tokenEndpoint, {
115
+ method: "POST",
116
+ body,
117
+ headers: requestHeaders
118
+ });
119
+ if (error) throw error;
120
+ return getOAuth2Tokens(data);
121
+ }
122
+ async function validateToken(token, jwksEndpoint) {
123
+ const { data, error } = await (0, __better_fetch_fetch.betterFetch)(jwksEndpoint, {
124
+ method: "GET",
125
+ headers: {
126
+ accept: "application/json",
127
+ "user-agent": "better-auth"
128
+ }
129
+ });
130
+ if (error) throw error;
131
+ const keys = data["keys"];
132
+ const header = JSON.parse(atob(token.split(".")[0]));
133
+ const key = keys.find((key$1) => key$1.kid === header.kid);
134
+ if (!key) throw new Error("Key not found");
135
+ return await (0, jose.jwtVerify)(token, key);
136
+ }
137
+
138
+ //#endregion
139
+ //#region src/oauth2/refresh-access-token.ts
140
+ function createRefreshAccessTokenRequest({ refreshToken, options, authentication, extraParams, resource }) {
141
+ const body = new URLSearchParams();
142
+ const headers = {
143
+ "content-type": "application/x-www-form-urlencoded",
144
+ accept: "application/json"
145
+ };
146
+ body.set("grant_type", "refresh_token");
147
+ body.set("refresh_token", refreshToken);
148
+ if (authentication === "basic") {
149
+ const primaryClientId = Array.isArray(options.clientId) ? options.clientId[0] : options.clientId;
150
+ if (primaryClientId) headers["authorization"] = "Basic " + __better_auth_utils_base64.base64.encode(`${primaryClientId}:${options.clientSecret ?? ""}`);
151
+ else headers["authorization"] = "Basic " + __better_auth_utils_base64.base64.encode(`:${options.clientSecret ?? ""}`);
152
+ } else {
153
+ const primaryClientId = Array.isArray(options.clientId) ? options.clientId[0] : options.clientId;
154
+ body.set("client_id", primaryClientId);
155
+ if (options.clientSecret) body.set("client_secret", options.clientSecret);
156
+ }
157
+ if (resource) if (typeof resource === "string") body.append("resource", resource);
158
+ else for (const _resource of resource) body.append("resource", _resource);
159
+ if (extraParams) for (const [key, value] of Object.entries(extraParams)) body.set(key, value);
160
+ return {
161
+ body,
162
+ headers
163
+ };
164
+ }
165
+ async function refreshAccessToken({ refreshToken, options, tokenEndpoint, authentication, extraParams }) {
166
+ const { body, headers } = createRefreshAccessTokenRequest({
167
+ refreshToken,
168
+ options,
169
+ authentication,
170
+ extraParams
171
+ });
172
+ const { data, error } = await (0, __better_fetch_fetch.betterFetch)(tokenEndpoint, {
173
+ method: "POST",
174
+ body,
175
+ headers
176
+ });
177
+ if (error) throw error;
178
+ const tokens = {
179
+ accessToken: data.access_token,
180
+ refreshToken: data.refresh_token,
181
+ tokenType: data.token_type,
182
+ scopes: data.scope?.split(" "),
183
+ idToken: data.id_token
184
+ };
185
+ if (data.expires_in) {
186
+ const now = /* @__PURE__ */ new Date();
187
+ tokens.accessTokenExpiresAt = new Date(now.getTime() + data.expires_in * 1e3);
188
+ }
189
+ return tokens;
190
+ }
191
+
192
+ //#endregion
193
+ //#region src/oauth2/client-credentials-token.ts
194
+ function createClientCredentialsTokenRequest({ options, scope, authentication, resource }) {
195
+ const body = new URLSearchParams();
196
+ const headers = {
197
+ "content-type": "application/x-www-form-urlencoded",
198
+ accept: "application/json"
199
+ };
200
+ body.set("grant_type", "client_credentials");
201
+ scope && body.set("scope", scope);
202
+ if (resource) if (typeof resource === "string") body.append("resource", resource);
203
+ else for (const _resource of resource) body.append("resource", _resource);
204
+ if (authentication === "basic") {
205
+ const primaryClientId = Array.isArray(options.clientId) ? options.clientId[0] : options.clientId;
206
+ headers["authorization"] = `Basic ${__better_auth_utils_base64.base64Url.encode(`${primaryClientId}:${options.clientSecret}`)}`;
207
+ } else {
208
+ const primaryClientId = Array.isArray(options.clientId) ? options.clientId[0] : options.clientId;
209
+ body.set("client_id", primaryClientId);
210
+ body.set("client_secret", options.clientSecret);
211
+ }
212
+ return {
213
+ body,
214
+ headers
215
+ };
216
+ }
217
+ async function clientCredentialsToken({ options, tokenEndpoint, scope, authentication, resource }) {
218
+ const { body, headers } = createClientCredentialsTokenRequest({
219
+ options,
220
+ scope,
221
+ authentication,
222
+ resource
223
+ });
224
+ const { data, error } = await (0, __better_fetch_fetch.betterFetch)(tokenEndpoint, {
225
+ method: "POST",
226
+ body,
227
+ headers
228
+ });
229
+ if (error) throw error;
230
+ const tokens = {
231
+ accessToken: data.access_token,
232
+ tokenType: data.token_type,
233
+ scopes: data.scope?.split(" ")
234
+ };
235
+ if (data.expires_in) {
236
+ const now = /* @__PURE__ */ new Date();
237
+ tokens.accessTokenExpiresAt = new Date(now.getTime() + data.expires_in * 1e3);
238
+ }
239
+ return tokens;
240
+ }
241
+
242
+ //#endregion
243
+ Object.defineProperty(exports, 'clientCredentialsToken', {
244
+ enumerable: true,
245
+ get: function () {
246
+ return clientCredentialsToken;
247
+ }
248
+ });
249
+ Object.defineProperty(exports, 'createAuthorizationCodeRequest', {
250
+ enumerable: true,
251
+ get: function () {
252
+ return createAuthorizationCodeRequest;
253
+ }
254
+ });
255
+ Object.defineProperty(exports, 'createAuthorizationURL', {
256
+ enumerable: true,
257
+ get: function () {
258
+ return createAuthorizationURL;
259
+ }
260
+ });
261
+ Object.defineProperty(exports, 'createClientCredentialsTokenRequest', {
262
+ enumerable: true,
263
+ get: function () {
264
+ return createClientCredentialsTokenRequest;
265
+ }
266
+ });
267
+ Object.defineProperty(exports, 'createRefreshAccessTokenRequest', {
268
+ enumerable: true,
269
+ get: function () {
270
+ return createRefreshAccessTokenRequest;
271
+ }
272
+ });
273
+ Object.defineProperty(exports, 'generateCodeChallenge', {
274
+ enumerable: true,
275
+ get: function () {
276
+ return generateCodeChallenge;
277
+ }
278
+ });
279
+ Object.defineProperty(exports, 'getOAuth2Tokens', {
280
+ enumerable: true,
281
+ get: function () {
282
+ return getOAuth2Tokens;
283
+ }
284
+ });
285
+ Object.defineProperty(exports, 'refreshAccessToken', {
286
+ enumerable: true,
287
+ get: function () {
288
+ return refreshAccessToken;
289
+ }
290
+ });
291
+ Object.defineProperty(exports, 'validateAuthorizationCode', {
292
+ enumerable: true,
293
+ get: function () {
294
+ return validateAuthorizationCode;
295
+ }
296
+ });
297
+ Object.defineProperty(exports, 'validateToken', {
298
+ enumerable: true,
299
+ get: function () {
300
+ return validateToken;
301
+ }
302
+ });