@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,156 @@
|
|
|
1
|
+
import { betterFetch } from "@better-fetch/fetch";
|
|
2
|
+
import { jwtVerify } from "jose";
|
|
3
|
+
import type { ProviderOptions } from "./index";
|
|
4
|
+
import { getOAuth2Tokens } from "./index";
|
|
5
|
+
import { base64 } from "@better-auth/utils/base64";
|
|
6
|
+
|
|
7
|
+
export function createAuthorizationCodeRequest({
|
|
8
|
+
code,
|
|
9
|
+
codeVerifier,
|
|
10
|
+
redirectURI,
|
|
11
|
+
options,
|
|
12
|
+
authentication,
|
|
13
|
+
deviceId,
|
|
14
|
+
headers,
|
|
15
|
+
additionalParams = {},
|
|
16
|
+
resource,
|
|
17
|
+
}: {
|
|
18
|
+
code: string;
|
|
19
|
+
redirectURI: string;
|
|
20
|
+
options: Partial<ProviderOptions>;
|
|
21
|
+
codeVerifier?: string;
|
|
22
|
+
deviceId?: string;
|
|
23
|
+
authentication?: "basic" | "post";
|
|
24
|
+
headers?: Record<string, string>;
|
|
25
|
+
additionalParams?: Record<string, string>;
|
|
26
|
+
resource?: string | string[];
|
|
27
|
+
}) {
|
|
28
|
+
const body = new URLSearchParams();
|
|
29
|
+
const requestHeaders: Record<string, any> = {
|
|
30
|
+
"content-type": "application/x-www-form-urlencoded",
|
|
31
|
+
accept: "application/json",
|
|
32
|
+
"user-agent": "better-auth",
|
|
33
|
+
...headers,
|
|
34
|
+
};
|
|
35
|
+
body.set("grant_type", "authorization_code");
|
|
36
|
+
body.set("code", code);
|
|
37
|
+
codeVerifier && body.set("code_verifier", codeVerifier);
|
|
38
|
+
options.clientKey && body.set("client_key", options.clientKey);
|
|
39
|
+
deviceId && body.set("device_id", deviceId);
|
|
40
|
+
body.set("redirect_uri", options.redirectURI || redirectURI);
|
|
41
|
+
if (resource) {
|
|
42
|
+
if (typeof resource === "string") {
|
|
43
|
+
body.append("resource", resource);
|
|
44
|
+
} else {
|
|
45
|
+
for (const _resource of resource) {
|
|
46
|
+
body.append("resource", _resource);
|
|
47
|
+
}
|
|
48
|
+
}
|
|
49
|
+
}
|
|
50
|
+
// Use standard Base64 encoding for HTTP Basic Auth (OAuth2 spec, RFC 7617)
|
|
51
|
+
// Fixes compatibility with providers like Notion, Twitter, etc.
|
|
52
|
+
if (authentication === "basic") {
|
|
53
|
+
const primaryClientId = Array.isArray(options.clientId)
|
|
54
|
+
? options.clientId[0]
|
|
55
|
+
: options.clientId;
|
|
56
|
+
const encodedCredentials = base64.encode(
|
|
57
|
+
`${primaryClientId}:${options.clientSecret ?? ""}`,
|
|
58
|
+
);
|
|
59
|
+
requestHeaders["authorization"] = `Basic ${encodedCredentials}`;
|
|
60
|
+
} else {
|
|
61
|
+
const primaryClientId = Array.isArray(options.clientId)
|
|
62
|
+
? options.clientId[0]
|
|
63
|
+
: options.clientId;
|
|
64
|
+
body.set("client_id", primaryClientId);
|
|
65
|
+
if (options.clientSecret) {
|
|
66
|
+
body.set("client_secret", options.clientSecret);
|
|
67
|
+
}
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
for (const [key, value] of Object.entries(additionalParams)) {
|
|
71
|
+
if (!body.has(key)) body.append(key, value);
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
return {
|
|
75
|
+
body,
|
|
76
|
+
headers: requestHeaders,
|
|
77
|
+
};
|
|
78
|
+
}
|
|
79
|
+
|
|
80
|
+
export async function validateAuthorizationCode({
|
|
81
|
+
code,
|
|
82
|
+
codeVerifier,
|
|
83
|
+
redirectURI,
|
|
84
|
+
options,
|
|
85
|
+
tokenEndpoint,
|
|
86
|
+
authentication,
|
|
87
|
+
deviceId,
|
|
88
|
+
headers,
|
|
89
|
+
additionalParams = {},
|
|
90
|
+
resource,
|
|
91
|
+
}: {
|
|
92
|
+
code: string;
|
|
93
|
+
redirectURI: string;
|
|
94
|
+
options: Partial<ProviderOptions>;
|
|
95
|
+
codeVerifier?: string;
|
|
96
|
+
deviceId?: string;
|
|
97
|
+
tokenEndpoint: string;
|
|
98
|
+
authentication?: "basic" | "post";
|
|
99
|
+
headers?: Record<string, string>;
|
|
100
|
+
additionalParams?: Record<string, string>;
|
|
101
|
+
resource?: string | string[];
|
|
102
|
+
}) {
|
|
103
|
+
const { body, headers: requestHeaders } = createAuthorizationCodeRequest({
|
|
104
|
+
code,
|
|
105
|
+
codeVerifier,
|
|
106
|
+
redirectURI,
|
|
107
|
+
options,
|
|
108
|
+
authentication,
|
|
109
|
+
deviceId,
|
|
110
|
+
headers,
|
|
111
|
+
additionalParams,
|
|
112
|
+
resource,
|
|
113
|
+
});
|
|
114
|
+
|
|
115
|
+
const { data, error } = await betterFetch<object>(tokenEndpoint, {
|
|
116
|
+
method: "POST",
|
|
117
|
+
body: body,
|
|
118
|
+
headers: requestHeaders,
|
|
119
|
+
});
|
|
120
|
+
|
|
121
|
+
if (error) {
|
|
122
|
+
throw error;
|
|
123
|
+
}
|
|
124
|
+
const tokens = getOAuth2Tokens(data);
|
|
125
|
+
return tokens;
|
|
126
|
+
}
|
|
127
|
+
|
|
128
|
+
export async function validateToken(token: string, jwksEndpoint: string) {
|
|
129
|
+
const { data, error } = await betterFetch<{
|
|
130
|
+
keys: {
|
|
131
|
+
kid: string;
|
|
132
|
+
kty: string;
|
|
133
|
+
use: string;
|
|
134
|
+
n: string;
|
|
135
|
+
e: string;
|
|
136
|
+
x5c: string[];
|
|
137
|
+
}[];
|
|
138
|
+
}>(jwksEndpoint, {
|
|
139
|
+
method: "GET",
|
|
140
|
+
headers: {
|
|
141
|
+
accept: "application/json",
|
|
142
|
+
"user-agent": "better-auth",
|
|
143
|
+
},
|
|
144
|
+
});
|
|
145
|
+
if (error) {
|
|
146
|
+
throw error;
|
|
147
|
+
}
|
|
148
|
+
const keys = data["keys"];
|
|
149
|
+
const header = JSON.parse(atob(token.split(".")[0]!));
|
|
150
|
+
const key = keys.find((key) => key.kid === header.kid);
|
|
151
|
+
if (!key) {
|
|
152
|
+
throw new Error("Key not found");
|
|
153
|
+
}
|
|
154
|
+
const verified = await jwtVerify(token, key);
|
|
155
|
+
return verified;
|
|
156
|
+
}
|
|
@@ -0,0 +1,213 @@
|
|
|
1
|
+
import { betterFetch } from "@better-fetch/fetch";
|
|
2
|
+
import { APIError } from "better-call";
|
|
3
|
+
import { decodeJwt, decodeProtectedHeader, importJWK, jwtVerify } from "jose";
|
|
4
|
+
import type { OAuthProvider, ProviderOptions } from "@better-auth/core/oauth2";
|
|
5
|
+
import {
|
|
6
|
+
refreshAccessToken,
|
|
7
|
+
createAuthorizationURL,
|
|
8
|
+
validateAuthorizationCode,
|
|
9
|
+
} from "@better-auth/core/oauth2";
|
|
10
|
+
export interface AppleProfile {
|
|
11
|
+
/**
|
|
12
|
+
* The subject registered claim identifies the principal that’s the subject
|
|
13
|
+
* of the identity token. Because this token is for your app, the value is
|
|
14
|
+
* the unique identifier for the user.
|
|
15
|
+
*/
|
|
16
|
+
sub: string;
|
|
17
|
+
/**
|
|
18
|
+
* A String value representing the user's email address.
|
|
19
|
+
* The email address is either the user's real email address or the proxy
|
|
20
|
+
* address, depending on their status private email relay service.
|
|
21
|
+
*/
|
|
22
|
+
email: string;
|
|
23
|
+
/**
|
|
24
|
+
* A string or Boolean value that indicates whether the service verifies
|
|
25
|
+
* the email. The value can either be a string ("true" or "false") or a
|
|
26
|
+
* Boolean (true or false). The system may not verify email addresses for
|
|
27
|
+
* Sign in with Apple at Work & School users, and this claim is "false" or
|
|
28
|
+
* false for those users.
|
|
29
|
+
*/
|
|
30
|
+
email_verified: true | "true";
|
|
31
|
+
/**
|
|
32
|
+
* A string or Boolean value that indicates whether the email that the user
|
|
33
|
+
* shares is the proxy address. The value can either be a string ("true" or
|
|
34
|
+
* "false") or a Boolean (true or false).
|
|
35
|
+
*/
|
|
36
|
+
is_private_email: boolean;
|
|
37
|
+
/**
|
|
38
|
+
* An Integer value that indicates whether the user appears to be a real
|
|
39
|
+
* person. Use the value of this claim to mitigate fraud. The possible
|
|
40
|
+
* values are: 0 (or Unsupported), 1 (or Unknown), 2 (or LikelyReal). For
|
|
41
|
+
* more information, see ASUserDetectionStatus. This claim is present only
|
|
42
|
+
* in iOS 14 and later, macOS 11 and later, watchOS 7 and later, tvOS 14
|
|
43
|
+
* and later. The claim isn’t present or supported for web-based apps.
|
|
44
|
+
*/
|
|
45
|
+
real_user_status: number;
|
|
46
|
+
/**
|
|
47
|
+
* The user’s full name in the format provided during the authorization
|
|
48
|
+
* process.
|
|
49
|
+
*/
|
|
50
|
+
name: string;
|
|
51
|
+
/**
|
|
52
|
+
* The URL to the user's profile picture.
|
|
53
|
+
*/
|
|
54
|
+
picture: string;
|
|
55
|
+
user?: AppleNonConformUser;
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
/**
|
|
59
|
+
* This is the shape of the `user` query parameter that Apple sends the first
|
|
60
|
+
* time the user consents to the app.
|
|
61
|
+
* @see https://developer.apple.com/documentation/signinwithapplerestapi/request-an-authorization-to-the-sign-in-with-apple-server./
|
|
62
|
+
*/
|
|
63
|
+
export interface AppleNonConformUser {
|
|
64
|
+
name: {
|
|
65
|
+
firstName: string;
|
|
66
|
+
lastName: string;
|
|
67
|
+
};
|
|
68
|
+
email: string;
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
export interface AppleOptions extends ProviderOptions<AppleProfile> {
|
|
72
|
+
clientId: string;
|
|
73
|
+
appBundleIdentifier?: string;
|
|
74
|
+
audience?: string | string[];
|
|
75
|
+
}
|
|
76
|
+
|
|
77
|
+
export const apple = (options: AppleOptions) => {
|
|
78
|
+
const tokenEndpoint = "https://appleid.apple.com/auth/token";
|
|
79
|
+
return {
|
|
80
|
+
id: "apple",
|
|
81
|
+
name: "Apple",
|
|
82
|
+
async createAuthorizationURL({ state, scopes, redirectURI }) {
|
|
83
|
+
const _scope = options.disableDefaultScope ? [] : ["email", "name"];
|
|
84
|
+
options.scope && _scope.push(...options.scope);
|
|
85
|
+
scopes && _scope.push(...scopes);
|
|
86
|
+
const url = await createAuthorizationURL({
|
|
87
|
+
id: "apple",
|
|
88
|
+
options,
|
|
89
|
+
authorizationEndpoint: "https://appleid.apple.com/auth/authorize",
|
|
90
|
+
scopes: _scope,
|
|
91
|
+
state,
|
|
92
|
+
redirectURI,
|
|
93
|
+
responseMode: "form_post",
|
|
94
|
+
responseType: "code id_token",
|
|
95
|
+
});
|
|
96
|
+
return url;
|
|
97
|
+
},
|
|
98
|
+
validateAuthorizationCode: async ({ code, codeVerifier, redirectURI }) => {
|
|
99
|
+
return validateAuthorizationCode({
|
|
100
|
+
code,
|
|
101
|
+
codeVerifier,
|
|
102
|
+
redirectURI,
|
|
103
|
+
options,
|
|
104
|
+
tokenEndpoint,
|
|
105
|
+
});
|
|
106
|
+
},
|
|
107
|
+
async verifyIdToken(token, nonce) {
|
|
108
|
+
if (options.disableIdTokenSignIn) {
|
|
109
|
+
return false;
|
|
110
|
+
}
|
|
111
|
+
if (options.verifyIdToken) {
|
|
112
|
+
return options.verifyIdToken(token, nonce);
|
|
113
|
+
}
|
|
114
|
+
const decodedHeader = decodeProtectedHeader(token);
|
|
115
|
+
const { kid, alg: jwtAlg } = decodedHeader;
|
|
116
|
+
if (!kid || !jwtAlg) return false;
|
|
117
|
+
const publicKey = await getApplePublicKey(kid);
|
|
118
|
+
const { payload: jwtClaims } = await jwtVerify(token, publicKey, {
|
|
119
|
+
algorithms: [jwtAlg],
|
|
120
|
+
issuer: "https://appleid.apple.com",
|
|
121
|
+
audience:
|
|
122
|
+
options.audience && options.audience.length
|
|
123
|
+
? options.audience
|
|
124
|
+
: options.appBundleIdentifier
|
|
125
|
+
? options.appBundleIdentifier
|
|
126
|
+
: options.clientId,
|
|
127
|
+
maxTokenAge: "1h",
|
|
128
|
+
});
|
|
129
|
+
["email_verified", "is_private_email"].forEach((field) => {
|
|
130
|
+
if (jwtClaims[field] !== undefined) {
|
|
131
|
+
jwtClaims[field] = Boolean(jwtClaims[field]);
|
|
132
|
+
}
|
|
133
|
+
});
|
|
134
|
+
if (nonce && jwtClaims.nonce !== nonce) {
|
|
135
|
+
return false;
|
|
136
|
+
}
|
|
137
|
+
return !!jwtClaims;
|
|
138
|
+
},
|
|
139
|
+
refreshAccessToken: options.refreshAccessToken
|
|
140
|
+
? options.refreshAccessToken
|
|
141
|
+
: async (refreshToken) => {
|
|
142
|
+
return refreshAccessToken({
|
|
143
|
+
refreshToken,
|
|
144
|
+
options: {
|
|
145
|
+
clientId: options.clientId,
|
|
146
|
+
clientKey: options.clientKey,
|
|
147
|
+
clientSecret: options.clientSecret,
|
|
148
|
+
},
|
|
149
|
+
tokenEndpoint: "https://appleid.apple.com/auth/token",
|
|
150
|
+
});
|
|
151
|
+
},
|
|
152
|
+
async getUserInfo(token) {
|
|
153
|
+
if (options.getUserInfo) {
|
|
154
|
+
return options.getUserInfo(token);
|
|
155
|
+
}
|
|
156
|
+
if (!token.idToken) {
|
|
157
|
+
return null;
|
|
158
|
+
}
|
|
159
|
+
const profile = decodeJwt<AppleProfile>(token.idToken);
|
|
160
|
+
if (!profile) {
|
|
161
|
+
return null;
|
|
162
|
+
}
|
|
163
|
+
const name = token.user
|
|
164
|
+
? `${token.user.name?.firstName} ${token.user.name?.lastName}`
|
|
165
|
+
: profile.name || profile.email;
|
|
166
|
+
const emailVerified =
|
|
167
|
+
typeof profile.email_verified === "boolean"
|
|
168
|
+
? profile.email_verified
|
|
169
|
+
: profile.email_verified === "true";
|
|
170
|
+
const enrichedProfile = {
|
|
171
|
+
...profile,
|
|
172
|
+
name,
|
|
173
|
+
};
|
|
174
|
+
const userMap = await options.mapProfileToUser?.(enrichedProfile);
|
|
175
|
+
return {
|
|
176
|
+
user: {
|
|
177
|
+
id: profile.sub,
|
|
178
|
+
name: enrichedProfile.name,
|
|
179
|
+
emailVerified: emailVerified,
|
|
180
|
+
email: profile.email,
|
|
181
|
+
...userMap,
|
|
182
|
+
},
|
|
183
|
+
data: enrichedProfile,
|
|
184
|
+
};
|
|
185
|
+
},
|
|
186
|
+
options,
|
|
187
|
+
} satisfies OAuthProvider<AppleProfile>;
|
|
188
|
+
};
|
|
189
|
+
|
|
190
|
+
export const getApplePublicKey = async (kid: string) => {
|
|
191
|
+
const APPLE_BASE_URL = "https://appleid.apple.com";
|
|
192
|
+
const JWKS_APPLE_URI = "/auth/keys";
|
|
193
|
+
const { data } = await betterFetch<{
|
|
194
|
+
keys: Array<{
|
|
195
|
+
kid: string;
|
|
196
|
+
alg: string;
|
|
197
|
+
kty: string;
|
|
198
|
+
use: string;
|
|
199
|
+
n: string;
|
|
200
|
+
e: string;
|
|
201
|
+
}>;
|
|
202
|
+
}>(`${APPLE_BASE_URL}${JWKS_APPLE_URI}`);
|
|
203
|
+
if (!data?.keys) {
|
|
204
|
+
throw new APIError("BAD_REQUEST", {
|
|
205
|
+
message: "Keys not found",
|
|
206
|
+
});
|
|
207
|
+
}
|
|
208
|
+
const jwk = data.keys.find((key) => key.kid === kid);
|
|
209
|
+
if (!jwk) {
|
|
210
|
+
throw new Error(`JWK with kid ${kid} not found`);
|
|
211
|
+
}
|
|
212
|
+
return await importJWK(jwk, jwk.alg);
|
|
213
|
+
};
|
|
@@ -0,0 +1,130 @@
|
|
|
1
|
+
import { betterFetch } from "@better-fetch/fetch";
|
|
2
|
+
import { BetterAuthError } from "../error";
|
|
3
|
+
import type { OAuthProvider, ProviderOptions } from "@better-auth/core/oauth2";
|
|
4
|
+
import {
|
|
5
|
+
createAuthorizationURL,
|
|
6
|
+
validateAuthorizationCode,
|
|
7
|
+
} from "@better-auth/core/oauth2";
|
|
8
|
+
import { logger } from "@better-auth/core/env";
|
|
9
|
+
import { refreshAccessToken } from "@better-auth/core/oauth2";
|
|
10
|
+
|
|
11
|
+
export interface AtlassianProfile {
|
|
12
|
+
account_type?: string;
|
|
13
|
+
account_id: string;
|
|
14
|
+
email?: string;
|
|
15
|
+
name: string;
|
|
16
|
+
picture?: string;
|
|
17
|
+
nickname?: string;
|
|
18
|
+
locale?: string;
|
|
19
|
+
extended_profile?: {
|
|
20
|
+
job_title?: string;
|
|
21
|
+
organization?: string;
|
|
22
|
+
department?: string;
|
|
23
|
+
location?: string;
|
|
24
|
+
};
|
|
25
|
+
}
|
|
26
|
+
export interface AtlassianOptions extends ProviderOptions<AtlassianProfile> {
|
|
27
|
+
clientId: string;
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
export const atlassian = (options: AtlassianOptions) => {
|
|
31
|
+
return {
|
|
32
|
+
id: "atlassian",
|
|
33
|
+
name: "Atlassian",
|
|
34
|
+
|
|
35
|
+
async createAuthorizationURL({ state, scopes, codeVerifier, redirectURI }) {
|
|
36
|
+
if (!options.clientId || !options.clientSecret) {
|
|
37
|
+
logger.error("Client Id and Secret are required for Atlassian");
|
|
38
|
+
throw new BetterAuthError("CLIENT_ID_AND_SECRET_REQUIRED");
|
|
39
|
+
}
|
|
40
|
+
if (!codeVerifier) {
|
|
41
|
+
throw new BetterAuthError("codeVerifier is required for Atlassian");
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
const _scopes = options.disableDefaultScope
|
|
45
|
+
? []
|
|
46
|
+
: ["read:jira-user", "offline_access"];
|
|
47
|
+
options.scope && _scopes.push(...options.scope);
|
|
48
|
+
scopes && _scopes.push(...scopes);
|
|
49
|
+
|
|
50
|
+
return createAuthorizationURL({
|
|
51
|
+
id: "atlassian",
|
|
52
|
+
options,
|
|
53
|
+
authorizationEndpoint: "https://auth.atlassian.com/authorize",
|
|
54
|
+
scopes: _scopes,
|
|
55
|
+
state,
|
|
56
|
+
codeVerifier,
|
|
57
|
+
redirectURI,
|
|
58
|
+
additionalParams: {
|
|
59
|
+
audience: "api.atlassian.com",
|
|
60
|
+
},
|
|
61
|
+
prompt: options.prompt,
|
|
62
|
+
});
|
|
63
|
+
},
|
|
64
|
+
|
|
65
|
+
validateAuthorizationCode: async ({ code, codeVerifier, redirectURI }) => {
|
|
66
|
+
return validateAuthorizationCode({
|
|
67
|
+
code,
|
|
68
|
+
codeVerifier,
|
|
69
|
+
redirectURI,
|
|
70
|
+
options,
|
|
71
|
+
tokenEndpoint: "https://auth.atlassian.com/oauth/token",
|
|
72
|
+
});
|
|
73
|
+
},
|
|
74
|
+
|
|
75
|
+
refreshAccessToken: options.refreshAccessToken
|
|
76
|
+
? options.refreshAccessToken
|
|
77
|
+
: async (refreshToken) => {
|
|
78
|
+
return refreshAccessToken({
|
|
79
|
+
refreshToken,
|
|
80
|
+
options: {
|
|
81
|
+
clientId: options.clientId,
|
|
82
|
+
clientSecret: options.clientSecret,
|
|
83
|
+
},
|
|
84
|
+
tokenEndpoint: "https://auth.atlassian.com/oauth/token",
|
|
85
|
+
});
|
|
86
|
+
},
|
|
87
|
+
|
|
88
|
+
async getUserInfo(token) {
|
|
89
|
+
if (options.getUserInfo) {
|
|
90
|
+
return options.getUserInfo(token);
|
|
91
|
+
}
|
|
92
|
+
|
|
93
|
+
if (!token.accessToken) {
|
|
94
|
+
return null;
|
|
95
|
+
}
|
|
96
|
+
|
|
97
|
+
try {
|
|
98
|
+
const { data: profile } = await betterFetch<{
|
|
99
|
+
account_id: string;
|
|
100
|
+
name: string;
|
|
101
|
+
email?: string;
|
|
102
|
+
picture?: string;
|
|
103
|
+
}>("https://api.atlassian.com/me", {
|
|
104
|
+
headers: { Authorization: `Bearer ${token.accessToken}` },
|
|
105
|
+
});
|
|
106
|
+
|
|
107
|
+
if (!profile) return null;
|
|
108
|
+
|
|
109
|
+
const userMap = await options.mapProfileToUser?.(profile);
|
|
110
|
+
|
|
111
|
+
return {
|
|
112
|
+
user: {
|
|
113
|
+
id: profile.account_id,
|
|
114
|
+
name: profile.name,
|
|
115
|
+
email: profile.email,
|
|
116
|
+
image: profile.picture,
|
|
117
|
+
emailVerified: false,
|
|
118
|
+
...userMap,
|
|
119
|
+
},
|
|
120
|
+
data: profile,
|
|
121
|
+
};
|
|
122
|
+
} catch (error) {
|
|
123
|
+
logger.error("Failed to fetch user info from Figma:", error);
|
|
124
|
+
return null;
|
|
125
|
+
}
|
|
126
|
+
},
|
|
127
|
+
|
|
128
|
+
options,
|
|
129
|
+
} satisfies OAuthProvider<AtlassianProfile>;
|
|
130
|
+
};
|