@better-auth/core 1.5.0-beta.4 → 1.5.0-beta.6

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 (178) hide show
  1. package/.turbo/turbo-build.log +170 -37
  2. package/dist/api/index.d.mts +188 -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/index.d.mts +3 -52
  7. package/dist/context/index.mjs +3 -1
  8. package/dist/context/request-state.d.mts +27 -0
  9. package/dist/context/request-state.mjs +49 -0
  10. package/dist/context/transaction.d.mts +16 -0
  11. package/dist/context/transaction.mjs +52 -0
  12. package/dist/db/adapter/factory.d.mts +27 -0
  13. package/dist/db/adapter/factory.mjs +738 -0
  14. package/dist/db/adapter/get-default-field-name.d.mts +18 -0
  15. package/dist/db/adapter/get-default-field-name.mjs +38 -0
  16. package/dist/db/adapter/get-default-model-name.d.mts +12 -0
  17. package/dist/db/adapter/get-default-model-name.mjs +32 -0
  18. package/dist/db/adapter/get-field-attributes.d.mts +29 -0
  19. package/dist/db/adapter/get-field-attributes.mjs +39 -0
  20. package/dist/db/adapter/get-field-name.d.mts +18 -0
  21. package/dist/db/adapter/get-field-name.mjs +33 -0
  22. package/dist/db/adapter/get-id-field.d.mts +39 -0
  23. package/dist/db/adapter/get-id-field.mjs +67 -0
  24. package/dist/db/adapter/get-model-name.d.mts +12 -0
  25. package/dist/db/adapter/get-model-name.mjs +23 -0
  26. package/dist/db/adapter/index.d.mts +513 -1
  27. package/dist/db/adapter/index.mjs +8 -970
  28. package/dist/db/adapter/types.d.mts +139 -0
  29. package/dist/db/adapter/utils.d.mts +7 -0
  30. package/dist/db/adapter/utils.mjs +38 -0
  31. package/dist/db/get-tables.d.mts +8 -0
  32. package/dist/{get-tables-CMc_Emww.mjs → db/get-tables.mjs} +1 -1
  33. package/dist/db/index.d.mts +10 -2
  34. package/dist/db/index.mjs +7 -60
  35. package/dist/db/plugin.d.mts +12 -0
  36. package/dist/db/schema/account.d.mts +26 -0
  37. package/dist/db/schema/account.mjs +19 -0
  38. package/dist/db/schema/rate-limit.d.mts +14 -0
  39. package/dist/db/schema/rate-limit.mjs +11 -0
  40. package/dist/db/schema/session.d.mts +21 -0
  41. package/dist/db/schema/session.mjs +14 -0
  42. package/dist/db/schema/shared.d.mts +10 -0
  43. package/dist/db/schema/shared.mjs +11 -0
  44. package/dist/db/schema/user.d.mts +20 -0
  45. package/dist/db/schema/user.mjs +13 -0
  46. package/dist/db/schema/verification.d.mts +19 -0
  47. package/dist/db/schema/verification.mjs +12 -0
  48. package/dist/db/type.d.mts +143 -0
  49. package/dist/env/color-depth.d.mts +4 -0
  50. package/dist/env/color-depth.mjs +88 -0
  51. package/dist/env/env-impl.d.mts +32 -0
  52. package/dist/env/env-impl.mjs +82 -0
  53. package/dist/env/index.d.mts +4 -2
  54. package/dist/env/index.mjs +3 -1
  55. package/dist/{index-BRBu0-5h.d.mts → env/logger.d.mts} +1 -35
  56. package/dist/env/logger.mjs +81 -0
  57. package/dist/error/codes.d.mts +186 -0
  58. package/dist/{error-GNtLPYaS.mjs → error/codes.mjs} +2 -29
  59. package/dist/error/index.d.mts +1 -185
  60. package/dist/error/index.mjs +28 -3
  61. package/dist/index.d.mts +7 -1
  62. package/dist/oauth2/client-credentials-token.d.mts +36 -0
  63. package/dist/oauth2/client-credentials-token.mjs +54 -0
  64. package/dist/oauth2/create-authorization-url.d.mts +45 -0
  65. package/dist/oauth2/create-authorization-url.mjs +42 -0
  66. package/dist/oauth2/index.d.mts +8 -2
  67. package/dist/oauth2/index.mjs +6 -2
  68. package/dist/oauth2/oauth-provider.d.mts +194 -0
  69. package/dist/oauth2/refresh-access-token.d.mts +36 -0
  70. package/dist/oauth2/refresh-access-token.mjs +58 -0
  71. package/dist/oauth2/utils.d.mts +7 -0
  72. package/dist/oauth2/utils.mjs +27 -0
  73. package/dist/oauth2/validate-authorization-code.d.mts +55 -0
  74. package/dist/oauth2/validate-authorization-code.mjs +71 -0
  75. package/dist/oauth2/verify.d.mts +49 -0
  76. package/dist/oauth2/verify.mjs +95 -0
  77. package/dist/social-providers/apple.d.mts +119 -0
  78. package/dist/social-providers/apple.mjs +102 -0
  79. package/dist/social-providers/atlassian.d.mts +72 -0
  80. package/dist/social-providers/atlassian.mjs +83 -0
  81. package/dist/social-providers/cognito.d.mts +87 -0
  82. package/dist/social-providers/cognito.mjs +165 -0
  83. package/dist/social-providers/discord.d.mts +126 -0
  84. package/dist/social-providers/discord.mjs +64 -0
  85. package/dist/social-providers/dropbox.d.mts +71 -0
  86. package/dist/social-providers/dropbox.mjs +75 -0
  87. package/dist/social-providers/facebook.d.mts +81 -0
  88. package/dist/social-providers/facebook.mjs +120 -0
  89. package/dist/social-providers/figma.d.mts +63 -0
  90. package/dist/social-providers/figma.mjs +84 -0
  91. package/dist/social-providers/github.d.mts +104 -0
  92. package/dist/social-providers/github.mjs +80 -0
  93. package/dist/social-providers/gitlab.d.mts +125 -0
  94. package/dist/social-providers/gitlab.mjs +82 -0
  95. package/dist/social-providers/google.d.mts +99 -0
  96. package/dist/social-providers/google.mjs +108 -0
  97. package/dist/social-providers/huggingface.d.mts +85 -0
  98. package/dist/social-providers/huggingface.mjs +75 -0
  99. package/dist/social-providers/index.d.mts +1723 -1
  100. package/dist/social-providers/index.mjs +33 -2569
  101. package/dist/social-providers/kakao.d.mts +163 -0
  102. package/dist/social-providers/kakao.mjs +72 -0
  103. package/dist/social-providers/kick.d.mts +75 -0
  104. package/dist/social-providers/kick.mjs +71 -0
  105. package/dist/social-providers/line.d.mts +107 -0
  106. package/dist/social-providers/line.mjs +113 -0
  107. package/dist/social-providers/linear.d.mts +70 -0
  108. package/dist/social-providers/linear.mjs +88 -0
  109. package/dist/social-providers/linkedin.d.mts +69 -0
  110. package/dist/social-providers/linkedin.mjs +76 -0
  111. package/dist/social-providers/microsoft-entra-id.d.mts +174 -0
  112. package/dist/social-providers/microsoft-entra-id.mjs +106 -0
  113. package/dist/social-providers/naver.d.mts +104 -0
  114. package/dist/social-providers/naver.mjs +67 -0
  115. package/dist/social-providers/notion.d.mts +66 -0
  116. package/dist/social-providers/notion.mjs +75 -0
  117. package/dist/social-providers/paybin.d.mts +73 -0
  118. package/dist/social-providers/paybin.mjs +85 -0
  119. package/dist/social-providers/paypal.d.mts +131 -0
  120. package/dist/social-providers/paypal.mjs +144 -0
  121. package/dist/social-providers/polar.d.mts +76 -0
  122. package/dist/social-providers/polar.mjs +73 -0
  123. package/dist/social-providers/reddit.d.mts +64 -0
  124. package/dist/social-providers/reddit.mjs +83 -0
  125. package/dist/social-providers/roblox.d.mts +72 -0
  126. package/dist/social-providers/roblox.mjs +59 -0
  127. package/dist/social-providers/salesforce.d.mts +81 -0
  128. package/dist/social-providers/salesforce.mjs +91 -0
  129. package/dist/social-providers/slack.d.mts +85 -0
  130. package/dist/social-providers/slack.mjs +68 -0
  131. package/dist/social-providers/spotify.d.mts +65 -0
  132. package/dist/social-providers/spotify.mjs +71 -0
  133. package/dist/social-providers/tiktok.d.mts +171 -0
  134. package/dist/social-providers/tiktok.mjs +62 -0
  135. package/dist/social-providers/twitch.d.mts +81 -0
  136. package/dist/social-providers/twitch.mjs +78 -0
  137. package/dist/social-providers/twitter.d.mts +140 -0
  138. package/dist/social-providers/twitter.mjs +87 -0
  139. package/dist/social-providers/vercel.d.mts +64 -0
  140. package/dist/social-providers/vercel.mjs +61 -0
  141. package/dist/social-providers/vk.d.mts +72 -0
  142. package/dist/social-providers/vk.mjs +83 -0
  143. package/dist/social-providers/zoom.d.mts +173 -0
  144. package/dist/social-providers/zoom.mjs +72 -0
  145. package/dist/types/context.d.mts +246 -0
  146. package/dist/types/cookie.d.mts +23 -0
  147. package/dist/types/helper.d.mts +8 -0
  148. package/dist/types/index.d.mts +8 -0
  149. package/dist/types/init-options.d.mts +1266 -0
  150. package/dist/types/plugin-client.d.mts +110 -0
  151. package/dist/types/plugin.d.mts +124 -0
  152. package/dist/utils/deprecate.d.mts +10 -0
  153. package/dist/utils/deprecate.mjs +17 -0
  154. package/dist/utils/{index.d.mts → error-codes.d.mts} +1 -19
  155. package/dist/utils/error-codes.mjs +11 -0
  156. package/dist/utils/id.d.mts +4 -0
  157. package/dist/utils/id.mjs +9 -0
  158. package/dist/utils/json.d.mts +4 -0
  159. package/dist/utils/json.mjs +25 -0
  160. package/dist/utils/string.d.mts +4 -0
  161. package/dist/utils/string.mjs +7 -0
  162. package/package.json +9 -6
  163. package/src/context/endpoint-context.ts +11 -2
  164. package/src/context/index.ts +0 -29
  165. package/src/context/request-state.ts +8 -2
  166. package/src/context/transaction.ts +11 -2
  167. package/src/db/adapter/get-id-field.ts +1 -1
  168. package/src/error/codes.ts +1 -1
  169. package/src/oauth2/create-authorization-url.ts +1 -1
  170. package/src/oauth2/oauth-provider.ts +6 -0
  171. package/tsdown.config.ts +3 -1
  172. package/dist/context-BBNwughv.mjs +0 -133
  173. package/dist/env-DbssmzoK.mjs +0 -245
  174. package/dist/index-B5x_W0dM.d.mts +0 -8054
  175. package/dist/oauth2-BjWM15hm.mjs +0 -326
  176. package/dist/utils/index.mjs +0 -4
  177. package/dist/utils-puAL36Bz.mjs +0 -63
  178. package/src/utils/index.ts +0 -5
@@ -0,0 +1,83 @@
1
+ import { logger } from "../env/logger.mjs";
2
+ import "../env/index.mjs";
3
+ import { BetterAuthError } from "../error/index.mjs";
4
+ import { createAuthorizationURL } from "../oauth2/create-authorization-url.mjs";
5
+ import { refreshAccessToken } from "../oauth2/refresh-access-token.mjs";
6
+ import { validateAuthorizationCode } from "../oauth2/validate-authorization-code.mjs";
7
+ import "../oauth2/index.mjs";
8
+ import { betterFetch } from "@better-fetch/fetch";
9
+
10
+ //#region src/social-providers/atlassian.ts
11
+ const atlassian = (options) => {
12
+ return {
13
+ id: "atlassian",
14
+ name: "Atlassian",
15
+ async createAuthorizationURL({ state, scopes, codeVerifier, redirectURI }) {
16
+ if (!options.clientId || !options.clientSecret) {
17
+ logger.error("Client Id and Secret are required for Atlassian");
18
+ throw new BetterAuthError("CLIENT_ID_AND_SECRET_REQUIRED");
19
+ }
20
+ if (!codeVerifier) throw new BetterAuthError("codeVerifier is required for Atlassian");
21
+ const _scopes = options.disableDefaultScope ? [] : ["read:jira-user", "offline_access"];
22
+ if (options.scope) _scopes.push(...options.scope);
23
+ if (scopes) _scopes.push(...scopes);
24
+ return createAuthorizationURL({
25
+ id: "atlassian",
26
+ options,
27
+ authorizationEndpoint: "https://auth.atlassian.com/authorize",
28
+ scopes: _scopes,
29
+ state,
30
+ codeVerifier,
31
+ redirectURI,
32
+ additionalParams: { audience: "api.atlassian.com" },
33
+ prompt: options.prompt
34
+ });
35
+ },
36
+ validateAuthorizationCode: async ({ code, codeVerifier, redirectURI }) => {
37
+ return validateAuthorizationCode({
38
+ code,
39
+ codeVerifier,
40
+ redirectURI,
41
+ options,
42
+ tokenEndpoint: "https://auth.atlassian.com/oauth/token"
43
+ });
44
+ },
45
+ refreshAccessToken: options.refreshAccessToken ? options.refreshAccessToken : async (refreshToken) => {
46
+ return refreshAccessToken({
47
+ refreshToken,
48
+ options: {
49
+ clientId: options.clientId,
50
+ clientSecret: options.clientSecret
51
+ },
52
+ tokenEndpoint: "https://auth.atlassian.com/oauth/token"
53
+ });
54
+ },
55
+ async getUserInfo(token) {
56
+ if (options.getUserInfo) return options.getUserInfo(token);
57
+ if (!token.accessToken) return null;
58
+ try {
59
+ const { data: profile } = await betterFetch("https://api.atlassian.com/me", { headers: { Authorization: `Bearer ${token.accessToken}` } });
60
+ if (!profile) return null;
61
+ const userMap = await options.mapProfileToUser?.(profile);
62
+ return {
63
+ user: {
64
+ id: profile.account_id,
65
+ name: profile.name,
66
+ email: profile.email,
67
+ image: profile.picture,
68
+ emailVerified: false,
69
+ ...userMap
70
+ },
71
+ data: profile
72
+ };
73
+ } catch (error) {
74
+ logger.error("Failed to fetch user info from Figma:", error);
75
+ return null;
76
+ }
77
+ },
78
+ options
79
+ };
80
+ };
81
+
82
+ //#endregion
83
+ export { atlassian };
@@ -0,0 +1,87 @@
1
+ import { OAuth2Tokens, ProviderOptions } from "../oauth2/oauth-provider.mjs";
2
+ import "../oauth2/index.mjs";
3
+
4
+ //#region src/social-providers/cognito.d.ts
5
+ interface CognitoProfile {
6
+ sub: string;
7
+ email: string;
8
+ email_verified: boolean;
9
+ name: string;
10
+ given_name?: string | undefined;
11
+ family_name?: string | undefined;
12
+ picture?: string | undefined;
13
+ username?: string | undefined;
14
+ locale?: string | undefined;
15
+ phone_number?: string | undefined;
16
+ phone_number_verified?: boolean | undefined;
17
+ aud: string;
18
+ iss: string;
19
+ exp: number;
20
+ iat: number;
21
+ [key: string]: any;
22
+ }
23
+ interface CognitoOptions extends ProviderOptions<CognitoProfile> {
24
+ clientId: string;
25
+ /**
26
+ * The Cognito domain (e.g., "your-app.auth.us-east-1.amazoncognito.com")
27
+ */
28
+ domain: string;
29
+ /**
30
+ * AWS region where User Pool is hosted (e.g., "us-east-1")
31
+ */
32
+ region: string;
33
+ userPoolId: string;
34
+ requireClientSecret?: boolean | undefined;
35
+ }
36
+ declare const cognito: (options: CognitoOptions) => {
37
+ id: "cognito";
38
+ name: string;
39
+ createAuthorizationURL({
40
+ state,
41
+ scopes,
42
+ codeVerifier,
43
+ redirectURI
44
+ }: {
45
+ state: string;
46
+ codeVerifier: string;
47
+ scopes?: string[] | undefined;
48
+ redirectURI: string;
49
+ display?: string | undefined;
50
+ loginHint?: string | undefined;
51
+ }): Promise<URL>;
52
+ validateAuthorizationCode: ({
53
+ code,
54
+ codeVerifier,
55
+ redirectURI
56
+ }: {
57
+ code: string;
58
+ redirectURI: string;
59
+ codeVerifier?: string | undefined;
60
+ deviceId?: string | undefined;
61
+ }) => Promise<OAuth2Tokens>;
62
+ refreshAccessToken: (refreshToken: string) => Promise<OAuth2Tokens>;
63
+ verifyIdToken(token: string, nonce: string | undefined): Promise<boolean>;
64
+ getUserInfo(token: OAuth2Tokens & {
65
+ user?: {
66
+ name?: {
67
+ firstName?: string;
68
+ lastName?: string;
69
+ };
70
+ email?: string;
71
+ } | undefined;
72
+ }): Promise<{
73
+ user: {
74
+ id: string;
75
+ name?: string;
76
+ email?: string | null;
77
+ image?: string;
78
+ emailVerified: boolean;
79
+ [key: string]: any;
80
+ };
81
+ data: any;
82
+ } | null>;
83
+ options: CognitoOptions;
84
+ };
85
+ declare const getCognitoPublicKey: (kid: string, region: string, userPoolId: string) => Promise<Uint8Array<ArrayBufferLike> | CryptoKey>;
86
+ //#endregion
87
+ export { CognitoOptions, CognitoProfile, cognito, getCognitoPublicKey };
@@ -0,0 +1,165 @@
1
+ import { logger } from "../env/logger.mjs";
2
+ import "../env/index.mjs";
3
+ import { APIError, BetterAuthError } from "../error/index.mjs";
4
+ import { createAuthorizationURL } from "../oauth2/create-authorization-url.mjs";
5
+ import { refreshAccessToken } from "../oauth2/refresh-access-token.mjs";
6
+ import { validateAuthorizationCode } from "../oauth2/validate-authorization-code.mjs";
7
+ import "../oauth2/index.mjs";
8
+ import { betterFetch } from "@better-fetch/fetch";
9
+ import { decodeJwt, decodeProtectedHeader, importJWK, jwtVerify } from "jose";
10
+
11
+ //#region src/social-providers/cognito.ts
12
+ const cognito = (options) => {
13
+ if (!options.domain || !options.region || !options.userPoolId) {
14
+ logger.error("Domain, region and userPoolId are required for Amazon Cognito. Make sure to provide them in the options.");
15
+ throw new BetterAuthError("DOMAIN_AND_REGION_REQUIRED");
16
+ }
17
+ const cleanDomain = options.domain.replace(/^https?:\/\//, "");
18
+ const authorizationEndpoint = `https://${cleanDomain}/oauth2/authorize`;
19
+ const tokenEndpoint = `https://${cleanDomain}/oauth2/token`;
20
+ const userInfoEndpoint = `https://${cleanDomain}/oauth2/userinfo`;
21
+ return {
22
+ id: "cognito",
23
+ name: "Cognito",
24
+ async createAuthorizationURL({ state, scopes, codeVerifier, redirectURI }) {
25
+ if (!options.clientId) {
26
+ logger.error("ClientId is required for Amazon Cognito. Make sure to provide them in the options.");
27
+ throw new BetterAuthError("CLIENT_ID_AND_SECRET_REQUIRED");
28
+ }
29
+ if (options.requireClientSecret && !options.clientSecret) {
30
+ logger.error("Client Secret is required when requireClientSecret is true. Make sure to provide it in the options.");
31
+ throw new BetterAuthError("CLIENT_SECRET_REQUIRED");
32
+ }
33
+ const _scopes = options.disableDefaultScope ? [] : [
34
+ "openid",
35
+ "profile",
36
+ "email"
37
+ ];
38
+ if (options.scope) _scopes.push(...options.scope);
39
+ if (scopes) _scopes.push(...scopes);
40
+ const url = await createAuthorizationURL({
41
+ id: "cognito",
42
+ options: { ...options },
43
+ authorizationEndpoint,
44
+ scopes: _scopes,
45
+ state,
46
+ codeVerifier,
47
+ redirectURI,
48
+ prompt: options.prompt
49
+ });
50
+ const scopeValue = url.searchParams.get("scope");
51
+ if (scopeValue) {
52
+ url.searchParams.delete("scope");
53
+ const encodedScope = encodeURIComponent(scopeValue);
54
+ const urlString = url.toString();
55
+ const separator = urlString.includes("?") ? "&" : "?";
56
+ return new URL(`${urlString}${separator}scope=${encodedScope}`);
57
+ }
58
+ return url;
59
+ },
60
+ validateAuthorizationCode: async ({ code, codeVerifier, redirectURI }) => {
61
+ return validateAuthorizationCode({
62
+ code,
63
+ codeVerifier,
64
+ redirectURI,
65
+ options,
66
+ tokenEndpoint
67
+ });
68
+ },
69
+ refreshAccessToken: options.refreshAccessToken ? options.refreshAccessToken : async (refreshToken) => {
70
+ return refreshAccessToken({
71
+ refreshToken,
72
+ options: {
73
+ clientId: options.clientId,
74
+ clientKey: options.clientKey,
75
+ clientSecret: options.clientSecret
76
+ },
77
+ tokenEndpoint
78
+ });
79
+ },
80
+ async verifyIdToken(token, nonce) {
81
+ if (options.disableIdTokenSignIn) return false;
82
+ if (options.verifyIdToken) return options.verifyIdToken(token, nonce);
83
+ try {
84
+ const { kid, alg: jwtAlg } = decodeProtectedHeader(token);
85
+ if (!kid || !jwtAlg) return false;
86
+ const publicKey = await getCognitoPublicKey(kid, options.region, options.userPoolId);
87
+ const expectedIssuer = `https://cognito-idp.${options.region}.amazonaws.com/${options.userPoolId}`;
88
+ const { payload: jwtClaims } = await jwtVerify(token, publicKey, {
89
+ algorithms: [jwtAlg],
90
+ issuer: expectedIssuer,
91
+ audience: options.clientId,
92
+ maxTokenAge: "1h"
93
+ });
94
+ if (nonce && jwtClaims.nonce !== nonce) return false;
95
+ return true;
96
+ } catch (error) {
97
+ logger.error("Failed to verify ID token:", error);
98
+ return false;
99
+ }
100
+ },
101
+ async getUserInfo(token) {
102
+ if (options.getUserInfo) return options.getUserInfo(token);
103
+ if (token.idToken) try {
104
+ const profile = decodeJwt(token.idToken);
105
+ if (!profile) return null;
106
+ const name = profile.name || profile.given_name || profile.username || profile.email;
107
+ const enrichedProfile = {
108
+ ...profile,
109
+ name
110
+ };
111
+ const userMap = await options.mapProfileToUser?.(enrichedProfile);
112
+ return {
113
+ user: {
114
+ id: profile.sub,
115
+ name: enrichedProfile.name,
116
+ email: profile.email,
117
+ image: profile.picture,
118
+ emailVerified: profile.email_verified,
119
+ ...userMap
120
+ },
121
+ data: enrichedProfile
122
+ };
123
+ } catch (error) {
124
+ logger.error("Failed to decode ID token:", error);
125
+ }
126
+ if (token.accessToken) try {
127
+ const { data: userInfo } = await betterFetch(userInfoEndpoint, { headers: { Authorization: `Bearer ${token.accessToken}` } });
128
+ if (userInfo) {
129
+ const userMap = await options.mapProfileToUser?.(userInfo);
130
+ return {
131
+ user: {
132
+ id: userInfo.sub,
133
+ name: userInfo.name || userInfo.given_name || userInfo.username,
134
+ email: userInfo.email,
135
+ image: userInfo.picture,
136
+ emailVerified: userInfo.email_verified,
137
+ ...userMap
138
+ },
139
+ data: userInfo
140
+ };
141
+ }
142
+ } catch (error) {
143
+ logger.error("Failed to fetch user info from Cognito:", error);
144
+ }
145
+ return null;
146
+ },
147
+ options
148
+ };
149
+ };
150
+ const getCognitoPublicKey = async (kid, region, userPoolId) => {
151
+ const COGNITO_JWKS_URI = `https://cognito-idp.${region}.amazonaws.com/${userPoolId}/.well-known/jwks.json`;
152
+ try {
153
+ const { data } = await betterFetch(COGNITO_JWKS_URI);
154
+ if (!data?.keys) throw new APIError("BAD_REQUEST", { message: "Keys not found" });
155
+ const jwk = data.keys.find((key) => key.kid === kid);
156
+ if (!jwk) throw new Error(`JWK with kid ${kid} not found`);
157
+ return await importJWK(jwk, jwk.alg);
158
+ } catch (error) {
159
+ logger.error("Failed to fetch Cognito public key:", error);
160
+ throw error;
161
+ }
162
+ };
163
+
164
+ //#endregion
165
+ export { cognito, getCognitoPublicKey };
@@ -0,0 +1,126 @@
1
+ import { OAuth2Tokens, ProviderOptions } from "../oauth2/oauth-provider.mjs";
2
+ import "../oauth2/index.mjs";
3
+
4
+ //#region src/social-providers/discord.d.ts
5
+ interface DiscordProfile extends Record<string, any> {
6
+ /** the user's id (i.e. the numerical snowflake) */
7
+ id: string;
8
+ /** the user's username, not unique across the platform */
9
+ username: string;
10
+ /** the user's Discord-tag */
11
+ discriminator: string;
12
+ /** the user's display name, if it is set */
13
+ global_name: string | null;
14
+ /**
15
+ * the user's avatar hash:
16
+ * https://discord.com/developers/docs/reference#image-formatting
17
+ */
18
+ avatar: string | null;
19
+ /** whether the user belongs to an OAuth2 application */
20
+ bot?: boolean | undefined;
21
+ /**
22
+ * whether the user is an Official Discord System user (part of the urgent
23
+ * message system)
24
+ */
25
+ system?: boolean | undefined;
26
+ /** whether the user has two factor enabled on their account */
27
+ mfa_enabled: boolean;
28
+ /**
29
+ * the user's banner hash:
30
+ * https://discord.com/developers/docs/reference#image-formatting
31
+ */
32
+ banner: string | null;
33
+ /** the user's banner color encoded as an integer representation of hexadecimal color code */
34
+ accent_color: number | null;
35
+ /**
36
+ * the user's chosen language option:
37
+ * https://discord.com/developers/docs/reference#locales
38
+ */
39
+ locale: string;
40
+ /** whether the email on this account has been verified */
41
+ verified: boolean;
42
+ /** the user's email */
43
+ email: string;
44
+ /**
45
+ * the flags on a user's account:
46
+ * https://discord.com/developers/docs/resources/user#user-object-user-flags
47
+ */
48
+ flags: number;
49
+ /**
50
+ * the type of Nitro subscription on a user's account:
51
+ * https://discord.com/developers/docs/resources/user#user-object-premium-types
52
+ */
53
+ premium_type: number;
54
+ /**
55
+ * the public flags on a user's account:
56
+ * https://discord.com/developers/docs/resources/user#user-object-user-flags
57
+ */
58
+ public_flags: number;
59
+ /** undocumented field; corresponds to the user's custom nickname */
60
+ display_name: string | null;
61
+ /**
62
+ * undocumented field; corresponds to the Discord feature where you can e.g.
63
+ * put your avatar inside of an ice cube
64
+ */
65
+ avatar_decoration: string | null;
66
+ /**
67
+ * undocumented field; corresponds to the premium feature where you can
68
+ * select a custom banner color
69
+ */
70
+ banner_color: string | null;
71
+ /** undocumented field; the CDN URL of their profile picture */
72
+ image_url: string;
73
+ }
74
+ interface DiscordOptions extends ProviderOptions<DiscordProfile> {
75
+ clientId: string;
76
+ prompt?: ("none" | "consent") | undefined;
77
+ permissions?: number | undefined;
78
+ }
79
+ declare const discord: (options: DiscordOptions) => {
80
+ id: "discord";
81
+ name: string;
82
+ createAuthorizationURL({
83
+ state,
84
+ scopes,
85
+ redirectURI
86
+ }: {
87
+ state: string;
88
+ codeVerifier: string;
89
+ scopes?: string[] | undefined;
90
+ redirectURI: string;
91
+ display?: string | undefined;
92
+ loginHint?: string | undefined;
93
+ }): URL;
94
+ validateAuthorizationCode: ({
95
+ code,
96
+ redirectURI
97
+ }: {
98
+ code: string;
99
+ redirectURI: string;
100
+ codeVerifier?: string | undefined;
101
+ deviceId?: string | undefined;
102
+ }) => Promise<OAuth2Tokens>;
103
+ refreshAccessToken: (refreshToken: string) => Promise<OAuth2Tokens>;
104
+ getUserInfo(token: OAuth2Tokens & {
105
+ user?: {
106
+ name?: {
107
+ firstName?: string;
108
+ lastName?: string;
109
+ };
110
+ email?: string;
111
+ } | undefined;
112
+ }): Promise<{
113
+ user: {
114
+ id: string;
115
+ name?: string;
116
+ email?: string | null;
117
+ image?: string;
118
+ emailVerified: boolean;
119
+ [key: string]: any;
120
+ };
121
+ data: any;
122
+ } | null>;
123
+ options: DiscordOptions;
124
+ };
125
+ //#endregion
126
+ export { DiscordOptions, DiscordProfile, discord };
@@ -0,0 +1,64 @@
1
+ import { refreshAccessToken } from "../oauth2/refresh-access-token.mjs";
2
+ import { validateAuthorizationCode } from "../oauth2/validate-authorization-code.mjs";
3
+ import "../oauth2/index.mjs";
4
+ import { betterFetch } from "@better-fetch/fetch";
5
+
6
+ //#region src/social-providers/discord.ts
7
+ const discord = (options) => {
8
+ return {
9
+ id: "discord",
10
+ name: "Discord",
11
+ createAuthorizationURL({ state, scopes, redirectURI }) {
12
+ const _scopes = options.disableDefaultScope ? [] : ["identify", "email"];
13
+ if (scopes) _scopes.push(...scopes);
14
+ if (options.scope) _scopes.push(...options.scope);
15
+ const permissionsParam = _scopes.includes("bot") && options.permissions !== void 0 ? `&permissions=${options.permissions}` : "";
16
+ return new URL(`https://discord.com/api/oauth2/authorize?scope=${_scopes.join("+")}&response_type=code&client_id=${options.clientId}&redirect_uri=${encodeURIComponent(options.redirectURI || redirectURI)}&state=${state}&prompt=${options.prompt || "none"}${permissionsParam}`);
17
+ },
18
+ validateAuthorizationCode: async ({ code, redirectURI }) => {
19
+ return validateAuthorizationCode({
20
+ code,
21
+ redirectURI,
22
+ options,
23
+ tokenEndpoint: "https://discord.com/api/oauth2/token"
24
+ });
25
+ },
26
+ refreshAccessToken: options.refreshAccessToken ? options.refreshAccessToken : async (refreshToken) => {
27
+ return refreshAccessToken({
28
+ refreshToken,
29
+ options: {
30
+ clientId: options.clientId,
31
+ clientKey: options.clientKey,
32
+ clientSecret: options.clientSecret
33
+ },
34
+ tokenEndpoint: "https://discord.com/api/oauth2/token"
35
+ });
36
+ },
37
+ async getUserInfo(token) {
38
+ if (options.getUserInfo) return options.getUserInfo(token);
39
+ const { data: profile, error } = await betterFetch("https://discord.com/api/users/@me", { headers: { authorization: `Bearer ${token.accessToken}` } });
40
+ if (error) return null;
41
+ if (profile.avatar === null) profile.image_url = `https://cdn.discordapp.com/embed/avatars/${profile.discriminator === "0" ? Number(BigInt(profile.id) >> BigInt(22)) % 6 : parseInt(profile.discriminator) % 5}.png`;
42
+ else {
43
+ const format = profile.avatar.startsWith("a_") ? "gif" : "png";
44
+ profile.image_url = `https://cdn.discordapp.com/avatars/${profile.id}/${profile.avatar}.${format}`;
45
+ }
46
+ const userMap = await options.mapProfileToUser?.(profile);
47
+ return {
48
+ user: {
49
+ id: profile.id,
50
+ name: profile.global_name || profile.username || "",
51
+ email: profile.email,
52
+ emailVerified: profile.verified,
53
+ image: profile.image_url,
54
+ ...userMap
55
+ },
56
+ data: profile
57
+ };
58
+ },
59
+ options
60
+ };
61
+ };
62
+
63
+ //#endregion
64
+ export { discord };
@@ -0,0 +1,71 @@
1
+ import { OAuth2Tokens, ProviderOptions } from "../oauth2/oauth-provider.mjs";
2
+ import "../oauth2/index.mjs";
3
+
4
+ //#region src/social-providers/dropbox.d.ts
5
+ interface DropboxProfile {
6
+ account_id: string;
7
+ name: {
8
+ given_name: string;
9
+ surname: string;
10
+ familiar_name: string;
11
+ display_name: string;
12
+ abbreviated_name: string;
13
+ };
14
+ email: string;
15
+ email_verified: boolean;
16
+ profile_photo_url: string;
17
+ }
18
+ interface DropboxOptions extends ProviderOptions<DropboxProfile> {
19
+ clientId: string;
20
+ accessType?: ("offline" | "online" | "legacy") | undefined;
21
+ }
22
+ declare const dropbox: (options: DropboxOptions) => {
23
+ id: "dropbox";
24
+ name: string;
25
+ createAuthorizationURL: ({
26
+ state,
27
+ scopes,
28
+ codeVerifier,
29
+ redirectURI
30
+ }: {
31
+ state: string;
32
+ codeVerifier: string;
33
+ scopes?: string[] | undefined;
34
+ redirectURI: string;
35
+ display?: string | undefined;
36
+ loginHint?: string | undefined;
37
+ }) => Promise<URL>;
38
+ validateAuthorizationCode: ({
39
+ code,
40
+ codeVerifier,
41
+ redirectURI
42
+ }: {
43
+ code: string;
44
+ redirectURI: string;
45
+ codeVerifier?: string | undefined;
46
+ deviceId?: string | undefined;
47
+ }) => Promise<OAuth2Tokens>;
48
+ refreshAccessToken: (refreshToken: string) => Promise<OAuth2Tokens>;
49
+ getUserInfo(token: OAuth2Tokens & {
50
+ user?: {
51
+ name?: {
52
+ firstName?: string;
53
+ lastName?: string;
54
+ };
55
+ email?: string;
56
+ } | undefined;
57
+ }): Promise<{
58
+ user: {
59
+ id: string;
60
+ name?: string;
61
+ email?: string | null;
62
+ image?: string;
63
+ emailVerified: boolean;
64
+ [key: string]: any;
65
+ };
66
+ data: any;
67
+ } | null>;
68
+ options: DropboxOptions;
69
+ };
70
+ //#endregion
71
+ export { DropboxOptions, DropboxProfile, dropbox };
@@ -0,0 +1,75 @@
1
+ import { createAuthorizationURL } from "../oauth2/create-authorization-url.mjs";
2
+ import { refreshAccessToken } from "../oauth2/refresh-access-token.mjs";
3
+ import { validateAuthorizationCode } from "../oauth2/validate-authorization-code.mjs";
4
+ import "../oauth2/index.mjs";
5
+ import { betterFetch } from "@better-fetch/fetch";
6
+
7
+ //#region src/social-providers/dropbox.ts
8
+ const dropbox = (options) => {
9
+ const tokenEndpoint = "https://api.dropboxapi.com/oauth2/token";
10
+ return {
11
+ id: "dropbox",
12
+ name: "Dropbox",
13
+ createAuthorizationURL: async ({ state, scopes, codeVerifier, redirectURI }) => {
14
+ const _scopes = options.disableDefaultScope ? [] : ["account_info.read"];
15
+ if (options.scope) _scopes.push(...options.scope);
16
+ if (scopes) _scopes.push(...scopes);
17
+ const additionalParams = {};
18
+ if (options.accessType) additionalParams.token_access_type = options.accessType;
19
+ return await createAuthorizationURL({
20
+ id: "dropbox",
21
+ options,
22
+ authorizationEndpoint: "https://www.dropbox.com/oauth2/authorize",
23
+ scopes: _scopes,
24
+ state,
25
+ redirectURI,
26
+ codeVerifier,
27
+ additionalParams
28
+ });
29
+ },
30
+ validateAuthorizationCode: async ({ code, codeVerifier, redirectURI }) => {
31
+ return await validateAuthorizationCode({
32
+ code,
33
+ codeVerifier,
34
+ redirectURI,
35
+ options,
36
+ tokenEndpoint
37
+ });
38
+ },
39
+ refreshAccessToken: options.refreshAccessToken ? options.refreshAccessToken : async (refreshToken) => {
40
+ return refreshAccessToken({
41
+ refreshToken,
42
+ options: {
43
+ clientId: options.clientId,
44
+ clientKey: options.clientKey,
45
+ clientSecret: options.clientSecret
46
+ },
47
+ tokenEndpoint: "https://api.dropbox.com/oauth2/token"
48
+ });
49
+ },
50
+ async getUserInfo(token) {
51
+ if (options.getUserInfo) return options.getUserInfo(token);
52
+ const { data: profile, error } = await betterFetch("https://api.dropboxapi.com/2/users/get_current_account", {
53
+ method: "POST",
54
+ headers: { Authorization: `Bearer ${token.accessToken}` }
55
+ });
56
+ if (error) return null;
57
+ const userMap = await options.mapProfileToUser?.(profile);
58
+ return {
59
+ user: {
60
+ id: profile.account_id,
61
+ name: profile.name?.display_name,
62
+ email: profile.email,
63
+ emailVerified: profile.email_verified || false,
64
+ image: profile.profile_photo_url,
65
+ ...userMap
66
+ },
67
+ data: profile
68
+ };
69
+ },
70
+ options
71
+ };
72
+ };
73
+
74
+ //#endregion
75
+ export { dropbox };