@better-auth/core 1.7.0-beta.3 → 1.7.0-beta.5
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/dist/api/index.d.mts +3 -3
- package/dist/context/global.mjs +1 -1
- package/dist/db/adapter/factory.mjs +62 -0
- package/dist/db/adapter/index.d.mts +35 -1
- package/dist/db/adapter/types.d.mts +1 -1
- package/dist/db/get-tables.mjs +3 -3
- package/dist/db/schema/account.d.mts +1 -1
- package/dist/db/schema/account.mjs +1 -1
- package/dist/db/type.d.mts +12 -0
- package/dist/env/env-impl.mjs +1 -1
- package/dist/error/codes.d.mts +6 -0
- package/dist/error/codes.mjs +6 -0
- package/dist/index.d.mts +2 -2
- package/dist/instrumentation/tracer.mjs +1 -1
- package/dist/oauth2/authorization-params.d.mts +12 -0
- package/dist/oauth2/authorization-params.mjs +12 -0
- package/dist/oauth2/basic-credentials.d.mts +30 -0
- package/dist/oauth2/basic-credentials.mjs +64 -0
- package/dist/oauth2/client-assertion.d.mts +38 -22
- package/dist/oauth2/client-assertion.mjs +63 -28
- package/dist/oauth2/client-credentials-token.d.mts +19 -40
- package/dist/oauth2/client-credentials-token.mjs +18 -29
- package/dist/oauth2/create-authorization-url.d.mts +13 -2
- package/dist/oauth2/create-authorization-url.mjs +28 -7
- package/dist/oauth2/index.d.mts +13 -8
- package/dist/oauth2/index.mjs +11 -7
- package/dist/oauth2/oauth-provider.d.mts +149 -11
- package/dist/oauth2/refresh-access-token.d.mts +20 -40
- package/dist/oauth2/refresh-access-token.mjs +20 -33
- package/dist/oauth2/scopes.d.mts +76 -0
- package/dist/oauth2/scopes.mjs +96 -0
- package/dist/oauth2/token-endpoint-auth.d.mts +17 -0
- package/dist/oauth2/token-endpoint-auth.mjs +89 -0
- package/dist/oauth2/utils.d.mts +9 -1
- package/dist/oauth2/utils.mjs +14 -2
- package/dist/oauth2/validate-authorization-code.d.mts +17 -52
- package/dist/oauth2/validate-authorization-code.mjs +17 -30
- package/dist/oauth2/verify-id-token.d.mts +26 -0
- package/dist/oauth2/verify-id-token.mjs +62 -0
- package/dist/oauth2/verify.d.mts +14 -0
- package/dist/oauth2/verify.mjs +38 -12
- package/dist/social-providers/apple.d.mts +18 -20
- package/dist/social-providers/apple.mjs +15 -28
- package/dist/social-providers/atlassian.d.mts +8 -2
- package/dist/social-providers/atlassian.mjs +9 -6
- package/dist/social-providers/cognito.d.mts +29 -3
- package/dist/social-providers/cognito.mjs +30 -34
- package/dist/social-providers/discord.d.mts +8 -2
- package/dist/social-providers/discord.mjs +20 -6
- package/dist/social-providers/dropbox.d.mts +8 -2
- package/dist/social-providers/dropbox.mjs +10 -9
- package/dist/social-providers/facebook.d.mts +24 -3
- package/dist/social-providers/facebook.mjs +51 -24
- package/dist/social-providers/figma.d.mts +8 -2
- package/dist/social-providers/figma.mjs +8 -7
- package/dist/social-providers/github.d.mts +8 -2
- package/dist/social-providers/github.mjs +9 -8
- package/dist/social-providers/gitlab.d.mts +8 -2
- package/dist/social-providers/gitlab.mjs +8 -7
- package/dist/social-providers/google.d.mts +32 -4
- package/dist/social-providers/google.mjs +26 -29
- package/dist/social-providers/huggingface.d.mts +8 -2
- package/dist/social-providers/huggingface.mjs +11 -10
- package/dist/social-providers/index.d.mts +322 -75
- package/dist/social-providers/kakao.d.mts +8 -2
- package/dist/social-providers/kakao.mjs +11 -10
- package/dist/social-providers/kick.d.mts +8 -2
- package/dist/social-providers/kick.mjs +7 -6
- package/dist/social-providers/line.d.mts +11 -3
- package/dist/social-providers/line.mjs +14 -15
- package/dist/social-providers/linear.d.mts +8 -2
- package/dist/social-providers/linear.mjs +7 -6
- package/dist/social-providers/linkedin.d.mts +8 -2
- package/dist/social-providers/linkedin.mjs +12 -11
- package/dist/social-providers/microsoft-entra-id.d.mts +33 -7
- package/dist/social-providers/microsoft-entra-id.mjs +28 -38
- package/dist/social-providers/naver.d.mts +8 -2
- package/dist/social-providers/naver.mjs +7 -6
- package/dist/social-providers/notion.d.mts +8 -2
- package/dist/social-providers/notion.mjs +9 -6
- package/dist/social-providers/paybin.d.mts +8 -2
- package/dist/social-providers/paybin.mjs +12 -11
- package/dist/social-providers/paypal.d.mts +8 -3
- package/dist/social-providers/paypal.mjs +10 -14
- package/dist/social-providers/polar.d.mts +8 -2
- package/dist/social-providers/polar.mjs +11 -10
- package/dist/social-providers/railway.d.mts +8 -2
- package/dist/social-providers/railway.mjs +11 -10
- package/dist/social-providers/reddit.d.mts +8 -2
- package/dist/social-providers/reddit.mjs +11 -9
- package/dist/social-providers/roblox.d.mts +8 -2
- package/dist/social-providers/roblox.mjs +15 -5
- package/dist/social-providers/salesforce.d.mts +8 -2
- package/dist/social-providers/salesforce.mjs +11 -10
- package/dist/social-providers/slack.d.mts +8 -2
- package/dist/social-providers/slack.mjs +18 -15
- package/dist/social-providers/spotify.d.mts +8 -2
- package/dist/social-providers/spotify.mjs +7 -6
- package/dist/social-providers/tiktok.d.mts +8 -2
- package/dist/social-providers/tiktok.mjs +21 -5
- package/dist/social-providers/twitch.d.mts +8 -2
- package/dist/social-providers/twitch.mjs +7 -6
- package/dist/social-providers/twitter.d.mts +7 -2
- package/dist/social-providers/twitter.mjs +11 -10
- package/dist/social-providers/vercel.d.mts +8 -2
- package/dist/social-providers/vercel.mjs +7 -9
- package/dist/social-providers/vk.d.mts +8 -2
- package/dist/social-providers/vk.mjs +7 -6
- package/dist/social-providers/wechat.d.mts +8 -2
- package/dist/social-providers/wechat.mjs +16 -6
- package/dist/social-providers/zoom.d.mts +10 -3
- package/dist/social-providers/zoom.mjs +14 -15
- package/dist/types/context.d.mts +33 -11
- package/dist/types/index.d.mts +1 -1
- package/dist/types/init-options.d.mts +121 -6
- package/dist/utils/ip.d.mts +5 -4
- package/dist/utils/ip.mjs +3 -3
- package/dist/utils/redirect-uri.d.mts +20 -0
- package/dist/utils/redirect-uri.mjs +48 -0
- package/dist/utils/string.d.mts +5 -1
- package/dist/utils/string.mjs +20 -1
- package/dist/utils/url.d.mts +18 -1
- package/dist/utils/url.mjs +30 -1
- package/package.json +13 -12
- package/src/db/adapter/factory.ts +126 -0
- package/src/db/adapter/index.ts +32 -0
- package/src/db/adapter/types.ts +1 -0
- package/src/db/get-tables.ts +8 -3
- package/src/db/schema/account.ts +14 -2
- package/src/db/type.ts +12 -0
- package/src/env/env-impl.ts +1 -2
- package/src/error/codes.ts +6 -0
- package/src/oauth2/authorization-params.ts +28 -0
- package/src/oauth2/basic-credentials.ts +87 -0
- package/src/oauth2/client-assertion.ts +131 -58
- package/src/oauth2/client-credentials-token.ts +48 -72
- package/src/oauth2/create-authorization-url.ts +30 -8
- package/src/oauth2/index.ts +42 -10
- package/src/oauth2/oauth-provider.ts +161 -12
- package/src/oauth2/refresh-access-token.ts +52 -78
- package/src/oauth2/scopes.ts +118 -0
- package/src/oauth2/token-endpoint-auth.ts +221 -0
- package/src/oauth2/utils.ts +21 -5
- package/src/oauth2/validate-authorization-code.ts +55 -85
- package/src/oauth2/verify-id-token.ts +111 -0
- package/src/oauth2/verify.ts +82 -15
- package/src/social-providers/apple.ts +32 -45
- package/src/social-providers/atlassian.ts +20 -9
- package/src/social-providers/cognito.ts +51 -48
- package/src/social-providers/discord.ts +37 -22
- package/src/social-providers/dropbox.ts +20 -12
- package/src/social-providers/facebook.ts +108 -57
- package/src/social-providers/figma.ts +21 -10
- package/src/social-providers/github.ts +16 -10
- package/src/social-providers/gitlab.ts +16 -8
- package/src/social-providers/google.ts +67 -46
- package/src/social-providers/huggingface.ts +20 -9
- package/src/social-providers/kakao.ts +18 -9
- package/src/social-providers/kick.ts +20 -8
- package/src/social-providers/line.ts +39 -37
- package/src/social-providers/linear.ts +20 -7
- package/src/social-providers/linkedin.ts +16 -10
- package/src/social-providers/microsoft-entra-id.ts +66 -64
- package/src/social-providers/naver.ts +14 -7
- package/src/social-providers/notion.ts +20 -7
- package/src/social-providers/paybin.ts +16 -11
- package/src/social-providers/paypal.ts +12 -25
- package/src/social-providers/polar.ts +20 -9
- package/src/social-providers/railway.ts +20 -9
- package/src/social-providers/reddit.ts +22 -10
- package/src/social-providers/roblox.ts +31 -15
- package/src/social-providers/salesforce.ts +21 -10
- package/src/social-providers/slack.ts +31 -16
- package/src/social-providers/spotify.ts +20 -7
- package/src/social-providers/tiktok.ts +32 -13
- package/src/social-providers/twitch.ts +14 -9
- package/src/social-providers/twitter.ts +18 -8
- package/src/social-providers/vercel.ts +24 -11
- package/src/social-providers/vk.ts +20 -7
- package/src/social-providers/wechat.ts +28 -8
- package/src/social-providers/zoom.ts +28 -19
- package/src/types/context.ts +33 -12
- package/src/types/index.ts +7 -0
- package/src/types/init-options.ts +148 -5
- package/src/utils/ip.ts +12 -13
- package/src/utils/redirect-uri.ts +54 -0
- package/src/utils/string.ts +37 -0
- package/src/utils/url.ts +28 -0
|
@@ -0,0 +1,221 @@
|
|
|
1
|
+
import { encodeBasicCredentials } from "./basic-credentials";
|
|
2
|
+
import type {
|
|
3
|
+
ClientAssertionGetter,
|
|
4
|
+
ClientAssertionGrantType,
|
|
5
|
+
} from "./client-assertion";
|
|
6
|
+
import { resolveClientAssertionParams } from "./client-assertion";
|
|
7
|
+
import { getPrimaryClientId } from "./utils";
|
|
8
|
+
|
|
9
|
+
export type TokenEndpointAuth =
|
|
10
|
+
| {
|
|
11
|
+
method: "none";
|
|
12
|
+
}
|
|
13
|
+
| {
|
|
14
|
+
method: "client_secret_basic";
|
|
15
|
+
}
|
|
16
|
+
| {
|
|
17
|
+
method: "client_secret_post";
|
|
18
|
+
}
|
|
19
|
+
| {
|
|
20
|
+
method: "private_key_jwt";
|
|
21
|
+
getClientAssertion: ClientAssertionGetter;
|
|
22
|
+
};
|
|
23
|
+
|
|
24
|
+
export type TokenEndpointAuthMethod = TokenEndpointAuth["method"];
|
|
25
|
+
|
|
26
|
+
export type TokenEndpointSecretAuthentication = "basic" | "post";
|
|
27
|
+
|
|
28
|
+
export interface TokenEndpointClientOptions {
|
|
29
|
+
clientId?: string | string[] | undefined;
|
|
30
|
+
clientSecret?: string | undefined;
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
export interface ApplyTokenEndpointAuthInput {
|
|
34
|
+
body: URLSearchParams;
|
|
35
|
+
headers: Record<string, string>;
|
|
36
|
+
options: TokenEndpointClientOptions;
|
|
37
|
+
tokenEndpoint: string;
|
|
38
|
+
grantType: ClientAssertionGrantType;
|
|
39
|
+
tokenEndpointAuth?: TokenEndpointAuth | undefined;
|
|
40
|
+
authentication?: TokenEndpointSecretAuthentication | undefined;
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
function getDefaultTokenEndpointAuth(
|
|
44
|
+
options: TokenEndpointClientOptions,
|
|
45
|
+
authentication?: TokenEndpointSecretAuthentication,
|
|
46
|
+
): TokenEndpointAuth {
|
|
47
|
+
if (authentication === "basic") {
|
|
48
|
+
return { method: "client_secret_basic" };
|
|
49
|
+
}
|
|
50
|
+
if (options.clientSecret) {
|
|
51
|
+
return { method: "client_secret_post" };
|
|
52
|
+
}
|
|
53
|
+
return { method: "none" };
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
function assertNoClientSecret(
|
|
57
|
+
method: "none" | "private_key_jwt",
|
|
58
|
+
options: TokenEndpointClientOptions,
|
|
59
|
+
body: URLSearchParams,
|
|
60
|
+
) {
|
|
61
|
+
if (options.clientSecret || body.has("client_secret")) {
|
|
62
|
+
throw new Error(
|
|
63
|
+
`${method} token endpoint authentication cannot be combined with clientSecret`,
|
|
64
|
+
);
|
|
65
|
+
}
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
function setClientId(body: URLSearchParams, clientId: string | undefined) {
|
|
69
|
+
if (clientId) {
|
|
70
|
+
body.set("client_id", clientId);
|
|
71
|
+
}
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
function assertClientSecretConfigured(
|
|
75
|
+
method: "client_secret_basic" | "client_secret_post",
|
|
76
|
+
options: TokenEndpointClientOptions,
|
|
77
|
+
): asserts options is TokenEndpointClientOptions & { clientSecret: string } {
|
|
78
|
+
if (!options.clientSecret) {
|
|
79
|
+
throw new Error(
|
|
80
|
+
`${method} token endpoint authentication requires clientSecret`,
|
|
81
|
+
);
|
|
82
|
+
}
|
|
83
|
+
}
|
|
84
|
+
|
|
85
|
+
function assertClientIdConfigured(
|
|
86
|
+
method: TokenEndpointAuthMethod,
|
|
87
|
+
clientId: string | undefined,
|
|
88
|
+
): asserts clientId is string {
|
|
89
|
+
if (!clientId) {
|
|
90
|
+
throw new Error(
|
|
91
|
+
`${method} token endpoint authentication requires clientId`,
|
|
92
|
+
);
|
|
93
|
+
}
|
|
94
|
+
}
|
|
95
|
+
|
|
96
|
+
function setClientSecretPostAuth({
|
|
97
|
+
body,
|
|
98
|
+
options,
|
|
99
|
+
clientId,
|
|
100
|
+
requireClientSecret,
|
|
101
|
+
}: {
|
|
102
|
+
body: URLSearchParams;
|
|
103
|
+
options: TokenEndpointClientOptions;
|
|
104
|
+
clientId: string | undefined;
|
|
105
|
+
requireClientSecret?: boolean | undefined;
|
|
106
|
+
}) {
|
|
107
|
+
if (requireClientSecret) {
|
|
108
|
+
assertClientSecretConfigured("client_secret_post", options);
|
|
109
|
+
}
|
|
110
|
+
if (options.clientSecret) {
|
|
111
|
+
assertClientIdConfigured("client_secret_post", clientId);
|
|
112
|
+
setClientId(body, clientId);
|
|
113
|
+
body.set("client_secret", options.clientSecret);
|
|
114
|
+
}
|
|
115
|
+
}
|
|
116
|
+
|
|
117
|
+
function setClientSecretBasicAuth({
|
|
118
|
+
headers,
|
|
119
|
+
options,
|
|
120
|
+
clientId,
|
|
121
|
+
body,
|
|
122
|
+
}: {
|
|
123
|
+
headers: Record<string, string>;
|
|
124
|
+
options: TokenEndpointClientOptions;
|
|
125
|
+
clientId: string | undefined;
|
|
126
|
+
body: URLSearchParams;
|
|
127
|
+
}) {
|
|
128
|
+
if (body.has("client_secret")) {
|
|
129
|
+
throw new Error(
|
|
130
|
+
"client_secret_basic token endpoint authentication cannot be combined with client_secret body parameters",
|
|
131
|
+
);
|
|
132
|
+
}
|
|
133
|
+
assertClientSecretConfigured("client_secret_basic", options);
|
|
134
|
+
assertClientIdConfigured("client_secret_basic", clientId);
|
|
135
|
+
headers.authorization = encodeBasicCredentials(
|
|
136
|
+
clientId,
|
|
137
|
+
options.clientSecret,
|
|
138
|
+
);
|
|
139
|
+
}
|
|
140
|
+
|
|
141
|
+
function assertCompleteManualClientAssertion(body: URLSearchParams) {
|
|
142
|
+
if (body.has("client_assertion") !== body.has("client_assertion_type")) {
|
|
143
|
+
throw new Error(
|
|
144
|
+
"client_assertion and client_assertion_type must both be provided",
|
|
145
|
+
);
|
|
146
|
+
}
|
|
147
|
+
}
|
|
148
|
+
|
|
149
|
+
export async function applyTokenEndpointAuth({
|
|
150
|
+
body,
|
|
151
|
+
headers,
|
|
152
|
+
options,
|
|
153
|
+
tokenEndpoint,
|
|
154
|
+
grantType,
|
|
155
|
+
tokenEndpointAuth,
|
|
156
|
+
authentication,
|
|
157
|
+
}: ApplyTokenEndpointAuthInput) {
|
|
158
|
+
assertCompleteManualClientAssertion(body);
|
|
159
|
+
|
|
160
|
+
const clientId = getPrimaryClientId(options.clientId);
|
|
161
|
+
if (body.has("client_assertion")) {
|
|
162
|
+
if (tokenEndpointAuth) {
|
|
163
|
+
throw new Error(
|
|
164
|
+
"client_assertion body parameters cannot be combined with tokenEndpointAuth",
|
|
165
|
+
);
|
|
166
|
+
}
|
|
167
|
+
assertNoClientSecret("private_key_jwt", options, body);
|
|
168
|
+
setClientId(body, clientId);
|
|
169
|
+
return;
|
|
170
|
+
}
|
|
171
|
+
|
|
172
|
+
const auth =
|
|
173
|
+
tokenEndpointAuth ?? getDefaultTokenEndpointAuth(options, authentication);
|
|
174
|
+
|
|
175
|
+
if (auth.method === "private_key_jwt") {
|
|
176
|
+
assertNoClientSecret(auth.method, options, body);
|
|
177
|
+
assertClientIdConfigured(auth.method, clientId);
|
|
178
|
+
if (!tokenEndpoint) {
|
|
179
|
+
throw new Error(
|
|
180
|
+
"private_key_jwt token endpoint authentication requires tokenEndpoint",
|
|
181
|
+
);
|
|
182
|
+
}
|
|
183
|
+
const assertionParams = await resolveClientAssertionParams({
|
|
184
|
+
getClientAssertion: auth.getClientAssertion,
|
|
185
|
+
context: {
|
|
186
|
+
clientId,
|
|
187
|
+
tokenEndpoint,
|
|
188
|
+
grantType,
|
|
189
|
+
},
|
|
190
|
+
});
|
|
191
|
+
setClientId(body, clientId);
|
|
192
|
+
for (const [key, value] of Object.entries(assertionParams)) {
|
|
193
|
+
body.set(key, value);
|
|
194
|
+
}
|
|
195
|
+
return;
|
|
196
|
+
}
|
|
197
|
+
|
|
198
|
+
if (auth.method === "none") {
|
|
199
|
+
assertNoClientSecret(auth.method, options, body);
|
|
200
|
+
if (grantType === "client_credentials") {
|
|
201
|
+
throw new Error(
|
|
202
|
+
"none token endpoint authentication cannot be used with client_credentials grant",
|
|
203
|
+
);
|
|
204
|
+
}
|
|
205
|
+
assertClientIdConfigured(auth.method, clientId);
|
|
206
|
+
setClientId(body, clientId);
|
|
207
|
+
return;
|
|
208
|
+
}
|
|
209
|
+
|
|
210
|
+
if (auth.method === "client_secret_basic") {
|
|
211
|
+
setClientSecretBasicAuth({ headers, options, clientId, body });
|
|
212
|
+
return;
|
|
213
|
+
}
|
|
214
|
+
|
|
215
|
+
setClientSecretPostAuth({
|
|
216
|
+
body,
|
|
217
|
+
options,
|
|
218
|
+
clientId,
|
|
219
|
+
requireClientSecret: tokenEndpointAuth?.method === "client_secret_post",
|
|
220
|
+
});
|
|
221
|
+
}
|
package/src/oauth2/utils.ts
CHANGED
|
@@ -1,5 +1,6 @@
|
|
|
1
1
|
import { base64Url } from "@better-auth/utils/base64";
|
|
2
2
|
import type { OAuth2Tokens } from "./oauth-provider";
|
|
3
|
+
import { parseScopeField } from "./scopes";
|
|
3
4
|
|
|
4
5
|
export function getOAuth2Tokens(data: Record<string, any>): OAuth2Tokens {
|
|
5
6
|
const getDate = (seconds: number) => {
|
|
@@ -17,17 +18,32 @@ export function getOAuth2Tokens(data: Record<string, any>): OAuth2Tokens {
|
|
|
17
18
|
refreshTokenExpiresAt: data.refresh_token_expires_in
|
|
18
19
|
? getDate(data.refresh_token_expires_in)
|
|
19
20
|
: undefined,
|
|
20
|
-
scopes: data
|
|
21
|
-
? typeof data.scope === "string"
|
|
22
|
-
? data.scope.split(" ")
|
|
23
|
-
: data.scope
|
|
24
|
-
: [],
|
|
21
|
+
scopes: parseScopeField(data.scope),
|
|
25
22
|
idToken: data.id_token,
|
|
26
23
|
// Preserve the raw token response for provider-specific fields
|
|
27
24
|
raw: data,
|
|
28
25
|
};
|
|
29
26
|
}
|
|
30
27
|
|
|
28
|
+
/**
|
|
29
|
+
* Fill in `accessTokenExpiresAt` from the provider's configured
|
|
30
|
+
* `accessTokenExpiresIn` when the token response omitted `expires_in`. Without a
|
|
31
|
+
* known expiry, `getAccessToken` cannot tell the token is expired and never
|
|
32
|
+
* refreshes it. No-op when the provider already supplied an expiry or no
|
|
33
|
+
* fallback is configured.
|
|
34
|
+
*/
|
|
35
|
+
export function applyDefaultAccessTokenExpiry(
|
|
36
|
+
tokens: OAuth2Tokens,
|
|
37
|
+
accessTokenExpiresIn: number | undefined,
|
|
38
|
+
): OAuth2Tokens {
|
|
39
|
+
if (!tokens.accessTokenExpiresAt && accessTokenExpiresIn) {
|
|
40
|
+
tokens.accessTokenExpiresAt = new Date(
|
|
41
|
+
Date.now() + accessTokenExpiresIn * 1000,
|
|
42
|
+
);
|
|
43
|
+
}
|
|
44
|
+
return tokens;
|
|
45
|
+
}
|
|
46
|
+
|
|
31
47
|
/**
|
|
32
48
|
* Return the provider's primary Client ID: the single string, or the entry at
|
|
33
49
|
* array index 0 for the cross-platform form used by ID token audience
|
|
@@ -1,11 +1,42 @@
|
|
|
1
|
-
import { base64 } from "@better-auth/utils/base64";
|
|
2
1
|
import { betterFetch } from "@better-fetch/fetch";
|
|
3
2
|
import { createRemoteJWKSet, jwtVerify } from "jose";
|
|
4
3
|
import type { AwaitableFunction } from "../types";
|
|
5
|
-
import type { ClientAssertionConfig } from "./client-assertion";
|
|
6
|
-
import { resolveAssertionParams } from "./client-assertion";
|
|
7
4
|
import type { ProviderOptions } from "./index";
|
|
8
5
|
import { getOAuth2Tokens } from "./index";
|
|
6
|
+
import type {
|
|
7
|
+
TokenEndpointAuth,
|
|
8
|
+
TokenEndpointSecretAuthentication,
|
|
9
|
+
} from "./token-endpoint-auth";
|
|
10
|
+
import { applyTokenEndpointAuth } from "./token-endpoint-auth";
|
|
11
|
+
|
|
12
|
+
interface AuthorizationCodeRequestInput {
|
|
13
|
+
code: string;
|
|
14
|
+
redirectURI: string;
|
|
15
|
+
options: AwaitableFunction<Partial<ProviderOptions>>;
|
|
16
|
+
codeVerifier?: string | undefined;
|
|
17
|
+
deviceId?: string | undefined;
|
|
18
|
+
authentication?: TokenEndpointSecretAuthentication | undefined;
|
|
19
|
+
tokenEndpointAuth?: TokenEndpointAuth | undefined;
|
|
20
|
+
tokenEndpoint?: string | undefined;
|
|
21
|
+
headers?: Record<string, string> | undefined;
|
|
22
|
+
additionalParams?: Record<string, string> | undefined;
|
|
23
|
+
resource?: (string | string[]) | undefined;
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
interface AuthorizationCodeRequestBaseInput {
|
|
27
|
+
code: string;
|
|
28
|
+
redirectURI: string;
|
|
29
|
+
options: Partial<ProviderOptions>;
|
|
30
|
+
codeVerifier?: string | undefined;
|
|
31
|
+
deviceId?: string | undefined;
|
|
32
|
+
headers?: Record<string, string> | undefined;
|
|
33
|
+
additionalParams?: Record<string, string> | undefined;
|
|
34
|
+
resource?: (string | string[]) | undefined;
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
interface ValidateAuthorizationCodeInput extends AuthorizationCodeRequestInput {
|
|
38
|
+
tokenEndpoint: string;
|
|
39
|
+
}
|
|
9
40
|
|
|
10
41
|
export async function authorizationCodeRequest({
|
|
11
42
|
code,
|
|
@@ -13,84 +44,50 @@ export async function authorizationCodeRequest({
|
|
|
13
44
|
redirectURI,
|
|
14
45
|
options,
|
|
15
46
|
authentication,
|
|
16
|
-
|
|
47
|
+
tokenEndpointAuth,
|
|
17
48
|
tokenEndpoint,
|
|
18
49
|
deviceId,
|
|
19
50
|
headers,
|
|
20
51
|
additionalParams = {},
|
|
21
52
|
resource,
|
|
22
|
-
}: {
|
|
23
|
-
code: string;
|
|
24
|
-
redirectURI: string;
|
|
25
|
-
options: AwaitableFunction<Partial<ProviderOptions>>;
|
|
26
|
-
codeVerifier?: string | undefined;
|
|
27
|
-
deviceId?: string | undefined;
|
|
28
|
-
authentication?: ("basic" | "post" | "private_key_jwt") | undefined;
|
|
29
|
-
clientAssertion?: ClientAssertionConfig | undefined;
|
|
30
|
-
/** Token endpoint URL. Used as the JWT `aud` claim when signing assertions. */
|
|
31
|
-
tokenEndpoint?: string | undefined;
|
|
32
|
-
headers?: Record<string, string> | undefined;
|
|
33
|
-
additionalParams?: Record<string, string> | undefined;
|
|
34
|
-
resource?: (string | string[]) | undefined;
|
|
35
|
-
}) {
|
|
53
|
+
}: AuthorizationCodeRequestInput) {
|
|
36
54
|
options = typeof options === "function" ? await options() : options;
|
|
37
|
-
|
|
38
|
-
if (authentication === "private_key_jwt") {
|
|
39
|
-
if (!clientAssertion) {
|
|
40
|
-
throw new Error(
|
|
41
|
-
"private_key_jwt authentication requires a clientAssertion configuration",
|
|
42
|
-
);
|
|
43
|
-
}
|
|
44
|
-
const primaryClientId = Array.isArray(options.clientId)
|
|
45
|
-
? options.clientId[0]
|
|
46
|
-
: options.clientId;
|
|
47
|
-
const assertionParams = await resolveAssertionParams({
|
|
48
|
-
clientAssertion,
|
|
49
|
-
clientId: primaryClientId,
|
|
50
|
-
tokenEndpoint,
|
|
51
|
-
});
|
|
52
|
-
additionalParams = { ...additionalParams, ...assertionParams };
|
|
53
|
-
}
|
|
54
|
-
|
|
55
|
-
return createAuthorizationCodeRequest({
|
|
55
|
+
const request = buildAuthorizationCodeRequest({
|
|
56
56
|
code,
|
|
57
57
|
codeVerifier,
|
|
58
58
|
redirectURI,
|
|
59
59
|
options,
|
|
60
|
-
authentication,
|
|
61
60
|
deviceId,
|
|
62
61
|
headers,
|
|
63
62
|
additionalParams,
|
|
64
63
|
resource,
|
|
65
64
|
});
|
|
65
|
+
|
|
66
|
+
await applyTokenEndpointAuth({
|
|
67
|
+
body: request.body,
|
|
68
|
+
headers: request.headers,
|
|
69
|
+
options,
|
|
70
|
+
tokenEndpoint: tokenEndpoint ?? "",
|
|
71
|
+
grantType: "authorization_code",
|
|
72
|
+
tokenEndpointAuth,
|
|
73
|
+
authentication,
|
|
74
|
+
});
|
|
75
|
+
|
|
76
|
+
return request;
|
|
66
77
|
}
|
|
67
78
|
|
|
68
|
-
|
|
69
|
-
* @deprecated use async'd authorizationCodeRequest instead
|
|
70
|
-
*/
|
|
71
|
-
export function createAuthorizationCodeRequest({
|
|
79
|
+
function buildAuthorizationCodeRequest({
|
|
72
80
|
code,
|
|
73
81
|
codeVerifier,
|
|
74
82
|
redirectURI,
|
|
75
83
|
options,
|
|
76
|
-
authentication,
|
|
77
84
|
deviceId,
|
|
78
85
|
headers,
|
|
79
86
|
additionalParams = {},
|
|
80
87
|
resource,
|
|
81
|
-
}: {
|
|
82
|
-
code: string;
|
|
83
|
-
redirectURI: string;
|
|
84
|
-
options: Partial<ProviderOptions>;
|
|
85
|
-
codeVerifier?: string | undefined;
|
|
86
|
-
deviceId?: string | undefined;
|
|
87
|
-
authentication?: ("basic" | "post" | "private_key_jwt") | undefined;
|
|
88
|
-
headers?: Record<string, string> | undefined;
|
|
89
|
-
additionalParams?: Record<string, string> | undefined;
|
|
90
|
-
resource?: (string | string[]) | undefined;
|
|
91
|
-
}) {
|
|
88
|
+
}: AuthorizationCodeRequestBaseInput) {
|
|
92
89
|
const body = new URLSearchParams();
|
|
93
|
-
const requestHeaders: Record<string,
|
|
90
|
+
const requestHeaders: Record<string, string> = {
|
|
94
91
|
"content-type": "application/x-www-form-urlencoded",
|
|
95
92
|
accept: "application/json",
|
|
96
93
|
...headers,
|
|
@@ -111,21 +108,6 @@ export function createAuthorizationCodeRequest({
|
|
|
111
108
|
}
|
|
112
109
|
}
|
|
113
110
|
}
|
|
114
|
-
const primaryClientId = Array.isArray(options.clientId)
|
|
115
|
-
? options.clientId[0]
|
|
116
|
-
: options.clientId;
|
|
117
|
-
if (authentication === "basic") {
|
|
118
|
-
const encodedCredentials = base64.encode(
|
|
119
|
-
`${primaryClientId}:${options.clientSecret ?? ""}`,
|
|
120
|
-
);
|
|
121
|
-
requestHeaders["authorization"] = `Basic ${encodedCredentials}`;
|
|
122
|
-
} else {
|
|
123
|
-
body.set("client_id", primaryClientId);
|
|
124
|
-
if (authentication !== "private_key_jwt" && options.clientSecret) {
|
|
125
|
-
body.set("client_secret", options.clientSecret);
|
|
126
|
-
}
|
|
127
|
-
}
|
|
128
|
-
|
|
129
111
|
for (const [key, value] of Object.entries(additionalParams)) {
|
|
130
112
|
if (!body.has(key)) body.append(key, value);
|
|
131
113
|
}
|
|
@@ -143,31 +125,19 @@ export async function validateAuthorizationCode({
|
|
|
143
125
|
options,
|
|
144
126
|
tokenEndpoint,
|
|
145
127
|
authentication,
|
|
146
|
-
|
|
128
|
+
tokenEndpointAuth,
|
|
147
129
|
deviceId,
|
|
148
130
|
headers,
|
|
149
131
|
additionalParams = {},
|
|
150
132
|
resource,
|
|
151
|
-
}: {
|
|
152
|
-
code: string;
|
|
153
|
-
redirectURI: string;
|
|
154
|
-
options: AwaitableFunction<Partial<ProviderOptions>>;
|
|
155
|
-
codeVerifier?: string | undefined;
|
|
156
|
-
deviceId?: string | undefined;
|
|
157
|
-
tokenEndpoint: string;
|
|
158
|
-
authentication?: ("basic" | "post" | "private_key_jwt") | undefined;
|
|
159
|
-
clientAssertion?: ClientAssertionConfig | undefined;
|
|
160
|
-
headers?: Record<string, string> | undefined;
|
|
161
|
-
additionalParams?: Record<string, string> | undefined;
|
|
162
|
-
resource?: (string | string[]) | undefined;
|
|
163
|
-
}) {
|
|
133
|
+
}: ValidateAuthorizationCodeInput) {
|
|
164
134
|
const { body, headers: requestHeaders } = await authorizationCodeRequest({
|
|
165
135
|
code,
|
|
166
136
|
codeVerifier,
|
|
167
137
|
redirectURI,
|
|
168
138
|
options,
|
|
169
139
|
authentication,
|
|
170
|
-
|
|
140
|
+
tokenEndpointAuth,
|
|
171
141
|
tokenEndpoint,
|
|
172
142
|
deviceId,
|
|
173
143
|
headers,
|
|
@@ -0,0 +1,111 @@
|
|
|
1
|
+
import { decodeProtectedHeader, jwtVerify } from "jose";
|
|
2
|
+
import type { ProviderOptions, UpstreamProvider } from "./oauth-provider";
|
|
3
|
+
|
|
4
|
+
async function sha256Hex(value: string) {
|
|
5
|
+
const data = new TextEncoder().encode(value);
|
|
6
|
+
const digest = await crypto.subtle.digest("SHA-256", data);
|
|
7
|
+
return Array.from(new Uint8Array(digest))
|
|
8
|
+
.map((byte) => byte.toString(16).padStart(2, "0"))
|
|
9
|
+
.join("");
|
|
10
|
+
}
|
|
11
|
+
|
|
12
|
+
async function nonceMatches(
|
|
13
|
+
claimNonce: unknown,
|
|
14
|
+
nonce: string,
|
|
15
|
+
comparison: "exact" | "exact-or-sha256" = "exact",
|
|
16
|
+
) {
|
|
17
|
+
if (typeof claimNonce !== "string") {
|
|
18
|
+
return false;
|
|
19
|
+
}
|
|
20
|
+
if (claimNonce === nonce) {
|
|
21
|
+
return true;
|
|
22
|
+
}
|
|
23
|
+
if (comparison === "exact-or-sha256") {
|
|
24
|
+
return claimNonce === (await sha256Hex(nonce));
|
|
25
|
+
}
|
|
26
|
+
return false;
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
/**
|
|
30
|
+
* Whether a provider can verify a client-submitted id_token.
|
|
31
|
+
*
|
|
32
|
+
* A provider supports id_token sign-in when it declares an {@link UpstreamProvider.idToken}
|
|
33
|
+
* verification config, or when the integrator supplies a `verifyIdToken` override on the
|
|
34
|
+
* provider options. A provider whose options set `disableIdTokenSignIn`, or that declares
|
|
35
|
+
* neither, rejects the client id_token sign-in path with `ID_TOKEN_NOT_SUPPORTED`.
|
|
36
|
+
*/
|
|
37
|
+
export function supportsIdTokenSignIn(provider: UpstreamProvider<any, any>) {
|
|
38
|
+
const options = (provider.options ?? {}) as Partial<ProviderOptions>;
|
|
39
|
+
if (options.disableIdTokenSignIn) {
|
|
40
|
+
return false;
|
|
41
|
+
}
|
|
42
|
+
return Boolean(provider.idToken || options.verifyIdToken);
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
/**
|
|
46
|
+
* Verify a client-submitted id_token against a provider's verification config.
|
|
47
|
+
*
|
|
48
|
+
* This is the single id_token verifier for every social provider. Providers no longer
|
|
49
|
+
* implement their own boolean `verifyIdToken`; they declare an {@link UpstreamProvider.idToken}
|
|
50
|
+
* config and this function performs the cryptographic check. The contract is fail-closed: a
|
|
51
|
+
* provider without a config (and without an integrator `verifyIdToken` override) returns
|
|
52
|
+
* `false`, so a forged token can never be accepted by omission.
|
|
53
|
+
*
|
|
54
|
+
* @returns `true` only when the token is authentic for the provider.
|
|
55
|
+
*/
|
|
56
|
+
export async function verifyProviderIdToken(
|
|
57
|
+
provider: UpstreamProvider<any, any>,
|
|
58
|
+
token: string,
|
|
59
|
+
nonce?: string,
|
|
60
|
+
): Promise<boolean> {
|
|
61
|
+
const options = (provider.options ?? {}) as Partial<ProviderOptions>;
|
|
62
|
+
if (options.disableIdTokenSignIn) {
|
|
63
|
+
return false;
|
|
64
|
+
}
|
|
65
|
+
// Every verification path is fail-closed: a throw from the integrator override, a custom
|
|
66
|
+
// remote verifier, the JWKS resolver, or signature checking resolves to `false` instead of
|
|
67
|
+
// escaping to the caller as a server error.
|
|
68
|
+
try {
|
|
69
|
+
if (options.verifyIdToken) {
|
|
70
|
+
return await options.verifyIdToken(token, nonce);
|
|
71
|
+
}
|
|
72
|
+
const config = provider.idToken;
|
|
73
|
+
if (!config) {
|
|
74
|
+
return false;
|
|
75
|
+
}
|
|
76
|
+
if ("verify" in config) {
|
|
77
|
+
return await config.verify(token, nonce);
|
|
78
|
+
}
|
|
79
|
+
// Opaque (non-JWS) tokens carry no signature to check. They are accepted only when the
|
|
80
|
+
// provider opts in, in which case getUserInfo resolves identity from the access token via
|
|
81
|
+
// the provider's userinfo endpoint, which validates it (e.g. Facebook Graph access tokens).
|
|
82
|
+
if (token.split(".").length !== 3) {
|
|
83
|
+
return config.allowOpaqueToken === true;
|
|
84
|
+
}
|
|
85
|
+
// `kid` is optional in JWS: a JWKS resolver can still select a key by algorithm, so
|
|
86
|
+
// key selection is left to config.jwks. The token's `alg` only seeds the default
|
|
87
|
+
// allowed-algorithms list when the provider does not pin one.
|
|
88
|
+
const { alg } = decodeProtectedHeader(token);
|
|
89
|
+
const { payload } = await jwtVerify(token, config.jwks, {
|
|
90
|
+
issuer: config.issuer,
|
|
91
|
+
audience: config.audience,
|
|
92
|
+
algorithms: config.algorithms ?? (alg ? [alg] : undefined),
|
|
93
|
+
maxTokenAge: config.maxTokenAge,
|
|
94
|
+
});
|
|
95
|
+
if (
|
|
96
|
+
nonce &&
|
|
97
|
+
!(await nonceMatches(payload.nonce, nonce, config.nonceComparison))
|
|
98
|
+
) {
|
|
99
|
+
return false;
|
|
100
|
+
}
|
|
101
|
+
// Provider-specific claim check on the now-verified payload (e.g. Google's
|
|
102
|
+
// hosted-domain `hd`). Standard checks have already passed, so a token that
|
|
103
|
+
// fails here is authentic but does not meet the provider's extra constraint.
|
|
104
|
+
if (config.verifyClaims && !config.verifyClaims(payload)) {
|
|
105
|
+
return false;
|
|
106
|
+
}
|
|
107
|
+
return true;
|
|
108
|
+
} catch {
|
|
109
|
+
return false;
|
|
110
|
+
}
|
|
111
|
+
}
|