@@ -21,6 +21,7 @@ const UnauthorizedClientError = require('../errors/unauthorized-client-error');
2121const isFormat = require ( '@node-oauth/formats' ) ;
2222const tokenUtil = require ( '../utils/token-util' ) ;
2323const url = require ( 'url' ) ;
24+ const pkce = require ( '../pkce/pkce' ) ;
2425
2526/**
2627 * Response types.
@@ -110,8 +111,10 @@ AuthorizeHandler.prototype.handle = function(request, response) {
110111 } )
111112 . then ( function ( authorizationCode ) {
112113 ResponseType = this . getResponseType ( request ) ;
114+ const codeChallenge = this . getCodeChallenge ( request ) ;
115+ const codeChallengeMethod = this . getCodeChallengeMethod ( request ) ;
113116
114- return this . saveAuthorizationCode ( authorizationCode , expiresAt , scope , client , uri , user ) ;
117+ return this . saveAuthorizationCode ( authorizationCode , expiresAt , scope , client , uri , user , codeChallenge , codeChallengeMethod ) ;
115118 } )
116119 . then ( function ( code ) {
117120 const responseType = new ResponseType ( code . authorizationCode ) ;
@@ -289,13 +292,20 @@ AuthorizeHandler.prototype.getRedirectUri = function(request, client) {
289292 * Save authorization code.
290293 */
291294
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 = {
294297 authorizationCode : authorizationCode ,
295298 expiresAt : expiresAt ,
296299 redirectUri : redirectUri ,
297300 scope : scope
298301 } ;
302+
303+ if ( codeChallenge && codeChallengeMethod ) {
304+ code = Object . assign ( {
305+ codeChallenge : codeChallenge ,
306+ codeChallengeMethod : codeChallengeMethod
307+ } , code ) ;
308+ }
299309 return promisify ( this . model . saveAuthorizationCode , 3 ) . call ( this . model , code , client , user ) ;
300310} ;
301311
@@ -365,6 +375,27 @@ AuthorizeHandler.prototype.updateResponse = function(response, redirectUri, stat
365375 response . redirect ( url . format ( redirectUri ) ) ;
366376} ;
367377
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+
368399/**
369400 * Export constructor.
370401 */
0 commit comments