@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,169 @@
|
|
|
1
|
+
import { betterFetch } from "@better-fetch/fetch";
|
|
2
|
+
import { decodeJwt } from "jose";
|
|
3
|
+
import type { OAuthProvider, ProviderOptions } from "@better-auth/core/oauth2";
|
|
4
|
+
import {
|
|
5
|
+
createAuthorizationURL,
|
|
6
|
+
refreshAccessToken,
|
|
7
|
+
validateAuthorizationCode,
|
|
8
|
+
} from "@better-auth/core/oauth2";
|
|
9
|
+
|
|
10
|
+
export interface LineIdTokenPayload {
|
|
11
|
+
iss: string;
|
|
12
|
+
sub: string;
|
|
13
|
+
aud: string;
|
|
14
|
+
exp: number;
|
|
15
|
+
iat: number;
|
|
16
|
+
name?: string;
|
|
17
|
+
picture?: string;
|
|
18
|
+
email?: string;
|
|
19
|
+
amr?: string[];
|
|
20
|
+
nonce?: string;
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
export interface LineUserInfo {
|
|
24
|
+
sub: string;
|
|
25
|
+
name?: string;
|
|
26
|
+
picture?: string;
|
|
27
|
+
email?: string;
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
export interface LineOptions
|
|
31
|
+
extends ProviderOptions<LineUserInfo | LineIdTokenPayload> {
|
|
32
|
+
clientId: string;
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
/**
|
|
36
|
+
* LINE Login v2.1
|
|
37
|
+
* - Authorization endpoint: https://access.line.me/oauth2/v2.1/authorize
|
|
38
|
+
* - Token endpoint: https://api.line.me/oauth2/v2.1/token
|
|
39
|
+
* - UserInfo endpoint: https://api.line.me/oauth2/v2.1/userinfo
|
|
40
|
+
* - Verify ID token: https://api.line.me/oauth2/v2.1/verify
|
|
41
|
+
*
|
|
42
|
+
* Docs: https://developers.line.biz/en/reference/line-login/#issue-access-token
|
|
43
|
+
*/
|
|
44
|
+
export const line = (options: LineOptions) => {
|
|
45
|
+
const authorizationEndpoint = "https://access.line.me/oauth2/v2.1/authorize";
|
|
46
|
+
const tokenEndpoint = "https://api.line.me/oauth2/v2.1/token";
|
|
47
|
+
const userInfoEndpoint = "https://api.line.me/oauth2/v2.1/userinfo";
|
|
48
|
+
const verifyIdTokenEndpoint = "https://api.line.me/oauth2/v2.1/verify";
|
|
49
|
+
|
|
50
|
+
return {
|
|
51
|
+
id: "line",
|
|
52
|
+
name: "LINE",
|
|
53
|
+
async createAuthorizationURL({
|
|
54
|
+
state,
|
|
55
|
+
scopes,
|
|
56
|
+
codeVerifier,
|
|
57
|
+
redirectURI,
|
|
58
|
+
loginHint,
|
|
59
|
+
}) {
|
|
60
|
+
const _scopes = options.disableDefaultScope
|
|
61
|
+
? []
|
|
62
|
+
: ["openid", "profile", "email"];
|
|
63
|
+
options.scope && _scopes.push(...options.scope);
|
|
64
|
+
scopes && _scopes.push(...scopes);
|
|
65
|
+
return await createAuthorizationURL({
|
|
66
|
+
id: "line",
|
|
67
|
+
options,
|
|
68
|
+
authorizationEndpoint,
|
|
69
|
+
scopes: _scopes,
|
|
70
|
+
state,
|
|
71
|
+
codeVerifier,
|
|
72
|
+
redirectURI,
|
|
73
|
+
loginHint,
|
|
74
|
+
});
|
|
75
|
+
},
|
|
76
|
+
validateAuthorizationCode: async ({ code, codeVerifier, redirectURI }) => {
|
|
77
|
+
return validateAuthorizationCode({
|
|
78
|
+
code,
|
|
79
|
+
codeVerifier,
|
|
80
|
+
redirectURI,
|
|
81
|
+
options,
|
|
82
|
+
tokenEndpoint,
|
|
83
|
+
});
|
|
84
|
+
},
|
|
85
|
+
refreshAccessToken: options.refreshAccessToken
|
|
86
|
+
? options.refreshAccessToken
|
|
87
|
+
: async (refreshToken) => {
|
|
88
|
+
return refreshAccessToken({
|
|
89
|
+
refreshToken,
|
|
90
|
+
options: {
|
|
91
|
+
clientId: options.clientId,
|
|
92
|
+
clientSecret: options.clientSecret,
|
|
93
|
+
},
|
|
94
|
+
tokenEndpoint,
|
|
95
|
+
});
|
|
96
|
+
},
|
|
97
|
+
async verifyIdToken(token, nonce) {
|
|
98
|
+
if (options.disableIdTokenSignIn) {
|
|
99
|
+
return false;
|
|
100
|
+
}
|
|
101
|
+
if (options.verifyIdToken) {
|
|
102
|
+
return options.verifyIdToken(token, nonce);
|
|
103
|
+
}
|
|
104
|
+
const body = new URLSearchParams();
|
|
105
|
+
body.set("id_token", token);
|
|
106
|
+
body.set("client_id", options.clientId);
|
|
107
|
+
if (nonce) body.set("nonce", nonce);
|
|
108
|
+
const { data, error } = await betterFetch<LineIdTokenPayload>(
|
|
109
|
+
verifyIdTokenEndpoint,
|
|
110
|
+
{
|
|
111
|
+
method: "POST",
|
|
112
|
+
headers: {
|
|
113
|
+
"content-type": "application/x-www-form-urlencoded",
|
|
114
|
+
},
|
|
115
|
+
body,
|
|
116
|
+
},
|
|
117
|
+
);
|
|
118
|
+
if (error || !data) {
|
|
119
|
+
return false;
|
|
120
|
+
}
|
|
121
|
+
// aud must match clientId; nonce (if provided) must also match
|
|
122
|
+
if (data.aud !== options.clientId) return false;
|
|
123
|
+
if (nonce && data.nonce && data.nonce !== nonce) return false;
|
|
124
|
+
return true;
|
|
125
|
+
},
|
|
126
|
+
async getUserInfo(token) {
|
|
127
|
+
if (options.getUserInfo) {
|
|
128
|
+
return options.getUserInfo(token);
|
|
129
|
+
}
|
|
130
|
+
let profile: LineUserInfo | LineIdTokenPayload | null = null;
|
|
131
|
+
// Prefer ID token if available
|
|
132
|
+
if (token.idToken) {
|
|
133
|
+
try {
|
|
134
|
+
profile = decodeJwt(token.idToken) as LineIdTokenPayload;
|
|
135
|
+
} catch {}
|
|
136
|
+
}
|
|
137
|
+
// Fallback to UserInfo endpoint
|
|
138
|
+
if (!profile) {
|
|
139
|
+
const { data } = await betterFetch<LineUserInfo>(userInfoEndpoint, {
|
|
140
|
+
headers: {
|
|
141
|
+
authorization: `Bearer ${token.accessToken}`,
|
|
142
|
+
},
|
|
143
|
+
});
|
|
144
|
+
profile = data || null;
|
|
145
|
+
}
|
|
146
|
+
if (!profile) return null;
|
|
147
|
+
const userMap = await options.mapProfileToUser?.(profile as any);
|
|
148
|
+
// ID preference order
|
|
149
|
+
const id = (profile as any).sub || (profile as any).userId;
|
|
150
|
+
const name = (profile as any).name || (profile as any).displayName;
|
|
151
|
+
const image =
|
|
152
|
+
(profile as any).picture || (profile as any).pictureUrl || undefined;
|
|
153
|
+
const email = (profile as any).email;
|
|
154
|
+
return {
|
|
155
|
+
user: {
|
|
156
|
+
id,
|
|
157
|
+
name,
|
|
158
|
+
email,
|
|
159
|
+
image,
|
|
160
|
+
// LINE does not expose email verification status in ID token/userinfo
|
|
161
|
+
emailVerified: false,
|
|
162
|
+
...userMap,
|
|
163
|
+
},
|
|
164
|
+
data: profile as any,
|
|
165
|
+
};
|
|
166
|
+
},
|
|
167
|
+
options,
|
|
168
|
+
} satisfies OAuthProvider<LineUserInfo | LineIdTokenPayload, LineOptions>;
|
|
169
|
+
};
|
|
@@ -0,0 +1,120 @@
|
|
|
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
|
+
interface LinearUser {
|
|
10
|
+
id: string;
|
|
11
|
+
name: string;
|
|
12
|
+
email: string;
|
|
13
|
+
avatarUrl?: string;
|
|
14
|
+
active: boolean;
|
|
15
|
+
createdAt: string;
|
|
16
|
+
updatedAt: string;
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
export interface LinearProfile {
|
|
20
|
+
data: {
|
|
21
|
+
viewer: LinearUser;
|
|
22
|
+
};
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
export interface LinearOptions extends ProviderOptions<LinearUser> {
|
|
26
|
+
clientId: string;
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
export const linear = (options: LinearOptions) => {
|
|
30
|
+
const tokenEndpoint = "https://api.linear.app/oauth/token";
|
|
31
|
+
return {
|
|
32
|
+
id: "linear",
|
|
33
|
+
name: "Linear",
|
|
34
|
+
createAuthorizationURL({ state, scopes, loginHint, redirectURI }) {
|
|
35
|
+
const _scopes = options.disableDefaultScope ? [] : ["read"];
|
|
36
|
+
options.scope && _scopes.push(...options.scope);
|
|
37
|
+
scopes && _scopes.push(...scopes);
|
|
38
|
+
return createAuthorizationURL({
|
|
39
|
+
id: "linear",
|
|
40
|
+
options,
|
|
41
|
+
authorizationEndpoint: "https://linear.app/oauth/authorize",
|
|
42
|
+
scopes: _scopes,
|
|
43
|
+
state,
|
|
44
|
+
redirectURI,
|
|
45
|
+
loginHint,
|
|
46
|
+
});
|
|
47
|
+
},
|
|
48
|
+
validateAuthorizationCode: async ({ code, redirectURI }) => {
|
|
49
|
+
return validateAuthorizationCode({
|
|
50
|
+
code,
|
|
51
|
+
redirectURI,
|
|
52
|
+
options,
|
|
53
|
+
tokenEndpoint,
|
|
54
|
+
});
|
|
55
|
+
},
|
|
56
|
+
refreshAccessToken: options.refreshAccessToken
|
|
57
|
+
? options.refreshAccessToken
|
|
58
|
+
: async (refreshToken) => {
|
|
59
|
+
return refreshAccessToken({
|
|
60
|
+
refreshToken,
|
|
61
|
+
options: {
|
|
62
|
+
clientId: options.clientId,
|
|
63
|
+
clientKey: options.clientKey,
|
|
64
|
+
clientSecret: options.clientSecret,
|
|
65
|
+
},
|
|
66
|
+
tokenEndpoint,
|
|
67
|
+
});
|
|
68
|
+
},
|
|
69
|
+
async getUserInfo(token) {
|
|
70
|
+
if (options.getUserInfo) {
|
|
71
|
+
return options.getUserInfo(token);
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
const { data: profile, error } = await betterFetch<LinearProfile>(
|
|
75
|
+
"https://api.linear.app/graphql",
|
|
76
|
+
{
|
|
77
|
+
method: "POST",
|
|
78
|
+
headers: {
|
|
79
|
+
"Content-Type": "application/json",
|
|
80
|
+
Authorization: `Bearer ${token.accessToken}`,
|
|
81
|
+
},
|
|
82
|
+
body: JSON.stringify({
|
|
83
|
+
query: `
|
|
84
|
+
query {
|
|
85
|
+
viewer {
|
|
86
|
+
id
|
|
87
|
+
name
|
|
88
|
+
email
|
|
89
|
+
avatarUrl
|
|
90
|
+
active
|
|
91
|
+
createdAt
|
|
92
|
+
updatedAt
|
|
93
|
+
}
|
|
94
|
+
}
|
|
95
|
+
`,
|
|
96
|
+
}),
|
|
97
|
+
},
|
|
98
|
+
);
|
|
99
|
+
if (error || !profile?.data?.viewer) {
|
|
100
|
+
return null;
|
|
101
|
+
}
|
|
102
|
+
|
|
103
|
+
const userData = profile.data.viewer;
|
|
104
|
+
const userMap = await options.mapProfileToUser?.(userData);
|
|
105
|
+
|
|
106
|
+
return {
|
|
107
|
+
user: {
|
|
108
|
+
id: profile.data.viewer.id,
|
|
109
|
+
name: profile.data.viewer.name,
|
|
110
|
+
email: profile.data.viewer.email,
|
|
111
|
+
image: profile.data.viewer.avatarUrl,
|
|
112
|
+
emailVerified: true,
|
|
113
|
+
...userMap,
|
|
114
|
+
},
|
|
115
|
+
data: userData,
|
|
116
|
+
};
|
|
117
|
+
},
|
|
118
|
+
options,
|
|
119
|
+
} satisfies OAuthProvider<LinearUser>;
|
|
120
|
+
};
|
|
@@ -0,0 +1,110 @@
|
|
|
1
|
+
import { betterFetch } from "@better-fetch/fetch";
|
|
2
|
+
import type { OAuthProvider, ProviderOptions } from "@better-auth/core/oauth2";
|
|
3
|
+
import {
|
|
4
|
+
createAuthorizationURL,
|
|
5
|
+
validateAuthorizationCode,
|
|
6
|
+
refreshAccessToken,
|
|
7
|
+
} from "@better-auth/core/oauth2";
|
|
8
|
+
|
|
9
|
+
export interface LinkedInProfile {
|
|
10
|
+
sub: string;
|
|
11
|
+
name: string;
|
|
12
|
+
given_name: string;
|
|
13
|
+
family_name: string;
|
|
14
|
+
picture: string;
|
|
15
|
+
locale: {
|
|
16
|
+
country: string;
|
|
17
|
+
language: string;
|
|
18
|
+
};
|
|
19
|
+
email: string;
|
|
20
|
+
email_verified: boolean;
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
export interface LinkedInOptions extends ProviderOptions<LinkedInProfile> {
|
|
24
|
+
clientId: string;
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
export const linkedin = (options: LinkedInOptions) => {
|
|
28
|
+
const authorizationEndpoint =
|
|
29
|
+
"https://www.linkedin.com/oauth/v2/authorization";
|
|
30
|
+
const tokenEndpoint = "https://www.linkedin.com/oauth/v2/accessToken";
|
|
31
|
+
|
|
32
|
+
return {
|
|
33
|
+
id: "linkedin",
|
|
34
|
+
name: "Linkedin",
|
|
35
|
+
createAuthorizationURL: async ({
|
|
36
|
+
state,
|
|
37
|
+
scopes,
|
|
38
|
+
redirectURI,
|
|
39
|
+
loginHint,
|
|
40
|
+
}) => {
|
|
41
|
+
const _scopes = options.disableDefaultScope
|
|
42
|
+
? []
|
|
43
|
+
: ["profile", "email", "openid"];
|
|
44
|
+
options.scope && _scopes.push(...options.scope);
|
|
45
|
+
scopes && _scopes.push(...scopes);
|
|
46
|
+
return await createAuthorizationURL({
|
|
47
|
+
id: "linkedin",
|
|
48
|
+
options,
|
|
49
|
+
authorizationEndpoint,
|
|
50
|
+
scopes: _scopes,
|
|
51
|
+
state,
|
|
52
|
+
loginHint,
|
|
53
|
+
redirectURI,
|
|
54
|
+
});
|
|
55
|
+
},
|
|
56
|
+
validateAuthorizationCode: async ({ code, redirectURI }) => {
|
|
57
|
+
return await validateAuthorizationCode({
|
|
58
|
+
code,
|
|
59
|
+
redirectURI,
|
|
60
|
+
options,
|
|
61
|
+
tokenEndpoint,
|
|
62
|
+
});
|
|
63
|
+
},
|
|
64
|
+
refreshAccessToken: options.refreshAccessToken
|
|
65
|
+
? options.refreshAccessToken
|
|
66
|
+
: async (refreshToken) => {
|
|
67
|
+
return refreshAccessToken({
|
|
68
|
+
refreshToken,
|
|
69
|
+
options: {
|
|
70
|
+
clientId: options.clientId,
|
|
71
|
+
clientKey: options.clientKey,
|
|
72
|
+
clientSecret: options.clientSecret,
|
|
73
|
+
},
|
|
74
|
+
tokenEndpoint,
|
|
75
|
+
});
|
|
76
|
+
},
|
|
77
|
+
async getUserInfo(token) {
|
|
78
|
+
if (options.getUserInfo) {
|
|
79
|
+
return options.getUserInfo(token);
|
|
80
|
+
}
|
|
81
|
+
const { data: profile, error } = await betterFetch<LinkedInProfile>(
|
|
82
|
+
"https://api.linkedin.com/v2/userinfo",
|
|
83
|
+
{
|
|
84
|
+
method: "GET",
|
|
85
|
+
headers: {
|
|
86
|
+
Authorization: `Bearer ${token.accessToken}`,
|
|
87
|
+
},
|
|
88
|
+
},
|
|
89
|
+
);
|
|
90
|
+
|
|
91
|
+
if (error) {
|
|
92
|
+
return null;
|
|
93
|
+
}
|
|
94
|
+
|
|
95
|
+
const userMap = await options.mapProfileToUser?.(profile);
|
|
96
|
+
return {
|
|
97
|
+
user: {
|
|
98
|
+
id: profile.sub,
|
|
99
|
+
name: profile.name,
|
|
100
|
+
email: profile.email,
|
|
101
|
+
emailVerified: profile.email_verified || false,
|
|
102
|
+
image: profile.picture,
|
|
103
|
+
...userMap,
|
|
104
|
+
},
|
|
105
|
+
data: profile,
|
|
106
|
+
};
|
|
107
|
+
},
|
|
108
|
+
options,
|
|
109
|
+
} satisfies OAuthProvider<LinkedInProfile>;
|
|
110
|
+
};
|
|
@@ -0,0 +1,243 @@
|
|
|
1
|
+
import {
|
|
2
|
+
validateAuthorizationCode,
|
|
3
|
+
createAuthorizationURL,
|
|
4
|
+
refreshAccessToken,
|
|
5
|
+
} from "@better-auth/core/oauth2";
|
|
6
|
+
import type { OAuthProvider, ProviderOptions } from "@better-auth/core/oauth2";
|
|
7
|
+
import { betterFetch } from "@better-fetch/fetch";
|
|
8
|
+
import { logger } from "@better-auth/core/env";
|
|
9
|
+
import { decodeJwt } from "jose";
|
|
10
|
+
import { base64 } from "@better-auth/utils/base64";
|
|
11
|
+
|
|
12
|
+
/**
|
|
13
|
+
* @see [Microsoft Identity Platform - Optional claims reference](https://learn.microsoft.com/en-us/entra/identity-platform/optional-claims-reference)
|
|
14
|
+
*/
|
|
15
|
+
export interface MicrosoftEntraIDProfile extends Record<string, any> {
|
|
16
|
+
/** Identifies the intended recipient of the token */
|
|
17
|
+
aud: string;
|
|
18
|
+
/** Identifies the issuer, or "authorization server" that constructs and returns the token */
|
|
19
|
+
iss: string;
|
|
20
|
+
/** Indicates when the authentication for the token occurred */
|
|
21
|
+
iat: Date;
|
|
22
|
+
/** Records the identity provider that authenticated the subject of the token */
|
|
23
|
+
idp: string;
|
|
24
|
+
/** Identifies the time before which the JWT can't be accepted for processing */
|
|
25
|
+
nbf: Date;
|
|
26
|
+
/** Identifies the expiration time on or after which the JWT can't be accepted for processing */
|
|
27
|
+
exp: Date;
|
|
28
|
+
/** Code hash included in ID tokens when issued with an OAuth 2.0 authorization code */
|
|
29
|
+
c_hash: string;
|
|
30
|
+
/** Access token hash included in ID tokens when issued with an OAuth 2.0 access token */
|
|
31
|
+
at_hash: string;
|
|
32
|
+
/** Internal claim used to record data for token reuse */
|
|
33
|
+
aio: string;
|
|
34
|
+
/** The primary username that represents the user */
|
|
35
|
+
preferred_username: string;
|
|
36
|
+
/** User's email address */
|
|
37
|
+
email: string;
|
|
38
|
+
/** Human-readable value that identifies the subject of the token */
|
|
39
|
+
name: string;
|
|
40
|
+
/** Matches the parameter included in the original authorize request */
|
|
41
|
+
nonce: string;
|
|
42
|
+
/** User's profile picture */
|
|
43
|
+
picture: string;
|
|
44
|
+
/** Immutable identifier for the user account */
|
|
45
|
+
oid: string;
|
|
46
|
+
/** Set of roles assigned to the user */
|
|
47
|
+
roles: string[];
|
|
48
|
+
/** Internal claim used to revalidate tokens */
|
|
49
|
+
rh: string;
|
|
50
|
+
/** Subject identifier - unique to application ID */
|
|
51
|
+
sub: string;
|
|
52
|
+
/** Tenant ID the user is signing in to */
|
|
53
|
+
tid: string;
|
|
54
|
+
/** Unique identifier for a session */
|
|
55
|
+
sid: string;
|
|
56
|
+
/** Token identifier claim */
|
|
57
|
+
uti: string;
|
|
58
|
+
/** Indicates if user is in at least one group */
|
|
59
|
+
hasgroups: boolean;
|
|
60
|
+
/** User account status in tenant (0 = member, 1 = guest) */
|
|
61
|
+
acct: 0 | 1;
|
|
62
|
+
/** Auth Context IDs */
|
|
63
|
+
acrs: string;
|
|
64
|
+
/** Time when the user last authenticated */
|
|
65
|
+
auth_time: Date;
|
|
66
|
+
/** User's country/region */
|
|
67
|
+
ctry: string;
|
|
68
|
+
/** IP address of requesting client when inside VNET */
|
|
69
|
+
fwd: string;
|
|
70
|
+
/** Group claims */
|
|
71
|
+
groups: string;
|
|
72
|
+
/** Login hint for SSO */
|
|
73
|
+
login_hint: string;
|
|
74
|
+
/** Resource tenant's country/region */
|
|
75
|
+
tenant_ctry: string;
|
|
76
|
+
/** Region of the resource tenant */
|
|
77
|
+
tenant_region_scope: string;
|
|
78
|
+
/** UserPrincipalName */
|
|
79
|
+
upn: string;
|
|
80
|
+
/** User's verified primary email addresses */
|
|
81
|
+
verified_primary_email: string[];
|
|
82
|
+
/** User's verified secondary email addresses */
|
|
83
|
+
verified_secondary_email: string[];
|
|
84
|
+
/** VNET specifier information */
|
|
85
|
+
vnet: string;
|
|
86
|
+
/** Client Capabilities */
|
|
87
|
+
xms_cc: string;
|
|
88
|
+
/** Whether user's email domain is verified */
|
|
89
|
+
xms_edov: boolean;
|
|
90
|
+
/** Preferred data location for Multi-Geo tenants */
|
|
91
|
+
xms_pdl: string;
|
|
92
|
+
/** User preferred language */
|
|
93
|
+
xms_pl: string;
|
|
94
|
+
/** Tenant preferred language */
|
|
95
|
+
xms_tpl: string;
|
|
96
|
+
/** Zero-touch Deployment ID */
|
|
97
|
+
ztdid: string;
|
|
98
|
+
/** IP Address */
|
|
99
|
+
ipaddr: string;
|
|
100
|
+
/** On-premises Security Identifier */
|
|
101
|
+
onprem_sid: string;
|
|
102
|
+
/** Password Expiration Time */
|
|
103
|
+
pwd_exp: number;
|
|
104
|
+
/** Change Password URL */
|
|
105
|
+
pwd_url: string;
|
|
106
|
+
/** Inside Corporate Network flag */
|
|
107
|
+
in_corp: string;
|
|
108
|
+
/** User's family name/surname */
|
|
109
|
+
family_name: string;
|
|
110
|
+
/** User's given/first name */
|
|
111
|
+
given_name: string;
|
|
112
|
+
}
|
|
113
|
+
|
|
114
|
+
export interface MicrosoftOptions
|
|
115
|
+
extends ProviderOptions<MicrosoftEntraIDProfile> {
|
|
116
|
+
clientId: string;
|
|
117
|
+
/**
|
|
118
|
+
* The tenant ID of the Microsoft account
|
|
119
|
+
* @default "common"
|
|
120
|
+
*/
|
|
121
|
+
tenantId?: string;
|
|
122
|
+
/**
|
|
123
|
+
* The authentication authority URL. Use the default "https://login.microsoftonline.com" for standard Entra ID or "https://<tenant-id>.ciamlogin.com" for CIAM scenarios.
|
|
124
|
+
* @default "https://login.microsoftonline.com"
|
|
125
|
+
*/
|
|
126
|
+
authority?: string;
|
|
127
|
+
/**
|
|
128
|
+
* The size of the profile photo
|
|
129
|
+
* @default 48
|
|
130
|
+
*/
|
|
131
|
+
profilePhotoSize?: 48 | 64 | 96 | 120 | 240 | 360 | 432 | 504 | 648;
|
|
132
|
+
/**
|
|
133
|
+
* Disable profile photo
|
|
134
|
+
*/
|
|
135
|
+
disableProfilePhoto?: boolean;
|
|
136
|
+
}
|
|
137
|
+
|
|
138
|
+
export const microsoft = (options: MicrosoftOptions) => {
|
|
139
|
+
const tenant = options.tenantId || "common";
|
|
140
|
+
const authority = options.authority || "https://login.microsoftonline.com";
|
|
141
|
+
const authorizationEndpoint = `${authority}/${tenant}/oauth2/v2.0/authorize`;
|
|
142
|
+
const tokenEndpoint = `${authority}/${tenant}/oauth2/v2.0/token`;
|
|
143
|
+
return {
|
|
144
|
+
id: "microsoft",
|
|
145
|
+
name: "Microsoft EntraID",
|
|
146
|
+
createAuthorizationURL(data) {
|
|
147
|
+
const scopes = options.disableDefaultScope
|
|
148
|
+
? []
|
|
149
|
+
: ["openid", "profile", "email", "User.Read", "offline_access"];
|
|
150
|
+
options.scope && scopes.push(...options.scope);
|
|
151
|
+
data.scopes && scopes.push(...data.scopes);
|
|
152
|
+
return createAuthorizationURL({
|
|
153
|
+
id: "microsoft",
|
|
154
|
+
options,
|
|
155
|
+
authorizationEndpoint,
|
|
156
|
+
state: data.state,
|
|
157
|
+
codeVerifier: data.codeVerifier,
|
|
158
|
+
scopes,
|
|
159
|
+
redirectURI: data.redirectURI,
|
|
160
|
+
prompt: options.prompt,
|
|
161
|
+
loginHint: data.loginHint,
|
|
162
|
+
});
|
|
163
|
+
},
|
|
164
|
+
validateAuthorizationCode({ code, codeVerifier, redirectURI }) {
|
|
165
|
+
return validateAuthorizationCode({
|
|
166
|
+
code,
|
|
167
|
+
codeVerifier,
|
|
168
|
+
redirectURI,
|
|
169
|
+
options,
|
|
170
|
+
tokenEndpoint,
|
|
171
|
+
});
|
|
172
|
+
},
|
|
173
|
+
async getUserInfo(token) {
|
|
174
|
+
if (options.getUserInfo) {
|
|
175
|
+
return options.getUserInfo(token);
|
|
176
|
+
}
|
|
177
|
+
if (!token.idToken) {
|
|
178
|
+
return null;
|
|
179
|
+
}
|
|
180
|
+
const user = decodeJwt(token.idToken) as MicrosoftEntraIDProfile;
|
|
181
|
+
const profilePhotoSize = options.profilePhotoSize || 48;
|
|
182
|
+
await betterFetch<ArrayBuffer>(
|
|
183
|
+
`https://graph.microsoft.com/v1.0/me/photos/${profilePhotoSize}x${profilePhotoSize}/$value`,
|
|
184
|
+
{
|
|
185
|
+
headers: {
|
|
186
|
+
Authorization: `Bearer ${token.accessToken}`,
|
|
187
|
+
},
|
|
188
|
+
async onResponse(context) {
|
|
189
|
+
if (options.disableProfilePhoto || !context.response.ok) {
|
|
190
|
+
return;
|
|
191
|
+
}
|
|
192
|
+
try {
|
|
193
|
+
const response = context.response.clone();
|
|
194
|
+
const pictureBuffer = await response.arrayBuffer();
|
|
195
|
+
const pictureBase64 = base64.encode(pictureBuffer);
|
|
196
|
+
user.picture = `data:image/jpeg;base64, ${pictureBase64}`;
|
|
197
|
+
} catch (e) {
|
|
198
|
+
logger.error(
|
|
199
|
+
e && typeof e === "object" && "name" in e
|
|
200
|
+
? (e.name as string)
|
|
201
|
+
: "",
|
|
202
|
+
e,
|
|
203
|
+
);
|
|
204
|
+
}
|
|
205
|
+
},
|
|
206
|
+
},
|
|
207
|
+
);
|
|
208
|
+
const userMap = await options.mapProfileToUser?.(user);
|
|
209
|
+
return {
|
|
210
|
+
user: {
|
|
211
|
+
id: user.sub,
|
|
212
|
+
name: user.name,
|
|
213
|
+
email: user.email,
|
|
214
|
+
image: user.picture,
|
|
215
|
+
emailVerified: true,
|
|
216
|
+
...userMap,
|
|
217
|
+
},
|
|
218
|
+
data: user,
|
|
219
|
+
};
|
|
220
|
+
},
|
|
221
|
+
refreshAccessToken: options.refreshAccessToken
|
|
222
|
+
? options.refreshAccessToken
|
|
223
|
+
: async (refreshToken) => {
|
|
224
|
+
const scopes = options.disableDefaultScope
|
|
225
|
+
? []
|
|
226
|
+
: ["openid", "profile", "email", "User.Read", "offline_access"];
|
|
227
|
+
options.scope && scopes.push(...options.scope);
|
|
228
|
+
|
|
229
|
+
return refreshAccessToken({
|
|
230
|
+
refreshToken,
|
|
231
|
+
options: {
|
|
232
|
+
clientId: options.clientId,
|
|
233
|
+
clientSecret: options.clientSecret,
|
|
234
|
+
},
|
|
235
|
+
extraParams: {
|
|
236
|
+
scope: scopes.join(" "), // Include the scopes in request to microsoft
|
|
237
|
+
},
|
|
238
|
+
tokenEndpoint,
|
|
239
|
+
});
|
|
240
|
+
},
|
|
241
|
+
options,
|
|
242
|
+
} satisfies OAuthProvider;
|
|
243
|
+
};
|