@@ -210,6 +210,51 @@ t.test('fastify-oauth2', t => {
210
210
makeRequests ( t , fastify )
211
211
} )
212
212
213
+ t . test ( 'callbackUri as function' , t => {
214
+ const fastify = createFastify ( { logger : { level : 'silent' } } )
215
+
216
+ fastify . register ( fastifyOauth2 , {
217
+ name : 'githubOAuth2' ,
218
+ credentials : {
219
+ client : {
220
+ id : 'my-client-id' ,
221
+ secret : 'my-secret'
222
+ } ,
223
+ auth : fastifyOauth2 . GITHUB_CONFIGURATION
224
+ } ,
225
+ startRedirectPath : '/login/github' ,
226
+ callbackUri : req => `${ req . protocol } ://localhost:3000/callback` ,
227
+ scope : [ 'notifications' ]
228
+ } )
229
+
230
+ fastify . get ( '/' , function ( request , reply ) {
231
+ if ( this . githubOAuth2 !== this . oauth2GithubOAuth2 ) {
232
+ throw new Error ( 'Expected oauth2GithubOAuth2 to match githubOAuth2' )
233
+ }
234
+ this . githubOAuth2 . getAccessTokenFromAuthorizationCodeFlow ( request , ( err , result ) => {
235
+ if ( err ) throw err
236
+
237
+ // attempts to refresh the token
238
+ this . githubOAuth2 . getNewAccessTokenUsingRefreshToken ( result . token , undefined , ( err , result ) => {
239
+ if ( err ) throw err
240
+
241
+ const newToken = result
242
+
243
+ reply . send ( {
244
+ access_token : newToken . token . access_token ,
245
+ refresh_token : newToken . token . refresh_token ,
246
+ expires_in : newToken . token . expires_in ,
247
+ token_type : newToken . token . token_type
248
+ } )
249
+ } )
250
+ } )
251
+ } )
252
+
253
+ t . teardown ( fastify . close . bind ( fastify ) )
254
+
255
+ makeRequests ( t , fastify )
256
+ } )
257
+
213
258
t . test ( 'promise' , t => {
214
259
const fastify = createFastify ( { logger : { level : 'silent' } } )
215
260
@@ -1399,7 +1444,7 @@ t.test('options.credentials should be an object', t => {
1399
1444
} )
1400
1445
} )
1401
1446
1402
- t . test ( 'options.callbackUri should be an object ' , t => {
1447
+ t . test ( 'options.callbackUri should be a string or a function ' , t => {
1403
1448
t . plan ( 1 )
1404
1449
1405
1450
const fastify = createFastify ( { logger : { level : 'silent' } } )
@@ -1415,7 +1460,7 @@ t.test('options.callbackUri should be an object', t => {
1415
1460
}
1416
1461
} )
1417
1462
. ready ( err => {
1418
- t . strictSame ( err . message , 'options.callbackUri should be a string' )
1463
+ t . strictSame ( err . message , 'options.callbackUri should be a string or a function ' )
1419
1464
} )
1420
1465
} )
1421
1466
0 commit comments