|
1 | | -import type { NextAuthConfig } from 'next-auth' |
2 | 1 | import fs from 'node:fs' |
3 | | -import { createSessionCallback } from './callbacks.js' |
4 | 2 | import { importPkcs8 } from './key.js' |
5 | 3 | import type { CreateSigningFnsParameters } from './token.js' |
6 | 4 | import type { HandlerOptions, NextAuthAllAccessOptions } from './types.js' |
7 | 5 | import { isJsonWebKeySet } from './types.js' |
8 | 6 | import { getIssuer, getOrigin, sanitizeKey } from './utils.js' |
9 | 7 |
|
10 | | -/** |
11 | | - * Wraps NextAuth with AllAccess code, which adds AllAccess endpoints and inserts |
12 | | - * access tokens into the session object. |
13 | | - */ |
14 | | -export function createInitializer(handler: any) { |
15 | | - return (options: NextAuthAllAccessOptions) => { |
16 | | - const _jwks = options.jwks |
17 | | - const jwksPath = options.jwksPath ?? process.env['ALLACCESS_JWKS_PATH'] |
18 | | - const privateKey = options.privateKey ?? process.env['ALLACCESS_PRIVATE_KEY'] |
| 8 | +export function createInitializerOptions(options: NextAuthAllAccessOptions) { |
| 9 | + const _jwks = options.jwks |
| 10 | + const jwksPath = options.jwksPath ?? process.env['ALLACCESS_JWKS_PATH'] |
| 11 | + const privateKey = options.privateKey ?? process.env['ALLACCESS_PRIVATE_KEY'] |
19 | 12 |
|
20 | | - if ((!jwksPath && !_jwks) || !privateKey) { |
21 | | - throw new Error('JWKS file path and private key are required') |
22 | | - } |
23 | | - |
24 | | - let jwks |
25 | | - |
26 | | - if (jwksPath) { |
27 | | - jwks = JSON.parse(fs.readFileSync(jwksPath, 'utf-8')) as unknown |
28 | | - } else { |
29 | | - jwks = _jwks |
30 | | - } |
31 | | - |
32 | | - if (!isJsonWebKeySet(jwks)) { |
33 | | - throw new Error('JWKS file is invalid') |
34 | | - } |
| 13 | + if ((!jwksPath && !_jwks) || !privateKey) { |
| 14 | + throw new Error('JWKS file path and private key are required') |
| 15 | + } |
35 | 16 |
|
36 | | - const kid = jwks.keys[0]?.kid |
| 17 | + let jwks |
37 | 18 |
|
38 | | - if (!kid) { |
39 | | - throw new Error('JWKS file is invalid') |
40 | | - } |
| 19 | + if (jwksPath) { |
| 20 | + jwks = JSON.parse(fs.readFileSync(jwksPath, 'utf-8')) as unknown |
| 21 | + } else { |
| 22 | + jwks = _jwks |
| 23 | + } |
41 | 24 |
|
42 | | - const issuer = getIssuer(options.issuer) |
| 25 | + if (!isJsonWebKeySet(jwks)) { |
| 26 | + throw new Error('JWKS file is invalid') |
| 27 | + } |
43 | 28 |
|
44 | | - const handlerOptions: HandlerOptions = { |
45 | | - issuer, |
46 | | - origin: getOrigin(options.origin), |
47 | | - jwks, |
48 | | - } |
| 29 | + const kid = jwks.keys[0]?.kid |
49 | 30 |
|
50 | | - const signingOptions: CreateSigningFnsParameters = { |
51 | | - clients: options.clients, |
52 | | - privateKey: importPkcs8(sanitizeKey(privateKey)), |
53 | | - issuer, |
54 | | - kid, |
55 | | - } |
| 31 | + if (!kid) { |
| 32 | + throw new Error('JWKS file is invalid') |
| 33 | + } |
56 | 34 |
|
57 | | - return (createNextAuth: (opt: NextAuthConfig) => any, nextAuthOptions: NextAuthConfig) => { |
58 | | - const sessionCallback = createSessionCallback(signingOptions, nextAuthOptions) |
| 35 | + const issuer = getIssuer(options.issuer) |
59 | 36 |
|
60 | | - nextAuthOptions.callbacks = { |
61 | | - ...nextAuthOptions.callbacks, |
62 | | - session: sessionCallback, |
63 | | - } |
| 37 | + const handlerOptions: HandlerOptions = { |
| 38 | + issuer, |
| 39 | + origin: getOrigin(options.origin), |
| 40 | + jwks, |
| 41 | + } |
64 | 42 |
|
65 | | - return handler(handlerOptions, createNextAuth(nextAuthOptions)) |
66 | | - } |
| 43 | + const signingOptions: CreateSigningFnsParameters = { |
| 44 | + clients: options.clients, |
| 45 | + privateKey: importPkcs8(sanitizeKey(privateKey)), |
| 46 | + issuer, |
| 47 | + kid, |
67 | 48 | } |
| 49 | + |
| 50 | + return { handlerOptions, signingOptions } |
68 | 51 | } |
0 commit comments