@@ -13,7 +13,7 @@ const DEFAULT_ENCRYPTION_ENABLED = false
13
13
14
14
const DEFAULT_MAX_AGE = 30 * 24 * 60 * 60 // 30 days
15
15
16
- const encode = async ( {
16
+ async function encode ( {
17
17
token = { } ,
18
18
maxAge = DEFAULT_MAX_AGE ,
19
19
secret,
@@ -28,9 +28,9 @@ const encode = async ({
28
28
zip : 'DEF'
29
29
} ,
30
30
encryption = DEFAULT_ENCRYPTION_ENABLED
31
- } = { } ) => {
31
+ } = { } ) {
32
32
// Signing Key
33
- const _signingKey = ( signingKey )
33
+ const _signingKey = signingKey
34
34
? jose . JWK . asKey ( JSON . parse ( signingKey ) )
35
35
: getDerivedSigningKey ( secret )
36
36
@@ -39,18 +39,17 @@ const encode = async ({
39
39
40
40
if ( encryption ) {
41
41
// Encryption Key
42
- const _encryptionKey = ( encryptionKey )
42
+ const _encryptionKey = encryptionKey
43
43
? jose . JWK . asKey ( JSON . parse ( encryptionKey ) )
44
44
: getDerivedEncryptionKey ( secret )
45
45
46
46
// Encrypt token
47
47
return jose . JWE . encrypt ( signedToken , _encryptionKey , encryptionOptions )
48
- } else {
49
- return signedToken
50
48
}
49
+ return signedToken
51
50
}
52
51
53
- const decode = async ( {
52
+ async function decode ( {
54
53
secret,
55
54
token,
56
55
maxAge = DEFAULT_MAX_AGE ,
@@ -66,14 +65,14 @@ const decode = async ({
66
65
algorithms : [ DEFAULT_ENCRYPTION_ALGORITHM ]
67
66
} ,
68
67
encryption = DEFAULT_ENCRYPTION_ENABLED
69
- } = { } ) => {
68
+ } = { } ) {
70
69
if ( ! token ) return null
71
70
72
71
let tokenToVerify = token
73
72
74
73
if ( encryption ) {
75
74
// Encryption Key
76
- const _encryptionKey = ( decryptionKey )
75
+ const _encryptionKey = decryptionKey
77
76
? jose . JWK . asKey ( JSON . parse ( decryptionKey ) )
78
77
: getDerivedEncryptionKey ( secret )
79
78
@@ -83,23 +82,32 @@ const decode = async ({
83
82
}
84
83
85
84
// Signing Key
86
- const _signingKey = ( verificationKey )
85
+ const _signingKey = verificationKey
87
86
? jose . JWK . asKey ( JSON . parse ( verificationKey ) )
88
87
: getDerivedSigningKey ( secret )
89
88
90
89
// Verify token
91
90
return jose . JWT . verify ( tokenToVerify , _signingKey , verificationOptions )
92
91
}
93
92
94
- const getToken = async ( args ) => {
93
+ /**
94
+ * Server-side method to retrieve the JWT from `req`.
95
+ * @param {{
96
+ * req: NextApiRequest
97
+ * secureCookie?: boolean
98
+ * cookieName?: string
99
+ * raw?: boolean
100
+ * }} params
101
+ */
102
+ async function getToken ( params ) {
95
103
const {
96
104
req,
97
105
// Use secure prefix for cookie name, unless URL is NEXTAUTH_URL is http://
98
106
// or not set (e.g. development or test instance) case use unprefixed name
99
107
secureCookie = ! ( ! process . env . NEXTAUTH_URL || process . env . NEXTAUTH_URL . startsWith ( 'http://' ) ) ,
100
108
cookieName = ( secureCookie ) ? '__Secure-next-auth.session-token' : 'next-auth.session-token' ,
101
109
raw = false
102
- } = args
110
+ } = params
103
111
if ( ! req ) throw new Error ( 'Must pass `req` to JWT getToken()' )
104
112
105
113
// Try to get token from cookie
@@ -108,7 +116,7 @@ const getToken = async (args) => {
108
116
// If cookie not found in cookie look for bearer token in authorization header.
109
117
// This allows clients that pass through tokens in headers rather than as
110
118
// cookies to use this helper function.
111
- if ( ! token && req . headers . authorization && req . headers . authorization . split ( ' ' ) [ 0 ] === 'Bearer' ) {
119
+ if ( ! token && req . headers . authorization ? .split ( ' ' ) [ 0 ] === 'Bearer' ) {
112
120
const urlEncodedToken = req . headers . authorization . split ( ' ' ) [ 1 ]
113
121
token = decodeURIComponent ( urlEncodedToken )
114
122
}
@@ -118,8 +126,8 @@ const getToken = async (args) => {
118
126
}
119
127
120
128
try {
121
- return await decode ( { token, ...args } )
122
- } catch ( error ) {
129
+ return decode ( { token, ...params } )
130
+ } catch {
123
131
return null
124
132
}
125
133
}
@@ -128,7 +136,7 @@ const getToken = async (args) => {
128
136
let DERIVED_SIGNING_KEY_WARNING = false
129
137
let DERIVED_ENCRYPTION_KEY_WARNING = false
130
138
131
- const getDerivedSigningKey = ( secret ) => {
139
+ function getDerivedSigningKey ( secret ) {
132
140
if ( ! DERIVED_SIGNING_KEY_WARNING ) {
133
141
logger . warn ( 'JWT_AUTO_GENERATED_SIGNING_KEY' )
134
142
DERIVED_SIGNING_KEY_WARNING = true
@@ -139,7 +147,7 @@ const getDerivedSigningKey = (secret) => {
139
147
return key
140
148
}
141
149
142
- const getDerivedEncryptionKey = ( secret ) => {
150
+ function getDerivedEncryptionKey ( secret ) {
143
151
if ( ! DERIVED_ENCRYPTION_KEY_WARNING ) {
144
152
logger . warn ( 'JWT_AUTO_GENERATED_ENCRYPTION_KEY' )
145
153
DERIVED_ENCRYPTION_KEY_WARNING = true
0 commit comments