@better-auth/core 1.4.12 → 1.4.14

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 (188) hide show
  1. package/.turbo/turbo-build.log +172 -35
  2. package/dist/api/index.d.mts +178 -1
  3. package/dist/api/index.mjs +2 -1
  4. package/dist/context/endpoint-context.d.mts +19 -0
  5. package/dist/context/endpoint-context.mjs +31 -0
  6. package/dist/context/global.d.mts +7 -0
  7. package/dist/context/global.mjs +37 -0
  8. package/dist/context/index.d.mts +5 -53
  9. package/dist/context/index.mjs +5 -2
  10. package/dist/context/request-state.d.mts +27 -0
  11. package/dist/context/request-state.mjs +49 -0
  12. package/dist/context/transaction.d.mts +16 -0
  13. package/dist/context/transaction.mjs +52 -0
  14. package/dist/db/adapter/factory.d.mts +27 -0
  15. package/dist/db/adapter/factory.mjs +738 -0
  16. package/dist/db/adapter/get-default-field-name.d.mts +18 -0
  17. package/dist/db/adapter/get-default-field-name.mjs +38 -0
  18. package/dist/db/adapter/get-default-model-name.d.mts +12 -0
  19. package/dist/db/adapter/get-default-model-name.mjs +32 -0
  20. package/dist/db/adapter/get-field-attributes.d.mts +29 -0
  21. package/dist/db/adapter/get-field-attributes.mjs +39 -0
  22. package/dist/db/adapter/get-field-name.d.mts +18 -0
  23. package/dist/db/adapter/get-field-name.mjs +33 -0
  24. package/dist/db/adapter/get-id-field.d.mts +39 -0
  25. package/dist/db/adapter/get-id-field.mjs +68 -0
  26. package/dist/db/adapter/get-model-name.d.mts +12 -0
  27. package/dist/db/adapter/get-model-name.mjs +23 -0
  28. package/dist/db/adapter/index.d.mts +513 -1
  29. package/dist/db/adapter/index.mjs +8 -970
  30. package/dist/db/adapter/types.d.mts +139 -0
  31. package/dist/db/adapter/utils.d.mts +7 -0
  32. package/dist/db/adapter/utils.mjs +38 -0
  33. package/dist/db/get-tables.d.mts +8 -0
  34. package/dist/{get-tables-CMc_Emww.mjs → db/get-tables.mjs} +7 -1
  35. package/dist/db/index.d.mts +10 -2
  36. package/dist/db/index.mjs +7 -60
  37. package/dist/db/plugin.d.mts +12 -0
  38. package/dist/db/schema/account.d.mts +26 -0
  39. package/dist/db/schema/account.mjs +19 -0
  40. package/dist/db/schema/rate-limit.d.mts +14 -0
  41. package/dist/db/schema/rate-limit.mjs +11 -0
  42. package/dist/db/schema/session.d.mts +21 -0
  43. package/dist/db/schema/session.mjs +14 -0
  44. package/dist/db/schema/shared.d.mts +10 -0
  45. package/dist/db/schema/shared.mjs +11 -0
  46. package/dist/db/schema/user.d.mts +20 -0
  47. package/dist/db/schema/user.mjs +13 -0
  48. package/dist/db/schema/verification.d.mts +19 -0
  49. package/dist/db/schema/verification.mjs +12 -0
  50. package/dist/db/type.d.mts +143 -0
  51. package/dist/env/color-depth.d.mts +4 -0
  52. package/dist/env/color-depth.mjs +88 -0
  53. package/dist/env/env-impl.d.mts +32 -0
  54. package/dist/env/env-impl.mjs +82 -0
  55. package/dist/env/index.d.mts +4 -2
  56. package/dist/env/index.mjs +3 -1
  57. package/dist/{index-BRBu0-5h.d.mts → env/logger.d.mts} +1 -35
  58. package/dist/env/logger.mjs +81 -0
  59. package/dist/error/codes.d.mts +48 -0
  60. package/dist/{error-DP1xOn7P.mjs → error/codes.mjs} +3 -14
  61. package/dist/error/index.d.mts +5 -48
  62. package/dist/error/index.mjs +12 -3
  63. package/dist/index.d.mts +8 -2
  64. package/dist/oauth2/client-credentials-token.d.mts +36 -0
  65. package/dist/oauth2/client-credentials-token.mjs +54 -0
  66. package/dist/oauth2/create-authorization-url.d.mts +45 -0
  67. package/dist/oauth2/create-authorization-url.mjs +42 -0
  68. package/dist/oauth2/index.d.mts +8 -2
  69. package/dist/oauth2/index.mjs +6 -2
  70. package/dist/oauth2/oauth-provider.d.mts +194 -0
  71. package/dist/oauth2/refresh-access-token.d.mts +36 -0
  72. package/dist/oauth2/refresh-access-token.mjs +58 -0
  73. package/dist/oauth2/utils.d.mts +7 -0
  74. package/dist/oauth2/utils.mjs +27 -0
  75. package/dist/oauth2/validate-authorization-code.d.mts +55 -0
  76. package/dist/oauth2/validate-authorization-code.mjs +71 -0
  77. package/dist/oauth2/verify.d.mts +49 -0
  78. package/dist/oauth2/verify.mjs +95 -0
  79. package/dist/social-providers/apple.d.mts +119 -0
  80. package/dist/social-providers/apple.mjs +102 -0
  81. package/dist/social-providers/atlassian.d.mts +72 -0
  82. package/dist/social-providers/atlassian.mjs +83 -0
  83. package/dist/social-providers/cognito.d.mts +87 -0
  84. package/dist/social-providers/cognito.mjs +166 -0
  85. package/dist/social-providers/discord.d.mts +126 -0
  86. package/dist/social-providers/discord.mjs +64 -0
  87. package/dist/social-providers/dropbox.d.mts +71 -0
  88. package/dist/social-providers/dropbox.mjs +75 -0
  89. package/dist/social-providers/facebook.d.mts +81 -0
  90. package/dist/social-providers/facebook.mjs +120 -0
  91. package/dist/social-providers/figma.d.mts +63 -0
  92. package/dist/social-providers/figma.mjs +86 -0
  93. package/dist/social-providers/github.d.mts +104 -0
  94. package/dist/social-providers/github.mjs +80 -0
  95. package/dist/social-providers/gitlab.d.mts +125 -0
  96. package/dist/social-providers/gitlab.mjs +82 -0
  97. package/dist/social-providers/google.d.mts +99 -0
  98. package/dist/social-providers/google.mjs +109 -0
  99. package/dist/social-providers/huggingface.d.mts +85 -0
  100. package/dist/social-providers/huggingface.mjs +75 -0
  101. package/dist/social-providers/index.d.mts +1723 -1
  102. package/dist/social-providers/index.mjs +33 -2570
  103. package/dist/social-providers/kakao.d.mts +163 -0
  104. package/dist/social-providers/kakao.mjs +72 -0
  105. package/dist/social-providers/kick.d.mts +75 -0
  106. package/dist/social-providers/kick.mjs +71 -0
  107. package/dist/social-providers/line.d.mts +107 -0
  108. package/dist/social-providers/line.mjs +113 -0
  109. package/dist/social-providers/linear.d.mts +70 -0
  110. package/dist/social-providers/linear.mjs +88 -0
  111. package/dist/social-providers/linkedin.d.mts +69 -0
  112. package/dist/social-providers/linkedin.mjs +76 -0
  113. package/dist/social-providers/microsoft-entra-id.d.mts +174 -0
  114. package/dist/social-providers/microsoft-entra-id.mjs +106 -0
  115. package/dist/social-providers/naver.d.mts +104 -0
  116. package/dist/social-providers/naver.mjs +67 -0
  117. package/dist/social-providers/notion.d.mts +66 -0
  118. package/dist/social-providers/notion.mjs +75 -0
  119. package/dist/social-providers/paybin.d.mts +73 -0
  120. package/dist/social-providers/paybin.mjs +85 -0
  121. package/dist/social-providers/paypal.d.mts +131 -0
  122. package/dist/social-providers/paypal.mjs +144 -0
  123. package/dist/social-providers/polar.d.mts +76 -0
  124. package/dist/social-providers/polar.mjs +73 -0
  125. package/dist/social-providers/reddit.d.mts +64 -0
  126. package/dist/social-providers/reddit.mjs +83 -0
  127. package/dist/social-providers/roblox.d.mts +72 -0
  128. package/dist/social-providers/roblox.mjs +59 -0
  129. package/dist/social-providers/salesforce.d.mts +81 -0
  130. package/dist/social-providers/salesforce.mjs +91 -0
  131. package/dist/social-providers/slack.d.mts +85 -0
  132. package/dist/social-providers/slack.mjs +68 -0
  133. package/dist/social-providers/spotify.d.mts +65 -0
  134. package/dist/social-providers/spotify.mjs +71 -0
  135. package/dist/social-providers/tiktok.d.mts +171 -0
  136. package/dist/social-providers/tiktok.mjs +62 -0
  137. package/dist/social-providers/twitch.d.mts +81 -0
  138. package/dist/social-providers/twitch.mjs +78 -0
  139. package/dist/social-providers/twitter.d.mts +140 -0
  140. package/dist/social-providers/twitter.mjs +87 -0
  141. package/dist/social-providers/vercel.d.mts +64 -0
  142. package/dist/social-providers/vercel.mjs +61 -0
  143. package/dist/social-providers/vk.d.mts +72 -0
  144. package/dist/social-providers/vk.mjs +83 -0
  145. package/dist/social-providers/zoom.d.mts +173 -0
  146. package/dist/social-providers/zoom.mjs +72 -0
  147. package/dist/types/context.d.mts +215 -0
  148. package/dist/types/cookie.d.mts +15 -0
  149. package/dist/types/helper.d.mts +8 -0
  150. package/dist/types/index.d.mts +8 -0
  151. package/dist/types/init-options.d.mts +1272 -0
  152. package/dist/types/plugin-client.d.mts +103 -0
  153. package/dist/types/plugin.d.mts +121 -0
  154. package/dist/utils/deprecate.d.mts +10 -0
  155. package/dist/utils/deprecate.mjs +17 -0
  156. package/dist/utils/error-codes.d.mts +9 -0
  157. package/dist/utils/error-codes.mjs +7 -0
  158. package/dist/utils/id.d.mts +4 -0
  159. package/dist/utils/id.mjs +9 -0
  160. package/dist/utils/index.d.mts +5 -26
  161. package/dist/utils/index.mjs +5 -2
  162. package/dist/utils/json.d.mts +4 -0
  163. package/dist/utils/json.mjs +25 -0
  164. package/dist/utils/string.d.mts +4 -0
  165. package/dist/utils/string.mjs +7 -0
  166. package/package.json +3 -3
  167. package/src/context/endpoint-context.ts +7 -15
  168. package/src/context/global.ts +57 -0
  169. package/src/context/index.ts +1 -0
  170. package/src/context/request-state.ts +7 -12
  171. package/src/context/transaction.ts +7 -16
  172. package/src/db/adapter/factory.ts +13 -13
  173. package/src/db/adapter/get-default-model-name.ts +1 -1
  174. package/src/db/adapter/get-id-field.ts +2 -2
  175. package/src/db/get-tables.ts +6 -0
  176. package/src/error/index.ts +2 -3
  177. package/src/social-providers/figma.ts +5 -3
  178. package/src/social-providers/gitlab.ts +1 -1
  179. package/src/types/context.ts +137 -131
  180. package/src/types/cookie.ts +6 -4
  181. package/src/types/index.ts +2 -1
  182. package/src/types/init-options.ts +6 -0
  183. package/tsdown.config.ts +9 -0
  184. package/dist/context-BGZ8V6DD.mjs +0 -126
  185. package/dist/env-DbssmzoK.mjs +0 -245
  186. package/dist/index-zgYuzZ7O.d.mts +0 -8020
  187. package/dist/oauth2-COJkghlT.mjs +0 -326
  188. package/dist/utils-U2L7n92V.mjs +0 -59
@@ -0,0 +1,36 @@
1
+ import { OAuth2Tokens, ProviderOptions } from "./oauth-provider.mjs";
2
+
3
+ //#region src/oauth2/client-credentials-token.d.ts
4
+ declare function createClientCredentialsTokenRequest({
5
+ options,
6
+ scope,
7
+ authentication,
8
+ resource
9
+ }: {
10
+ options: ProviderOptions & {
11
+ clientSecret: string;
12
+ };
13
+ scope?: string | undefined;
14
+ authentication?: ("basic" | "post") | undefined;
15
+ resource?: (string | string[]) | undefined;
16
+ }): {
17
+ body: URLSearchParams;
18
+ headers: Record<string, any>;
19
+ };
20
+ declare function clientCredentialsToken({
21
+ options,
22
+ tokenEndpoint,
23
+ scope,
24
+ authentication,
25
+ resource
26
+ }: {
27
+ options: ProviderOptions & {
28
+ clientSecret: string;
29
+ };
30
+ tokenEndpoint: string;
31
+ scope: string;
32
+ authentication?: ("basic" | "post") | undefined;
33
+ resource?: (string | string[]) | undefined;
34
+ }): Promise<OAuth2Tokens>;
35
+ //#endregion
36
+ export { clientCredentialsToken, createClientCredentialsTokenRequest };
@@ -0,0 +1,54 @@
1
+ import { base64Url } from "@better-auth/utils/base64";
2
+ import { betterFetch } from "@better-fetch/fetch";
3
+
4
+ //#region src/oauth2/client-credentials-token.ts
5
+ function createClientCredentialsTokenRequest({ options, scope, authentication, resource }) {
6
+ const body = new URLSearchParams();
7
+ const headers = {
8
+ "content-type": "application/x-www-form-urlencoded",
9
+ accept: "application/json"
10
+ };
11
+ body.set("grant_type", "client_credentials");
12
+ scope && body.set("scope", scope);
13
+ if (resource) if (typeof resource === "string") body.append("resource", resource);
14
+ else for (const _resource of resource) body.append("resource", _resource);
15
+ if (authentication === "basic") {
16
+ const primaryClientId = Array.isArray(options.clientId) ? options.clientId[0] : options.clientId;
17
+ headers["authorization"] = `Basic ${base64Url.encode(`${primaryClientId}:${options.clientSecret}`)}`;
18
+ } else {
19
+ const primaryClientId = Array.isArray(options.clientId) ? options.clientId[0] : options.clientId;
20
+ body.set("client_id", primaryClientId);
21
+ body.set("client_secret", options.clientSecret);
22
+ }
23
+ return {
24
+ body,
25
+ headers
26
+ };
27
+ }
28
+ async function clientCredentialsToken({ options, tokenEndpoint, scope, authentication, resource }) {
29
+ const { body, headers } = createClientCredentialsTokenRequest({
30
+ options,
31
+ scope,
32
+ authentication,
33
+ resource
34
+ });
35
+ const { data, error } = await betterFetch(tokenEndpoint, {
36
+ method: "POST",
37
+ body,
38
+ headers
39
+ });
40
+ if (error) throw error;
41
+ const tokens = {
42
+ accessToken: data.access_token,
43
+ tokenType: data.token_type,
44
+ scopes: data.scope?.split(" ")
45
+ };
46
+ if (data.expires_in) {
47
+ const now = /* @__PURE__ */ new Date();
48
+ tokens.accessTokenExpiresAt = new Date(now.getTime() + data.expires_in * 1e3);
49
+ }
50
+ return tokens;
51
+ }
52
+
53
+ //#endregion
54
+ export { clientCredentialsToken, createClientCredentialsTokenRequest };
@@ -0,0 +1,45 @@
1
+ import { ProviderOptions } from "./oauth-provider.mjs";
2
+ import "./index.mjs";
3
+
4
+ //#region src/oauth2/create-authorization-url.d.ts
5
+ declare function createAuthorizationURL({
6
+ id,
7
+ options,
8
+ authorizationEndpoint,
9
+ state,
10
+ codeVerifier,
11
+ scopes,
12
+ claims,
13
+ redirectURI,
14
+ duration,
15
+ prompt,
16
+ accessType,
17
+ responseType,
18
+ display,
19
+ loginHint,
20
+ hd,
21
+ responseMode,
22
+ additionalParams,
23
+ scopeJoiner
24
+ }: {
25
+ id: string;
26
+ options: ProviderOptions;
27
+ redirectURI: string;
28
+ authorizationEndpoint: string;
29
+ state: string;
30
+ codeVerifier?: string | undefined;
31
+ scopes?: string[] | undefined;
32
+ claims?: string[] | undefined;
33
+ duration?: string | undefined;
34
+ prompt?: string | undefined;
35
+ accessType?: string | undefined;
36
+ responseType?: string | undefined;
37
+ display?: string | undefined;
38
+ loginHint?: string | undefined;
39
+ hd?: string | undefined;
40
+ responseMode?: string | undefined;
41
+ additionalParams?: Record<string, string> | undefined;
42
+ scopeJoiner?: string | undefined;
43
+ }): Promise<URL>;
44
+ //#endregion
45
+ export { createAuthorizationURL };
@@ -0,0 +1,42 @@
1
+ import { generateCodeChallenge } from "./utils.mjs";
2
+
3
+ //#region src/oauth2/create-authorization-url.ts
4
+ async function createAuthorizationURL({ id, options, authorizationEndpoint, state, codeVerifier, scopes, claims, redirectURI, duration, prompt, accessType, responseType, display, loginHint, hd, responseMode, additionalParams, scopeJoiner }) {
5
+ const url = new URL(options.authorizationEndpoint || authorizationEndpoint);
6
+ url.searchParams.set("response_type", responseType || "code");
7
+ const primaryClientId = Array.isArray(options.clientId) ? options.clientId[0] : options.clientId;
8
+ url.searchParams.set("client_id", primaryClientId);
9
+ url.searchParams.set("state", state);
10
+ if (scopes) url.searchParams.set("scope", scopes.join(scopeJoiner || " "));
11
+ url.searchParams.set("redirect_uri", options.redirectURI || redirectURI);
12
+ duration && url.searchParams.set("duration", duration);
13
+ display && url.searchParams.set("display", display);
14
+ loginHint && url.searchParams.set("login_hint", loginHint);
15
+ prompt && url.searchParams.set("prompt", prompt);
16
+ hd && url.searchParams.set("hd", hd);
17
+ accessType && url.searchParams.set("access_type", accessType);
18
+ responseMode && url.searchParams.set("response_mode", responseMode);
19
+ if (codeVerifier) {
20
+ const codeChallenge = await generateCodeChallenge(codeVerifier);
21
+ url.searchParams.set("code_challenge_method", "S256");
22
+ url.searchParams.set("code_challenge", codeChallenge);
23
+ }
24
+ if (claims) {
25
+ const claimsObj = claims.reduce((acc, claim) => {
26
+ acc[claim] = null;
27
+ return acc;
28
+ }, {});
29
+ url.searchParams.set("claims", JSON.stringify({ id_token: {
30
+ email: null,
31
+ email_verified: null,
32
+ ...claimsObj
33
+ } }));
34
+ }
35
+ if (additionalParams) Object.entries(additionalParams).forEach(([key, value]) => {
36
+ url.searchParams.set(key, value);
37
+ });
38
+ return url;
39
+ }
40
+
41
+ //#endregion
42
+ export { createAuthorizationURL };
@@ -1,2 +1,8 @@
1
- import { Bn as refreshAccessToken, Fn as validateAuthorizationCode, Gn as OAuth2UserInfo, Hn as clientCredentialsToken, In as validateToken, Kn as OAuthProvider, Ln as generateCodeChallenge, Mn as verifyAccessToken, Nn as verifyJwsAccessToken, Pn as createAuthorizationCodeRequest, Rn as getOAuth2Tokens, Un as createClientCredentialsTokenRequest, Vn as createAuthorizationURL, Wn as OAuth2Tokens, jn as getJwks, qn as ProviderOptions, zn as createRefreshAccessTokenRequest } from "../index-zgYuzZ7O.mjs";
2
- export { OAuth2Tokens, OAuth2UserInfo, OAuthProvider, ProviderOptions, clientCredentialsToken, createAuthorizationCodeRequest, createAuthorizationURL, createClientCredentialsTokenRequest, createRefreshAccessTokenRequest, generateCodeChallenge, getJwks, getOAuth2Tokens, refreshAccessToken, validateAuthorizationCode, validateToken, verifyAccessToken, verifyJwsAccessToken };
1
+ import { OAuth2Tokens, OAuth2UserInfo, OAuthProvider, ProviderOptions } from "./oauth-provider.mjs";
2
+ import { clientCredentialsToken, createClientCredentialsTokenRequest } from "./client-credentials-token.mjs";
3
+ import { createAuthorizationURL } from "./create-authorization-url.mjs";
4
+ import { createRefreshAccessTokenRequest, refreshAccessToken } from "./refresh-access-token.mjs";
5
+ import { generateCodeChallenge, getOAuth2Tokens } from "./utils.mjs";
6
+ import { createAuthorizationCodeRequest, validateAuthorizationCode, validateToken } from "./validate-authorization-code.mjs";
7
+ import { getJwks, verifyAccessToken, verifyJwsAccessToken } from "./verify.mjs";
8
+ export { type OAuth2Tokens, type OAuth2UserInfo, type OAuthProvider, type ProviderOptions, clientCredentialsToken, createAuthorizationCodeRequest, createAuthorizationURL, createClientCredentialsTokenRequest, createRefreshAccessTokenRequest, generateCodeChallenge, getJwks, getOAuth2Tokens, refreshAccessToken, validateAuthorizationCode, validateToken, verifyAccessToken, verifyJwsAccessToken };
@@ -1,4 +1,8 @@
1
- import "../env-DbssmzoK.mjs";
2
- import { a as validateAuthorizationCode, c as refreshAccessToken, d as getOAuth2Tokens, f as clientCredentialsToken, i as createAuthorizationCodeRequest, l as createAuthorizationURL, n as verifyAccessToken, o as validateToken, p as createClientCredentialsTokenRequest, r as verifyJwsAccessToken, s as createRefreshAccessTokenRequest, t as getJwks, u as generateCodeChallenge } from "../oauth2-COJkghlT.mjs";
1
+ import { clientCredentialsToken, createClientCredentialsTokenRequest } from "./client-credentials-token.mjs";
2
+ import { generateCodeChallenge, getOAuth2Tokens } from "./utils.mjs";
3
+ import { createAuthorizationURL } from "./create-authorization-url.mjs";
4
+ import { createRefreshAccessTokenRequest, refreshAccessToken } from "./refresh-access-token.mjs";
5
+ import { createAuthorizationCodeRequest, validateAuthorizationCode, validateToken } from "./validate-authorization-code.mjs";
6
+ import { getJwks, verifyAccessToken, verifyJwsAccessToken } from "./verify.mjs";
3
7
 
4
8
  export { clientCredentialsToken, createAuthorizationCodeRequest, createAuthorizationURL, createClientCredentialsTokenRequest, createRefreshAccessTokenRequest, generateCodeChallenge, getJwks, getOAuth2Tokens, refreshAccessToken, validateAuthorizationCode, validateToken, verifyAccessToken, verifyJwsAccessToken };
@@ -0,0 +1,194 @@
1
+ import { Awaitable, LiteralString } from "../types/helper.mjs";
2
+ import "../types/index.mjs";
3
+
4
+ //#region src/oauth2/oauth-provider.d.ts
5
+ interface OAuth2Tokens {
6
+ tokenType?: string | undefined;
7
+ accessToken?: string | undefined;
8
+ refreshToken?: string | undefined;
9
+ accessTokenExpiresAt?: Date | undefined;
10
+ refreshTokenExpiresAt?: Date | undefined;
11
+ scopes?: string[] | undefined;
12
+ idToken?: string | undefined;
13
+ /**
14
+ * Raw token response from the provider.
15
+ * Preserves provider-specific fields that are not part of the standard OAuth2 token response.
16
+ */
17
+ raw?: Record<string, unknown> | undefined;
18
+ }
19
+ type OAuth2UserInfo = {
20
+ id: string | number;
21
+ name?: string | undefined;
22
+ email?: (string | null) | undefined;
23
+ image?: string | undefined;
24
+ emailVerified: boolean;
25
+ };
26
+ interface OAuthProvider<T extends Record<string, any> = Record<string, any>, O extends Record<string, any> = Partial<ProviderOptions>> {
27
+ id: LiteralString;
28
+ createAuthorizationURL: (data: {
29
+ state: string;
30
+ codeVerifier: string;
31
+ scopes?: string[] | undefined;
32
+ redirectURI: string;
33
+ display?: string | undefined;
34
+ loginHint?: string | undefined;
35
+ }) => Awaitable<URL>;
36
+ name: string;
37
+ validateAuthorizationCode: (data: {
38
+ code: string;
39
+ redirectURI: string;
40
+ codeVerifier?: string | undefined;
41
+ deviceId?: string | undefined;
42
+ }) => Promise<OAuth2Tokens>;
43
+ getUserInfo: (token: OAuth2Tokens & {
44
+ /**
45
+ * The user object from the provider
46
+ * This is only available for some providers like Apple
47
+ */
48
+ user?: {
49
+ name?: {
50
+ firstName?: string;
51
+ lastName?: string;
52
+ };
53
+ email?: string;
54
+ } | undefined;
55
+ }) => Promise<{
56
+ user: OAuth2UserInfo;
57
+ data: T;
58
+ } | null>;
59
+ /**
60
+ * Custom function to refresh a token
61
+ */
62
+ refreshAccessToken?: ((refreshToken: string) => Promise<OAuth2Tokens>) | undefined;
63
+ revokeToken?: ((token: string) => Promise<void>) | undefined;
64
+ /**
65
+ * Verify the id token
66
+ * @param token - The id token
67
+ * @param nonce - The nonce
68
+ * @returns True if the id token is valid, false otherwise
69
+ */
70
+ verifyIdToken?: ((token: string, nonce?: string) => Promise<boolean>) | undefined;
71
+ /**
72
+ * Disable implicit sign up for new users. When set to true for the provider,
73
+ * sign-in need to be called with with requestSignUp as true to create new users.
74
+ */
75
+ disableImplicitSignUp?: boolean | undefined;
76
+ /**
77
+ * Disable sign up for new users.
78
+ */
79
+ disableSignUp?: boolean | undefined;
80
+ /**
81
+ * Options for the provider
82
+ */
83
+ options?: O | undefined;
84
+ }
85
+ type ProviderOptions<Profile extends Record<string, any> = any> = {
86
+ /**
87
+ * The client ID of your application.
88
+ *
89
+ * This is usually a string but can be any type depending on the provider.
90
+ */
91
+ clientId?: unknown | undefined;
92
+ /**
93
+ * The client secret of your application
94
+ */
95
+ clientSecret?: string | undefined;
96
+ /**
97
+ * The scopes you want to request from the provider
98
+ */
99
+ scope?: string[] | undefined;
100
+ /**
101
+ * Remove default scopes of the provider
102
+ */
103
+ disableDefaultScope?: boolean | undefined;
104
+ /**
105
+ * The redirect URL for your application. This is where the provider will
106
+ * redirect the user after the sign in process. Make sure this URL is
107
+ * whitelisted in the provider's dashboard.
108
+ */
109
+ redirectURI?: string | undefined;
110
+ /**
111
+ * Custom authorization endpoint URL.
112
+ * Use this to override the default authorization endpoint of the provider.
113
+ * Useful for testing with local OAuth servers or using sandbox environments.
114
+ */
115
+ authorizationEndpoint?: string | undefined;
116
+ /**
117
+ * The client key of your application
118
+ * Tiktok Social Provider uses this field instead of clientId
119
+ */
120
+ clientKey?: string | undefined;
121
+ /**
122
+ * Disable provider from allowing users to sign in
123
+ * with this provider with an id token sent from the
124
+ * client.
125
+ */
126
+ disableIdTokenSignIn?: boolean | undefined;
127
+ /**
128
+ * verifyIdToken function to verify the id token
129
+ */
130
+ verifyIdToken?: ((token: string, nonce?: string) => Promise<boolean>) | undefined;
131
+ /**
132
+ * Custom function to get user info from the provider
133
+ */
134
+ getUserInfo?: ((token: OAuth2Tokens) => Promise<{
135
+ user: {
136
+ id: string;
137
+ name?: string;
138
+ email?: string | null;
139
+ image?: string;
140
+ emailVerified: boolean;
141
+ [key: string]: any;
142
+ };
143
+ data: any;
144
+ } | null>) | undefined;
145
+ /**
146
+ * Custom function to refresh a token
147
+ */
148
+ refreshAccessToken?: ((refreshToken: string) => Promise<OAuth2Tokens>) | undefined;
149
+ /**
150
+ * Custom function to map the provider profile to a
151
+ * user.
152
+ */
153
+ mapProfileToUser?: ((profile: Profile) => {
154
+ id?: string;
155
+ name?: string;
156
+ email?: string | null;
157
+ image?: string;
158
+ emailVerified?: boolean;
159
+ [key: string]: any;
160
+ } | Promise<{
161
+ id?: string;
162
+ name?: string;
163
+ email?: string | null;
164
+ image?: string;
165
+ emailVerified?: boolean;
166
+ [key: string]: any;
167
+ }>) | undefined;
168
+ /**
169
+ * Disable implicit sign up for new users. When set to true for the provider,
170
+ * sign-in need to be called with with requestSignUp as true to create new users.
171
+ */
172
+ disableImplicitSignUp?: boolean | undefined;
173
+ /**
174
+ * Disable sign up for new users.
175
+ */
176
+ disableSignUp?: boolean | undefined;
177
+ /**
178
+ * The prompt to use for the authorization code request
179
+ */
180
+ prompt?: ("select_account" | "consent" | "login" | "none" | "select_account consent") | undefined;
181
+ /**
182
+ * The response mode to use for the authorization code request
183
+ */
184
+ responseMode?: ("query" | "form_post") | undefined;
185
+ /**
186
+ * If enabled, the user info will be overridden with the provider user info
187
+ * This is useful if you want to use the provider user info to update the user info
188
+ *
189
+ * @default false
190
+ */
191
+ overrideUserInfoOnSignIn?: boolean | undefined;
192
+ };
193
+ //#endregion
194
+ export { OAuth2Tokens, OAuth2UserInfo, OAuthProvider, ProviderOptions };
@@ -0,0 +1,36 @@
1
+ import { OAuth2Tokens, ProviderOptions } from "./oauth-provider.mjs";
2
+
3
+ //#region src/oauth2/refresh-access-token.d.ts
4
+ declare function createRefreshAccessTokenRequest({
5
+ refreshToken,
6
+ options,
7
+ authentication,
8
+ extraParams,
9
+ resource
10
+ }: {
11
+ refreshToken: string;
12
+ options: Partial<ProviderOptions>;
13
+ authentication?: ("basic" | "post") | undefined;
14
+ extraParams?: Record<string, string> | undefined;
15
+ resource?: (string | string[]) | undefined;
16
+ }): {
17
+ body: URLSearchParams;
18
+ headers: Record<string, any>;
19
+ };
20
+ declare function refreshAccessToken({
21
+ refreshToken,
22
+ options,
23
+ tokenEndpoint,
24
+ authentication,
25
+ extraParams
26
+ }: {
27
+ refreshToken: string;
28
+ options: Partial<ProviderOptions>;
29
+ tokenEndpoint: string;
30
+ authentication?: ("basic" | "post") | undefined;
31
+ extraParams?: Record<string, string> | undefined;
32
+ /** @deprecated always "refresh_token" */
33
+ grantType?: string | undefined;
34
+ }): Promise<OAuth2Tokens>;
35
+ //#endregion
36
+ export { createRefreshAccessTokenRequest, refreshAccessToken };
@@ -0,0 +1,58 @@
1
+ import { base64 } from "@better-auth/utils/base64";
2
+ import { betterFetch } from "@better-fetch/fetch";
3
+
4
+ //#region src/oauth2/refresh-access-token.ts
5
+ function createRefreshAccessTokenRequest({ refreshToken, options, authentication, extraParams, resource }) {
6
+ const body = new URLSearchParams();
7
+ const headers = {
8
+ "content-type": "application/x-www-form-urlencoded",
9
+ accept: "application/json"
10
+ };
11
+ body.set("grant_type", "refresh_token");
12
+ body.set("refresh_token", refreshToken);
13
+ if (authentication === "basic") {
14
+ const primaryClientId = Array.isArray(options.clientId) ? options.clientId[0] : options.clientId;
15
+ if (primaryClientId) headers["authorization"] = "Basic " + base64.encode(`${primaryClientId}:${options.clientSecret ?? ""}`);
16
+ else headers["authorization"] = "Basic " + base64.encode(`:${options.clientSecret ?? ""}`);
17
+ } else {
18
+ const primaryClientId = Array.isArray(options.clientId) ? options.clientId[0] : options.clientId;
19
+ body.set("client_id", primaryClientId);
20
+ if (options.clientSecret) body.set("client_secret", options.clientSecret);
21
+ }
22
+ if (resource) if (typeof resource === "string") body.append("resource", resource);
23
+ else for (const _resource of resource) body.append("resource", _resource);
24
+ if (extraParams) for (const [key, value] of Object.entries(extraParams)) body.set(key, value);
25
+ return {
26
+ body,
27
+ headers
28
+ };
29
+ }
30
+ async function refreshAccessToken({ refreshToken, options, tokenEndpoint, authentication, extraParams }) {
31
+ const { body, headers } = createRefreshAccessTokenRequest({
32
+ refreshToken,
33
+ options,
34
+ authentication,
35
+ extraParams
36
+ });
37
+ const { data, error } = await betterFetch(tokenEndpoint, {
38
+ method: "POST",
39
+ body,
40
+ headers
41
+ });
42
+ if (error) throw error;
43
+ const tokens = {
44
+ accessToken: data.access_token,
45
+ refreshToken: data.refresh_token,
46
+ tokenType: data.token_type,
47
+ scopes: data.scope?.split(" "),
48
+ idToken: data.id_token
49
+ };
50
+ if (data.expires_in) {
51
+ const now = /* @__PURE__ */ new Date();
52
+ tokens.accessTokenExpiresAt = new Date(now.getTime() + data.expires_in * 1e3);
53
+ }
54
+ return tokens;
55
+ }
56
+
57
+ //#endregion
58
+ export { createRefreshAccessTokenRequest, refreshAccessToken };
@@ -0,0 +1,7 @@
1
+ import { OAuth2Tokens } from "./oauth-provider.mjs";
2
+
3
+ //#region src/oauth2/utils.d.ts
4
+ declare function getOAuth2Tokens(data: Record<string, any>): OAuth2Tokens;
5
+ declare function generateCodeChallenge(codeVerifier: string): Promise<string>;
6
+ //#endregion
7
+ export { generateCodeChallenge, getOAuth2Tokens };
@@ -0,0 +1,27 @@
1
+ import { base64Url } from "@better-auth/utils/base64";
2
+
3
+ //#region src/oauth2/utils.ts
4
+ function getOAuth2Tokens(data) {
5
+ const getDate = (seconds) => {
6
+ const now = /* @__PURE__ */ new Date();
7
+ return new Date(now.getTime() + seconds * 1e3);
8
+ };
9
+ return {
10
+ tokenType: data.token_type,
11
+ accessToken: data.access_token,
12
+ refreshToken: data.refresh_token,
13
+ accessTokenExpiresAt: data.expires_in ? getDate(data.expires_in) : void 0,
14
+ refreshTokenExpiresAt: data.refresh_token_expires_in ? getDate(data.refresh_token_expires_in) : void 0,
15
+ scopes: data?.scope ? typeof data.scope === "string" ? data.scope.split(" ") : data.scope : [],
16
+ idToken: data.id_token,
17
+ raw: data
18
+ };
19
+ }
20
+ async function generateCodeChallenge(codeVerifier) {
21
+ const data = new TextEncoder().encode(codeVerifier);
22
+ const hash = await crypto.subtle.digest("SHA-256", data);
23
+ return base64Url.encode(new Uint8Array(hash), { padding: false });
24
+ }
25
+
26
+ //#endregion
27
+ export { generateCodeChallenge, getOAuth2Tokens };
@@ -0,0 +1,55 @@
1
+ import { OAuth2Tokens, ProviderOptions } from "./oauth-provider.mjs";
2
+ import "./index.mjs";
3
+ import * as jose0 from "jose";
4
+
5
+ //#region src/oauth2/validate-authorization-code.d.ts
6
+ declare function createAuthorizationCodeRequest({
7
+ code,
8
+ codeVerifier,
9
+ redirectURI,
10
+ options,
11
+ authentication,
12
+ deviceId,
13
+ headers,
14
+ additionalParams,
15
+ resource
16
+ }: {
17
+ code: string;
18
+ redirectURI: string;
19
+ options: Partial<ProviderOptions>;
20
+ codeVerifier?: string | undefined;
21
+ deviceId?: string | undefined;
22
+ authentication?: ("basic" | "post") | undefined;
23
+ headers?: Record<string, string> | undefined;
24
+ additionalParams?: Record<string, string> | undefined;
25
+ resource?: (string | string[]) | undefined;
26
+ }): {
27
+ body: URLSearchParams;
28
+ headers: Record<string, any>;
29
+ };
30
+ declare function validateAuthorizationCode({
31
+ code,
32
+ codeVerifier,
33
+ redirectURI,
34
+ options,
35
+ tokenEndpoint,
36
+ authentication,
37
+ deviceId,
38
+ headers,
39
+ additionalParams,
40
+ resource
41
+ }: {
42
+ code: string;
43
+ redirectURI: string;
44
+ options: Partial<ProviderOptions>;
45
+ codeVerifier?: string | undefined;
46
+ deviceId?: string | undefined;
47
+ tokenEndpoint: string;
48
+ authentication?: ("basic" | "post") | undefined;
49
+ headers?: Record<string, string> | undefined;
50
+ additionalParams?: Record<string, string> | undefined;
51
+ resource?: (string | string[]) | undefined;
52
+ }): Promise<OAuth2Tokens>;
53
+ declare function validateToken(token: string, jwksEndpoint: string): Promise<jose0.JWTVerifyResult<jose0.JWTPayload>>;
54
+ //#endregion
55
+ export { createAuthorizationCodeRequest, validateAuthorizationCode, validateToken };
@@ -0,0 +1,71 @@
1
+ import { getOAuth2Tokens } from "./utils.mjs";
2
+ import "./index.mjs";
3
+ import { base64 } from "@better-auth/utils/base64";
4
+ import { betterFetch } from "@better-fetch/fetch";
5
+ import { jwtVerify } from "jose";
6
+
7
+ //#region src/oauth2/validate-authorization-code.ts
8
+ function createAuthorizationCodeRequest({ code, codeVerifier, redirectURI, options, authentication, deviceId, headers, additionalParams = {}, resource }) {
9
+ const body = new URLSearchParams();
10
+ const requestHeaders = {
11
+ "content-type": "application/x-www-form-urlencoded",
12
+ accept: "application/json",
13
+ ...headers
14
+ };
15
+ body.set("grant_type", "authorization_code");
16
+ body.set("code", code);
17
+ codeVerifier && body.set("code_verifier", codeVerifier);
18
+ options.clientKey && body.set("client_key", options.clientKey);
19
+ deviceId && body.set("device_id", deviceId);
20
+ body.set("redirect_uri", options.redirectURI || redirectURI);
21
+ if (resource) if (typeof resource === "string") body.append("resource", resource);
22
+ else for (const _resource of resource) body.append("resource", _resource);
23
+ if (authentication === "basic") {
24
+ const primaryClientId = Array.isArray(options.clientId) ? options.clientId[0] : options.clientId;
25
+ requestHeaders["authorization"] = `Basic ${base64.encode(`${primaryClientId}:${options.clientSecret ?? ""}`)}`;
26
+ } else {
27
+ const primaryClientId = Array.isArray(options.clientId) ? options.clientId[0] : options.clientId;
28
+ body.set("client_id", primaryClientId);
29
+ if (options.clientSecret) body.set("client_secret", options.clientSecret);
30
+ }
31
+ for (const [key, value] of Object.entries(additionalParams)) if (!body.has(key)) body.append(key, value);
32
+ return {
33
+ body,
34
+ headers: requestHeaders
35
+ };
36
+ }
37
+ async function validateAuthorizationCode({ code, codeVerifier, redirectURI, options, tokenEndpoint, authentication, deviceId, headers, additionalParams = {}, resource }) {
38
+ const { body, headers: requestHeaders } = createAuthorizationCodeRequest({
39
+ code,
40
+ codeVerifier,
41
+ redirectURI,
42
+ options,
43
+ authentication,
44
+ deviceId,
45
+ headers,
46
+ additionalParams,
47
+ resource
48
+ });
49
+ const { data, error } = await betterFetch(tokenEndpoint, {
50
+ method: "POST",
51
+ body,
52
+ headers: requestHeaders
53
+ });
54
+ if (error) throw error;
55
+ return getOAuth2Tokens(data);
56
+ }
57
+ async function validateToken(token, jwksEndpoint) {
58
+ const { data, error } = await betterFetch(jwksEndpoint, {
59
+ method: "GET",
60
+ headers: { accept: "application/json" }
61
+ });
62
+ if (error) throw error;
63
+ const keys = data["keys"];
64
+ const header = JSON.parse(atob(token.split(".")[0]));
65
+ const key = keys.find((key$1) => key$1.kid === header.kid);
66
+ if (!key) throw new Error("Key not found");
67
+ return await jwtVerify(token, key);
68
+ }
69
+
70
+ //#endregion
71
+ export { createAuthorizationCodeRequest, validateAuthorizationCode, validateToken };