@@ -18,7 +18,7 @@ const Request = require('../request');
1818const Response = require ( '../response' ) ;
1919const ServerError = require ( '../errors/server-error' ) ;
2020const UnauthorizedClientError = require ( '../errors/unauthorized-client-error' ) ;
21- const is = require ( '../validator/is ' ) ;
21+ const isFormat = require ( '@node-oauth/formats ' ) ;
2222const tokenUtil = require ( '../utils/token-util' ) ;
2323const url = require ( 'url' ) ;
2424
@@ -96,6 +96,12 @@ AuthorizeHandler.prototype.handle = function(request, response) {
9696 let ResponseType ;
9797
9898 return Promise . bind ( this )
99+ . then ( function ( ) {
100+ state = this . getState ( request ) ;
101+ if ( request . query . allowed === 'false' ) {
102+ throw new AccessDeniedError ( 'Access denied: user denied access to application' ) ;
103+ }
104+ } )
99105 . then ( function ( ) {
100106 const requestedScope = this . getScope ( request ) ;
101107
@@ -107,7 +113,6 @@ AuthorizeHandler.prototype.handle = function(request, response) {
107113 return this . generateAuthorizationCode ( client , user , scope ) ;
108114 } )
109115 . then ( function ( authorizationCode ) {
110- state = this . getState ( request ) ;
111116 ResponseType = this . getResponseType ( request ) ;
112117
113118 return this . saveAuthorizationCode ( authorizationCode , expiresAt , scope , client , uri , user ) ;
@@ -167,13 +172,13 @@ AuthorizeHandler.prototype.getClient = function(request) {
167172 throw new InvalidRequestError ( 'Missing parameter: `client_id`' ) ;
168173 }
169174
170- if ( ! is . vschar ( clientId ) ) {
175+ if ( ! isFormat . vschar ( clientId ) ) {
171176 throw new InvalidRequestError ( 'Invalid parameter: `client_id`' ) ;
172177 }
173178
174179 const redirectUri = request . body . redirect_uri || request . query . redirect_uri ;
175180
176- if ( redirectUri && ! is . uri ( redirectUri ) ) {
181+ if ( redirectUri && ! isFormat . uri ( redirectUri ) ) {
177182 throw new InvalidRequestError ( 'Invalid request: `redirect_uri` is not a valid URI' ) ;
178183 }
179184 return promisify ( this . model . getClient , 2 ) . call ( this . model , clientId , null )
@@ -233,7 +238,7 @@ AuthorizeHandler.prototype.validateScope = function(user, client, scope) {
233238AuthorizeHandler . prototype . getScope = function ( request ) {
234239 const scope = request . body . scope || request . query . scope ;
235240
236- if ( ! is . nqschar ( scope ) ) {
241+ if ( ! isFormat . nqschar ( scope ) ) {
237242 throw new InvalidScopeError ( 'Invalid parameter: `scope`' ) ;
238243 }
239244
@@ -246,13 +251,14 @@ AuthorizeHandler.prototype.getScope = function(request) {
246251
247252AuthorizeHandler . prototype . getState = function ( request ) {
248253 const state = request . body . state || request . query . state ;
249-
250- if ( ! this . allowEmptyState && ! state ) {
251- throw new InvalidRequestError ( 'Missing parameter: `state`' ) ;
252- }
253-
254- if ( ! is . vschar ( state ) ) {
255- throw new InvalidRequestError ( 'Invalid parameter: `state`' ) ;
254+ const stateExists = state && state . length > 0 ;
255+ const stateIsValid = stateExists
256+ ? isFormat . vschar ( state )
257+ : this . allowEmptyState ;
258+
259+ if ( ! stateIsValid ) {
260+ const message = ( ! stateExists ) ? 'Missing' : 'Invalid' ;
261+ throw new InvalidRequestError ( `${ message } parameter: \`state\`` ) ;
256262 }
257263
258264 return state ;
0 commit comments