@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,31 @@
|
|
|
1
|
+
import { defineErrorCodes } from "@better-auth/core/utils";
|
|
2
|
+
|
|
3
|
+
export const BASE_ERROR_CODES = defineErrorCodes({
|
|
4
|
+
USER_NOT_FOUND: "User not found",
|
|
5
|
+
FAILED_TO_CREATE_USER: "Failed to create user",
|
|
6
|
+
FAILED_TO_CREATE_SESSION: "Failed to create session",
|
|
7
|
+
FAILED_TO_UPDATE_USER: "Failed to update user",
|
|
8
|
+
FAILED_TO_GET_SESSION: "Failed to get session",
|
|
9
|
+
INVALID_PASSWORD: "Invalid password",
|
|
10
|
+
INVALID_EMAIL: "Invalid email",
|
|
11
|
+
INVALID_EMAIL_OR_PASSWORD: "Invalid email or password",
|
|
12
|
+
SOCIAL_ACCOUNT_ALREADY_LINKED: "Social account already linked",
|
|
13
|
+
PROVIDER_NOT_FOUND: "Provider not found",
|
|
14
|
+
INVALID_TOKEN: "Invalid token",
|
|
15
|
+
ID_TOKEN_NOT_SUPPORTED: "id_token not supported",
|
|
16
|
+
FAILED_TO_GET_USER_INFO: "Failed to get user info",
|
|
17
|
+
USER_EMAIL_NOT_FOUND: "User email not found",
|
|
18
|
+
EMAIL_NOT_VERIFIED: "Email not verified",
|
|
19
|
+
PASSWORD_TOO_SHORT: "Password too short",
|
|
20
|
+
PASSWORD_TOO_LONG: "Password too long",
|
|
21
|
+
USER_ALREADY_EXISTS: "User already exists.",
|
|
22
|
+
USER_ALREADY_EXISTS_USE_ANOTHER_EMAIL:
|
|
23
|
+
"User already exists. Use another email.",
|
|
24
|
+
EMAIL_CAN_NOT_BE_UPDATED: "Email can not be updated",
|
|
25
|
+
CREDENTIAL_ACCOUNT_NOT_FOUND: "Credential account not found",
|
|
26
|
+
SESSION_EXPIRED: "Session expired. Re-authenticate to perform this action.",
|
|
27
|
+
FAILED_TO_UNLINK_LAST_ACCOUNT: "You can't unlink your last account",
|
|
28
|
+
ACCOUNT_NOT_FOUND: "Account not found",
|
|
29
|
+
USER_ALREADY_HAS_PASSWORD:
|
|
30
|
+
"User already has a password. Provide that to delete the account.",
|
|
31
|
+
});
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
export class BetterAuthError extends Error {
|
|
2
|
+
constructor(message: string, cause?: string) {
|
|
3
|
+
super(message);
|
|
4
|
+
this.name = "BetterAuthError";
|
|
5
|
+
this.message = message;
|
|
6
|
+
this.cause = cause;
|
|
7
|
+
this.stack = "";
|
|
8
|
+
}
|
|
9
|
+
}
|
|
10
|
+
|
|
11
|
+
export { BASE_ERROR_CODES } from "./codes";
|
package/src/index.ts
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
export
|
|
1
|
+
export * from "./types";
|
|
@@ -0,0 +1,33 @@
|
|
|
1
|
+
import { createEndpoint, createMiddleware } from "better-call";
|
|
2
|
+
import type { AuthContext } from "../types";
|
|
3
|
+
|
|
4
|
+
export const optionsMiddleware = createMiddleware(async () => {
|
|
5
|
+
/**
|
|
6
|
+
* This will be passed on the instance of
|
|
7
|
+
* the context. Used to infer the type
|
|
8
|
+
* here.
|
|
9
|
+
*/
|
|
10
|
+
return {} as AuthContext;
|
|
11
|
+
});
|
|
12
|
+
|
|
13
|
+
export const createAuthMiddleware = createMiddleware.create({
|
|
14
|
+
use: [
|
|
15
|
+
optionsMiddleware,
|
|
16
|
+
/**
|
|
17
|
+
* Only use for post hooks
|
|
18
|
+
*/
|
|
19
|
+
createMiddleware(async () => {
|
|
20
|
+
return {} as {
|
|
21
|
+
returned?: unknown;
|
|
22
|
+
responseHeaders?: Headers;
|
|
23
|
+
};
|
|
24
|
+
}),
|
|
25
|
+
],
|
|
26
|
+
});
|
|
27
|
+
|
|
28
|
+
export const createAuthEndpoint = createEndpoint.create({
|
|
29
|
+
use: [optionsMiddleware],
|
|
30
|
+
});
|
|
31
|
+
|
|
32
|
+
export type AuthEndpoint = ReturnType<typeof createAuthEndpoint>;
|
|
33
|
+
export type AuthMiddleware = ReturnType<typeof createAuthMiddleware>;
|
|
@@ -0,0 +1,102 @@
|
|
|
1
|
+
import { betterFetch } from "@better-fetch/fetch";
|
|
2
|
+
import { base64Url } from "@better-auth/utils/base64";
|
|
3
|
+
import type { OAuth2Tokens, ProviderOptions } from "./oauth-provider";
|
|
4
|
+
|
|
5
|
+
export function createClientCredentialsTokenRequest({
|
|
6
|
+
options,
|
|
7
|
+
scope,
|
|
8
|
+
authentication,
|
|
9
|
+
resource,
|
|
10
|
+
}: {
|
|
11
|
+
options: ProviderOptions & { clientSecret: string };
|
|
12
|
+
scope?: string;
|
|
13
|
+
authentication?: "basic" | "post";
|
|
14
|
+
resource?: string | string[];
|
|
15
|
+
}) {
|
|
16
|
+
const body = new URLSearchParams();
|
|
17
|
+
const headers: Record<string, any> = {
|
|
18
|
+
"content-type": "application/x-www-form-urlencoded",
|
|
19
|
+
accept: "application/json",
|
|
20
|
+
};
|
|
21
|
+
|
|
22
|
+
body.set("grant_type", "client_credentials");
|
|
23
|
+
scope && body.set("scope", scope);
|
|
24
|
+
if (resource) {
|
|
25
|
+
if (typeof resource === "string") {
|
|
26
|
+
body.append("resource", resource);
|
|
27
|
+
} else {
|
|
28
|
+
for (const _resource of resource) {
|
|
29
|
+
body.append("resource", _resource);
|
|
30
|
+
}
|
|
31
|
+
}
|
|
32
|
+
}
|
|
33
|
+
if (authentication === "basic") {
|
|
34
|
+
const primaryClientId = Array.isArray(options.clientId)
|
|
35
|
+
? options.clientId[0]
|
|
36
|
+
: options.clientId;
|
|
37
|
+
const encodedCredentials = base64Url.encode(
|
|
38
|
+
`${primaryClientId}:${options.clientSecret}`,
|
|
39
|
+
);
|
|
40
|
+
headers["authorization"] = `Basic ${encodedCredentials}`;
|
|
41
|
+
} else {
|
|
42
|
+
const primaryClientId = Array.isArray(options.clientId)
|
|
43
|
+
? options.clientId[0]
|
|
44
|
+
: options.clientId;
|
|
45
|
+
body.set("client_id", primaryClientId);
|
|
46
|
+
body.set("client_secret", options.clientSecret);
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
return {
|
|
50
|
+
body,
|
|
51
|
+
headers,
|
|
52
|
+
};
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
export async function clientCredentialsToken({
|
|
56
|
+
options,
|
|
57
|
+
tokenEndpoint,
|
|
58
|
+
scope,
|
|
59
|
+
authentication,
|
|
60
|
+
resource,
|
|
61
|
+
}: {
|
|
62
|
+
options: ProviderOptions & { clientSecret: string };
|
|
63
|
+
tokenEndpoint: string;
|
|
64
|
+
scope: string;
|
|
65
|
+
authentication?: "basic" | "post";
|
|
66
|
+
resource?: string | string[];
|
|
67
|
+
}): Promise<OAuth2Tokens> {
|
|
68
|
+
const { body, headers } = createClientCredentialsTokenRequest({
|
|
69
|
+
options,
|
|
70
|
+
scope,
|
|
71
|
+
authentication,
|
|
72
|
+
resource,
|
|
73
|
+
});
|
|
74
|
+
|
|
75
|
+
const { data, error } = await betterFetch<{
|
|
76
|
+
access_token: string;
|
|
77
|
+
expires_in?: number;
|
|
78
|
+
token_type?: string;
|
|
79
|
+
scope?: string;
|
|
80
|
+
}>(tokenEndpoint, {
|
|
81
|
+
method: "POST",
|
|
82
|
+
body,
|
|
83
|
+
headers,
|
|
84
|
+
});
|
|
85
|
+
if (error) {
|
|
86
|
+
throw error;
|
|
87
|
+
}
|
|
88
|
+
const tokens: OAuth2Tokens = {
|
|
89
|
+
accessToken: data.access_token,
|
|
90
|
+
tokenType: data.token_type,
|
|
91
|
+
scopes: data.scope?.split(" "),
|
|
92
|
+
};
|
|
93
|
+
|
|
94
|
+
if (data.expires_in) {
|
|
95
|
+
const now = new Date();
|
|
96
|
+
tokens.accessTokenExpiresAt = new Date(
|
|
97
|
+
now.getTime() + data.expires_in * 1000,
|
|
98
|
+
);
|
|
99
|
+
}
|
|
100
|
+
|
|
101
|
+
return tokens;
|
|
102
|
+
}
|
|
@@ -0,0 +1,85 @@
|
|
|
1
|
+
import type { ProviderOptions } from "./index";
|
|
2
|
+
import { generateCodeChallenge } from "./utils";
|
|
3
|
+
|
|
4
|
+
export async function createAuthorizationURL({
|
|
5
|
+
id,
|
|
6
|
+
options,
|
|
7
|
+
authorizationEndpoint,
|
|
8
|
+
state,
|
|
9
|
+
codeVerifier,
|
|
10
|
+
scopes,
|
|
11
|
+
claims,
|
|
12
|
+
redirectURI,
|
|
13
|
+
duration,
|
|
14
|
+
prompt,
|
|
15
|
+
accessType,
|
|
16
|
+
responseType,
|
|
17
|
+
display,
|
|
18
|
+
loginHint,
|
|
19
|
+
hd,
|
|
20
|
+
responseMode,
|
|
21
|
+
additionalParams,
|
|
22
|
+
scopeJoiner,
|
|
23
|
+
}: {
|
|
24
|
+
id: string;
|
|
25
|
+
options: ProviderOptions;
|
|
26
|
+
redirectURI: string;
|
|
27
|
+
authorizationEndpoint: string;
|
|
28
|
+
state: string;
|
|
29
|
+
codeVerifier?: string;
|
|
30
|
+
scopes: string[];
|
|
31
|
+
claims?: string[];
|
|
32
|
+
duration?: string;
|
|
33
|
+
prompt?: string;
|
|
34
|
+
accessType?: string;
|
|
35
|
+
responseType?: string;
|
|
36
|
+
display?: string;
|
|
37
|
+
loginHint?: string;
|
|
38
|
+
hd?: string;
|
|
39
|
+
responseMode?: string;
|
|
40
|
+
additionalParams?: Record<string, string>;
|
|
41
|
+
scopeJoiner?: string;
|
|
42
|
+
}) {
|
|
43
|
+
const url = new URL(authorizationEndpoint);
|
|
44
|
+
url.searchParams.set("response_type", responseType || "code");
|
|
45
|
+
const primaryClientId = Array.isArray(options.clientId)
|
|
46
|
+
? options.clientId[0]
|
|
47
|
+
: options.clientId;
|
|
48
|
+
url.searchParams.set("client_id", primaryClientId);
|
|
49
|
+
url.searchParams.set("state", state);
|
|
50
|
+
url.searchParams.set("scope", scopes.join(scopeJoiner || " "));
|
|
51
|
+
url.searchParams.set("redirect_uri", options.redirectURI || redirectURI);
|
|
52
|
+
duration && url.searchParams.set("duration", duration);
|
|
53
|
+
display && url.searchParams.set("display", display);
|
|
54
|
+
loginHint && url.searchParams.set("login_hint", loginHint);
|
|
55
|
+
prompt && url.searchParams.set("prompt", prompt);
|
|
56
|
+
hd && url.searchParams.set("hd", hd);
|
|
57
|
+
accessType && url.searchParams.set("access_type", accessType);
|
|
58
|
+
responseMode && url.searchParams.set("response_mode", responseMode);
|
|
59
|
+
if (codeVerifier) {
|
|
60
|
+
const codeChallenge = await generateCodeChallenge(codeVerifier);
|
|
61
|
+
url.searchParams.set("code_challenge_method", "S256");
|
|
62
|
+
url.searchParams.set("code_challenge", codeChallenge);
|
|
63
|
+
}
|
|
64
|
+
if (claims) {
|
|
65
|
+
const claimsObj = claims.reduce(
|
|
66
|
+
(acc, claim) => {
|
|
67
|
+
acc[claim] = null;
|
|
68
|
+
return acc;
|
|
69
|
+
},
|
|
70
|
+
{} as Record<string, null>,
|
|
71
|
+
);
|
|
72
|
+
url.searchParams.set(
|
|
73
|
+
"claims",
|
|
74
|
+
JSON.stringify({
|
|
75
|
+
id_token: { email: null, email_verified: null, ...claimsObj },
|
|
76
|
+
}),
|
|
77
|
+
);
|
|
78
|
+
}
|
|
79
|
+
if (additionalParams) {
|
|
80
|
+
Object.entries(additionalParams).forEach(([key, value]) => {
|
|
81
|
+
url.searchParams.set(key, value);
|
|
82
|
+
});
|
|
83
|
+
}
|
|
84
|
+
return url;
|
|
85
|
+
}
|
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
export type {
|
|
2
|
+
OAuth2Tokens,
|
|
3
|
+
OAuthProvider,
|
|
4
|
+
OAuth2UserInfo,
|
|
5
|
+
ProviderOptions,
|
|
6
|
+
} from "./oauth-provider";
|
|
7
|
+
|
|
8
|
+
export { generateCodeChallenge, getOAuth2Tokens } from "./utils";
|
|
9
|
+
export { createAuthorizationURL } from "./create-authorization-url";
|
|
10
|
+
export {
|
|
11
|
+
createAuthorizationCodeRequest,
|
|
12
|
+
validateAuthorizationCode,
|
|
13
|
+
validateToken,
|
|
14
|
+
} from "./validate-authorization-code";
|
|
15
|
+
export {
|
|
16
|
+
createRefreshAccessTokenRequest,
|
|
17
|
+
refreshAccessToken,
|
|
18
|
+
} from "./refresh-access-token";
|
|
19
|
+
export {
|
|
20
|
+
clientCredentialsToken,
|
|
21
|
+
createClientCredentialsTokenRequest,
|
|
22
|
+
} from "./client-credentials-token";
|
|
@@ -0,0 +1,194 @@
|
|
|
1
|
+
import type { LiteralString } from "../types";
|
|
2
|
+
|
|
3
|
+
export interface OAuth2Tokens {
|
|
4
|
+
tokenType?: string;
|
|
5
|
+
accessToken?: string;
|
|
6
|
+
refreshToken?: string;
|
|
7
|
+
accessTokenExpiresAt?: Date;
|
|
8
|
+
refreshTokenExpiresAt?: Date;
|
|
9
|
+
scopes?: string[];
|
|
10
|
+
idToken?: string;
|
|
11
|
+
}
|
|
12
|
+
|
|
13
|
+
export type OAuth2UserInfo = {
|
|
14
|
+
id: string | number;
|
|
15
|
+
name?: string;
|
|
16
|
+
email?: string | null;
|
|
17
|
+
image?: string;
|
|
18
|
+
emailVerified: boolean;
|
|
19
|
+
};
|
|
20
|
+
|
|
21
|
+
export interface OAuthProvider<
|
|
22
|
+
T extends Record<string, any> = Record<string, any>,
|
|
23
|
+
O extends Record<string, any> = Partial<ProviderOptions>,
|
|
24
|
+
> {
|
|
25
|
+
id: LiteralString;
|
|
26
|
+
createAuthorizationURL: (data: {
|
|
27
|
+
state: string;
|
|
28
|
+
codeVerifier: string;
|
|
29
|
+
scopes?: string[];
|
|
30
|
+
redirectURI: string;
|
|
31
|
+
display?: string;
|
|
32
|
+
loginHint?: string;
|
|
33
|
+
}) => Promise<URL> | URL;
|
|
34
|
+
name: string;
|
|
35
|
+
validateAuthorizationCode: (data: {
|
|
36
|
+
code: string;
|
|
37
|
+
redirectURI: string;
|
|
38
|
+
codeVerifier?: string;
|
|
39
|
+
deviceId?: string;
|
|
40
|
+
}) => Promise<OAuth2Tokens>;
|
|
41
|
+
getUserInfo: (
|
|
42
|
+
token: OAuth2Tokens & {
|
|
43
|
+
/**
|
|
44
|
+
* The user object from the provider
|
|
45
|
+
* This is only available for some providers like Apple
|
|
46
|
+
*/
|
|
47
|
+
user?: {
|
|
48
|
+
name?: {
|
|
49
|
+
firstName?: string;
|
|
50
|
+
lastName?: string;
|
|
51
|
+
};
|
|
52
|
+
email?: string;
|
|
53
|
+
};
|
|
54
|
+
},
|
|
55
|
+
) => Promise<{
|
|
56
|
+
user: OAuth2UserInfo;
|
|
57
|
+
data: T;
|
|
58
|
+
} | null>;
|
|
59
|
+
/**
|
|
60
|
+
* Custom function to refresh a token
|
|
61
|
+
*/
|
|
62
|
+
refreshAccessToken?: (refreshToken: string) => Promise<OAuth2Tokens>;
|
|
63
|
+
revokeToken?: (token: string) => Promise<void>;
|
|
64
|
+
/**
|
|
65
|
+
* Verify the id token
|
|
66
|
+
* @param token - The id token
|
|
67
|
+
* @param nonce - The nonce
|
|
68
|
+
* @returns True if the id token is valid, false otherwise
|
|
69
|
+
*/
|
|
70
|
+
verifyIdToken?: (token: string, nonce?: string) => Promise<boolean>;
|
|
71
|
+
/**
|
|
72
|
+
* Disable implicit sign up for new users. When set to true for the provider,
|
|
73
|
+
* sign-in need to be called with with requestSignUp as true to create new users.
|
|
74
|
+
*/
|
|
75
|
+
disableImplicitSignUp?: boolean;
|
|
76
|
+
/**
|
|
77
|
+
* Disable sign up for new users.
|
|
78
|
+
*/
|
|
79
|
+
disableSignUp?: boolean;
|
|
80
|
+
/**
|
|
81
|
+
* Options for the provider
|
|
82
|
+
*/
|
|
83
|
+
options?: O;
|
|
84
|
+
}
|
|
85
|
+
|
|
86
|
+
export type ProviderOptions<Profile extends Record<string, any> = any> = {
|
|
87
|
+
/**
|
|
88
|
+
* The client ID of your application.
|
|
89
|
+
*
|
|
90
|
+
* This is usually a string but can be any type depending on the provider.
|
|
91
|
+
*/
|
|
92
|
+
clientId?: unknown;
|
|
93
|
+
/**
|
|
94
|
+
* The client secret of your application
|
|
95
|
+
*/
|
|
96
|
+
clientSecret?: string;
|
|
97
|
+
/**
|
|
98
|
+
* The scopes you want to request from the provider
|
|
99
|
+
*/
|
|
100
|
+
scope?: string[];
|
|
101
|
+
/**
|
|
102
|
+
* Remove default scopes of the provider
|
|
103
|
+
*/
|
|
104
|
+
disableDefaultScope?: boolean;
|
|
105
|
+
/**
|
|
106
|
+
* The redirect URL for your application. This is where the provider will
|
|
107
|
+
* redirect the user after the sign in process. Make sure this URL is
|
|
108
|
+
* whitelisted in the provider's dashboard.
|
|
109
|
+
*/
|
|
110
|
+
redirectURI?: string;
|
|
111
|
+
/**
|
|
112
|
+
* The client key of your application
|
|
113
|
+
* Tiktok Social Provider uses this field instead of clientId
|
|
114
|
+
*/
|
|
115
|
+
clientKey?: string;
|
|
116
|
+
/**
|
|
117
|
+
* Disable provider from allowing users to sign in
|
|
118
|
+
* with this provider with an id token sent from the
|
|
119
|
+
* client.
|
|
120
|
+
*/
|
|
121
|
+
disableIdTokenSignIn?: boolean;
|
|
122
|
+
/**
|
|
123
|
+
* verifyIdToken function to verify the id token
|
|
124
|
+
*/
|
|
125
|
+
verifyIdToken?: (token: string, nonce?: string) => Promise<boolean>;
|
|
126
|
+
/**
|
|
127
|
+
* Custom function to get user info from the provider
|
|
128
|
+
*/
|
|
129
|
+
getUserInfo?: (token: OAuth2Tokens) => Promise<{
|
|
130
|
+
user: {
|
|
131
|
+
id: string;
|
|
132
|
+
name?: string;
|
|
133
|
+
email?: string | null;
|
|
134
|
+
image?: string;
|
|
135
|
+
emailVerified: boolean;
|
|
136
|
+
[key: string]: any;
|
|
137
|
+
};
|
|
138
|
+
data: any;
|
|
139
|
+
}>;
|
|
140
|
+
/**
|
|
141
|
+
* Custom function to refresh a token
|
|
142
|
+
*/
|
|
143
|
+
refreshAccessToken?: (refreshToken: string) => Promise<OAuth2Tokens>;
|
|
144
|
+
/**
|
|
145
|
+
* Custom function to map the provider profile to a
|
|
146
|
+
* user.
|
|
147
|
+
*/
|
|
148
|
+
mapProfileToUser?: (profile: Profile) =>
|
|
149
|
+
| {
|
|
150
|
+
id?: string;
|
|
151
|
+
name?: string;
|
|
152
|
+
email?: string | null;
|
|
153
|
+
image?: string;
|
|
154
|
+
emailVerified?: boolean;
|
|
155
|
+
[key: string]: any;
|
|
156
|
+
}
|
|
157
|
+
| Promise<{
|
|
158
|
+
id?: string;
|
|
159
|
+
name?: string;
|
|
160
|
+
email?: string | null;
|
|
161
|
+
image?: string;
|
|
162
|
+
emailVerified?: boolean;
|
|
163
|
+
[key: string]: any;
|
|
164
|
+
}>;
|
|
165
|
+
/**
|
|
166
|
+
* Disable implicit sign up for new users. When set to true for the provider,
|
|
167
|
+
* sign-in need to be called with with requestSignUp as true to create new users.
|
|
168
|
+
*/
|
|
169
|
+
disableImplicitSignUp?: boolean;
|
|
170
|
+
/**
|
|
171
|
+
* Disable sign up for new users.
|
|
172
|
+
*/
|
|
173
|
+
disableSignUp?: boolean;
|
|
174
|
+
/**
|
|
175
|
+
* The prompt to use for the authorization code request
|
|
176
|
+
*/
|
|
177
|
+
prompt?:
|
|
178
|
+
| "select_account"
|
|
179
|
+
| "consent"
|
|
180
|
+
| "login"
|
|
181
|
+
| "none"
|
|
182
|
+
| "select_account consent";
|
|
183
|
+
/**
|
|
184
|
+
* The response mode to use for the authorization code request
|
|
185
|
+
*/
|
|
186
|
+
responseMode?: "query" | "form_post";
|
|
187
|
+
/**
|
|
188
|
+
* If enabled, the user info will be overridden with the provider user info
|
|
189
|
+
* This is useful if you want to use the provider user info to update the user info
|
|
190
|
+
*
|
|
191
|
+
* @default false
|
|
192
|
+
*/
|
|
193
|
+
overrideUserInfoOnSignIn?: boolean;
|
|
194
|
+
};
|
|
@@ -0,0 +1,124 @@
|
|
|
1
|
+
import { betterFetch } from "@better-fetch/fetch";
|
|
2
|
+
import type { OAuth2Tokens, ProviderOptions } from "./oauth-provider";
|
|
3
|
+
import { base64 } from "@better-auth/utils/base64";
|
|
4
|
+
|
|
5
|
+
export function createRefreshAccessTokenRequest({
|
|
6
|
+
refreshToken,
|
|
7
|
+
options,
|
|
8
|
+
authentication,
|
|
9
|
+
extraParams,
|
|
10
|
+
resource,
|
|
11
|
+
}: {
|
|
12
|
+
refreshToken: string;
|
|
13
|
+
options: Partial<ProviderOptions>;
|
|
14
|
+
authentication?: "basic" | "post";
|
|
15
|
+
extraParams?: Record<string, string>;
|
|
16
|
+
resource?: string | string[];
|
|
17
|
+
}) {
|
|
18
|
+
const body = new URLSearchParams();
|
|
19
|
+
const headers: Record<string, any> = {
|
|
20
|
+
"content-type": "application/x-www-form-urlencoded",
|
|
21
|
+
accept: "application/json",
|
|
22
|
+
};
|
|
23
|
+
|
|
24
|
+
body.set("grant_type", "refresh_token");
|
|
25
|
+
body.set("refresh_token", refreshToken);
|
|
26
|
+
// Use standard Base64 encoding for HTTP Basic Auth (OAuth2 spec, RFC 7617)
|
|
27
|
+
// Fixes compatibility with providers like Notion, Twitter, etc.
|
|
28
|
+
if (authentication === "basic") {
|
|
29
|
+
const primaryClientId = Array.isArray(options.clientId)
|
|
30
|
+
? options.clientId[0]
|
|
31
|
+
: options.clientId;
|
|
32
|
+
if (primaryClientId) {
|
|
33
|
+
headers["authorization"] =
|
|
34
|
+
"Basic " +
|
|
35
|
+
base64.encode(`${primaryClientId}:${options.clientSecret ?? ""}`);
|
|
36
|
+
} else {
|
|
37
|
+
headers["authorization"] =
|
|
38
|
+
"Basic " + base64.encode(`:${options.clientSecret ?? ""}`);
|
|
39
|
+
}
|
|
40
|
+
} else {
|
|
41
|
+
const primaryClientId = Array.isArray(options.clientId)
|
|
42
|
+
? options.clientId[0]
|
|
43
|
+
: options.clientId;
|
|
44
|
+
body.set("client_id", primaryClientId);
|
|
45
|
+
if (options.clientSecret) {
|
|
46
|
+
body.set("client_secret", options.clientSecret);
|
|
47
|
+
}
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
if (resource) {
|
|
51
|
+
if (typeof resource === "string") {
|
|
52
|
+
body.append("resource", resource);
|
|
53
|
+
} else {
|
|
54
|
+
for (const _resource of resource) {
|
|
55
|
+
body.append("resource", _resource);
|
|
56
|
+
}
|
|
57
|
+
}
|
|
58
|
+
}
|
|
59
|
+
if (extraParams) {
|
|
60
|
+
for (const [key, value] of Object.entries(extraParams)) {
|
|
61
|
+
body.set(key, value);
|
|
62
|
+
}
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
return {
|
|
66
|
+
body,
|
|
67
|
+
headers,
|
|
68
|
+
};
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
export async function refreshAccessToken({
|
|
72
|
+
refreshToken,
|
|
73
|
+
options,
|
|
74
|
+
tokenEndpoint,
|
|
75
|
+
authentication,
|
|
76
|
+
extraParams,
|
|
77
|
+
}: {
|
|
78
|
+
refreshToken: string;
|
|
79
|
+
options: Partial<ProviderOptions>;
|
|
80
|
+
tokenEndpoint: string;
|
|
81
|
+
authentication?: "basic" | "post";
|
|
82
|
+
extraParams?: Record<string, string>;
|
|
83
|
+
/** @deprecated always "refresh_token" */
|
|
84
|
+
grantType?: string;
|
|
85
|
+
}): Promise<OAuth2Tokens> {
|
|
86
|
+
const { body, headers } = createRefreshAccessTokenRequest({
|
|
87
|
+
refreshToken,
|
|
88
|
+
options,
|
|
89
|
+
authentication,
|
|
90
|
+
extraParams,
|
|
91
|
+
});
|
|
92
|
+
|
|
93
|
+
const { data, error } = await betterFetch<{
|
|
94
|
+
access_token: string;
|
|
95
|
+
refresh_token?: string;
|
|
96
|
+
expires_in?: number;
|
|
97
|
+
token_type?: string;
|
|
98
|
+
scope?: string;
|
|
99
|
+
id_token?: string;
|
|
100
|
+
}>(tokenEndpoint, {
|
|
101
|
+
method: "POST",
|
|
102
|
+
body,
|
|
103
|
+
headers,
|
|
104
|
+
});
|
|
105
|
+
if (error) {
|
|
106
|
+
throw error;
|
|
107
|
+
}
|
|
108
|
+
const tokens: OAuth2Tokens = {
|
|
109
|
+
accessToken: data.access_token,
|
|
110
|
+
refreshToken: data.refresh_token,
|
|
111
|
+
tokenType: data.token_type,
|
|
112
|
+
scopes: data.scope?.split(" "),
|
|
113
|
+
idToken: data.id_token,
|
|
114
|
+
};
|
|
115
|
+
|
|
116
|
+
if (data.expires_in) {
|
|
117
|
+
const now = new Date();
|
|
118
|
+
tokens.accessTokenExpiresAt = new Date(
|
|
119
|
+
now.getTime() + data.expires_in * 1000,
|
|
120
|
+
);
|
|
121
|
+
}
|
|
122
|
+
|
|
123
|
+
return tokens;
|
|
124
|
+
}
|
|
@@ -0,0 +1,36 @@
|
|
|
1
|
+
import { base64Url } from "@better-auth/utils/base64";
|
|
2
|
+
import type { OAuth2Tokens } from "./oauth-provider";
|
|
3
|
+
|
|
4
|
+
export function getOAuth2Tokens(data: Record<string, any>): OAuth2Tokens {
|
|
5
|
+
const getDate = (seconds: number) => {
|
|
6
|
+
const now = new Date();
|
|
7
|
+
return new Date(now.getTime() + seconds * 1000);
|
|
8
|
+
};
|
|
9
|
+
|
|
10
|
+
return {
|
|
11
|
+
tokenType: data.token_type,
|
|
12
|
+
accessToken: data.access_token,
|
|
13
|
+
refreshToken: data.refresh_token,
|
|
14
|
+
accessTokenExpiresAt: data.expires_in
|
|
15
|
+
? getDate(data.expires_in)
|
|
16
|
+
: undefined,
|
|
17
|
+
refreshTokenExpiresAt: data.refresh_token_expires_in
|
|
18
|
+
? getDate(data.refresh_token_expires_in)
|
|
19
|
+
: undefined,
|
|
20
|
+
scopes: data?.scope
|
|
21
|
+
? typeof data.scope === "string"
|
|
22
|
+
? data.scope.split(" ")
|
|
23
|
+
: data.scope
|
|
24
|
+
: [],
|
|
25
|
+
idToken: data.id_token,
|
|
26
|
+
};
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
export async function generateCodeChallenge(codeVerifier: string) {
|
|
30
|
+
const encoder = new TextEncoder();
|
|
31
|
+
const data = encoder.encode(codeVerifier);
|
|
32
|
+
const hash = await crypto.subtle.digest("SHA-256", data);
|
|
33
|
+
return base64Url.encode(new Uint8Array(hash), {
|
|
34
|
+
padding: false,
|
|
35
|
+
});
|
|
36
|
+
}
|