@better-auth/core 1.4.0-beta.9 → 1.4.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (151) hide show
  1. package/.turbo/turbo-build.log +41 -76
  2. package/dist/api/index.d.mts +3 -0
  3. package/dist/api/index.mjs +26 -0
  4. package/dist/async_hooks/index.d.mts +2 -10
  5. package/dist/async_hooks/index.mjs +2 -24
  6. package/dist/async_hooks-BfRfbd1J.mjs +18 -0
  7. package/dist/context/index.d.mts +54 -0
  8. package/dist/context/index.mjs +4 -0
  9. package/dist/context-DgQ9XGBl.mjs +114 -0
  10. package/dist/db/adapter/index.d.mts +3 -14
  11. package/dist/db/adapter/index.mjs +1 -1
  12. package/dist/db/index.d.mts +3 -39
  13. package/dist/db/index.mjs +46 -55
  14. package/dist/env/index.d.mts +2 -36
  15. package/dist/env/index.mjs +2 -299
  16. package/dist/env-DwlNAN_D.mjs +245 -0
  17. package/dist/error/index.d.mts +31 -29
  18. package/dist/error/index.mjs +3 -40
  19. package/dist/error-BhAKg8LX.mjs +45 -0
  20. package/dist/index-CdubV7uy.d.mts +82 -0
  21. package/dist/index-CkAWdKH8.d.mts +7352 -0
  22. package/dist/index-DgwIISs7.d.mts +7 -0
  23. package/dist/index.d.mts +3 -180
  24. package/dist/index.mjs +1 -1
  25. package/dist/oauth2/index.d.mts +3 -99
  26. package/dist/oauth2/index.mjs +2 -356
  27. package/dist/oauth2-DmgZmPEg.mjs +236 -0
  28. package/dist/social-providers/index.d.mts +3 -3903
  29. package/dist/social-providers/index.mjs +2434 -2654
  30. package/dist/utils/index.d.mts +5 -6
  31. package/dist/utils/index.mjs +2 -4
  32. package/dist/utils-C5EN75oV.mjs +7 -0
  33. package/package.json +70 -111
  34. package/src/api/index.ts +53 -0
  35. package/src/async_hooks/index.ts +1 -9
  36. package/src/context/endpoint-context.ts +49 -0
  37. package/src/context/index.ts +21 -0
  38. package/src/context/request-state.test.ts +94 -0
  39. package/src/context/request-state.ts +90 -0
  40. package/src/context/transaction.ts +73 -0
  41. package/src/db/adapter/index.ts +215 -129
  42. package/src/db/index.ts +12 -13
  43. package/src/db/plugin.ts +3 -3
  44. package/src/db/type.ts +54 -42
  45. package/src/env/color-depth.ts +5 -4
  46. package/src/env/env-impl.ts +2 -1
  47. package/src/env/index.ts +9 -9
  48. package/src/env/logger.test.ts +3 -2
  49. package/src/env/logger.ts +11 -9
  50. package/src/error/codes.ts +1 -1
  51. package/src/error/index.ts +1 -1
  52. package/src/oauth2/client-credentials-token.ts +9 -9
  53. package/src/oauth2/create-authorization-url.ts +12 -12
  54. package/src/oauth2/index.ts +10 -11
  55. package/src/oauth2/oauth-provider.ts +96 -74
  56. package/src/oauth2/refresh-access-token.ts +12 -12
  57. package/src/oauth2/utils.ts +2 -0
  58. package/src/oauth2/validate-authorization-code.ts +13 -15
  59. package/src/social-providers/apple.ts +8 -8
  60. package/src/social-providers/atlassian.ts +21 -19
  61. package/src/social-providers/cognito.ts +15 -15
  62. package/src/social-providers/discord.ts +8 -11
  63. package/src/social-providers/dropbox.ts +5 -5
  64. package/src/social-providers/facebook.ts +12 -10
  65. package/src/social-providers/figma.ts +6 -6
  66. package/src/social-providers/github.ts +4 -4
  67. package/src/social-providers/gitlab.ts +13 -10
  68. package/src/social-providers/google.ts +13 -13
  69. package/src/social-providers/huggingface.ts +27 -25
  70. package/src/social-providers/index.ts +30 -24
  71. package/src/social-providers/kakao.ts +41 -41
  72. package/src/social-providers/kick.ts +7 -9
  73. package/src/social-providers/line.ts +12 -12
  74. package/src/social-providers/linear.ts +9 -8
  75. package/src/social-providers/linkedin.ts +5 -5
  76. package/src/social-providers/microsoft-entra-id.ts +31 -15
  77. package/src/social-providers/naver.ts +5 -5
  78. package/src/social-providers/notion.ts +11 -9
  79. package/src/social-providers/paybin.ts +122 -0
  80. package/src/social-providers/paypal.ts +31 -29
  81. package/src/social-providers/polar.ts +110 -0
  82. package/src/social-providers/reddit.ts +6 -6
  83. package/src/social-providers/roblox.ts +15 -14
  84. package/src/social-providers/salesforce.ts +20 -18
  85. package/src/social-providers/slack.ts +4 -7
  86. package/src/social-providers/spotify.ts +5 -5
  87. package/src/social-providers/tiktok.ts +32 -33
  88. package/src/social-providers/twitch.ts +8 -8
  89. package/src/social-providers/twitter.ts +49 -45
  90. package/src/social-providers/vk.ts +14 -17
  91. package/src/social-providers/zoom.ts +29 -14
  92. package/src/types/context.ts +67 -67
  93. package/src/types/cookie.ts +1 -0
  94. package/src/types/index.ts +13 -11
  95. package/src/types/init-options.ts +1134 -911
  96. package/src/types/plugin-client.ts +61 -13
  97. package/src/types/plugin.ts +81 -57
  98. package/tsconfig.json +2 -5
  99. package/{build.config.ts → tsdown.config.ts} +8 -11
  100. package/vitest.config.ts +3 -0
  101. package/dist/async_hooks/index.cjs +0 -27
  102. package/dist/async_hooks/index.d.cts +0 -10
  103. package/dist/async_hooks/index.d.ts +0 -10
  104. package/dist/db/adapter/index.cjs +0 -2
  105. package/dist/db/adapter/index.d.cts +0 -14
  106. package/dist/db/adapter/index.d.ts +0 -14
  107. package/dist/db/index.cjs +0 -91
  108. package/dist/db/index.d.cts +0 -39
  109. package/dist/db/index.d.ts +0 -39
  110. package/dist/env/index.cjs +0 -315
  111. package/dist/env/index.d.cts +0 -36
  112. package/dist/env/index.d.ts +0 -36
  113. package/dist/error/index.cjs +0 -44
  114. package/dist/error/index.d.cts +0 -33
  115. package/dist/error/index.d.ts +0 -33
  116. package/dist/index.cjs +0 -2
  117. package/dist/index.d.cts +0 -180
  118. package/dist/index.d.ts +0 -180
  119. package/dist/middleware/index.cjs +0 -25
  120. package/dist/middleware/index.d.cts +0 -14
  121. package/dist/middleware/index.d.mts +0 -14
  122. package/dist/middleware/index.d.ts +0 -14
  123. package/dist/middleware/index.mjs +0 -21
  124. package/dist/oauth2/index.cjs +0 -368
  125. package/dist/oauth2/index.d.cts +0 -99
  126. package/dist/oauth2/index.d.ts +0 -99
  127. package/dist/shared/core.2rWMW9q9.d.ts +0 -13
  128. package/dist/shared/core.40VTWh-p.d.cts +0 -217
  129. package/dist/shared/core.BfcVdsSf.d.cts +0 -181
  130. package/dist/shared/core.Bisb2Bdk.d.mts +0 -181
  131. package/dist/shared/core.BwoNUcJQ.d.cts +0 -53
  132. package/dist/shared/core.BwoNUcJQ.d.mts +0 -53
  133. package/dist/shared/core.BwoNUcJQ.d.ts +0 -53
  134. package/dist/shared/core.CErFRCOZ.d.mts +0 -1684
  135. package/dist/shared/core.CGN6D-Mh.d.ts +0 -181
  136. package/dist/shared/core.CPuIItYE.d.ts +0 -217
  137. package/dist/shared/core.CftpHMDz.d.cts +0 -13
  138. package/dist/shared/core.Db7zJyxf.d.ts +0 -1684
  139. package/dist/shared/core.DqaxObkf.d.cts +0 -1684
  140. package/dist/shared/core.MjcDoj7R.d.cts +0 -5
  141. package/dist/shared/core.MjcDoj7R.d.mts +0 -5
  142. package/dist/shared/core.MjcDoj7R.d.ts +0 -5
  143. package/dist/shared/core.g2ZbxAEV.d.mts +0 -217
  144. package/dist/shared/core.g9ACQ8v2.d.mts +0 -13
  145. package/dist/social-providers/index.cjs +0 -2793
  146. package/dist/social-providers/index.d.cts +0 -3903
  147. package/dist/social-providers/index.d.ts +0 -3903
  148. package/dist/utils/index.cjs +0 -7
  149. package/dist/utils/index.d.cts +0 -10
  150. package/dist/utils/index.d.ts +0 -10
  151. package/src/middleware/index.ts +0 -33
@@ -0,0 +1,236 @@
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/client-credentials-token.ts
6
+ function createClientCredentialsTokenRequest({ options, scope, authentication, resource }) {
7
+ const body = new URLSearchParams();
8
+ const headers = {
9
+ "content-type": "application/x-www-form-urlencoded",
10
+ accept: "application/json"
11
+ };
12
+ body.set("grant_type", "client_credentials");
13
+ scope && body.set("scope", scope);
14
+ if (resource) if (typeof resource === "string") body.append("resource", resource);
15
+ else for (const _resource of resource) body.append("resource", _resource);
16
+ if (authentication === "basic") {
17
+ const primaryClientId = Array.isArray(options.clientId) ? options.clientId[0] : options.clientId;
18
+ headers["authorization"] = `Basic ${base64Url.encode(`${primaryClientId}:${options.clientSecret}`)}`;
19
+ } else {
20
+ const primaryClientId = Array.isArray(options.clientId) ? options.clientId[0] : options.clientId;
21
+ body.set("client_id", primaryClientId);
22
+ body.set("client_secret", options.clientSecret);
23
+ }
24
+ return {
25
+ body,
26
+ headers
27
+ };
28
+ }
29
+ async function clientCredentialsToken({ options, tokenEndpoint, scope, authentication, resource }) {
30
+ const { body, headers } = createClientCredentialsTokenRequest({
31
+ options,
32
+ scope,
33
+ authentication,
34
+ resource
35
+ });
36
+ const { data, error } = await betterFetch(tokenEndpoint, {
37
+ method: "POST",
38
+ body,
39
+ headers
40
+ });
41
+ if (error) throw error;
42
+ const tokens = {
43
+ accessToken: data.access_token,
44
+ tokenType: data.token_type,
45
+ scopes: data.scope?.split(" ")
46
+ };
47
+ if (data.expires_in) {
48
+ const now = /* @__PURE__ */ new Date();
49
+ tokens.accessTokenExpiresAt = new Date(now.getTime() + data.expires_in * 1e3);
50
+ }
51
+ return tokens;
52
+ }
53
+
54
+ //#endregion
55
+ //#region src/oauth2/utils.ts
56
+ function getOAuth2Tokens(data) {
57
+ const getDate = (seconds) => {
58
+ const now = /* @__PURE__ */ new Date();
59
+ return new Date(now.getTime() + seconds * 1e3);
60
+ };
61
+ return {
62
+ tokenType: data.token_type,
63
+ accessToken: data.access_token,
64
+ refreshToken: data.refresh_token,
65
+ accessTokenExpiresAt: data.expires_in ? getDate(data.expires_in) : void 0,
66
+ refreshTokenExpiresAt: data.refresh_token_expires_in ? getDate(data.refresh_token_expires_in) : void 0,
67
+ scopes: data?.scope ? typeof data.scope === "string" ? data.scope.split(" ") : data.scope : [],
68
+ idToken: data.id_token,
69
+ raw: data
70
+ };
71
+ }
72
+ async function generateCodeChallenge(codeVerifier) {
73
+ const data = new TextEncoder().encode(codeVerifier);
74
+ const hash = await crypto.subtle.digest("SHA-256", data);
75
+ return base64Url.encode(new Uint8Array(hash), { padding: false });
76
+ }
77
+
78
+ //#endregion
79
+ //#region src/oauth2/create-authorization-url.ts
80
+ async function createAuthorizationURL({ id, options, authorizationEndpoint, state, codeVerifier, scopes, claims, redirectURI, duration, prompt, accessType, responseType, display, loginHint, hd, responseMode, additionalParams, scopeJoiner }) {
81
+ const url = new URL(authorizationEndpoint);
82
+ url.searchParams.set("response_type", responseType || "code");
83
+ const primaryClientId = Array.isArray(options.clientId) ? options.clientId[0] : options.clientId;
84
+ url.searchParams.set("client_id", primaryClientId);
85
+ url.searchParams.set("state", state);
86
+ url.searchParams.set("scope", scopes.join(scopeJoiner || " "));
87
+ url.searchParams.set("redirect_uri", options.redirectURI || redirectURI);
88
+ duration && url.searchParams.set("duration", duration);
89
+ display && url.searchParams.set("display", display);
90
+ loginHint && url.searchParams.set("login_hint", loginHint);
91
+ prompt && url.searchParams.set("prompt", prompt);
92
+ hd && url.searchParams.set("hd", hd);
93
+ accessType && url.searchParams.set("access_type", accessType);
94
+ responseMode && url.searchParams.set("response_mode", responseMode);
95
+ if (codeVerifier) {
96
+ const codeChallenge = await generateCodeChallenge(codeVerifier);
97
+ url.searchParams.set("code_challenge_method", "S256");
98
+ url.searchParams.set("code_challenge", codeChallenge);
99
+ }
100
+ if (claims) {
101
+ const claimsObj = claims.reduce((acc, claim) => {
102
+ acc[claim] = null;
103
+ return acc;
104
+ }, {});
105
+ url.searchParams.set("claims", JSON.stringify({ id_token: {
106
+ email: null,
107
+ email_verified: null,
108
+ ...claimsObj
109
+ } }));
110
+ }
111
+ if (additionalParams) Object.entries(additionalParams).forEach(([key, value]) => {
112
+ url.searchParams.set(key, value);
113
+ });
114
+ return url;
115
+ }
116
+
117
+ //#endregion
118
+ //#region src/oauth2/refresh-access-token.ts
119
+ function createRefreshAccessTokenRequest({ refreshToken, options, authentication, extraParams, resource }) {
120
+ const body = new URLSearchParams();
121
+ const headers = {
122
+ "content-type": "application/x-www-form-urlencoded",
123
+ accept: "application/json"
124
+ };
125
+ body.set("grant_type", "refresh_token");
126
+ body.set("refresh_token", refreshToken);
127
+ if (authentication === "basic") {
128
+ const primaryClientId = Array.isArray(options.clientId) ? options.clientId[0] : options.clientId;
129
+ if (primaryClientId) headers["authorization"] = "Basic " + base64.encode(`${primaryClientId}:${options.clientSecret ?? ""}`);
130
+ else headers["authorization"] = "Basic " + base64.encode(`:${options.clientSecret ?? ""}`);
131
+ } else {
132
+ const primaryClientId = Array.isArray(options.clientId) ? options.clientId[0] : options.clientId;
133
+ body.set("client_id", primaryClientId);
134
+ if (options.clientSecret) body.set("client_secret", options.clientSecret);
135
+ }
136
+ if (resource) if (typeof resource === "string") body.append("resource", resource);
137
+ else for (const _resource of resource) body.append("resource", _resource);
138
+ if (extraParams) for (const [key, value] of Object.entries(extraParams)) body.set(key, value);
139
+ return {
140
+ body,
141
+ headers
142
+ };
143
+ }
144
+ async function refreshAccessToken({ refreshToken, options, tokenEndpoint, authentication, extraParams }) {
145
+ const { body, headers } = createRefreshAccessTokenRequest({
146
+ refreshToken,
147
+ options,
148
+ authentication,
149
+ extraParams
150
+ });
151
+ const { data, error } = await betterFetch(tokenEndpoint, {
152
+ method: "POST",
153
+ body,
154
+ headers
155
+ });
156
+ if (error) throw error;
157
+ const tokens = {
158
+ accessToken: data.access_token,
159
+ refreshToken: data.refresh_token,
160
+ tokenType: data.token_type,
161
+ scopes: data.scope?.split(" "),
162
+ idToken: data.id_token
163
+ };
164
+ if (data.expires_in) {
165
+ const now = /* @__PURE__ */ new Date();
166
+ tokens.accessTokenExpiresAt = new Date(now.getTime() + data.expires_in * 1e3);
167
+ }
168
+ return tokens;
169
+ }
170
+
171
+ //#endregion
172
+ //#region src/oauth2/validate-authorization-code.ts
173
+ function createAuthorizationCodeRequest({ code, codeVerifier, redirectURI, options, authentication, deviceId, headers, additionalParams = {}, resource }) {
174
+ const body = new URLSearchParams();
175
+ const requestHeaders = {
176
+ "content-type": "application/x-www-form-urlencoded",
177
+ accept: "application/json",
178
+ ...headers
179
+ };
180
+ body.set("grant_type", "authorization_code");
181
+ body.set("code", code);
182
+ codeVerifier && body.set("code_verifier", codeVerifier);
183
+ options.clientKey && body.set("client_key", options.clientKey);
184
+ deviceId && body.set("device_id", deviceId);
185
+ body.set("redirect_uri", options.redirectURI || redirectURI);
186
+ if (resource) if (typeof resource === "string") body.append("resource", resource);
187
+ else for (const _resource of resource) body.append("resource", _resource);
188
+ if (authentication === "basic") {
189
+ const primaryClientId = Array.isArray(options.clientId) ? options.clientId[0] : options.clientId;
190
+ requestHeaders["authorization"] = `Basic ${base64.encode(`${primaryClientId}:${options.clientSecret ?? ""}`)}`;
191
+ } else {
192
+ const primaryClientId = Array.isArray(options.clientId) ? options.clientId[0] : options.clientId;
193
+ body.set("client_id", primaryClientId);
194
+ if (options.clientSecret) body.set("client_secret", options.clientSecret);
195
+ }
196
+ for (const [key, value] of Object.entries(additionalParams)) if (!body.has(key)) body.append(key, value);
197
+ return {
198
+ body,
199
+ headers: requestHeaders
200
+ };
201
+ }
202
+ async function validateAuthorizationCode({ code, codeVerifier, redirectURI, options, tokenEndpoint, authentication, deviceId, headers, additionalParams = {}, resource }) {
203
+ const { body, headers: requestHeaders } = createAuthorizationCodeRequest({
204
+ code,
205
+ codeVerifier,
206
+ redirectURI,
207
+ options,
208
+ authentication,
209
+ deviceId,
210
+ headers,
211
+ additionalParams,
212
+ resource
213
+ });
214
+ const { data, error } = await betterFetch(tokenEndpoint, {
215
+ method: "POST",
216
+ body,
217
+ headers: requestHeaders
218
+ });
219
+ if (error) throw error;
220
+ return getOAuth2Tokens(data);
221
+ }
222
+ async function validateToken(token, jwksEndpoint) {
223
+ const { data, error } = await betterFetch(jwksEndpoint, {
224
+ method: "GET",
225
+ headers: { accept: "application/json" }
226
+ });
227
+ if (error) throw error;
228
+ const keys = data["keys"];
229
+ const header = JSON.parse(atob(token.split(".")[0]));
230
+ const key = keys.find((key$1) => key$1.kid === header.kid);
231
+ if (!key) throw new Error("Key not found");
232
+ return await jwtVerify(token, key);
233
+ }
234
+
235
+ //#endregion
236
+ export { refreshAccessToken as a, getOAuth2Tokens as c, createRefreshAccessTokenRequest as i, clientCredentialsToken as l, validateAuthorizationCode as n, createAuthorizationURL as o, validateToken as r, generateCodeChallenge as s, createAuthorizationCodeRequest as t, createClientCredentialsTokenRequest as u };