@@ -21,6 +21,7 @@ const UnauthorizedClientError = require('../errors/unauthorized-client-error');
21
21
const isFormat = require ( '@node-oauth/formats' ) ;
22
22
const tokenUtil = require ( '../utils/token-util' ) ;
23
23
const url = require ( 'url' ) ;
24
+ const pkce = require ( '../pkce/pkce' ) ;
24
25
25
26
/**
26
27
* Response types.
@@ -77,10 +78,6 @@ AuthorizeHandler.prototype.handle = function(request, response) {
77
78
throw new InvalidArgumentError ( 'Invalid argument: `response` must be an instance of Response' ) ;
78
79
}
79
80
80
- if ( request . query . allowed === 'false' || request . body . allowed === 'false' ) {
81
- return Promise . reject ( new AccessDeniedError ( 'Access denied: user denied access to application' ) ) ;
82
- }
83
-
84
81
const fns = [
85
82
this . getAuthorizationCodeLifetime ( ) ,
86
83
this . getClient ( request ) ,
@@ -98,7 +95,7 @@ AuthorizeHandler.prototype.handle = function(request, response) {
98
95
return Promise . bind ( this )
99
96
. then ( function ( ) {
100
97
state = this . getState ( request ) ;
101
- if ( request . query . allowed === 'false' ) {
98
+ if ( request . query . allowed === 'false' || request . body . allowed === 'false' ) {
102
99
throw new AccessDeniedError ( 'Access denied: user denied access to application' ) ;
103
100
}
104
101
} )
@@ -114,8 +111,10 @@ AuthorizeHandler.prototype.handle = function(request, response) {
114
111
} )
115
112
. then ( function ( authorizationCode ) {
116
113
ResponseType = this . getResponseType ( request ) ;
114
+ const codeChallenge = this . getCodeChallenge ( request ) ;
115
+ const codeChallengeMethod = this . getCodeChallengeMethod ( request ) ;
117
116
118
- return this . saveAuthorizationCode ( authorizationCode , expiresAt , scope , client , uri , user ) ;
117
+ return this . saveAuthorizationCode ( authorizationCode , expiresAt , scope , client , uri , user , codeChallenge , codeChallengeMethod ) ;
119
118
} )
120
119
. then ( function ( code ) {
121
120
const responseType = new ResponseType ( code . authorizationCode ) ;
@@ -293,13 +292,20 @@ AuthorizeHandler.prototype.getRedirectUri = function(request, client) {
293
292
* Save authorization code.
294
293
*/
295
294
296
- AuthorizeHandler . prototype . saveAuthorizationCode = function ( authorizationCode , expiresAt , scope , client , redirectUri , user ) {
297
- const code = {
295
+ AuthorizeHandler . prototype . saveAuthorizationCode = function ( authorizationCode , expiresAt , scope , client , redirectUri , user , codeChallenge , codeChallengeMethod ) {
296
+ let code = {
298
297
authorizationCode : authorizationCode ,
299
298
expiresAt : expiresAt ,
300
299
redirectUri : redirectUri ,
301
300
scope : scope
302
301
} ;
302
+
303
+ if ( codeChallenge && codeChallengeMethod ) {
304
+ code = Object . assign ( {
305
+ codeChallenge : codeChallenge ,
306
+ codeChallengeMethod : codeChallengeMethod
307
+ } , code ) ;
308
+ }
303
309
return promisify ( this . model . saveAuthorizationCode , 3 ) . call ( this . model , code , client , user ) ;
304
310
} ;
305
311
@@ -369,6 +375,27 @@ AuthorizeHandler.prototype.updateResponse = function(response, redirectUri, stat
369
375
response . redirect ( url . format ( redirectUri ) ) ;
370
376
} ;
371
377
378
+ AuthorizeHandler . prototype . getCodeChallenge = function ( request ) {
379
+ return request . body . code_challenge ;
380
+ } ;
381
+
382
+ /**
383
+ * Get code challenge method from request or defaults to plain.
384
+ * https://www.rfc-editor.org/rfc/rfc7636#section-4.3
385
+ *
386
+ * @throws {InvalidRequestError } if request contains unsupported code_challenge_method
387
+ * (see https://www.rfc-editor.org/rfc/rfc7636#section-4.4)
388
+ */
389
+ AuthorizeHandler . prototype . getCodeChallengeMethod = function ( request ) {
390
+ const algorithm = request . body . code_challenge_method ;
391
+
392
+ if ( algorithm && ! pkce . isValidMethod ( algorithm ) ) {
393
+ throw new InvalidRequestError ( `Invalid request: transform algorithm '${ algorithm } ' not supported` ) ;
394
+ }
395
+
396
+ return algorithm || 'plain' ;
397
+ } ;
398
+
372
399
/**
373
400
* Export constructor.
374
401
*/
0 commit comments