-
Notifications
You must be signed in to change notification settings - Fork 454
Expand file tree
/
Copy pathclient.ts
More file actions
792 lines (705 loc) · 26.8 KB
/
client.ts
File metadata and controls
792 lines (705 loc) · 26.8 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
import type { IncomingMessage, ServerResponse } from "node:http";
import { cookies } from "next/headers.js";
import { NextRequest, NextResponse } from "next/server.js";
import { NextApiRequest, NextApiResponse } from "next/types.js";
import {
AccessTokenError,
AccessTokenErrorCode,
AccessTokenForConnectionError,
AccessTokenForConnectionErrorCode,
} from "../errors/index.js";
import {
AccessTokenForConnectionOptions,
AuthorizationParameters,
SessionData,
SessionDataStore,
StartInteractiveLoginOptions
} from "../types/index.js";
import {
AuthClient,
BeforeSessionSavedHook,
OnCallbackHook,
RoutesOptions
} from "./auth-client.js";
import { RequestCookies, ResponseCookies } from "./cookies.js";
import {
AbstractSessionStore,
SessionConfiguration,
SessionCookieOptions
} from "./session/abstract-session-store.js";
import { StatefulSessionStore } from "./session/stateful-session-store.js";
import { StatelessSessionStore } from "./session/stateless-session-store.js";
import {
TransactionCookieOptions,
TransactionStore
} from "./transaction-store.js";
export interface Auth0ClientOptions {
// authorization server configuration
/**
* The Auth0 domain for the tenant (e.g.: `example.us.auth0.com`).
*
* If it's not specified, it will be loaded from the `AUTH0_DOMAIN` environment variable.
*/
domain?: string;
/**
* The Auth0 client ID.
*
* If it's not specified, it will be loaded from the `AUTH0_CLIENT_ID` environment variable.
*/
clientId?: string;
/**
* The Auth0 client secret.
*
* If it's not specified, it will be loaded from the `AUTH0_CLIENT_SECRET` environment variable.
*/
clientSecret?: string;
/**
* Additional parameters to send to the `/authorize` endpoint.
*/
authorizationParameters?: AuthorizationParameters;
/**
* If enabled, the SDK will use the Pushed Authorization Requests (PAR) protocol when communicating with the authorization server.
*/
pushedAuthorizationRequests?: boolean;
/**
* Private key for use with `private_key_jwt` clients.
* This should be a string that is the contents of a PEM file or a CryptoKey.
*/
clientAssertionSigningKey?: string | CryptoKey;
/**
* The algorithm used to sign the client assertion JWT.
* Uses one of `token_endpoint_auth_signing_alg_values_supported` if not specified.
* If the Authorization Server discovery document does not list `token_endpoint_auth_signing_alg_values_supported`
* this property will be required.
*/
clientAssertionSigningAlg?: string;
// application configuration
/**
* The URL of your application (e.g.: `http://localhost:3000`).
*
* If it's not specified, it will be loaded from the `APP_BASE_URL` environment variable.
*/
appBaseUrl?: string;
/**
* A 32-byte, hex-encoded secret used for encrypting cookies.
*
* If it's not specified, it will be loaded from the `AUTH0_SECRET` environment variable.
*/
secret?: string;
/**
* The path to redirect the user to after successfully authenticating. Defaults to `/`.
*/
signInReturnToPath?: string;
// session configuration
/**
* Configure the session timeouts and whether to use rolling sessions or not.
*
* See [Session configuration](https://github.com/auth0/nextjs-auth0/blob/main/EXAMPLES.md#session-configuration) for additional details.
*/
session?: SessionConfiguration;
// transaction cookie configuration
/**
* Configure the transaction cookie used to store the state of the authentication transaction.
*/
transactionCookie?: TransactionCookieOptions;
// hooks
/**
* A method to manipulate the session before persisting it.
*
* See [beforeSessionSaved](https://github.com/auth0/nextjs-auth0/blob/main/EXAMPLES.md#beforesessionsaved) for additional details
*/
beforeSessionSaved?: BeforeSessionSavedHook;
/**
* A method to handle errors or manage redirects after attempting to authenticate.
*
* See [onCallback](https://github.com/auth0/nextjs-auth0/blob/main/EXAMPLES.md#oncallback) for additional details
*/
onCallback?: OnCallbackHook;
// provide a session store to persist sessions in your own data store
/**
* A custom session store implementation used to persist sessions to a data store.
*
* See [Database sessions](https://github.com/auth0/nextjs-auth0/blob/main/EXAMPLES.md#database-sessions) for additional details.
*/
sessionStore?: SessionDataStore;
/**
* Configure the paths for the authentication routes.
*
* See [Custom routes](https://github.com/auth0/nextjs-auth0/blob/main/EXAMPLES.md#custom-routes) for additional details.
*/
routes?: RoutesOptions;
/**
* Allow insecure requests to be made to the authorization server. This can be useful when testing
* with a mock OIDC provider that does not support TLS, locally.
* This option can only be used when NODE_ENV is not set to `production`.
*/
allowInsecureRequests?: boolean;
/**
* Integer value for the HTTP timeout in milliseconds for authentication requests.
* Defaults to `5000` ms.
*/
httpTimeout?: number;
/**
* Boolean value to opt-out of sending the library name and version to your authorization server
* via the `Auth0-Client` header. Defaults to `true`.
*/
enableTelemetry?: boolean;
/**
* Boolean value to enable the `/auth/access-token` endpoint for use in the client app.
*
* Defaults to `true`.
*
* NOTE: Set this to `false` if your client does not need to directly interact with resource servers (Token Mediating Backend). This will be false for most apps.
*
* A security best practice is to disable this to avoid exposing access tokens to the client app.
*
* See: https://datatracker.ietf.org/doc/html/draft-ietf-oauth-browser-based-apps#name-token-mediating-backend
*/
enableAccessTokenEndpoint?: boolean;
}
export type PagesRouterRequest = IncomingMessage | NextApiRequest;
export type PagesRouterResponse =
| ServerResponse<IncomingMessage>
| NextApiResponse;
export class Auth0Client {
private transactionStore: TransactionStore;
private sessionStore: AbstractSessionStore;
private authClient: AuthClient;
constructor(options: Auth0ClientOptions = {}) {
// Extract and validate required options
const {
domain,
clientId,
clientSecret,
appBaseUrl,
secret,
clientAssertionSigningKey
} = this.validateAndExtractRequiredOptions(options);
const clientAssertionSigningAlg =
options.clientAssertionSigningAlg ||
process.env.AUTH0_CLIENT_ASSERTION_SIGNING_ALG;
const sessionCookieOptions: SessionCookieOptions = {
name: options.session?.cookie?.name ?? "__session",
secure:
options.session?.cookie?.secure ??
process.env.AUTH0_COOKIE_SECURE === "true",
sameSite:
options.session?.cookie?.sameSite ??
(process.env.AUTH0_COOKIE_SAME_SITE as "lax" | "strict" | "none") ??
"lax",
path:
options.session?.cookie?.path ?? process.env.AUTH0_COOKIE_PATH ?? "/",
transient:
options.session?.cookie?.transient ??
process.env.AUTH0_COOKIE_TRANSIENT === "true",
domain: options.session?.cookie?.domain ?? process.env.AUTH0_COOKIE_DOMAIN
};
const transactionCookieOptions: TransactionCookieOptions = {
prefix: options.transactionCookie?.prefix ?? "__txn_",
secure: options.transactionCookie?.secure ?? false,
sameSite: options.transactionCookie?.sameSite ?? "lax",
path: options.transactionCookie?.path ?? "/"
};
if (appBaseUrl) {
const { protocol } = new URL(appBaseUrl);
if (protocol === "https:") {
sessionCookieOptions.secure = true;
transactionCookieOptions.secure = true;
}
}
this.transactionStore = new TransactionStore({
...options.session,
secret,
cookieOptions: transactionCookieOptions
});
this.sessionStore = options.sessionStore
? new StatefulSessionStore({
...options.session,
secret,
store: options.sessionStore,
cookieOptions: sessionCookieOptions
})
: new StatelessSessionStore({
...options.session,
secret,
cookieOptions: sessionCookieOptions
});
this.authClient = new AuthClient({
transactionStore: this.transactionStore,
sessionStore: this.sessionStore,
domain,
clientId,
clientSecret,
clientAssertionSigningKey,
clientAssertionSigningAlg,
authorizationParameters: options.authorizationParameters,
pushedAuthorizationRequests: options.pushedAuthorizationRequests,
appBaseUrl,
secret,
signInReturnToPath: options.signInReturnToPath,
beforeSessionSaved: options.beforeSessionSaved,
onCallback: options.onCallback,
routes: options.routes,
allowInsecureRequests: options.allowInsecureRequests,
httpTimeout: options.httpTimeout,
enableTelemetry: options.enableTelemetry,
enableAccessTokenEndpoint: options.enableAccessTokenEndpoint
});
}
/**
* middleware mounts the SDK routes to run as a middleware function.
*/
middleware(req: NextRequest): Promise<NextResponse> {
return this.authClient.handler.bind(this.authClient)(req);
}
/**
* getSession returns the session data for the current request.
*
* This method can be used in Server Components, Server Actions, and Route Handlers in the **App Router**.
*/
async getSession(): Promise<SessionData | null>;
/**
* getSession returns the session data for the current request.
*
* This method can be used in middleware and `getServerSideProps`, API routes in the **Pages Router**.
*/
async getSession(
req: PagesRouterRequest | NextRequest
): Promise<SessionData | null>;
/**
* getSession returns the session data for the current request.
*/
async getSession(
req?: PagesRouterRequest | NextRequest
): Promise<SessionData | null> {
if (req) {
// middleware usage
if (req instanceof NextRequest) {
return this.sessionStore.get(req.cookies);
}
// pages router usage
return this.sessionStore.get(this.createRequestCookies(req));
}
// app router usage: Server Components, Server Actions, Route Handlers
return this.sessionStore.get(await cookies());
}
/**
* getAccessToken returns the access token.
*
* This method can be used in Server Components, Server Actions, and Route Handlers in the **App Router**.
*
* NOTE: Server Components cannot set cookies. Calling `getAccessToken()` in a Server Component will cause the access token to be refreshed, if it is expired, and the updated token set will not to be persisted.
* It is recommended to call `getAccessToken(req, res)` in the middleware if you need to retrieve the access token in a Server Component to ensure the updated token set is persisted.
*/
/**
* @param options Optional configuration for getting the access token.
* @param options.refresh Force a refresh of the access token.
*/
async getAccessToken(
options?: GetAccessTokenOptions
): Promise<{ token: string; expiresAt: number; scope?: string }>;
/**
* getAccessToken returns the access token.
*
* This method can be used in middleware and `getServerSideProps`, API routes in the **Pages Router**.
*
* @param req The request object.
* @param res The response object.
* @param options Optional configuration for getting the access token.
* @param options.refresh Force a refresh of the access token.
*/
async getAccessToken(
req: PagesRouterRequest | NextRequest,
res: PagesRouterResponse | NextResponse,
options?: GetAccessTokenOptions
): Promise<{ token: string; expiresAt: number; scope?: string }>;
/**
* getAccessToken returns the access token.
*
* NOTE: Server Components cannot set cookies. Calling `getAccessToken()` in a Server Component will cause the access token to be refreshed, if it is expired, and the updated token set will not to be persisted.
* It is recommended to call `getAccessToken(req, res)` in the middleware if you need to retrieve the access token in a Server Component to ensure the updated token set is persisted.
*/
async getAccessToken(
arg1?: PagesRouterRequest | NextRequest | GetAccessTokenOptions,
arg2?: PagesRouterResponse | NextResponse,
arg3?: GetAccessTokenOptions
): Promise<{ token: string; expiresAt: number; scope?: string }> {
const defaultOptions: Required<GetAccessTokenOptions> = {
refresh: false
};
let req: PagesRouterRequest | NextRequest | undefined = undefined;
let res: PagesRouterResponse | NextResponse | undefined = undefined;
let options: GetAccessTokenOptions = {};
// Determine which overload was called based on arguments
if (
arg1 &&
(arg1 instanceof Request || typeof (arg1 as any).headers === "object")
) {
// Case: getAccessToken(req, res, options?)
req = arg1 as PagesRouterRequest | NextRequest;
res = arg2; // arg2 must be Response if arg1 is Request
// Merge provided options (arg3) with defaults
options = { ...defaultOptions, ...(arg3 ?? {}) };
if (!res) {
throw new TypeError(
"getAccessToken(req, res): The 'res' argument is missing. Both 'req' and 'res' must be provided together for Pages Router or middleware usage."
);
}
} else {
// Case: getAccessToken(options?) or getAccessToken()
// arg1 (if present) must be options, arg2 and arg3 must be undefined.
if (arg2 !== undefined || arg3 !== undefined) {
throw new TypeError(
"getAccessToken: Invalid arguments. Valid signatures are getAccessToken(), getAccessToken(options), or getAccessToken(req, res, options)."
);
}
// Merge provided options (arg1) with defaults
options = {
...defaultOptions,
...((arg1 as GetAccessTokenOptions) ?? {})
};
}
const session: SessionData | null = req
? await this.getSession(req)
: await this.getSession();
if (!session) {
throw new AccessTokenError(
AccessTokenErrorCode.MISSING_SESSION,
"The user does not have an active session."
);
}
const [error, tokenSet] = await this.authClient.getTokenSet(
session.tokenSet,
options.refresh
);
if (error) {
throw error;
}
// update the session with the new token set, if necessary
if (
tokenSet.accessToken !== session.tokenSet.accessToken ||
tokenSet.expiresAt !== session.tokenSet.expiresAt ||
tokenSet.refreshToken !== session.tokenSet.refreshToken
) {
await this.saveToSession(
{
...session,
tokenSet
},
req,
res
);
}
return {
token: tokenSet.accessToken,
scope: tokenSet.scope,
expiresAt: tokenSet.expiresAt
};
}
/**
* Retrieves an access token for a connection.
*
* This method can be used in Server Components, Server Actions, and Route Handlers in the **App Router**.
*
* NOTE: Server Components cannot set cookies. Calling `getAccessTokenForConnection()` in a Server Component will cause the access token to be refreshed, if it is expired, and the updated token set will not to be persisted.
* It is recommended to call `getAccessTokenForConnection(req, res)` in the middleware if you need to retrieve the access token in a Server Component to ensure the updated token set is persisted.
*/
async getAccessTokenForConnection(
options: AccessTokenForConnectionOptions
): Promise<{ token: string; expiresAt: number }>;
/**
* Retrieves an access token for a connection.
*
* This method can be used in middleware and `getServerSideProps`, API routes in the **Pages Router**.
*/
async getAccessTokenForConnection(
options: AccessTokenForConnectionOptions,
req: PagesRouterRequest | NextRequest | undefined,
res: PagesRouterResponse | NextResponse | undefined
): Promise<{ token: string; expiresAt: number }>;
/**
* Retrieves an access token for a connection.
*
* This method attempts to obtain an access token for a specified connection.
* It first checks if a session exists, either from the provided request or from cookies.
* If no session is found, it throws a `AccessTokenForConnectionError` indicating
* that the user does not have an active session.
*
* @param {AccessTokenForConnectionOptions} options - Options for retrieving an access token for a connection.
* @param {PagesRouterRequest | NextRequest} [req] - An optional request object from which to extract session information.
* @param {PagesRouterResponse | NextResponse} [res] - An optional response object from which to extract session information.
*
* @throws {AccessTokenForConnectionError} If the user does not have an active session.
* @throws {Error} If there is an error during the token exchange process.
*
* @returns {Promise<{ token: string; expiresAt: number; scope?: string }} An object containing the access token and its expiration time.
*/
async getAccessTokenForConnection(
options: AccessTokenForConnectionOptions,
req?: PagesRouterRequest | NextRequest,
res?: PagesRouterResponse | NextResponse
): Promise<{ token: string; expiresAt: number; scope?: string }> {
const session: SessionData | null = req
? await this.getSession(req)
: await this.getSession();
if (!session) {
throw new AccessTokenForConnectionError(
AccessTokenForConnectionErrorCode.MISSING_SESSION,
"The user does not have an active session."
);
}
// Find the connection token set in the session
const existingTokenSet = session.connectionTokenSets?.find(
(tokenSet) => tokenSet.connection === options.connection
);
const [error, retrievedTokenSet] =
await this.authClient.getConnectionTokenSet(
session.tokenSet,
existingTokenSet,
options
);
if (error !== null) {
throw error;
}
// If we didnt have a corresponding connection token set in the session
// or if the one we have in the session does not match the one we received
// We want to update the store incase we retrieved a token set.
if (
retrievedTokenSet &&
(!existingTokenSet ||
retrievedTokenSet.accessToken !== existingTokenSet.accessToken ||
retrievedTokenSet.expiresAt !== existingTokenSet.expiresAt ||
retrievedTokenSet.scope !== existingTokenSet.scope)
) {
let tokenSets;
// If we already had the connection token set in the session
// we need to update the item in the array
// If not, we need to add it.
if (existingTokenSet) {
tokenSets = session.connectionTokenSets?.map((tokenSet) =>
tokenSet.connection === options.connection
? retrievedTokenSet
: tokenSet
);
} else {
tokenSets = [...(session.connectionTokenSets || []), retrievedTokenSet];
}
await this.saveToSession(
{
...session,
connectionTokenSets: tokenSets
},
req,
res
);
}
return {
token: retrievedTokenSet.accessToken,
scope: retrievedTokenSet.scope,
expiresAt: retrievedTokenSet.expiresAt
};
}
/**
* updateSession updates the session of the currently authenticated user. If the user does not have a session, an error is thrown.
*
* This method can be used in middleware and `getServerSideProps`, API routes, and middleware in the **Pages Router**.
*/
async updateSession(
req: PagesRouterRequest | NextRequest,
res: PagesRouterResponse | NextResponse,
session: SessionData
): Promise<void>;
/**
* updateSession updates the session of the currently authenticated user. If the user does not have a session, an error is thrown.
*
* This method can be used in Server Actions and Route Handlers in the **App Router**.
*/
async updateSession(session: SessionData): Promise<void>;
/**
* updateSession updates the session of the currently authenticated user. If the user does not have a session, an error is thrown.
*/
async updateSession(
reqOrSession: PagesRouterRequest | NextRequest | SessionData,
res?: PagesRouterResponse | NextResponse,
sessionData?: SessionData
) {
if (!res) {
// app router: Server Actions, Route Handlers
const existingSession = await this.getSession();
if (!existingSession) {
throw new Error("The user is not authenticated.");
}
const updatedSession = reqOrSession as SessionData;
if (!updatedSession) {
throw new Error("The session data is missing.");
}
await this.sessionStore.set(await cookies(), await cookies(), {
...updatedSession,
internal: {
...existingSession.internal
}
});
} else {
const req = reqOrSession as PagesRouterRequest | NextRequest;
if (!sessionData) {
throw new Error("The session data is missing.");
}
if (req instanceof NextRequest && res instanceof NextResponse) {
// middleware usage
const existingSession = await this.getSession(req);
if (!existingSession) {
throw new Error("The user is not authenticated.");
}
await this.sessionStore.set(req.cookies, res.cookies, {
...sessionData,
internal: {
...existingSession.internal
}
});
} else {
// pages router usage
const existingSession = await this.getSession(
req as PagesRouterRequest
);
if (!existingSession) {
throw new Error("The user is not authenticated.");
}
const resHeaders = new Headers();
const resCookies = new ResponseCookies(resHeaders);
const updatedSession = sessionData as SessionData;
const reqCookies = this.createRequestCookies(req as PagesRouterRequest);
const pagesRouterRes = res as PagesRouterResponse;
await this.sessionStore.set(reqCookies, resCookies, {
...updatedSession,
internal: {
...existingSession.internal
}
});
for (const [key, value] of resHeaders.entries()) {
pagesRouterRes.setHeader(key, value);
}
}
}
}
private createRequestCookies(req: PagesRouterRequest) {
const headers = new Headers();
for (const key in req.headers) {
if (Array.isArray(req.headers[key])) {
for (const value of req.headers[key]) {
headers.append(key, value);
}
} else {
headers.append(key, req.headers[key] ?? "");
}
}
return new RequestCookies(headers);
}
async startInteractiveLogin(
options: StartInteractiveLoginOptions
): Promise<NextResponse> {
return this.authClient.startInteractiveLogin(options);
}
private async saveToSession(
data: SessionData,
req?: PagesRouterRequest | NextRequest,
res?: PagesRouterResponse | NextResponse
) {
if (req && res) {
if (req instanceof NextRequest && res instanceof NextResponse) {
// middleware usage
await this.sessionStore.set(req.cookies, res.cookies, data);
} else {
// pages router usage
const resHeaders = new Headers();
const resCookies = new ResponseCookies(resHeaders);
const pagesRouterRes = res as PagesRouterResponse;
await this.sessionStore.set(
this.createRequestCookies(req as PagesRouterRequest),
resCookies,
data
);
for (const [key, value] of resHeaders.entries()) {
pagesRouterRes.setHeader(key, value);
}
}
} else {
// app router usage: Server Components, Server Actions, Route Handlers
try {
await this.sessionStore.set(await cookies(), await cookies(), data);
} catch (e) {
if (process.env.NODE_ENV === "development") {
console.warn(
"Failed to persist the updated token set. `getAccessToken()` was likely called from a Server Component which cannot set cookies."
);
}
}
}
}
/**
* Validates and extracts required configuration options.
* @param options The client options
* @returns The validated required options
* @throws ConfigurationError if any required option is missing
*/
private validateAndExtractRequiredOptions(options: Auth0ClientOptions) {
// Base required options that are always needed
const requiredOptions = {
domain: options.domain ?? process.env.AUTH0_DOMAIN,
clientId: options.clientId ?? process.env.AUTH0_CLIENT_ID,
appBaseUrl: options.appBaseUrl ?? process.env.APP_BASE_URL,
secret: options.secret ?? process.env.AUTH0_SECRET
};
// Check client authentication options - either clientSecret OR clientAssertionSigningKey must be provided
const clientSecret =
options.clientSecret ?? process.env.AUTH0_CLIENT_SECRET;
const clientAssertionSigningKey =
options.clientAssertionSigningKey ??
process.env.AUTH0_CLIENT_ASSERTION_SIGNING_KEY;
const hasClientAuthentication = !!(
clientSecret || clientAssertionSigningKey
);
const missing = Object.entries(requiredOptions)
.filter(([, value]) => !value)
.map(([key]) => key);
// Add client authentication error if neither option is provided
if (!hasClientAuthentication) {
missing.push("clientAuthentication");
}
if (missing.length) {
// Map of option keys to their exact environment variable names
const envVarNames: Record<string, string> = {
domain: "AUTH0_DOMAIN",
clientId: "AUTH0_CLIENT_ID",
appBaseUrl: "APP_BASE_URL",
secret: "AUTH0_SECRET"
};
// Standard intro message explaining the issue
let errorMessage =
"WARNING: Not all required options were provided when creating an instance of Auth0Client. Ensure to provide all missing options, either by passing it to the Auth0Client constructor, or by setting the corresponding environment variable.\n";
// Add specific details for each missing option
missing.forEach((key) => {
if (key === "clientAuthentication") {
errorMessage += `Missing: clientAuthentication: Set either AUTH0_CLIENT_SECRET env var or AUTH0_CLIENT_ASSERTION_SIGNING_KEY env var, or pass clientSecret or clientAssertionSigningKey in options\n`;
} else if (envVarNames[key]) {
errorMessage += `Missing: ${key}: Set ${envVarNames[key]} env var or pass ${key} in options\n`;
} else {
errorMessage += `Missing: ${key}\n`;
}
});
console.error(errorMessage.trim());
}
// Prepare the result object with all validated options
const result = {
...requiredOptions,
clientSecret,
clientAssertionSigningKey
};
// Type-safe assignment after validation
return result as {
[K in keyof typeof result]: NonNullable<(typeof result)[K]>;
};
}
}
export type GetAccessTokenOptions = {
refresh?: boolean;
};