Skip to content

Commit 2d87102

Browse files
committed
better types
1 parent 5f8ecb7 commit 2d87102

8 files changed

Lines changed: 153 additions & 137 deletions

File tree

package.json

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -23,20 +23,20 @@
2323
"exports": {
2424
".": "./dist/index.js",
2525
"./react": "./dist/react/index.js",
26-
"./app": "./dist/app/index.js",
27-
"./pages": "./dist/pages/index.js"
26+
"./v5": "./dist/v5/index.js",
27+
"./v4": "./dist/v4/index.js"
2828
},
2929
"types": "./dist",
3030
"typesVersions": {
3131
"*": {
3232
"react": [
3333
"dist/react/index.d.ts"
3434
],
35-
"app": [
36-
"dist/app/index.d.ts"
35+
"v5": [
36+
"dist/v5/index.d.ts"
3737
],
38-
"pages": [
39-
"dist/pages/index.d.ts"
38+
"v4": [
39+
"dist/v4/index.d.ts"
4040
]
4141
}
4242
},
@@ -61,8 +61,8 @@
6161
"dist/types.js",
6262
"dist/utils.d.ts",
6363
"dist/utils.js",
64-
"dist/app",
65-
"dist/pages"
64+
"dist/v5",
65+
"dist/v4"
6666
],
6767
"scripts": {
6868
"build": "del-cli dist && tsc -p tsconfig.build.json",

src/app/index.ts

Lines changed: 0 additions & 39 deletions
This file was deleted.

src/callbacks.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ import { createSigningFns } from './token.js'
99
*/
1010
export function createSessionCallback(
1111
signingOptions: CreateSigningFnsParameters,
12-
nextAuthOptions: NextAuthConfig,
12+
nextAuthOptions: Pick<NextAuthConfig, 'callbacks'>,
1313
): CallbacksOptions['session'] {
1414
const signAccessTokens = createSigningFns(signingOptions)
1515

src/index.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
export { createNextAuthAllAccess as default } from './pages/index.js'
1+
export { createNextAuthAllAccess as default } from './v4/index.js'

src/next-auth-all-access.ts

Lines changed: 33 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -1,68 +1,51 @@
1-
import type { NextAuthConfig } from 'next-auth'
21
import fs from 'node:fs'
3-
import { createSessionCallback } from './callbacks.js'
42
import { importPkcs8 } from './key.js'
53
import type { CreateSigningFnsParameters } from './token.js'
64
import type { HandlerOptions, NextAuthAllAccessOptions } from './types.js'
75
import { isJsonWebKeySet } from './types.js'
86
import { getIssuer, getOrigin, sanitizeKey } from './utils.js'
97

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']
1912

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+
}
3516

36-
const kid = jwks.keys[0]?.kid
17+
let jwks
3718

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+
}
4124

42-
const issuer = getIssuer(options.issuer)
25+
if (!isJsonWebKeySet(jwks)) {
26+
throw new Error('JWKS file is invalid')
27+
}
4328

44-
const handlerOptions: HandlerOptions = {
45-
issuer,
46-
origin: getOrigin(options.origin),
47-
jwks,
48-
}
29+
const kid = jwks.keys[0]?.kid
4930

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+
}
5634

57-
return (createNextAuth: (opt: NextAuthConfig) => any, nextAuthOptions: NextAuthConfig) => {
58-
const sessionCallback = createSessionCallback(signingOptions, nextAuthOptions)
35+
const issuer = getIssuer(options.issuer)
5936

60-
nextAuthOptions.callbacks = {
61-
...nextAuthOptions.callbacks,
62-
session: sessionCallback,
63-
}
37+
const handlerOptions: HandlerOptions = {
38+
issuer,
39+
origin: getOrigin(options.origin),
40+
jwks,
41+
}
6442

65-
return handler(handlerOptions, createNextAuth(nextAuthOptions))
66-
}
43+
const signingOptions: CreateSigningFnsParameters = {
44+
clients: options.clients,
45+
privateKey: importPkcs8(sanitizeKey(privateKey)),
46+
issuer,
47+
kid,
6748
}
49+
50+
return { handlerOptions, signingOptions }
6851
}

src/pages/index.ts

Lines changed: 0 additions & 38 deletions
This file was deleted.

src/v4/index.ts

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
import type { NextApiHandler, NextApiRequest, NextApiResponse } from 'next'
2+
import { NextAuthConfig } from 'next-auth'
3+
import { createSessionCallback } from '../callbacks.js'
4+
import jwksHandler from '../handlers/jwks.js'
5+
import openidConfigurationHandler from '../handlers/openid-configuration.js'
6+
import { createInitializerOptions } from '../next-auth-all-access.js'
7+
import type { HandlerOptions, NextAuthAllAccessOptions } from '../types.js'
8+
9+
/**
10+
* Wrap NextAuth returning a v4-style API handler
11+
*/
12+
function wrapNextAuth(options: HandlerOptions, nextAuth: NextApiHandler): NextApiHandler {
13+
return async (req: NextApiRequest, res: NextApiResponse) => {
14+
let {
15+
query: { nextauth: route },
16+
} = req
17+
18+
route = (Array.isArray(route) ? route : [route]).join('/')
19+
20+
switch (route) {
21+
case 'all-access/jwks.json': {
22+
const response = jwksHandler(options)
23+
res.send(response)
24+
return
25+
}
26+
27+
case 'all-access/.well-known/openid-configuration': {
28+
const response = openidConfigurationHandler(options)
29+
res.send(response)
30+
return
31+
}
32+
33+
default: {
34+
await nextAuth(req, res)
35+
}
36+
}
37+
}
38+
}
39+
40+
export const createNextAuthAllAccess = (options: NextAuthAllAccessOptions) => {
41+
const { handlerOptions, signingOptions } = createInitializerOptions(options)
42+
43+
return (
44+
createNextAuth: (options: Pick<NextAuthConfig, 'callbacks'>) => any,
45+
nextAuthOptions: Pick<NextAuthConfig, 'callbacks'>,
46+
): NextApiHandler => {
47+
const sessionCallback = createSessionCallback(signingOptions, nextAuthOptions)
48+
49+
nextAuthOptions.callbacks = {
50+
...nextAuthOptions.callbacks,
51+
session: sessionCallback,
52+
}
53+
54+
return wrapNextAuth(handlerOptions, createNextAuth(nextAuthOptions))
55+
}
56+
}

src/v5/index.ts

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
import type NextAuth from 'next-auth'
2+
import type { NextAuthConfig, NextAuthResult } from 'next-auth'
3+
import type { NextRequest } from 'next/server'
4+
import { createSessionCallback } from '../callbacks.js'
5+
import jwksHandler from '../handlers/jwks.js'
6+
import openidConfigurationHandler from '../handlers/openid-configuration.js'
7+
import { createInitializerOptions } from '../next-auth-all-access.js'
8+
import type { HandlerOptions, NextAuthAllAccessOptions } from '../types.js'
9+
10+
/**
11+
* Wrap NextAuth returning a v5-style `NextAuthResult`
12+
*/
13+
function wrapNextAuth(options: HandlerOptions, nextAuth: NextAuthResult) {
14+
const { GET } = nextAuth.handlers
15+
16+
const getWrapper = async (req: NextRequest) => {
17+
const { pathname } = req.nextUrl
18+
19+
if (pathname.endsWith('all-access/jwks.json')) {
20+
const response = jwksHandler(options)
21+
return Response.json(response)
22+
}
23+
24+
if (pathname.endsWith('all-access/.well-known/openid-configuration')) {
25+
const response = openidConfigurationHandler(options)
26+
return Response.json(response)
27+
}
28+
29+
return GET(req)
30+
}
31+
32+
return {
33+
...nextAuth,
34+
handlers: {
35+
...nextAuth.handlers,
36+
GET: getWrapper,
37+
},
38+
}
39+
}
40+
41+
export const createNextAuthAllAccess = (options: NextAuthAllAccessOptions) => {
42+
const { handlerOptions, signingOptions } = createInitializerOptions(options)
43+
44+
return (createNextAuth: typeof NextAuth, nextAuthOptions: NextAuthConfig): NextAuthResult => {
45+
const sessionCallback = createSessionCallback(signingOptions, nextAuthOptions)
46+
47+
nextAuthOptions.callbacks = {
48+
...nextAuthOptions.callbacks,
49+
session: sessionCallback,
50+
}
51+
52+
return wrapNextAuth(handlerOptions, createNextAuth(nextAuthOptions))
53+
}
54+
}

0 commit comments

Comments
 (0)