@better-auth/core 1.3.26 → 1.3.28
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.
- package/.turbo/turbo-build.log +60 -9
- package/build.config.ts +7 -0
- package/dist/db/adapter/index.cjs +2 -0
- package/dist/db/adapter/index.d.cts +14 -0
- package/dist/db/adapter/index.d.mts +14 -0
- package/dist/db/adapter/index.d.ts +14 -0
- package/dist/db/adapter/index.mjs +1 -0
- package/dist/db/index.cjs +89 -0
- package/dist/db/index.d.cts +16 -107
- package/dist/db/index.d.mts +16 -107
- package/dist/db/index.d.ts +16 -107
- package/dist/db/index.mjs +69 -0
- package/dist/env/index.cjs +312 -0
- package/dist/env/index.d.cts +36 -0
- package/dist/env/index.d.mts +36 -0
- package/dist/env/index.d.ts +36 -0
- package/dist/env/index.mjs +297 -0
- package/dist/error/index.cjs +44 -0
- package/dist/error/index.d.cts +33 -0
- package/dist/error/index.d.mts +33 -0
- package/dist/error/index.d.ts +33 -0
- package/dist/error/index.mjs +41 -0
- package/dist/index.d.cts +179 -1
- package/dist/index.d.mts +179 -1
- package/dist/index.d.ts +179 -1
- package/dist/middleware/index.cjs +25 -0
- package/dist/middleware/index.d.cts +14 -0
- package/dist/middleware/index.d.mts +14 -0
- package/dist/middleware/index.d.ts +14 -0
- package/dist/middleware/index.mjs +21 -0
- package/dist/oauth2/index.cjs +368 -0
- package/dist/oauth2/index.d.cts +100 -0
- package/dist/oauth2/index.d.mts +100 -0
- package/dist/oauth2/index.d.ts +100 -0
- package/dist/oauth2/index.mjs +357 -0
- package/dist/shared/core.BJPBStdk.d.ts +1693 -0
- package/dist/shared/core.Bl6TpxyD.d.mts +181 -0
- package/dist/shared/core.Bqe5IGAi.d.ts +13 -0
- package/dist/shared/core.BwoNUcJQ.d.cts +53 -0
- package/dist/shared/core.BwoNUcJQ.d.mts +53 -0
- package/dist/shared/core.BwoNUcJQ.d.ts +53 -0
- package/dist/shared/core.CajxAutx.d.cts +143 -0
- package/dist/shared/core.CajxAutx.d.mts +143 -0
- package/dist/shared/core.CajxAutx.d.ts +143 -0
- package/dist/shared/core.CkkLHQWc.d.mts +1693 -0
- package/dist/shared/core.DkdZ1o38.d.ts +181 -0
- package/dist/shared/core.Dl-70uns.d.cts +84 -0
- package/dist/shared/core.Dl-70uns.d.mts +84 -0
- package/dist/shared/core.Dl-70uns.d.ts +84 -0
- package/dist/shared/core.DyEdx0m7.d.cts +181 -0
- package/dist/shared/core.E9DfzGLz.d.mts +13 -0
- package/dist/shared/core.HqYn20Fi.d.cts +13 -0
- package/dist/shared/core.gYIBmdi1.d.cts +1693 -0
- package/dist/social-providers/index.cjs +2793 -0
- package/dist/social-providers/index.d.cts +3903 -0
- package/dist/social-providers/index.d.mts +3903 -0
- package/dist/social-providers/index.d.ts +3903 -0
- package/dist/social-providers/index.mjs +2743 -0
- package/dist/utils/index.cjs +7 -0
- package/dist/utils/index.d.cts +10 -0
- package/dist/utils/index.d.mts +10 -0
- package/dist/utils/index.d.ts +10 -0
- package/dist/utils/index.mjs +5 -0
- package/package.json +109 -2
- package/src/db/adapter/index.ts +448 -0
- package/src/db/index.ts +13 -0
- package/src/db/plugin.ts +11 -0
- package/src/db/schema/account.ts +34 -0
- package/src/db/schema/rate-limit.ts +21 -0
- package/src/db/schema/session.ts +17 -0
- package/src/db/schema/shared.ts +7 -0
- package/src/db/schema/user.ts +16 -0
- package/src/db/schema/verification.ts +15 -0
- package/src/db/type.ts +50 -0
- package/src/env/color-depth.ts +172 -0
- package/src/env/env-impl.ts +123 -0
- package/src/env/index.ts +23 -0
- package/src/env/logger.test.ts +33 -0
- package/src/env/logger.ts +145 -0
- package/src/error/codes.ts +31 -0
- package/src/error/index.ts +11 -0
- package/src/index.ts +1 -1
- package/src/middleware/index.ts +33 -0
- package/src/oauth2/client-credentials-token.ts +102 -0
- package/src/oauth2/create-authorization-url.ts +85 -0
- package/src/oauth2/index.ts +22 -0
- package/src/oauth2/oauth-provider.ts +194 -0
- package/src/oauth2/refresh-access-token.ts +124 -0
- package/src/oauth2/utils.ts +36 -0
- package/src/oauth2/validate-authorization-code.ts +156 -0
- package/src/social-providers/apple.ts +213 -0
- package/src/social-providers/atlassian.ts +130 -0
- package/src/social-providers/cognito.ts +269 -0
- package/src/social-providers/discord.ts +172 -0
- package/src/social-providers/dropbox.ts +112 -0
- package/src/social-providers/facebook.ts +204 -0
- package/src/social-providers/figma.ts +115 -0
- package/src/social-providers/github.ts +154 -0
- package/src/social-providers/gitlab.ts +152 -0
- package/src/social-providers/google.ts +171 -0
- package/src/social-providers/huggingface.ts +116 -0
- package/src/social-providers/index.ts +118 -0
- package/src/social-providers/kakao.ts +178 -0
- package/src/social-providers/kick.ts +95 -0
- package/src/social-providers/line.ts +169 -0
- package/src/social-providers/linear.ts +120 -0
- package/src/social-providers/linkedin.ts +110 -0
- package/src/social-providers/microsoft-entra-id.ts +243 -0
- package/src/social-providers/naver.ts +112 -0
- package/src/social-providers/notion.ts +106 -0
- package/src/social-providers/paypal.ts +261 -0
- package/src/social-providers/reddit.ts +122 -0
- package/src/social-providers/roblox.ts +110 -0
- package/src/social-providers/salesforce.ts +157 -0
- package/src/social-providers/slack.ts +114 -0
- package/src/social-providers/spotify.ts +93 -0
- package/src/social-providers/tiktok.ts +211 -0
- package/src/social-providers/twitch.ts +111 -0
- package/src/social-providers/twitter.ts +194 -0
- package/src/social-providers/vk.ts +128 -0
- package/src/social-providers/zoom.ts +218 -0
- package/src/types/context.ts +313 -0
- package/src/types/cookie.ts +7 -0
- package/src/types/helper.ts +5 -0
- package/src/types/index.ts +20 -1
- package/src/types/init-options.ts +1161 -0
- package/src/types/plugin-client.ts +69 -0
- package/src/types/plugin.ts +134 -0
- package/src/utils/error-codes.ts +51 -0
- package/src/utils/index.ts +1 -0
|
@@ -0,0 +1,269 @@
|
|
|
1
|
+
import { betterFetch } from "@better-fetch/fetch";
|
|
2
|
+
import { decodeJwt, decodeProtectedHeader, importJWK, jwtVerify } from "jose";
|
|
3
|
+
import { BetterAuthError } from "../error";
|
|
4
|
+
import type { OAuthProvider, ProviderOptions } from "@better-auth/core/oauth2";
|
|
5
|
+
import {
|
|
6
|
+
createAuthorizationURL,
|
|
7
|
+
validateAuthorizationCode,
|
|
8
|
+
} from "@better-auth/core/oauth2";
|
|
9
|
+
import { logger } from "@better-auth/core/env";
|
|
10
|
+
import { refreshAccessToken } from "@better-auth/core/oauth2";
|
|
11
|
+
import { APIError } from "better-call";
|
|
12
|
+
|
|
13
|
+
export interface CognitoProfile {
|
|
14
|
+
sub: string;
|
|
15
|
+
email: string;
|
|
16
|
+
email_verified: boolean;
|
|
17
|
+
name: string;
|
|
18
|
+
given_name?: string;
|
|
19
|
+
family_name?: string;
|
|
20
|
+
picture?: string;
|
|
21
|
+
username?: string;
|
|
22
|
+
locale?: string;
|
|
23
|
+
phone_number?: string;
|
|
24
|
+
phone_number_verified?: boolean;
|
|
25
|
+
aud: string;
|
|
26
|
+
iss: string;
|
|
27
|
+
exp: number;
|
|
28
|
+
iat: number;
|
|
29
|
+
// Custom attributes from Cognito can be added here
|
|
30
|
+
[key: string]: any;
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
export interface CognitoOptions extends ProviderOptions<CognitoProfile> {
|
|
34
|
+
clientId: string;
|
|
35
|
+
/**
|
|
36
|
+
* The Cognito domain (e.g., "your-app.auth.us-east-1.amazoncognito.com")
|
|
37
|
+
*/
|
|
38
|
+
domain: string;
|
|
39
|
+
/**
|
|
40
|
+
* AWS region where User Pool is hosted (e.g., "us-east-1")
|
|
41
|
+
*/
|
|
42
|
+
region: string;
|
|
43
|
+
userPoolId: string;
|
|
44
|
+
requireClientSecret?: boolean;
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
export const cognito = (options: CognitoOptions) => {
|
|
48
|
+
if (!options.domain || !options.region || !options.userPoolId) {
|
|
49
|
+
logger.error(
|
|
50
|
+
"Domain, region and userPoolId are required for Amazon Cognito. Make sure to provide them in the options.",
|
|
51
|
+
);
|
|
52
|
+
throw new BetterAuthError("DOMAIN_AND_REGION_REQUIRED");
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
const cleanDomain = options.domain.replace(/^https?:\/\//, "");
|
|
56
|
+
const authorizationEndpoint = `https://${cleanDomain}/oauth2/authorize`;
|
|
57
|
+
const tokenEndpoint = `https://${cleanDomain}/oauth2/token`;
|
|
58
|
+
const userInfoEndpoint = `https://${cleanDomain}/oauth2/userinfo`;
|
|
59
|
+
|
|
60
|
+
return {
|
|
61
|
+
id: "cognito",
|
|
62
|
+
name: "Cognito",
|
|
63
|
+
async createAuthorizationURL({ state, scopes, codeVerifier, redirectURI }) {
|
|
64
|
+
if (!options.clientId) {
|
|
65
|
+
logger.error(
|
|
66
|
+
"ClientId is required for Amazon Cognito. Make sure to provide them in the options.",
|
|
67
|
+
);
|
|
68
|
+
throw new BetterAuthError("CLIENT_ID_AND_SECRET_REQUIRED");
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
if (options.requireClientSecret && !options.clientSecret) {
|
|
72
|
+
logger.error(
|
|
73
|
+
"Client Secret is required when requireClientSecret is true. Make sure to provide it in the options.",
|
|
74
|
+
);
|
|
75
|
+
throw new BetterAuthError("CLIENT_SECRET_REQUIRED");
|
|
76
|
+
}
|
|
77
|
+
const _scopes = options.disableDefaultScope
|
|
78
|
+
? []
|
|
79
|
+
: ["openid", "profile", "email"];
|
|
80
|
+
options.scope && _scopes.push(...options.scope);
|
|
81
|
+
scopes && _scopes.push(...scopes);
|
|
82
|
+
|
|
83
|
+
const url = await createAuthorizationURL({
|
|
84
|
+
id: "cognito",
|
|
85
|
+
options: {
|
|
86
|
+
...options,
|
|
87
|
+
},
|
|
88
|
+
authorizationEndpoint,
|
|
89
|
+
scopes: _scopes,
|
|
90
|
+
state,
|
|
91
|
+
codeVerifier,
|
|
92
|
+
redirectURI,
|
|
93
|
+
prompt: options.prompt,
|
|
94
|
+
});
|
|
95
|
+
return url;
|
|
96
|
+
},
|
|
97
|
+
|
|
98
|
+
validateAuthorizationCode: async ({ code, codeVerifier, redirectURI }) => {
|
|
99
|
+
return validateAuthorizationCode({
|
|
100
|
+
code,
|
|
101
|
+
codeVerifier,
|
|
102
|
+
redirectURI,
|
|
103
|
+
options,
|
|
104
|
+
tokenEndpoint,
|
|
105
|
+
});
|
|
106
|
+
},
|
|
107
|
+
|
|
108
|
+
refreshAccessToken: options.refreshAccessToken
|
|
109
|
+
? options.refreshAccessToken
|
|
110
|
+
: async (refreshToken) => {
|
|
111
|
+
return refreshAccessToken({
|
|
112
|
+
refreshToken,
|
|
113
|
+
options: {
|
|
114
|
+
clientId: options.clientId,
|
|
115
|
+
clientKey: options.clientKey,
|
|
116
|
+
clientSecret: options.clientSecret,
|
|
117
|
+
},
|
|
118
|
+
tokenEndpoint,
|
|
119
|
+
});
|
|
120
|
+
},
|
|
121
|
+
|
|
122
|
+
async verifyIdToken(token, nonce) {
|
|
123
|
+
if (options.disableIdTokenSignIn) {
|
|
124
|
+
return false;
|
|
125
|
+
}
|
|
126
|
+
if (options.verifyIdToken) {
|
|
127
|
+
return options.verifyIdToken(token, nonce);
|
|
128
|
+
}
|
|
129
|
+
|
|
130
|
+
try {
|
|
131
|
+
const decodedHeader = decodeProtectedHeader(token);
|
|
132
|
+
const { kid, alg: jwtAlg } = decodedHeader;
|
|
133
|
+
if (!kid || !jwtAlg) return false;
|
|
134
|
+
|
|
135
|
+
const publicKey = await getCognitoPublicKey(
|
|
136
|
+
kid,
|
|
137
|
+
options.region,
|
|
138
|
+
options.userPoolId,
|
|
139
|
+
);
|
|
140
|
+
const expectedIssuer = `https://cognito-idp.${options.region}.amazonaws.com/${options.userPoolId}`;
|
|
141
|
+
|
|
142
|
+
const { payload: jwtClaims } = await jwtVerify(token, publicKey, {
|
|
143
|
+
algorithms: [jwtAlg],
|
|
144
|
+
issuer: expectedIssuer,
|
|
145
|
+
audience: options.clientId,
|
|
146
|
+
maxTokenAge: "1h",
|
|
147
|
+
});
|
|
148
|
+
|
|
149
|
+
if (nonce && jwtClaims.nonce !== nonce) {
|
|
150
|
+
return false;
|
|
151
|
+
}
|
|
152
|
+
return true;
|
|
153
|
+
} catch (error) {
|
|
154
|
+
logger.error("Failed to verify ID token:", error);
|
|
155
|
+
return false;
|
|
156
|
+
}
|
|
157
|
+
},
|
|
158
|
+
|
|
159
|
+
async getUserInfo(token) {
|
|
160
|
+
if (options.getUserInfo) {
|
|
161
|
+
return options.getUserInfo(token);
|
|
162
|
+
}
|
|
163
|
+
|
|
164
|
+
if (token.idToken) {
|
|
165
|
+
try {
|
|
166
|
+
const profile = decodeJwt<CognitoProfile>(token.idToken);
|
|
167
|
+
if (!profile) {
|
|
168
|
+
return null;
|
|
169
|
+
}
|
|
170
|
+
const name =
|
|
171
|
+
profile.name ||
|
|
172
|
+
profile.given_name ||
|
|
173
|
+
profile.username ||
|
|
174
|
+
profile.email;
|
|
175
|
+
const enrichedProfile = {
|
|
176
|
+
...profile,
|
|
177
|
+
name,
|
|
178
|
+
};
|
|
179
|
+
const userMap = await options.mapProfileToUser?.(enrichedProfile);
|
|
180
|
+
|
|
181
|
+
return {
|
|
182
|
+
user: {
|
|
183
|
+
id: profile.sub,
|
|
184
|
+
name: enrichedProfile.name,
|
|
185
|
+
email: profile.email,
|
|
186
|
+
image: profile.picture,
|
|
187
|
+
emailVerified: profile.email_verified,
|
|
188
|
+
...userMap,
|
|
189
|
+
},
|
|
190
|
+
data: enrichedProfile,
|
|
191
|
+
};
|
|
192
|
+
} catch (error) {
|
|
193
|
+
logger.error("Failed to decode ID token:", error);
|
|
194
|
+
}
|
|
195
|
+
}
|
|
196
|
+
|
|
197
|
+
if (token.accessToken) {
|
|
198
|
+
try {
|
|
199
|
+
const { data: userInfo } = await betterFetch<CognitoProfile>(
|
|
200
|
+
userInfoEndpoint,
|
|
201
|
+
{
|
|
202
|
+
headers: {
|
|
203
|
+
Authorization: `Bearer ${token.accessToken}`,
|
|
204
|
+
},
|
|
205
|
+
},
|
|
206
|
+
);
|
|
207
|
+
|
|
208
|
+
if (userInfo) {
|
|
209
|
+
const userMap = await options.mapProfileToUser?.(userInfo);
|
|
210
|
+
return {
|
|
211
|
+
user: {
|
|
212
|
+
id: userInfo.sub,
|
|
213
|
+
name: userInfo.name || userInfo.given_name || userInfo.username,
|
|
214
|
+
email: userInfo.email,
|
|
215
|
+
image: userInfo.picture,
|
|
216
|
+
emailVerified: userInfo.email_verified,
|
|
217
|
+
...userMap,
|
|
218
|
+
},
|
|
219
|
+
data: userInfo,
|
|
220
|
+
};
|
|
221
|
+
}
|
|
222
|
+
} catch (error) {
|
|
223
|
+
logger.error("Failed to fetch user info from Cognito:", error);
|
|
224
|
+
}
|
|
225
|
+
}
|
|
226
|
+
|
|
227
|
+
return null;
|
|
228
|
+
},
|
|
229
|
+
|
|
230
|
+
options,
|
|
231
|
+
} satisfies OAuthProvider<CognitoProfile>;
|
|
232
|
+
};
|
|
233
|
+
|
|
234
|
+
export const getCognitoPublicKey = async (
|
|
235
|
+
kid: string,
|
|
236
|
+
region: string,
|
|
237
|
+
userPoolId: string,
|
|
238
|
+
) => {
|
|
239
|
+
const COGNITO_JWKS_URI = `https://cognito-idp.${region}.amazonaws.com/${userPoolId}/.well-known/jwks.json`;
|
|
240
|
+
|
|
241
|
+
try {
|
|
242
|
+
const { data } = await betterFetch<{
|
|
243
|
+
keys: Array<{
|
|
244
|
+
kid: string;
|
|
245
|
+
alg: string;
|
|
246
|
+
kty: string;
|
|
247
|
+
use: string;
|
|
248
|
+
n: string;
|
|
249
|
+
e: string;
|
|
250
|
+
}>;
|
|
251
|
+
}>(COGNITO_JWKS_URI);
|
|
252
|
+
|
|
253
|
+
if (!data?.keys) {
|
|
254
|
+
throw new APIError("BAD_REQUEST", {
|
|
255
|
+
message: "Keys not found",
|
|
256
|
+
});
|
|
257
|
+
}
|
|
258
|
+
|
|
259
|
+
const jwk = data.keys.find((key) => key.kid === kid);
|
|
260
|
+
if (!jwk) {
|
|
261
|
+
throw new Error(`JWK with kid ${kid} not found`);
|
|
262
|
+
}
|
|
263
|
+
|
|
264
|
+
return await importJWK(jwk, jwk.alg);
|
|
265
|
+
} catch (error) {
|
|
266
|
+
logger.error("Failed to fetch Cognito public key:", error);
|
|
267
|
+
throw error;
|
|
268
|
+
}
|
|
269
|
+
};
|
|
@@ -0,0 +1,172 @@
|
|
|
1
|
+
import { betterFetch } from "@better-fetch/fetch";
|
|
2
|
+
import type { OAuthProvider, ProviderOptions } from "@better-auth/core/oauth2";
|
|
3
|
+
import {
|
|
4
|
+
refreshAccessToken,
|
|
5
|
+
validateAuthorizationCode,
|
|
6
|
+
} from "@better-auth/core/oauth2";
|
|
7
|
+
export interface DiscordProfile extends Record<string, any> {
|
|
8
|
+
/** the user's id (i.e. the numerical snowflake) */
|
|
9
|
+
id: string;
|
|
10
|
+
/** the user's username, not unique across the platform */
|
|
11
|
+
username: string;
|
|
12
|
+
/** the user's Discord-tag */
|
|
13
|
+
discriminator: string;
|
|
14
|
+
/** the user's display name, if it is set */
|
|
15
|
+
global_name: string | null;
|
|
16
|
+
/**
|
|
17
|
+
* the user's avatar hash:
|
|
18
|
+
* https://discord.com/developers/docs/reference#image-formatting
|
|
19
|
+
*/
|
|
20
|
+
avatar: string | null;
|
|
21
|
+
/** whether the user belongs to an OAuth2 application */
|
|
22
|
+
bot?: boolean;
|
|
23
|
+
/**
|
|
24
|
+
* whether the user is an Official Discord System user (part of the urgent
|
|
25
|
+
* message system)
|
|
26
|
+
*/
|
|
27
|
+
system?: boolean;
|
|
28
|
+
/** whether the user has two factor enabled on their account */
|
|
29
|
+
mfa_enabled: boolean;
|
|
30
|
+
/**
|
|
31
|
+
* the user's banner hash:
|
|
32
|
+
* https://discord.com/developers/docs/reference#image-formatting
|
|
33
|
+
*/
|
|
34
|
+
banner: string | null;
|
|
35
|
+
|
|
36
|
+
/** the user's banner color encoded as an integer representation of hexadecimal color code */
|
|
37
|
+
accent_color: number | null;
|
|
38
|
+
|
|
39
|
+
/**
|
|
40
|
+
* the user's chosen language option:
|
|
41
|
+
* https://discord.com/developers/docs/reference#locales
|
|
42
|
+
*/
|
|
43
|
+
locale: string;
|
|
44
|
+
/** whether the email on this account has been verified */
|
|
45
|
+
verified: boolean;
|
|
46
|
+
/** the user's email */
|
|
47
|
+
email: string;
|
|
48
|
+
/**
|
|
49
|
+
* the flags on a user's account:
|
|
50
|
+
* https://discord.com/developers/docs/resources/user#user-object-user-flags
|
|
51
|
+
*/
|
|
52
|
+
flags: number;
|
|
53
|
+
/**
|
|
54
|
+
* the type of Nitro subscription on a user's account:
|
|
55
|
+
* https://discord.com/developers/docs/resources/user#user-object-premium-types
|
|
56
|
+
*/
|
|
57
|
+
premium_type: number;
|
|
58
|
+
/**
|
|
59
|
+
* the public flags on a user's account:
|
|
60
|
+
* https://discord.com/developers/docs/resources/user#user-object-user-flags
|
|
61
|
+
*/
|
|
62
|
+
public_flags: number;
|
|
63
|
+
/** undocumented field; corresponds to the user's custom nickname */
|
|
64
|
+
display_name: string | null;
|
|
65
|
+
/**
|
|
66
|
+
* undocumented field; corresponds to the Discord feature where you can e.g.
|
|
67
|
+
* put your avatar inside of an ice cube
|
|
68
|
+
*/
|
|
69
|
+
avatar_decoration: string | null;
|
|
70
|
+
/**
|
|
71
|
+
* undocumented field; corresponds to the premium feature where you can
|
|
72
|
+
* select a custom banner color
|
|
73
|
+
*/
|
|
74
|
+
banner_color: string | null;
|
|
75
|
+
/** undocumented field; the CDN URL of their profile picture */
|
|
76
|
+
image_url: string;
|
|
77
|
+
}
|
|
78
|
+
|
|
79
|
+
export interface DiscordOptions extends ProviderOptions<DiscordProfile> {
|
|
80
|
+
clientId: string;
|
|
81
|
+
prompt?: "none" | "consent";
|
|
82
|
+
permissions?: number;
|
|
83
|
+
}
|
|
84
|
+
|
|
85
|
+
export const discord = (options: DiscordOptions) => {
|
|
86
|
+
return {
|
|
87
|
+
id: "discord",
|
|
88
|
+
name: "Discord",
|
|
89
|
+
createAuthorizationURL({ state, scopes, redirectURI }) {
|
|
90
|
+
const _scopes = options.disableDefaultScope ? [] : ["identify", "email"];
|
|
91
|
+
scopes && _scopes.push(...scopes);
|
|
92
|
+
options.scope && _scopes.push(...options.scope);
|
|
93
|
+
const hasBotScope = _scopes.includes("bot");
|
|
94
|
+
const permissionsParam =
|
|
95
|
+
hasBotScope && options.permissions !== undefined
|
|
96
|
+
? `&permissions=${options.permissions}`
|
|
97
|
+
: "";
|
|
98
|
+
return new URL(
|
|
99
|
+
`https://discord.com/api/oauth2/authorize?scope=${_scopes.join(
|
|
100
|
+
"+",
|
|
101
|
+
)}&response_type=code&client_id=${
|
|
102
|
+
options.clientId
|
|
103
|
+
}&redirect_uri=${encodeURIComponent(
|
|
104
|
+
options.redirectURI || redirectURI,
|
|
105
|
+
)}&state=${state}&prompt=${
|
|
106
|
+
options.prompt || "none"
|
|
107
|
+
}${permissionsParam}`,
|
|
108
|
+
);
|
|
109
|
+
},
|
|
110
|
+
validateAuthorizationCode: async ({ code, redirectURI }) => {
|
|
111
|
+
return validateAuthorizationCode({
|
|
112
|
+
code,
|
|
113
|
+
redirectURI,
|
|
114
|
+
options,
|
|
115
|
+
tokenEndpoint: "https://discord.com/api/oauth2/token",
|
|
116
|
+
});
|
|
117
|
+
},
|
|
118
|
+
refreshAccessToken: options.refreshAccessToken
|
|
119
|
+
? options.refreshAccessToken
|
|
120
|
+
: async (refreshToken) => {
|
|
121
|
+
return refreshAccessToken({
|
|
122
|
+
refreshToken,
|
|
123
|
+
options: {
|
|
124
|
+
clientId: options.clientId,
|
|
125
|
+
clientKey: options.clientKey,
|
|
126
|
+
clientSecret: options.clientSecret,
|
|
127
|
+
},
|
|
128
|
+
tokenEndpoint: "https://discord.com/api/oauth2/token",
|
|
129
|
+
});
|
|
130
|
+
},
|
|
131
|
+
async getUserInfo(token) {
|
|
132
|
+
if (options.getUserInfo) {
|
|
133
|
+
return options.getUserInfo(token);
|
|
134
|
+
}
|
|
135
|
+
const { data: profile, error } = await betterFetch<DiscordProfile>(
|
|
136
|
+
"https://discord.com/api/users/@me",
|
|
137
|
+
{
|
|
138
|
+
headers: {
|
|
139
|
+
authorization: `Bearer ${token.accessToken}`,
|
|
140
|
+
},
|
|
141
|
+
},
|
|
142
|
+
);
|
|
143
|
+
|
|
144
|
+
if (error) {
|
|
145
|
+
return null;
|
|
146
|
+
}
|
|
147
|
+
if (profile.avatar === null) {
|
|
148
|
+
const defaultAvatarNumber =
|
|
149
|
+
profile.discriminator === "0"
|
|
150
|
+
? Number(BigInt(profile.id) >> BigInt(22)) % 6
|
|
151
|
+
: parseInt(profile.discriminator) % 5;
|
|
152
|
+
profile.image_url = `https://cdn.discordapp.com/embed/avatars/${defaultAvatarNumber}.png`;
|
|
153
|
+
} else {
|
|
154
|
+
const format = profile.avatar.startsWith("a_") ? "gif" : "png";
|
|
155
|
+
profile.image_url = `https://cdn.discordapp.com/avatars/${profile.id}/${profile.avatar}.${format}`;
|
|
156
|
+
}
|
|
157
|
+
const userMap = await options.mapProfileToUser?.(profile);
|
|
158
|
+
return {
|
|
159
|
+
user: {
|
|
160
|
+
id: profile.id,
|
|
161
|
+
name: profile.global_name || profile.username || "",
|
|
162
|
+
email: profile.email,
|
|
163
|
+
emailVerified: profile.verified,
|
|
164
|
+
image: profile.image_url,
|
|
165
|
+
...userMap,
|
|
166
|
+
},
|
|
167
|
+
data: profile,
|
|
168
|
+
};
|
|
169
|
+
},
|
|
170
|
+
options,
|
|
171
|
+
} satisfies OAuthProvider<DiscordProfile>;
|
|
172
|
+
};
|
|
@@ -0,0 +1,112 @@
|
|
|
1
|
+
import { betterFetch } from "@better-fetch/fetch";
|
|
2
|
+
import type { OAuthProvider, ProviderOptions } from "@better-auth/core/oauth2";
|
|
3
|
+
import {
|
|
4
|
+
createAuthorizationURL,
|
|
5
|
+
refreshAccessToken,
|
|
6
|
+
validateAuthorizationCode,
|
|
7
|
+
} from "@better-auth/core/oauth2";
|
|
8
|
+
|
|
9
|
+
export interface DropboxProfile {
|
|
10
|
+
account_id: string;
|
|
11
|
+
name: {
|
|
12
|
+
given_name: string;
|
|
13
|
+
surname: string;
|
|
14
|
+
familiar_name: string;
|
|
15
|
+
display_name: string;
|
|
16
|
+
abbreviated_name: string;
|
|
17
|
+
};
|
|
18
|
+
email: string;
|
|
19
|
+
email_verified: boolean;
|
|
20
|
+
profile_photo_url: string;
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
export interface DropboxOptions extends ProviderOptions<DropboxProfile> {
|
|
24
|
+
clientId: string;
|
|
25
|
+
accessType?: "offline" | "online" | "legacy";
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
export const dropbox = (options: DropboxOptions) => {
|
|
29
|
+
const tokenEndpoint = "https://api.dropboxapi.com/oauth2/token";
|
|
30
|
+
|
|
31
|
+
return {
|
|
32
|
+
id: "dropbox",
|
|
33
|
+
name: "Dropbox",
|
|
34
|
+
createAuthorizationURL: async ({
|
|
35
|
+
state,
|
|
36
|
+
scopes,
|
|
37
|
+
codeVerifier,
|
|
38
|
+
redirectURI,
|
|
39
|
+
}) => {
|
|
40
|
+
const _scopes = options.disableDefaultScope ? [] : ["account_info.read"];
|
|
41
|
+
options.scope && _scopes.push(...options.scope);
|
|
42
|
+
scopes && _scopes.push(...scopes);
|
|
43
|
+
const additionalParams: Record<string, string> = {};
|
|
44
|
+
if (options.accessType) {
|
|
45
|
+
additionalParams.token_access_type = options.accessType;
|
|
46
|
+
}
|
|
47
|
+
return await createAuthorizationURL({
|
|
48
|
+
id: "dropbox",
|
|
49
|
+
options,
|
|
50
|
+
authorizationEndpoint: "https://www.dropbox.com/oauth2/authorize",
|
|
51
|
+
scopes: _scopes,
|
|
52
|
+
state,
|
|
53
|
+
redirectURI,
|
|
54
|
+
codeVerifier,
|
|
55
|
+
additionalParams,
|
|
56
|
+
});
|
|
57
|
+
},
|
|
58
|
+
validateAuthorizationCode: async ({ code, codeVerifier, redirectURI }) => {
|
|
59
|
+
return await validateAuthorizationCode({
|
|
60
|
+
code,
|
|
61
|
+
codeVerifier,
|
|
62
|
+
redirectURI,
|
|
63
|
+
options,
|
|
64
|
+
tokenEndpoint,
|
|
65
|
+
});
|
|
66
|
+
},
|
|
67
|
+
refreshAccessToken: options.refreshAccessToken
|
|
68
|
+
? options.refreshAccessToken
|
|
69
|
+
: async (refreshToken) => {
|
|
70
|
+
return refreshAccessToken({
|
|
71
|
+
refreshToken,
|
|
72
|
+
options: {
|
|
73
|
+
clientId: options.clientId,
|
|
74
|
+
clientKey: options.clientKey,
|
|
75
|
+
clientSecret: options.clientSecret,
|
|
76
|
+
},
|
|
77
|
+
tokenEndpoint: "https://api.dropbox.com/oauth2/token",
|
|
78
|
+
});
|
|
79
|
+
},
|
|
80
|
+
async getUserInfo(token) {
|
|
81
|
+
if (options.getUserInfo) {
|
|
82
|
+
return options.getUserInfo(token);
|
|
83
|
+
}
|
|
84
|
+
const { data: profile, error } = await betterFetch<DropboxProfile>(
|
|
85
|
+
"https://api.dropboxapi.com/2/users/get_current_account",
|
|
86
|
+
{
|
|
87
|
+
method: "POST",
|
|
88
|
+
headers: {
|
|
89
|
+
Authorization: `Bearer ${token.accessToken}`,
|
|
90
|
+
},
|
|
91
|
+
},
|
|
92
|
+
);
|
|
93
|
+
|
|
94
|
+
if (error) {
|
|
95
|
+
return null;
|
|
96
|
+
}
|
|
97
|
+
const userMap = await options.mapProfileToUser?.(profile);
|
|
98
|
+
return {
|
|
99
|
+
user: {
|
|
100
|
+
id: profile.account_id,
|
|
101
|
+
name: profile.name?.display_name,
|
|
102
|
+
email: profile.email,
|
|
103
|
+
emailVerified: profile.email_verified || false,
|
|
104
|
+
image: profile.profile_photo_url,
|
|
105
|
+
...userMap,
|
|
106
|
+
},
|
|
107
|
+
data: profile,
|
|
108
|
+
};
|
|
109
|
+
},
|
|
110
|
+
options,
|
|
111
|
+
} satisfies OAuthProvider<DropboxProfile>;
|
|
112
|
+
};
|