@@ -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.
@@ -110,8 +111,10 @@ AuthorizeHandler.prototype.handle = function(request, response) {
110
111
} )
111
112
. then ( function ( authorizationCode ) {
112
113
ResponseType = this . getResponseType ( request ) ;
114
+ const codeChallenge = this . getCodeChallenge ( request ) ;
115
+ const codeChallengeMethod = this . getCodeChallengeMethod ( request ) ;
113
116
114
- return this . saveAuthorizationCode ( authorizationCode , expiresAt , scope , client , uri , user ) ;
117
+ return this . saveAuthorizationCode ( authorizationCode , expiresAt , scope , client , uri , user , codeChallenge , codeChallengeMethod ) ;
115
118
} )
116
119
. then ( function ( code ) {
117
120
const responseType = new ResponseType ( code . authorizationCode ) ;
@@ -289,13 +292,20 @@ AuthorizeHandler.prototype.getRedirectUri = function(request, client) {
289
292
* Save authorization code.
290
293
*/
291
294
292
- AuthorizeHandler . prototype . saveAuthorizationCode = function ( authorizationCode , expiresAt , scope , client , redirectUri , user ) {
293
- const code = {
295
+ AuthorizeHandler . prototype . saveAuthorizationCode = function ( authorizationCode , expiresAt , scope , client , redirectUri , user , codeChallenge , codeChallengeMethod ) {
296
+ let code = {
294
297
authorizationCode : authorizationCode ,
295
298
expiresAt : expiresAt ,
296
299
redirectUri : redirectUri ,
297
300
scope : scope
298
301
} ;
302
+
303
+ if ( codeChallenge && codeChallengeMethod ) {
304
+ code = Object . assign ( {
305
+ codeChallenge : codeChallenge ,
306
+ codeChallengeMethod : codeChallengeMethod
307
+ } , code ) ;
308
+ }
299
309
return promisify ( this . model . saveAuthorizationCode , 3 ) . call ( this . model , code , client , user ) ;
300
310
} ;
301
311
@@ -365,6 +375,27 @@ AuthorizeHandler.prototype.updateResponse = function(response, redirectUri, stat
365
375
response . redirect ( url . format ( redirectUri ) ) ;
366
376
} ;
367
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
+
368
399
/**
369
400
* Export constructor.
370
401
*/
0 commit comments