@@ -63,7 +63,10 @@ export class OAuth2Server {
63
63
*/
64
64
constructor ( { serverOptions = { } , model, routes, debug } = { } ) {
65
65
check ( serverOptions , OptionsSchema . serverOptions )
66
-
66
+ if ( debug ) {
67
+ console . debug ( '[OAuth2Server]: create new instance' )
68
+ console . debug ( '[OAuth2Server]: serveroptions' , serverOptions )
69
+ }
67
70
this . instanceId = Random . id ( )
68
71
this . config = {
69
72
serverOptions : Object . assign ( { } , OAuth2ServerDefaults . serverOptions , serverOptions ) ,
@@ -120,9 +123,8 @@ export class OAuth2Server {
120
123
* @param secret
121
124
* @returns { }
122
125
*/
123
- registerClient ( { title, homepage, description, privacyLink, redirectUris, grants, clientId, secret } ) {
124
- const self = this
125
- return Promise . await ( self . model . createClient ( {
126
+ async registerClient ( { title, homepage, description, privacyLink, redirectUris, grants, clientId, secret } ) {
127
+ return this . model . createClient ( {
126
128
title,
127
129
homepage,
128
130
description,
@@ -131,45 +133,44 @@ export class OAuth2Server {
131
133
grants,
132
134
clientId,
133
135
secret
134
- } ) )
136
+ } )
135
137
}
136
138
137
139
authorizeHandler ( options ) {
138
140
const self = this
139
- return function ( req , res , next ) {
141
+ return async function ( req , res , next ) {
140
142
const request = new Request ( req )
141
143
const response = new Response ( res )
142
- return self . oauth . authorize ( request , response , options )
143
- . then ( function ( code ) {
144
- res . locals . oauth = { code : code }
145
- next ( )
146
- } )
147
- . catch ( function ( err ) {
148
- // handle error condition
149
- res . writeHead ( 500 )
150
- res . end ( err )
151
- } )
144
+
145
+ try {
146
+ const code = await self . oauth . authorize ( request , response , options )
147
+ res . locals . oauth = { code : code }
148
+ next ( )
149
+ } catch ( err ) {
150
+ res . writeHead ( 500 )
151
+ res . end ( err )
152
+ }
152
153
}
153
154
}
154
155
155
156
authenticateHandler ( options ) {
156
157
const self = this
157
- return function ( req , res , next ) {
158
+ return async function ( req , res , next ) {
158
159
const request = new Request ( req )
159
160
const response = new Response ( res )
160
- return self . oauth . authenticate ( request , response , options )
161
- . then ( function ( token ) {
162
- req . data = Object . assign ( { } , req . data , token )
163
- next ( )
164
- } )
165
- . catch ( function ( err ) {
166
- return errorHandler ( res , {
167
- status : err . status ,
168
- error : err . name ,
169
- description : err . message ,
170
- debug : self . debug
171
- } )
161
+
162
+ try {
163
+ const token = await self . oauth . authenticate ( request , response , options )
164
+ req . data = Object . assign ( { } , req . data , token )
165
+ next ( )
166
+ } catch ( err ) {
167
+ return errorHandler ( res , {
168
+ status : err . status ,
169
+ error : err . name ,
170
+ description : err . message ,
171
+ debug : self . debug
172
172
} )
173
+ }
173
174
}
174
175
}
175
176
@@ -214,10 +215,11 @@ const initRoutes = (self, { accessTokenUrl = '/oauth/token', authorizeUrl = '/oa
214
215
return true
215
216
}
216
217
217
- const getValidatedClient = ( req , res ) => {
218
+ const getValidatedClient = async ( req , res ) => {
218
219
const clientId = req . method . toLowerCase ( ) === 'get' ? req . query . client_id : req . body . client_id
219
220
const secret = req . method . toLowerCase ( ) === 'get' ? req . query . client_secret : req . body . client_secret
220
- const client = Promise . await ( self . model . getClient ( clientId , secret ) )
221
+ const client = await self . model . getClient ( clientId , secret )
222
+
221
223
if ( ! client ) {
222
224
// unauthorized_client - The client is not authorized to request an authorization code using this method.
223
225
return errorHandler ( res , {
@@ -228,6 +230,7 @@ const initRoutes = (self, { accessTokenUrl = '/oauth/token', authorizeUrl = '/oa
228
230
debug : self . debug
229
231
} )
230
232
}
233
+
231
234
return client
232
235
}
233
236
@@ -279,7 +282,7 @@ const initRoutes = (self, { accessTokenUrl = '/oauth/token', authorizeUrl = '/oa
279
282
// If there is something wrong with the syntax of the request, such as the redirect_uri or client_id is invalid,
280
283
// then it’s important not to redirect the user and instead you should show the error message directly.
281
284
// This is to avoid letting your authorization server be used as an open redirector.
282
- route ( 'get' , authorizeUrl , function ( req , res , next ) {
285
+ route ( 'get' , authorizeUrl , async function ( req , res , next ) {
283
286
if ( ! validateParams ( req . query , requiredAuthorizeGetParams , self . debug ) ) {
284
287
return errorHandler ( res , {
285
288
status : 400 ,
@@ -293,7 +296,7 @@ const initRoutes = (self, { accessTokenUrl = '/oauth/token', authorizeUrl = '/oa
293
296
const validResponseType = validateResponseType ( req , res )
294
297
if ( ! validResponseType ) return
295
298
296
- const client = getValidatedClient ( req , res )
299
+ const client = await getValidatedClient ( req , res )
297
300
if ( ! client ) return
298
301
299
302
const redirectUri = getValidatedRedirectUri ( req , res , client )
@@ -305,7 +308,7 @@ const initRoutes = (self, { accessTokenUrl = '/oauth/token', authorizeUrl = '/oa
305
308
// STEP 2: ADD USER TO THE REQUEST
306
309
// validate all inputs again, since all inputs
307
310
// could have been manipulated within form
308
- route ( 'post' , authorizeUrl , function ( req , res , next ) {
311
+ route ( 'post' , authorizeUrl , async function ( req , res , next ) {
309
312
if ( ! validateParams ( req . body , requiredAuthorizePostParams , self . debug ) ) {
310
313
return errorHandler ( res , {
311
314
error : 'invalid_request' ,
@@ -316,7 +319,7 @@ const initRoutes = (self, { accessTokenUrl = '/oauth/token', authorizeUrl = '/oa
316
319
} )
317
320
}
318
321
319
- const client = getValidatedClient ( req , res )
322
+ const client = await getValidatedClient ( req , res )
320
323
if ( ! client ) return
321
324
322
325
const validRedirectUri = getValidatedRedirectUri ( req , res , client )
@@ -366,7 +369,7 @@ const initRoutes = (self, { accessTokenUrl = '/oauth/token', authorizeUrl = '/oa
366
369
// - on allow, assign the client_id to the user's authorized clients
367
370
// - on deny, ...?
368
371
// - construct the redirect query and redirect to the redirect_uri
369
- route ( 'post' , authorizeUrl , function ( req , res /*, next */ ) {
372
+ route ( 'post' , authorizeUrl , async function ( req , res /*, next */ ) {
370
373
const request = new Request ( req )
371
374
const response = new Response ( res )
372
375
const authorizeOptions = {
@@ -377,37 +380,36 @@ const initRoutes = (self, { accessTokenUrl = '/oauth/token', authorizeUrl = '/oa
377
380
}
378
381
}
379
382
380
- return self . oauth . authorize ( request , response , authorizeOptions )
381
- . then ( bind ( function ( code ) {
382
- const query = new URLSearchParams ( {
383
- code : code . authorizationCode ,
384
- user : req . user . id ,
385
- state : req . body . state
386
- } )
387
-
388
- const finalRedirectUri = `${ req . body . redirect_uri } ?${ query } `
383
+ try {
384
+ const code = await self . oauth . authorize ( request , response , authorizeOptions )
385
+ const query = new URLSearchParams ( {
386
+ code : code . authorizationCode ,
387
+ user : req . user . id ,
388
+ state : req . body . state
389
+ } )
389
390
390
- res . statusCode = 302
391
- res . setHeader ( 'Location' , finalRedirectUri )
392
- res . end ( )
393
- } ) )
394
- . catch ( function ( err ) {
395
- errorHandler ( res , {
396
- originalError : err ,
397
- error : err . name ,
398
- description : err . message ,
399
- status : err . statusCode ,
400
- state : req . body . state ,
401
- debug : self . debug
402
- } )
391
+ const finalRedirectUri = ` ${ req . body . redirect_uri } ? ${ query } `
392
+
393
+ res . statusCode = 302
394
+ res . setHeader ( 'Location' , finalRedirectUri )
395
+ res . end ( )
396
+ } catch ( err ) {
397
+ errorHandler ( res , {
398
+ originalError : err ,
399
+ error : err . name ,
400
+ description : err . message ,
401
+ status : err . statusCode ,
402
+ state : req . body . state ,
403
+ debug : self . debug
403
404
} )
405
+ }
404
406
} )
405
407
406
408
// STEP 4: GENERATE ACCESS TOKEN RESPONSE
407
409
// - validate params
408
410
// - validate authorization code
409
411
// - issue accessToken and refreshToken
410
- route ( 'post' , accessTokenUrl , function ( req , res , next ) {
412
+ route ( 'post' , accessTokenUrl , async function ( req , res , /* next */ ) {
411
413
if ( ! validateParams ( req . body , requiredAccessTokenPostParams , self . debug ) ) {
412
414
return errorHandler ( res , {
413
415
status : 400 ,
@@ -421,30 +423,29 @@ const initRoutes = (self, { accessTokenUrl = '/oauth/token', authorizeUrl = '/oa
421
423
const request = new Request ( req )
422
424
const response = new Response ( res )
423
425
424
- return self . oauth . token ( request , response )
425
- . then ( function ( token ) {
426
- res . writeHead ( 200 , {
427
- 'Content-Type' : 'application/json' ,
428
- 'Cache-Control' : 'no-store' ,
429
- Pragma : 'no-cache'
430
- } )
431
- const body = JSON . stringify ( {
432
- access_token : token . accessToken ,
433
- token_type : 'bearer' ,
434
- expires_in : token . accessTokenExpiresAt ,
435
- refresh_token : token . refreshToken
436
- } )
437
- res . end ( body )
426
+ try {
427
+ const token = await self . oauth . token ( request , response )
428
+ res . writeHead ( 200 , {
429
+ 'Content-Type' : 'application/json' ,
430
+ 'Cache-Control' : 'no-store' ,
431
+ Pragma : 'no-cache'
438
432
} )
439
- . catch ( function ( err ) {
440
- return errorHandler ( res , {
441
- error : 'unauthorized_client' ,
442
- description : err . message ,
443
- state : req . body . state ,
444
- debug : self . debug ,
445
- status : err . statusCode
446
- } )
433
+ const body = JSON . stringify ( {
434
+ access_token : token . accessToken ,
435
+ token_type : 'bearer' ,
436
+ expires_in : token . accessTokenExpiresAt ,
437
+ refresh_token : token . refreshToken
438
+ } )
439
+ res . end ( body )
440
+ } catch ( err ) {
441
+ return errorHandler ( res , {
442
+ error : 'unauthorized_client' ,
443
+ description : err . message ,
444
+ state : req . body . state ,
445
+ debug : self . debug ,
446
+ status : err . statusCode
447
447
} )
448
+ }
448
449
} )
449
450
450
451
route ( 'use' , fallbackUrl , function ( req , res , next ) {
0 commit comments