@better-auth/core 1.4.0-beta.7 → 1.4.0-beta.8

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.
Files changed (39) hide show
  1. package/.turbo/turbo-build.log +12 -4
  2. package/build.config.ts +2 -0
  3. package/dist/db/index.cjs +16 -0
  4. package/dist/db/index.d.cts +14 -3
  5. package/dist/db/index.d.mts +14 -3
  6. package/dist/db/index.d.ts +14 -3
  7. package/dist/db/index.mjs +16 -1
  8. package/dist/env/index.cjs +315 -0
  9. package/dist/env/index.d.cts +85 -0
  10. package/dist/env/index.d.mts +85 -0
  11. package/dist/env/index.d.ts +85 -0
  12. package/dist/env/index.mjs +300 -0
  13. package/dist/index.d.cts +2 -2
  14. package/dist/index.d.mts +2 -2
  15. package/dist/index.d.ts +2 -2
  16. package/dist/oauth2/index.cjs +368 -0
  17. package/dist/oauth2/index.d.cts +277 -0
  18. package/dist/oauth2/index.d.mts +277 -0
  19. package/dist/oauth2/index.d.ts +277 -0
  20. package/dist/oauth2/index.mjs +357 -0
  21. package/dist/shared/{core.CnvFgghY.d.cts → core.DeNN5HMO.d.cts} +27 -1
  22. package/dist/shared/{core.CnvFgghY.d.mts → core.DeNN5HMO.d.mts} +27 -1
  23. package/dist/shared/{core.CnvFgghY.d.ts → core.DeNN5HMO.d.ts} +27 -1
  24. package/package.json +29 -1
  25. package/src/db/index.ts +2 -0
  26. package/src/db/schema/rate-limit.ts +21 -0
  27. package/src/db/type.ts +28 -0
  28. package/src/env/color-depth.ts +171 -0
  29. package/src/env/env-impl.ts +123 -0
  30. package/src/env/index.ts +23 -0
  31. package/src/env/logger.test.ts +33 -0
  32. package/src/env/logger.ts +145 -0
  33. package/src/oauth2/client-credentials-token.ts +102 -0
  34. package/src/oauth2/create-authorization-url.ts +85 -0
  35. package/src/oauth2/index.ts +22 -0
  36. package/src/oauth2/oauth-provider.ts +194 -0
  37. package/src/oauth2/refresh-access-token.ts +124 -0
  38. package/src/oauth2/utils.ts +36 -0
  39. package/src/oauth2/validate-authorization-code.ts +156 -0
@@ -0,0 +1,277 @@
1
+ import { a as LiteralString } from '../shared/core.DeNN5HMO.mjs';
2
+ import * as jose from 'jose';
3
+ import 'zod';
4
+
5
+ interface OAuth2Tokens {
6
+ tokenType?: string;
7
+ accessToken?: string;
8
+ refreshToken?: string;
9
+ accessTokenExpiresAt?: Date;
10
+ refreshTokenExpiresAt?: Date;
11
+ scopes?: string[];
12
+ idToken?: string;
13
+ }
14
+ type OAuth2UserInfo = {
15
+ id: string | number;
16
+ name?: string;
17
+ email?: string | null;
18
+ image?: string;
19
+ emailVerified: boolean;
20
+ };
21
+ interface OAuthProvider<T extends Record<string, any> = Record<string, any>, O extends Record<string, any> = Partial<ProviderOptions>> {
22
+ id: LiteralString;
23
+ createAuthorizationURL: (data: {
24
+ state: string;
25
+ codeVerifier: string;
26
+ scopes?: string[];
27
+ redirectURI: string;
28
+ display?: string;
29
+ loginHint?: string;
30
+ }) => Promise<URL> | URL;
31
+ name: string;
32
+ validateAuthorizationCode: (data: {
33
+ code: string;
34
+ redirectURI: string;
35
+ codeVerifier?: string;
36
+ deviceId?: string;
37
+ }) => Promise<OAuth2Tokens>;
38
+ getUserInfo: (token: OAuth2Tokens & {
39
+ /**
40
+ * The user object from the provider
41
+ * This is only available for some providers like Apple
42
+ */
43
+ user?: {
44
+ name?: {
45
+ firstName?: string;
46
+ lastName?: string;
47
+ };
48
+ email?: string;
49
+ };
50
+ }) => Promise<{
51
+ user: OAuth2UserInfo;
52
+ data: T;
53
+ } | null>;
54
+ /**
55
+ * Custom function to refresh a token
56
+ */
57
+ refreshAccessToken?: (refreshToken: string) => Promise<OAuth2Tokens>;
58
+ revokeToken?: (token: string) => Promise<void>;
59
+ /**
60
+ * Verify the id token
61
+ * @param token - The id token
62
+ * @param nonce - The nonce
63
+ * @returns True if the id token is valid, false otherwise
64
+ */
65
+ verifyIdToken?: (token: string, nonce?: string) => Promise<boolean>;
66
+ /**
67
+ * Disable implicit sign up for new users. When set to true for the provider,
68
+ * sign-in need to be called with with requestSignUp as true to create new users.
69
+ */
70
+ disableImplicitSignUp?: boolean;
71
+ /**
72
+ * Disable sign up for new users.
73
+ */
74
+ disableSignUp?: boolean;
75
+ /**
76
+ * Options for the provider
77
+ */
78
+ options?: O;
79
+ }
80
+ type ProviderOptions<Profile extends Record<string, any> = any> = {
81
+ /**
82
+ * The client ID of your application.
83
+ *
84
+ * This is usually a string but can be any type depending on the provider.
85
+ */
86
+ clientId?: unknown;
87
+ /**
88
+ * The client secret of your application
89
+ */
90
+ clientSecret?: string;
91
+ /**
92
+ * The scopes you want to request from the provider
93
+ */
94
+ scope?: string[];
95
+ /**
96
+ * Remove default scopes of the provider
97
+ */
98
+ disableDefaultScope?: boolean;
99
+ /**
100
+ * The redirect URL for your application. This is where the provider will
101
+ * redirect the user after the sign in process. Make sure this URL is
102
+ * whitelisted in the provider's dashboard.
103
+ */
104
+ redirectURI?: string;
105
+ /**
106
+ * The client key of your application
107
+ * Tiktok Social Provider uses this field instead of clientId
108
+ */
109
+ clientKey?: string;
110
+ /**
111
+ * Disable provider from allowing users to sign in
112
+ * with this provider with an id token sent from the
113
+ * client.
114
+ */
115
+ disableIdTokenSignIn?: boolean;
116
+ /**
117
+ * verifyIdToken function to verify the id token
118
+ */
119
+ verifyIdToken?: (token: string, nonce?: string) => Promise<boolean>;
120
+ /**
121
+ * Custom function to get user info from the provider
122
+ */
123
+ getUserInfo?: (token: OAuth2Tokens) => Promise<{
124
+ user: {
125
+ id: string;
126
+ name?: string;
127
+ email?: string | null;
128
+ image?: string;
129
+ emailVerified: boolean;
130
+ [key: string]: any;
131
+ };
132
+ data: any;
133
+ }>;
134
+ /**
135
+ * Custom function to refresh a token
136
+ */
137
+ refreshAccessToken?: (refreshToken: string) => Promise<OAuth2Tokens>;
138
+ /**
139
+ * Custom function to map the provider profile to a
140
+ * user.
141
+ */
142
+ mapProfileToUser?: (profile: Profile) => {
143
+ id?: string;
144
+ name?: string;
145
+ email?: string | null;
146
+ image?: string;
147
+ emailVerified?: boolean;
148
+ [key: string]: any;
149
+ } | Promise<{
150
+ id?: string;
151
+ name?: string;
152
+ email?: string | null;
153
+ image?: string;
154
+ emailVerified?: boolean;
155
+ [key: string]: any;
156
+ }>;
157
+ /**
158
+ * Disable implicit sign up for new users. When set to true for the provider,
159
+ * sign-in need to be called with with requestSignUp as true to create new users.
160
+ */
161
+ disableImplicitSignUp?: boolean;
162
+ /**
163
+ * Disable sign up for new users.
164
+ */
165
+ disableSignUp?: boolean;
166
+ /**
167
+ * The prompt to use for the authorization code request
168
+ */
169
+ prompt?: "select_account" | "consent" | "login" | "none" | "select_account consent";
170
+ /**
171
+ * The response mode to use for the authorization code request
172
+ */
173
+ responseMode?: "query" | "form_post";
174
+ /**
175
+ * If enabled, the user info will be overridden with the provider user info
176
+ * This is useful if you want to use the provider user info to update the user info
177
+ *
178
+ * @default false
179
+ */
180
+ overrideUserInfoOnSignIn?: boolean;
181
+ };
182
+
183
+ declare function getOAuth2Tokens(data: Record<string, any>): OAuth2Tokens;
184
+ declare function generateCodeChallenge(codeVerifier: string): Promise<string>;
185
+
186
+ declare function createAuthorizationURL({ id, options, authorizationEndpoint, state, codeVerifier, scopes, claims, redirectURI, duration, prompt, accessType, responseType, display, loginHint, hd, responseMode, additionalParams, scopeJoiner, }: {
187
+ id: string;
188
+ options: ProviderOptions;
189
+ redirectURI: string;
190
+ authorizationEndpoint: string;
191
+ state: string;
192
+ codeVerifier?: string;
193
+ scopes: string[];
194
+ claims?: string[];
195
+ duration?: string;
196
+ prompt?: string;
197
+ accessType?: string;
198
+ responseType?: string;
199
+ display?: string;
200
+ loginHint?: string;
201
+ hd?: string;
202
+ responseMode?: string;
203
+ additionalParams?: Record<string, string>;
204
+ scopeJoiner?: string;
205
+ }): Promise<URL>;
206
+
207
+ declare function createAuthorizationCodeRequest({ code, codeVerifier, redirectURI, options, authentication, deviceId, headers, additionalParams, resource, }: {
208
+ code: string;
209
+ redirectURI: string;
210
+ options: Partial<ProviderOptions>;
211
+ codeVerifier?: string;
212
+ deviceId?: string;
213
+ authentication?: "basic" | "post";
214
+ headers?: Record<string, string>;
215
+ additionalParams?: Record<string, string>;
216
+ resource?: string | string[];
217
+ }): {
218
+ body: URLSearchParams;
219
+ headers: Record<string, any>;
220
+ };
221
+ declare function validateAuthorizationCode({ code, codeVerifier, redirectURI, options, tokenEndpoint, authentication, deviceId, headers, additionalParams, resource, }: {
222
+ code: string;
223
+ redirectURI: string;
224
+ options: Partial<ProviderOptions>;
225
+ codeVerifier?: string;
226
+ deviceId?: string;
227
+ tokenEndpoint: string;
228
+ authentication?: "basic" | "post";
229
+ headers?: Record<string, string>;
230
+ additionalParams?: Record<string, string>;
231
+ resource?: string | string[];
232
+ }): Promise<OAuth2Tokens>;
233
+ declare function validateToken(token: string, jwksEndpoint: string): Promise<jose.JWTVerifyResult<jose.JWTPayload>>;
234
+
235
+ declare function createRefreshAccessTokenRequest({ refreshToken, options, authentication, extraParams, resource, }: {
236
+ refreshToken: string;
237
+ options: Partial<ProviderOptions>;
238
+ authentication?: "basic" | "post";
239
+ extraParams?: Record<string, string>;
240
+ resource?: string | string[];
241
+ }): {
242
+ body: URLSearchParams;
243
+ headers: Record<string, any>;
244
+ };
245
+ declare function refreshAccessToken({ refreshToken, options, tokenEndpoint, authentication, extraParams, }: {
246
+ refreshToken: string;
247
+ options: Partial<ProviderOptions>;
248
+ tokenEndpoint: string;
249
+ authentication?: "basic" | "post";
250
+ extraParams?: Record<string, string>;
251
+ /** @deprecated always "refresh_token" */
252
+ grantType?: string;
253
+ }): Promise<OAuth2Tokens>;
254
+
255
+ declare function createClientCredentialsTokenRequest({ options, scope, authentication, resource, }: {
256
+ options: ProviderOptions & {
257
+ clientSecret: string;
258
+ };
259
+ scope?: string;
260
+ authentication?: "basic" | "post";
261
+ resource?: string | string[];
262
+ }): {
263
+ body: URLSearchParams;
264
+ headers: Record<string, any>;
265
+ };
266
+ declare function clientCredentialsToken({ options, tokenEndpoint, scope, authentication, resource, }: {
267
+ options: ProviderOptions & {
268
+ clientSecret: string;
269
+ };
270
+ tokenEndpoint: string;
271
+ scope: string;
272
+ authentication?: "basic" | "post";
273
+ resource?: string | string[];
274
+ }): Promise<OAuth2Tokens>;
275
+
276
+ export { clientCredentialsToken, createAuthorizationCodeRequest, createAuthorizationURL, createClientCredentialsTokenRequest, createRefreshAccessTokenRequest, generateCodeChallenge, getOAuth2Tokens, refreshAccessToken, validateAuthorizationCode, validateToken };
277
+ export type { OAuth2Tokens, OAuth2UserInfo, OAuthProvider, ProviderOptions };
@@ -0,0 +1,277 @@
1
+ import { a as LiteralString } from '../shared/core.DeNN5HMO.js';
2
+ import * as jose from 'jose';
3
+ import 'zod';
4
+
5
+ interface OAuth2Tokens {
6
+ tokenType?: string;
7
+ accessToken?: string;
8
+ refreshToken?: string;
9
+ accessTokenExpiresAt?: Date;
10
+ refreshTokenExpiresAt?: Date;
11
+ scopes?: string[];
12
+ idToken?: string;
13
+ }
14
+ type OAuth2UserInfo = {
15
+ id: string | number;
16
+ name?: string;
17
+ email?: string | null;
18
+ image?: string;
19
+ emailVerified: boolean;
20
+ };
21
+ interface OAuthProvider<T extends Record<string, any> = Record<string, any>, O extends Record<string, any> = Partial<ProviderOptions>> {
22
+ id: LiteralString;
23
+ createAuthorizationURL: (data: {
24
+ state: string;
25
+ codeVerifier: string;
26
+ scopes?: string[];
27
+ redirectURI: string;
28
+ display?: string;
29
+ loginHint?: string;
30
+ }) => Promise<URL> | URL;
31
+ name: string;
32
+ validateAuthorizationCode: (data: {
33
+ code: string;
34
+ redirectURI: string;
35
+ codeVerifier?: string;
36
+ deviceId?: string;
37
+ }) => Promise<OAuth2Tokens>;
38
+ getUserInfo: (token: OAuth2Tokens & {
39
+ /**
40
+ * The user object from the provider
41
+ * This is only available for some providers like Apple
42
+ */
43
+ user?: {
44
+ name?: {
45
+ firstName?: string;
46
+ lastName?: string;
47
+ };
48
+ email?: string;
49
+ };
50
+ }) => Promise<{
51
+ user: OAuth2UserInfo;
52
+ data: T;
53
+ } | null>;
54
+ /**
55
+ * Custom function to refresh a token
56
+ */
57
+ refreshAccessToken?: (refreshToken: string) => Promise<OAuth2Tokens>;
58
+ revokeToken?: (token: string) => Promise<void>;
59
+ /**
60
+ * Verify the id token
61
+ * @param token - The id token
62
+ * @param nonce - The nonce
63
+ * @returns True if the id token is valid, false otherwise
64
+ */
65
+ verifyIdToken?: (token: string, nonce?: string) => Promise<boolean>;
66
+ /**
67
+ * Disable implicit sign up for new users. When set to true for the provider,
68
+ * sign-in need to be called with with requestSignUp as true to create new users.
69
+ */
70
+ disableImplicitSignUp?: boolean;
71
+ /**
72
+ * Disable sign up for new users.
73
+ */
74
+ disableSignUp?: boolean;
75
+ /**
76
+ * Options for the provider
77
+ */
78
+ options?: O;
79
+ }
80
+ type ProviderOptions<Profile extends Record<string, any> = any> = {
81
+ /**
82
+ * The client ID of your application.
83
+ *
84
+ * This is usually a string but can be any type depending on the provider.
85
+ */
86
+ clientId?: unknown;
87
+ /**
88
+ * The client secret of your application
89
+ */
90
+ clientSecret?: string;
91
+ /**
92
+ * The scopes you want to request from the provider
93
+ */
94
+ scope?: string[];
95
+ /**
96
+ * Remove default scopes of the provider
97
+ */
98
+ disableDefaultScope?: boolean;
99
+ /**
100
+ * The redirect URL for your application. This is where the provider will
101
+ * redirect the user after the sign in process. Make sure this URL is
102
+ * whitelisted in the provider's dashboard.
103
+ */
104
+ redirectURI?: string;
105
+ /**
106
+ * The client key of your application
107
+ * Tiktok Social Provider uses this field instead of clientId
108
+ */
109
+ clientKey?: string;
110
+ /**
111
+ * Disable provider from allowing users to sign in
112
+ * with this provider with an id token sent from the
113
+ * client.
114
+ */
115
+ disableIdTokenSignIn?: boolean;
116
+ /**
117
+ * verifyIdToken function to verify the id token
118
+ */
119
+ verifyIdToken?: (token: string, nonce?: string) => Promise<boolean>;
120
+ /**
121
+ * Custom function to get user info from the provider
122
+ */
123
+ getUserInfo?: (token: OAuth2Tokens) => Promise<{
124
+ user: {
125
+ id: string;
126
+ name?: string;
127
+ email?: string | null;
128
+ image?: string;
129
+ emailVerified: boolean;
130
+ [key: string]: any;
131
+ };
132
+ data: any;
133
+ }>;
134
+ /**
135
+ * Custom function to refresh a token
136
+ */
137
+ refreshAccessToken?: (refreshToken: string) => Promise<OAuth2Tokens>;
138
+ /**
139
+ * Custom function to map the provider profile to a
140
+ * user.
141
+ */
142
+ mapProfileToUser?: (profile: Profile) => {
143
+ id?: string;
144
+ name?: string;
145
+ email?: string | null;
146
+ image?: string;
147
+ emailVerified?: boolean;
148
+ [key: string]: any;
149
+ } | Promise<{
150
+ id?: string;
151
+ name?: string;
152
+ email?: string | null;
153
+ image?: string;
154
+ emailVerified?: boolean;
155
+ [key: string]: any;
156
+ }>;
157
+ /**
158
+ * Disable implicit sign up for new users. When set to true for the provider,
159
+ * sign-in need to be called with with requestSignUp as true to create new users.
160
+ */
161
+ disableImplicitSignUp?: boolean;
162
+ /**
163
+ * Disable sign up for new users.
164
+ */
165
+ disableSignUp?: boolean;
166
+ /**
167
+ * The prompt to use for the authorization code request
168
+ */
169
+ prompt?: "select_account" | "consent" | "login" | "none" | "select_account consent";
170
+ /**
171
+ * The response mode to use for the authorization code request
172
+ */
173
+ responseMode?: "query" | "form_post";
174
+ /**
175
+ * If enabled, the user info will be overridden with the provider user info
176
+ * This is useful if you want to use the provider user info to update the user info
177
+ *
178
+ * @default false
179
+ */
180
+ overrideUserInfoOnSignIn?: boolean;
181
+ };
182
+
183
+ declare function getOAuth2Tokens(data: Record<string, any>): OAuth2Tokens;
184
+ declare function generateCodeChallenge(codeVerifier: string): Promise<string>;
185
+
186
+ declare function createAuthorizationURL({ id, options, authorizationEndpoint, state, codeVerifier, scopes, claims, redirectURI, duration, prompt, accessType, responseType, display, loginHint, hd, responseMode, additionalParams, scopeJoiner, }: {
187
+ id: string;
188
+ options: ProviderOptions;
189
+ redirectURI: string;
190
+ authorizationEndpoint: string;
191
+ state: string;
192
+ codeVerifier?: string;
193
+ scopes: string[];
194
+ claims?: string[];
195
+ duration?: string;
196
+ prompt?: string;
197
+ accessType?: string;
198
+ responseType?: string;
199
+ display?: string;
200
+ loginHint?: string;
201
+ hd?: string;
202
+ responseMode?: string;
203
+ additionalParams?: Record<string, string>;
204
+ scopeJoiner?: string;
205
+ }): Promise<URL>;
206
+
207
+ declare function createAuthorizationCodeRequest({ code, codeVerifier, redirectURI, options, authentication, deviceId, headers, additionalParams, resource, }: {
208
+ code: string;
209
+ redirectURI: string;
210
+ options: Partial<ProviderOptions>;
211
+ codeVerifier?: string;
212
+ deviceId?: string;
213
+ authentication?: "basic" | "post";
214
+ headers?: Record<string, string>;
215
+ additionalParams?: Record<string, string>;
216
+ resource?: string | string[];
217
+ }): {
218
+ body: URLSearchParams;
219
+ headers: Record<string, any>;
220
+ };
221
+ declare function validateAuthorizationCode({ code, codeVerifier, redirectURI, options, tokenEndpoint, authentication, deviceId, headers, additionalParams, resource, }: {
222
+ code: string;
223
+ redirectURI: string;
224
+ options: Partial<ProviderOptions>;
225
+ codeVerifier?: string;
226
+ deviceId?: string;
227
+ tokenEndpoint: string;
228
+ authentication?: "basic" | "post";
229
+ headers?: Record<string, string>;
230
+ additionalParams?: Record<string, string>;
231
+ resource?: string | string[];
232
+ }): Promise<OAuth2Tokens>;
233
+ declare function validateToken(token: string, jwksEndpoint: string): Promise<jose.JWTVerifyResult<jose.JWTPayload>>;
234
+
235
+ declare function createRefreshAccessTokenRequest({ refreshToken, options, authentication, extraParams, resource, }: {
236
+ refreshToken: string;
237
+ options: Partial<ProviderOptions>;
238
+ authentication?: "basic" | "post";
239
+ extraParams?: Record<string, string>;
240
+ resource?: string | string[];
241
+ }): {
242
+ body: URLSearchParams;
243
+ headers: Record<string, any>;
244
+ };
245
+ declare function refreshAccessToken({ refreshToken, options, tokenEndpoint, authentication, extraParams, }: {
246
+ refreshToken: string;
247
+ options: Partial<ProviderOptions>;
248
+ tokenEndpoint: string;
249
+ authentication?: "basic" | "post";
250
+ extraParams?: Record<string, string>;
251
+ /** @deprecated always "refresh_token" */
252
+ grantType?: string;
253
+ }): Promise<OAuth2Tokens>;
254
+
255
+ declare function createClientCredentialsTokenRequest({ options, scope, authentication, resource, }: {
256
+ options: ProviderOptions & {
257
+ clientSecret: string;
258
+ };
259
+ scope?: string;
260
+ authentication?: "basic" | "post";
261
+ resource?: string | string[];
262
+ }): {
263
+ body: URLSearchParams;
264
+ headers: Record<string, any>;
265
+ };
266
+ declare function clientCredentialsToken({ options, tokenEndpoint, scope, authentication, resource, }: {
267
+ options: ProviderOptions & {
268
+ clientSecret: string;
269
+ };
270
+ tokenEndpoint: string;
271
+ scope: string;
272
+ authentication?: "basic" | "post";
273
+ resource?: string | string[];
274
+ }): Promise<OAuth2Tokens>;
275
+
276
+ export { clientCredentialsToken, createAuthorizationCodeRequest, createAuthorizationURL, createClientCredentialsTokenRequest, createRefreshAccessTokenRequest, generateCodeChallenge, getOAuth2Tokens, refreshAccessToken, validateAuthorizationCode, validateToken };
277
+ export type { OAuth2Tokens, OAuth2UserInfo, OAuthProvider, ProviderOptions };