@@ -11,8 +11,9 @@ const kGenerateCallbackUriParams = Symbol.for('fastify-oauth2.generate-callback-
11
11
12
12
const { promisify, callbackify } = require ( 'node:util' )
13
13
14
+ const DEFAULT_VERIFIER_COOKIE_NAME = 'oauth2-code-verifier'
15
+ const DEFAULT_REDIRECT_STATE_COOKIE_NAME = 'oauth2-redirect-state'
14
16
const USER_AGENT = 'fastify-oauth2'
15
- const VERIFIER_COOKIE_NAME = 'oauth2-code-verifier'
16
17
const PKCE_METHODS = [ 'S256' , 'plain' ]
17
18
18
19
const random = ( bytes = 32 ) => randomBytes ( bytes ) . toString ( 'base64url' )
@@ -25,7 +26,10 @@ function defaultGenerateStateFunction (request, callback) {
25
26
26
27
function defaultCheckStateFunction ( request , callback ) {
27
28
const state = request . query . state
28
- const stateCookie = request . cookies [ 'oauth2-redirect-state' ]
29
+ const stateCookie =
30
+ request . cookies [
31
+ this . redirectStateCookieName
32
+ ]
29
33
if ( stateCookie && state === stateCookie ) {
30
34
callback ( )
31
35
return
@@ -98,6 +102,20 @@ function fastifyOauth2 (fastify, options, next) {
98
102
if ( ! options . discovery && ! options . credentials . auth ) {
99
103
return next ( new Error ( 'options.discovery.issuer or credentials.auth have to be given' ) )
100
104
}
105
+ if (
106
+ options . verifierCookieName &&
107
+ typeof options . verifierCookieName !== 'string'
108
+ ) {
109
+ return next ( new Error ( 'options.verifierCookieName should be a string' ) )
110
+ }
111
+ if (
112
+ options . redirectStateCookieName &&
113
+ typeof options . redirectStateCookieName !== 'string'
114
+ ) {
115
+ return next (
116
+ new Error ( 'options.redirectStateCookieName should be a string' )
117
+ )
118
+ }
101
119
if ( ! fastify . hasReplyDecorator ( 'cookie' ) ) {
102
120
fastify . register ( require ( '@fastify/cookie' ) )
103
121
}
@@ -116,10 +134,16 @@ function fastifyOauth2 (fastify, options, next) {
116
134
tokenRequestParams = { } ,
117
135
scope,
118
136
generateStateFunction = defaultGenerateStateFunction ,
119
- checkStateFunction = defaultCheckStateFunction ,
137
+ checkStateFunction = defaultCheckStateFunction . bind ( {
138
+ redirectStateCookieName :
139
+ configured . redirectStateCookieName ||
140
+ DEFAULT_REDIRECT_STATE_COOKIE_NAME
141
+ } ) ,
120
142
startRedirectPath,
121
143
tags = [ ] ,
122
- schema = { tags }
144
+ schema = { tags } ,
145
+ redirectStateCookieName = DEFAULT_REDIRECT_STATE_COOKIE_NAME ,
146
+ verifierCookieName = DEFAULT_VERIFIER_COOKIE_NAME
123
147
} = configured
124
148
125
149
if ( userAgent ) {
@@ -153,7 +177,7 @@ function fastifyOauth2 (fastify, options, next) {
153
177
return
154
178
}
155
179
156
- reply . setCookie ( 'oauth2-redirect-state' , state , cookieOpts )
180
+ reply . setCookie ( redirectStateCookieName , state , cookieOpts )
157
181
158
182
// when PKCE extension is used
159
183
let pkceParams = { }
@@ -164,7 +188,7 @@ function fastifyOauth2 (fastify, options, next) {
164
188
code_challenge : challenge ,
165
189
code_challenge_method : configured . pkce
166
190
}
167
- reply . setCookie ( VERIFIER_COOKIE_NAME , verifier , cookieOpts )
191
+ reply . setCookie ( verifierCookieName , verifier , cookieOpts )
168
192
}
169
193
170
194
const urlOptions = Object . assign ( { } , generateCallbackUriParams ( callbackUriParams , request , scope , state ) , {
@@ -227,7 +251,7 @@ function fastifyOauth2 (fastify, options, next) {
227
251
228
252
function getAccessTokenFromAuthorizationCodeFlowCallbacked ( request , reply , callback ) {
229
253
const code = request . query . code
230
- const pkceParams = configured . pkce ? { code_verifier : request . cookies [ 'oauth2-code-verifier' ] } : { }
254
+ const pkceParams = configured . pkce ? { code_verifier : request . cookies [ verifierCookieName ] } : { }
231
255
232
256
const _callback = typeof reply === 'function' ? reply : callback
233
257
@@ -299,7 +323,7 @@ function fastifyOauth2 (fastify, options, next) {
299
323
}
300
324
301
325
function clearCodeVerifierCookie ( reply ) {
302
- reply . clearCookie ( VERIFIER_COOKIE_NAME , cookieOpts )
326
+ reply . clearCookie ( verifierCookieName , cookieOpts )
303
327
}
304
328
305
329
const pUserInfo = promisify ( userInfoCallbacked )
0 commit comments