@better-auth/core 1.4.12-beta.2 → 1.4.13
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 +172 -35
- package/dist/api/index.d.mts +178 -1
- package/dist/api/index.mjs +2 -1
- package/dist/context/endpoint-context.d.mts +19 -0
- package/dist/context/endpoint-context.mjs +31 -0
- package/dist/context/global.d.mts +7 -0
- package/dist/context/global.mjs +37 -0
- package/dist/context/index.d.mts +5 -53
- package/dist/context/index.mjs +5 -2
- package/dist/context/request-state.d.mts +27 -0
- package/dist/context/request-state.mjs +49 -0
- package/dist/context/transaction.d.mts +16 -0
- package/dist/context/transaction.mjs +52 -0
- package/dist/db/adapter/factory.d.mts +27 -0
- package/dist/db/adapter/factory.mjs +738 -0
- package/dist/db/adapter/get-default-field-name.d.mts +18 -0
- package/dist/db/adapter/get-default-field-name.mjs +38 -0
- package/dist/db/adapter/get-default-model-name.d.mts +12 -0
- package/dist/db/adapter/get-default-model-name.mjs +32 -0
- package/dist/db/adapter/get-field-attributes.d.mts +29 -0
- package/dist/db/adapter/get-field-attributes.mjs +39 -0
- package/dist/db/adapter/get-field-name.d.mts +18 -0
- package/dist/db/adapter/get-field-name.mjs +33 -0
- package/dist/db/adapter/get-id-field.d.mts +39 -0
- package/dist/db/adapter/get-id-field.mjs +68 -0
- package/dist/db/adapter/get-model-name.d.mts +12 -0
- package/dist/db/adapter/get-model-name.mjs +23 -0
- package/dist/db/adapter/index.d.mts +513 -1
- package/dist/db/adapter/index.mjs +8 -970
- package/dist/db/adapter/types.d.mts +139 -0
- package/dist/db/adapter/utils.d.mts +7 -0
- package/dist/db/adapter/utils.mjs +38 -0
- package/dist/db/get-tables.d.mts +8 -0
- package/dist/{get-tables-CMc_Emww.mjs → db/get-tables.mjs} +1 -1
- package/dist/db/index.d.mts +10 -2
- package/dist/db/index.mjs +7 -60
- package/dist/db/plugin.d.mts +12 -0
- package/dist/db/schema/account.d.mts +26 -0
- package/dist/db/schema/account.mjs +19 -0
- package/dist/db/schema/rate-limit.d.mts +14 -0
- package/dist/db/schema/rate-limit.mjs +11 -0
- package/dist/db/schema/session.d.mts +21 -0
- package/dist/db/schema/session.mjs +14 -0
- package/dist/db/schema/shared.d.mts +10 -0
- package/dist/db/schema/shared.mjs +11 -0
- package/dist/db/schema/user.d.mts +20 -0
- package/dist/db/schema/user.mjs +13 -0
- package/dist/db/schema/verification.d.mts +19 -0
- package/dist/db/schema/verification.mjs +12 -0
- package/dist/db/type.d.mts +143 -0
- package/dist/env/color-depth.d.mts +4 -0
- package/dist/env/color-depth.mjs +88 -0
- package/dist/env/env-impl.d.mts +32 -0
- package/dist/env/env-impl.mjs +82 -0
- package/dist/env/index.d.mts +4 -2
- package/dist/env/index.mjs +3 -1
- package/dist/{index-BRBu0-5h.d.mts → env/logger.d.mts} +1 -35
- package/dist/env/logger.mjs +81 -0
- package/dist/error/codes.d.mts +48 -0
- package/dist/{error-DP1xOn7P.mjs → error/codes.mjs} +3 -14
- package/dist/error/index.d.mts +5 -48
- package/dist/error/index.mjs +12 -3
- package/dist/index.d.mts +8 -2
- package/dist/oauth2/client-credentials-token.d.mts +36 -0
- package/dist/oauth2/client-credentials-token.mjs +54 -0
- package/dist/oauth2/create-authorization-url.d.mts +45 -0
- package/dist/oauth2/create-authorization-url.mjs +42 -0
- package/dist/oauth2/index.d.mts +8 -2
- package/dist/oauth2/index.mjs +6 -2
- package/dist/oauth2/oauth-provider.d.mts +194 -0
- package/dist/oauth2/refresh-access-token.d.mts +36 -0
- package/dist/oauth2/refresh-access-token.mjs +58 -0
- package/dist/oauth2/utils.d.mts +7 -0
- package/dist/oauth2/utils.mjs +27 -0
- package/dist/oauth2/validate-authorization-code.d.mts +55 -0
- package/dist/oauth2/validate-authorization-code.mjs +71 -0
- package/dist/oauth2/verify.d.mts +49 -0
- package/dist/oauth2/verify.mjs +95 -0
- package/dist/social-providers/apple.d.mts +119 -0
- package/dist/social-providers/apple.mjs +102 -0
- package/dist/social-providers/atlassian.d.mts +72 -0
- package/dist/social-providers/atlassian.mjs +83 -0
- package/dist/social-providers/cognito.d.mts +87 -0
- package/dist/social-providers/cognito.mjs +166 -0
- package/dist/social-providers/discord.d.mts +126 -0
- package/dist/social-providers/discord.mjs +64 -0
- package/dist/social-providers/dropbox.d.mts +71 -0
- package/dist/social-providers/dropbox.mjs +75 -0
- package/dist/social-providers/facebook.d.mts +81 -0
- package/dist/social-providers/facebook.mjs +120 -0
- package/dist/social-providers/figma.d.mts +63 -0
- package/dist/social-providers/figma.mjs +84 -0
- package/dist/social-providers/github.d.mts +104 -0
- package/dist/social-providers/github.mjs +80 -0
- package/dist/social-providers/gitlab.d.mts +125 -0
- package/dist/social-providers/gitlab.mjs +82 -0
- package/dist/social-providers/google.d.mts +99 -0
- package/dist/social-providers/google.mjs +109 -0
- package/dist/social-providers/huggingface.d.mts +85 -0
- package/dist/social-providers/huggingface.mjs +75 -0
- package/dist/social-providers/index.d.mts +1723 -1
- package/dist/social-providers/index.mjs +33 -2570
- package/dist/social-providers/kakao.d.mts +163 -0
- package/dist/social-providers/kakao.mjs +72 -0
- package/dist/social-providers/kick.d.mts +75 -0
- package/dist/social-providers/kick.mjs +71 -0
- package/dist/social-providers/line.d.mts +107 -0
- package/dist/social-providers/line.mjs +113 -0
- package/dist/social-providers/linear.d.mts +70 -0
- package/dist/social-providers/linear.mjs +88 -0
- package/dist/social-providers/linkedin.d.mts +69 -0
- package/dist/social-providers/linkedin.mjs +76 -0
- package/dist/social-providers/microsoft-entra-id.d.mts +174 -0
- package/dist/social-providers/microsoft-entra-id.mjs +106 -0
- package/dist/social-providers/naver.d.mts +104 -0
- package/dist/social-providers/naver.mjs +67 -0
- package/dist/social-providers/notion.d.mts +66 -0
- package/dist/social-providers/notion.mjs +75 -0
- package/dist/social-providers/paybin.d.mts +73 -0
- package/dist/social-providers/paybin.mjs +85 -0
- package/dist/social-providers/paypal.d.mts +131 -0
- package/dist/social-providers/paypal.mjs +144 -0
- package/dist/social-providers/polar.d.mts +76 -0
- package/dist/social-providers/polar.mjs +73 -0
- package/dist/social-providers/reddit.d.mts +64 -0
- package/dist/social-providers/reddit.mjs +83 -0
- package/dist/social-providers/roblox.d.mts +72 -0
- package/dist/social-providers/roblox.mjs +59 -0
- package/dist/social-providers/salesforce.d.mts +81 -0
- package/dist/social-providers/salesforce.mjs +91 -0
- package/dist/social-providers/slack.d.mts +85 -0
- package/dist/social-providers/slack.mjs +68 -0
- package/dist/social-providers/spotify.d.mts +65 -0
- package/dist/social-providers/spotify.mjs +71 -0
- package/dist/social-providers/tiktok.d.mts +171 -0
- package/dist/social-providers/tiktok.mjs +62 -0
- package/dist/social-providers/twitch.d.mts +81 -0
- package/dist/social-providers/twitch.mjs +78 -0
- package/dist/social-providers/twitter.d.mts +140 -0
- package/dist/social-providers/twitter.mjs +87 -0
- package/dist/social-providers/vercel.d.mts +64 -0
- package/dist/social-providers/vercel.mjs +61 -0
- package/dist/social-providers/vk.d.mts +72 -0
- package/dist/social-providers/vk.mjs +83 -0
- package/dist/social-providers/zoom.d.mts +173 -0
- package/dist/social-providers/zoom.mjs +72 -0
- package/dist/types/context.d.mts +215 -0
- package/dist/types/cookie.d.mts +15 -0
- package/dist/types/helper.d.mts +8 -0
- package/dist/types/index.d.mts +8 -0
- package/dist/types/init-options.d.mts +1266 -0
- package/dist/types/plugin-client.d.mts +103 -0
- package/dist/types/plugin.d.mts +121 -0
- package/dist/utils/deprecate.d.mts +10 -0
- package/dist/utils/deprecate.mjs +17 -0
- package/dist/utils/error-codes.d.mts +9 -0
- package/dist/utils/error-codes.mjs +7 -0
- package/dist/utils/id.d.mts +4 -0
- package/dist/utils/id.mjs +9 -0
- package/dist/utils/index.d.mts +5 -26
- package/dist/utils/index.mjs +5 -2
- package/dist/utils/json.d.mts +4 -0
- package/dist/utils/json.mjs +25 -0
- package/dist/utils/string.d.mts +4 -0
- package/dist/utils/string.mjs +7 -0
- package/package.json +1 -1
- package/src/context/endpoint-context.ts +7 -15
- package/src/context/global.ts +57 -0
- package/src/context/index.ts +1 -0
- package/src/context/request-state.ts +7 -12
- package/src/context/transaction.ts +7 -16
- package/src/db/adapter/factory.ts +13 -13
- package/src/db/adapter/get-default-model-name.ts +1 -1
- package/src/db/adapter/get-id-field.ts +2 -2
- package/src/error/index.ts +2 -3
- package/src/social-providers/gitlab.ts +1 -1
- package/src/types/context.ts +137 -131
- package/src/types/cookie.ts +6 -4
- package/src/types/index.ts +2 -1
- package/tsdown.config.ts +9 -0
- package/dist/context-BGZ8V6DD.mjs +0 -126
- package/dist/env-DbssmzoK.mjs +0 -245
- package/dist/index-zgYuzZ7O.d.mts +0 -8020
- package/dist/oauth2-COJkghlT.mjs +0 -326
- package/dist/utils-U2L7n92V.mjs +0 -59
package/dist/oauth2-COJkghlT.mjs
DELETED
|
@@ -1,326 +0,0 @@
|
|
|
1
|
-
import { i as logger } from "./env-DbssmzoK.mjs";
|
|
2
|
-
import { base64, base64Url } from "@better-auth/utils/base64";
|
|
3
|
-
import { betterFetch } from "@better-fetch/fetch";
|
|
4
|
-
import { UnsecuredJWT, createLocalJWKSet, decodeProtectedHeader, jwtVerify } from "jose";
|
|
5
|
-
import { APIError } from "better-call";
|
|
6
|
-
|
|
7
|
-
//#region src/oauth2/client-credentials-token.ts
|
|
8
|
-
function createClientCredentialsTokenRequest({ options, scope, authentication, resource }) {
|
|
9
|
-
const body = new URLSearchParams();
|
|
10
|
-
const headers = {
|
|
11
|
-
"content-type": "application/x-www-form-urlencoded",
|
|
12
|
-
accept: "application/json"
|
|
13
|
-
};
|
|
14
|
-
body.set("grant_type", "client_credentials");
|
|
15
|
-
scope && body.set("scope", scope);
|
|
16
|
-
if (resource) if (typeof resource === "string") body.append("resource", resource);
|
|
17
|
-
else for (const _resource of resource) body.append("resource", _resource);
|
|
18
|
-
if (authentication === "basic") {
|
|
19
|
-
const primaryClientId = Array.isArray(options.clientId) ? options.clientId[0] : options.clientId;
|
|
20
|
-
headers["authorization"] = `Basic ${base64Url.encode(`${primaryClientId}:${options.clientSecret}`)}`;
|
|
21
|
-
} else {
|
|
22
|
-
const primaryClientId = Array.isArray(options.clientId) ? options.clientId[0] : options.clientId;
|
|
23
|
-
body.set("client_id", primaryClientId);
|
|
24
|
-
body.set("client_secret", options.clientSecret);
|
|
25
|
-
}
|
|
26
|
-
return {
|
|
27
|
-
body,
|
|
28
|
-
headers
|
|
29
|
-
};
|
|
30
|
-
}
|
|
31
|
-
async function clientCredentialsToken({ options, tokenEndpoint, scope, authentication, resource }) {
|
|
32
|
-
const { body, headers } = createClientCredentialsTokenRequest({
|
|
33
|
-
options,
|
|
34
|
-
scope,
|
|
35
|
-
authentication,
|
|
36
|
-
resource
|
|
37
|
-
});
|
|
38
|
-
const { data, error } = await betterFetch(tokenEndpoint, {
|
|
39
|
-
method: "POST",
|
|
40
|
-
body,
|
|
41
|
-
headers
|
|
42
|
-
});
|
|
43
|
-
if (error) throw error;
|
|
44
|
-
const tokens = {
|
|
45
|
-
accessToken: data.access_token,
|
|
46
|
-
tokenType: data.token_type,
|
|
47
|
-
scopes: data.scope?.split(" ")
|
|
48
|
-
};
|
|
49
|
-
if (data.expires_in) {
|
|
50
|
-
const now = /* @__PURE__ */ new Date();
|
|
51
|
-
tokens.accessTokenExpiresAt = new Date(now.getTime() + data.expires_in * 1e3);
|
|
52
|
-
}
|
|
53
|
-
return tokens;
|
|
54
|
-
}
|
|
55
|
-
|
|
56
|
-
//#endregion
|
|
57
|
-
//#region src/oauth2/utils.ts
|
|
58
|
-
function getOAuth2Tokens(data) {
|
|
59
|
-
const getDate = (seconds) => {
|
|
60
|
-
const now = /* @__PURE__ */ new Date();
|
|
61
|
-
return new Date(now.getTime() + seconds * 1e3);
|
|
62
|
-
};
|
|
63
|
-
return {
|
|
64
|
-
tokenType: data.token_type,
|
|
65
|
-
accessToken: data.access_token,
|
|
66
|
-
refreshToken: data.refresh_token,
|
|
67
|
-
accessTokenExpiresAt: data.expires_in ? getDate(data.expires_in) : void 0,
|
|
68
|
-
refreshTokenExpiresAt: data.refresh_token_expires_in ? getDate(data.refresh_token_expires_in) : void 0,
|
|
69
|
-
scopes: data?.scope ? typeof data.scope === "string" ? data.scope.split(" ") : data.scope : [],
|
|
70
|
-
idToken: data.id_token,
|
|
71
|
-
raw: data
|
|
72
|
-
};
|
|
73
|
-
}
|
|
74
|
-
async function generateCodeChallenge(codeVerifier) {
|
|
75
|
-
const data = new TextEncoder().encode(codeVerifier);
|
|
76
|
-
const hash = await crypto.subtle.digest("SHA-256", data);
|
|
77
|
-
return base64Url.encode(new Uint8Array(hash), { padding: false });
|
|
78
|
-
}
|
|
79
|
-
|
|
80
|
-
//#endregion
|
|
81
|
-
//#region src/oauth2/create-authorization-url.ts
|
|
82
|
-
async function createAuthorizationURL({ id, options, authorizationEndpoint, state, codeVerifier, scopes, claims, redirectURI, duration, prompt, accessType, responseType, display, loginHint, hd, responseMode, additionalParams, scopeJoiner }) {
|
|
83
|
-
const url = new URL(options.authorizationEndpoint || authorizationEndpoint);
|
|
84
|
-
url.searchParams.set("response_type", responseType || "code");
|
|
85
|
-
const primaryClientId = Array.isArray(options.clientId) ? options.clientId[0] : options.clientId;
|
|
86
|
-
url.searchParams.set("client_id", primaryClientId);
|
|
87
|
-
url.searchParams.set("state", state);
|
|
88
|
-
if (scopes) url.searchParams.set("scope", scopes.join(scopeJoiner || " "));
|
|
89
|
-
url.searchParams.set("redirect_uri", options.redirectURI || redirectURI);
|
|
90
|
-
duration && url.searchParams.set("duration", duration);
|
|
91
|
-
display && url.searchParams.set("display", display);
|
|
92
|
-
loginHint && url.searchParams.set("login_hint", loginHint);
|
|
93
|
-
prompt && url.searchParams.set("prompt", prompt);
|
|
94
|
-
hd && url.searchParams.set("hd", hd);
|
|
95
|
-
accessType && url.searchParams.set("access_type", accessType);
|
|
96
|
-
responseMode && url.searchParams.set("response_mode", responseMode);
|
|
97
|
-
if (codeVerifier) {
|
|
98
|
-
const codeChallenge = await generateCodeChallenge(codeVerifier);
|
|
99
|
-
url.searchParams.set("code_challenge_method", "S256");
|
|
100
|
-
url.searchParams.set("code_challenge", codeChallenge);
|
|
101
|
-
}
|
|
102
|
-
if (claims) {
|
|
103
|
-
const claimsObj = claims.reduce((acc, claim) => {
|
|
104
|
-
acc[claim] = null;
|
|
105
|
-
return acc;
|
|
106
|
-
}, {});
|
|
107
|
-
url.searchParams.set("claims", JSON.stringify({ id_token: {
|
|
108
|
-
email: null,
|
|
109
|
-
email_verified: null,
|
|
110
|
-
...claimsObj
|
|
111
|
-
} }));
|
|
112
|
-
}
|
|
113
|
-
if (additionalParams) Object.entries(additionalParams).forEach(([key, value]) => {
|
|
114
|
-
url.searchParams.set(key, value);
|
|
115
|
-
});
|
|
116
|
-
return url;
|
|
117
|
-
}
|
|
118
|
-
|
|
119
|
-
//#endregion
|
|
120
|
-
//#region src/oauth2/refresh-access-token.ts
|
|
121
|
-
function createRefreshAccessTokenRequest({ refreshToken, options, authentication, extraParams, resource }) {
|
|
122
|
-
const body = new URLSearchParams();
|
|
123
|
-
const headers = {
|
|
124
|
-
"content-type": "application/x-www-form-urlencoded",
|
|
125
|
-
accept: "application/json"
|
|
126
|
-
};
|
|
127
|
-
body.set("grant_type", "refresh_token");
|
|
128
|
-
body.set("refresh_token", refreshToken);
|
|
129
|
-
if (authentication === "basic") {
|
|
130
|
-
const primaryClientId = Array.isArray(options.clientId) ? options.clientId[0] : options.clientId;
|
|
131
|
-
if (primaryClientId) headers["authorization"] = "Basic " + base64.encode(`${primaryClientId}:${options.clientSecret ?? ""}`);
|
|
132
|
-
else headers["authorization"] = "Basic " + base64.encode(`:${options.clientSecret ?? ""}`);
|
|
133
|
-
} else {
|
|
134
|
-
const primaryClientId = Array.isArray(options.clientId) ? options.clientId[0] : options.clientId;
|
|
135
|
-
body.set("client_id", primaryClientId);
|
|
136
|
-
if (options.clientSecret) body.set("client_secret", options.clientSecret);
|
|
137
|
-
}
|
|
138
|
-
if (resource) if (typeof resource === "string") body.append("resource", resource);
|
|
139
|
-
else for (const _resource of resource) body.append("resource", _resource);
|
|
140
|
-
if (extraParams) for (const [key, value] of Object.entries(extraParams)) body.set(key, value);
|
|
141
|
-
return {
|
|
142
|
-
body,
|
|
143
|
-
headers
|
|
144
|
-
};
|
|
145
|
-
}
|
|
146
|
-
async function refreshAccessToken({ refreshToken, options, tokenEndpoint, authentication, extraParams }) {
|
|
147
|
-
const { body, headers } = createRefreshAccessTokenRequest({
|
|
148
|
-
refreshToken,
|
|
149
|
-
options,
|
|
150
|
-
authentication,
|
|
151
|
-
extraParams
|
|
152
|
-
});
|
|
153
|
-
const { data, error } = await betterFetch(tokenEndpoint, {
|
|
154
|
-
method: "POST",
|
|
155
|
-
body,
|
|
156
|
-
headers
|
|
157
|
-
});
|
|
158
|
-
if (error) throw error;
|
|
159
|
-
const tokens = {
|
|
160
|
-
accessToken: data.access_token,
|
|
161
|
-
refreshToken: data.refresh_token,
|
|
162
|
-
tokenType: data.token_type,
|
|
163
|
-
scopes: data.scope?.split(" "),
|
|
164
|
-
idToken: data.id_token
|
|
165
|
-
};
|
|
166
|
-
if (data.expires_in) {
|
|
167
|
-
const now = /* @__PURE__ */ new Date();
|
|
168
|
-
tokens.accessTokenExpiresAt = new Date(now.getTime() + data.expires_in * 1e3);
|
|
169
|
-
}
|
|
170
|
-
return tokens;
|
|
171
|
-
}
|
|
172
|
-
|
|
173
|
-
//#endregion
|
|
174
|
-
//#region src/oauth2/validate-authorization-code.ts
|
|
175
|
-
function createAuthorizationCodeRequest({ code, codeVerifier, redirectURI, options, authentication, deviceId, headers, additionalParams = {}, resource }) {
|
|
176
|
-
const body = new URLSearchParams();
|
|
177
|
-
const requestHeaders = {
|
|
178
|
-
"content-type": "application/x-www-form-urlencoded",
|
|
179
|
-
accept: "application/json",
|
|
180
|
-
...headers
|
|
181
|
-
};
|
|
182
|
-
body.set("grant_type", "authorization_code");
|
|
183
|
-
body.set("code", code);
|
|
184
|
-
codeVerifier && body.set("code_verifier", codeVerifier);
|
|
185
|
-
options.clientKey && body.set("client_key", options.clientKey);
|
|
186
|
-
deviceId && body.set("device_id", deviceId);
|
|
187
|
-
body.set("redirect_uri", options.redirectURI || redirectURI);
|
|
188
|
-
if (resource) if (typeof resource === "string") body.append("resource", resource);
|
|
189
|
-
else for (const _resource of resource) body.append("resource", _resource);
|
|
190
|
-
if (authentication === "basic") {
|
|
191
|
-
const primaryClientId = Array.isArray(options.clientId) ? options.clientId[0] : options.clientId;
|
|
192
|
-
requestHeaders["authorization"] = `Basic ${base64.encode(`${primaryClientId}:${options.clientSecret ?? ""}`)}`;
|
|
193
|
-
} else {
|
|
194
|
-
const primaryClientId = Array.isArray(options.clientId) ? options.clientId[0] : options.clientId;
|
|
195
|
-
body.set("client_id", primaryClientId);
|
|
196
|
-
if (options.clientSecret) body.set("client_secret", options.clientSecret);
|
|
197
|
-
}
|
|
198
|
-
for (const [key, value] of Object.entries(additionalParams)) if (!body.has(key)) body.append(key, value);
|
|
199
|
-
return {
|
|
200
|
-
body,
|
|
201
|
-
headers: requestHeaders
|
|
202
|
-
};
|
|
203
|
-
}
|
|
204
|
-
async function validateAuthorizationCode({ code, codeVerifier, redirectURI, options, tokenEndpoint, authentication, deviceId, headers, additionalParams = {}, resource }) {
|
|
205
|
-
const { body, headers: requestHeaders } = createAuthorizationCodeRequest({
|
|
206
|
-
code,
|
|
207
|
-
codeVerifier,
|
|
208
|
-
redirectURI,
|
|
209
|
-
options,
|
|
210
|
-
authentication,
|
|
211
|
-
deviceId,
|
|
212
|
-
headers,
|
|
213
|
-
additionalParams,
|
|
214
|
-
resource
|
|
215
|
-
});
|
|
216
|
-
const { data, error } = await betterFetch(tokenEndpoint, {
|
|
217
|
-
method: "POST",
|
|
218
|
-
body,
|
|
219
|
-
headers: requestHeaders
|
|
220
|
-
});
|
|
221
|
-
if (error) throw error;
|
|
222
|
-
return getOAuth2Tokens(data);
|
|
223
|
-
}
|
|
224
|
-
async function validateToken(token, jwksEndpoint) {
|
|
225
|
-
const { data, error } = await betterFetch(jwksEndpoint, {
|
|
226
|
-
method: "GET",
|
|
227
|
-
headers: { accept: "application/json" }
|
|
228
|
-
});
|
|
229
|
-
if (error) throw error;
|
|
230
|
-
const keys = data["keys"];
|
|
231
|
-
const header = JSON.parse(atob(token.split(".")[0]));
|
|
232
|
-
const key = keys.find((key$1) => key$1.kid === header.kid);
|
|
233
|
-
if (!key) throw new Error("Key not found");
|
|
234
|
-
return await jwtVerify(token, key);
|
|
235
|
-
}
|
|
236
|
-
|
|
237
|
-
//#endregion
|
|
238
|
-
//#region src/oauth2/verify.ts
|
|
239
|
-
/** Last fetched jwks used locally in getJwks @internal */
|
|
240
|
-
let jwks;
|
|
241
|
-
/**
|
|
242
|
-
* Performs local verification of an access token for your APIs.
|
|
243
|
-
*
|
|
244
|
-
* Can also be configured for remote verification.
|
|
245
|
-
*/
|
|
246
|
-
async function verifyJwsAccessToken(token, opts) {
|
|
247
|
-
try {
|
|
248
|
-
const jwt = await jwtVerify(token, createLocalJWKSet(await getJwks(token, opts)), opts.verifyOptions);
|
|
249
|
-
if (jwt.payload.azp) jwt.payload.client_id = jwt.payload.azp;
|
|
250
|
-
return jwt.payload;
|
|
251
|
-
} catch (error) {
|
|
252
|
-
if (error instanceof Error) throw error;
|
|
253
|
-
throw new Error(error);
|
|
254
|
-
}
|
|
255
|
-
}
|
|
256
|
-
async function getJwks(token, opts) {
|
|
257
|
-
let jwtHeaders;
|
|
258
|
-
try {
|
|
259
|
-
jwtHeaders = decodeProtectedHeader(token);
|
|
260
|
-
} catch (error) {
|
|
261
|
-
if (error instanceof Error) throw error;
|
|
262
|
-
throw new Error(error);
|
|
263
|
-
}
|
|
264
|
-
if (!jwtHeaders.kid) throw new Error("Missing jwt kid");
|
|
265
|
-
if (!jwks || !jwks.keys.find((jwk) => jwk.kid === jwtHeaders.kid)) {
|
|
266
|
-
jwks = typeof opts.jwksFetch === "string" ? await betterFetch(opts.jwksFetch, { headers: { Accept: "application/json" } }).then(async (res) => {
|
|
267
|
-
if (res.error) throw new Error(`Jwks failed: ${res.error.message ?? res.error.statusText}`);
|
|
268
|
-
return res.data;
|
|
269
|
-
}) : await opts.jwksFetch();
|
|
270
|
-
if (!jwks) throw new Error("No jwks found");
|
|
271
|
-
}
|
|
272
|
-
return jwks;
|
|
273
|
-
}
|
|
274
|
-
/**
|
|
275
|
-
* Performs local verification of an access token for your API.
|
|
276
|
-
*
|
|
277
|
-
* Can also be configured for remote verification.
|
|
278
|
-
*/
|
|
279
|
-
async function verifyAccessToken(token, opts) {
|
|
280
|
-
let payload;
|
|
281
|
-
if (opts.jwksUrl && !opts?.remoteVerify?.force) try {
|
|
282
|
-
payload = await verifyJwsAccessToken(token, {
|
|
283
|
-
jwksFetch: opts.jwksUrl,
|
|
284
|
-
verifyOptions: opts.verifyOptions
|
|
285
|
-
});
|
|
286
|
-
} catch (error) {
|
|
287
|
-
if (error instanceof Error) if (error.name === "TypeError" || error.name === "JWSInvalid") {} else if (error.name === "JWTExpired") throw new APIError("UNAUTHORIZED", { message: "token expired" });
|
|
288
|
-
else if (error.name === "JWTInvalid") throw new APIError("UNAUTHORIZED", { message: "token invalid" });
|
|
289
|
-
else throw error;
|
|
290
|
-
else throw new Error(error);
|
|
291
|
-
}
|
|
292
|
-
if (opts?.remoteVerify) {
|
|
293
|
-
const { data: introspect, error: introspectError } = await betterFetch(opts.remoteVerify.introspectUrl, {
|
|
294
|
-
method: "POST",
|
|
295
|
-
headers: {
|
|
296
|
-
Accept: "application/json",
|
|
297
|
-
"Content-Type": "application/x-www-form-urlencoded"
|
|
298
|
-
},
|
|
299
|
-
body: new URLSearchParams({
|
|
300
|
-
client_id: opts.remoteVerify.clientId,
|
|
301
|
-
client_secret: opts.remoteVerify.clientSecret,
|
|
302
|
-
token,
|
|
303
|
-
token_type_hint: "access_token"
|
|
304
|
-
}).toString()
|
|
305
|
-
});
|
|
306
|
-
if (introspectError) logger.error(`Introspection failed: ${introspectError.message ?? introspectError.statusText}`);
|
|
307
|
-
if (!introspect) throw new APIError("INTERNAL_SERVER_ERROR", { message: "introspection failed" });
|
|
308
|
-
if (!introspect.active) throw new APIError("UNAUTHORIZED", { message: "token inactive" });
|
|
309
|
-
try {
|
|
310
|
-
const unsecuredJwt = new UnsecuredJWT(introspect).encode();
|
|
311
|
-
const { audience: _audience, ...verifyOptions } = opts.verifyOptions;
|
|
312
|
-
payload = (introspect.aud ? UnsecuredJWT.decode(unsecuredJwt, opts.verifyOptions) : UnsecuredJWT.decode(unsecuredJwt, verifyOptions)).payload;
|
|
313
|
-
} catch (error) {
|
|
314
|
-
throw new Error(error);
|
|
315
|
-
}
|
|
316
|
-
}
|
|
317
|
-
if (!payload) throw new APIError("UNAUTHORIZED", { message: `no token payload` });
|
|
318
|
-
if (opts.scopes) {
|
|
319
|
-
const validScopes = new Set(payload.scope?.split(" "));
|
|
320
|
-
for (const sc of opts.scopes) if (!validScopes.has(sc)) throw new APIError("FORBIDDEN", { message: `invalid scope ${sc}` });
|
|
321
|
-
}
|
|
322
|
-
return payload;
|
|
323
|
-
}
|
|
324
|
-
|
|
325
|
-
//#endregion
|
|
326
|
-
export { validateAuthorizationCode as a, refreshAccessToken as c, getOAuth2Tokens as d, clientCredentialsToken as f, createAuthorizationCodeRequest as i, createAuthorizationURL as l, verifyAccessToken as n, validateToken as o, createClientCredentialsTokenRequest as p, verifyJwsAccessToken as r, createRefreshAccessTokenRequest as s, getJwks as t, generateCodeChallenge as u };
|
package/dist/utils-U2L7n92V.mjs
DELETED
|
@@ -1,59 +0,0 @@
|
|
|
1
|
-
import { i as logger } from "./env-DbssmzoK.mjs";
|
|
2
|
-
import { createRandomStringGenerator } from "@better-auth/utils/random";
|
|
3
|
-
|
|
4
|
-
//#region src/utils/deprecate.ts
|
|
5
|
-
/**
|
|
6
|
-
* Wraps a function to log a deprecation warning at once.
|
|
7
|
-
*/
|
|
8
|
-
function deprecate(fn, message, logger$1) {
|
|
9
|
-
let warned = false;
|
|
10
|
-
return function(...args) {
|
|
11
|
-
if (!warned) {
|
|
12
|
-
(logger$1?.warn ?? console.warn)(`[Deprecation] ${message}`);
|
|
13
|
-
warned = true;
|
|
14
|
-
}
|
|
15
|
-
return fn.apply(this, args);
|
|
16
|
-
};
|
|
17
|
-
}
|
|
18
|
-
|
|
19
|
-
//#endregion
|
|
20
|
-
//#region src/utils/error-codes.ts
|
|
21
|
-
function defineErrorCodes(codes) {
|
|
22
|
-
return codes;
|
|
23
|
-
}
|
|
24
|
-
|
|
25
|
-
//#endregion
|
|
26
|
-
//#region src/utils/id.ts
|
|
27
|
-
const generateId = (size) => {
|
|
28
|
-
return createRandomStringGenerator("a-z", "A-Z", "0-9")(size || 32);
|
|
29
|
-
};
|
|
30
|
-
|
|
31
|
-
//#endregion
|
|
32
|
-
//#region src/utils/json.ts
|
|
33
|
-
function safeJSONParse(data) {
|
|
34
|
-
function reviver(_, value) {
|
|
35
|
-
if (typeof value === "string") {
|
|
36
|
-
if (/^\d{4}-\d{2}-\d{2}T\d{2}:\d{2}:\d{2}(?:\.\d+)?Z$/.test(value)) {
|
|
37
|
-
const date = new Date(value);
|
|
38
|
-
if (!isNaN(date.getTime())) return date;
|
|
39
|
-
}
|
|
40
|
-
}
|
|
41
|
-
return value;
|
|
42
|
-
}
|
|
43
|
-
try {
|
|
44
|
-
if (typeof data !== "string") return data;
|
|
45
|
-
return JSON.parse(data, reviver);
|
|
46
|
-
} catch (e) {
|
|
47
|
-
logger.error("Error parsing JSON", { error: e });
|
|
48
|
-
return null;
|
|
49
|
-
}
|
|
50
|
-
}
|
|
51
|
-
|
|
52
|
-
//#endregion
|
|
53
|
-
//#region src/utils/string.ts
|
|
54
|
-
function capitalizeFirstLetter(str) {
|
|
55
|
-
return str.charAt(0).toUpperCase() + str.slice(1);
|
|
56
|
-
}
|
|
57
|
-
|
|
58
|
-
//#endregion
|
|
59
|
-
export { deprecate as a, defineErrorCodes as i, safeJSONParse as n, generateId as r, capitalizeFirstLetter as t };
|