@@ -18,7 +18,7 @@ const Request = require('../request');
18
18
const Response = require ( '../response' ) ;
19
19
const ServerError = require ( '../errors/server-error' ) ;
20
20
const UnauthorizedClientError = require ( '../errors/unauthorized-client-error' ) ;
21
- const is = require ( '../validator/is ' ) ;
21
+ const isFormat = require ( '@node-oauth/formats ' ) ;
22
22
const tokenUtil = require ( '../utils/token-util' ) ;
23
23
const url = require ( 'url' ) ;
24
24
@@ -96,6 +96,12 @@ AuthorizeHandler.prototype.handle = function(request, response) {
96
96
let ResponseType ;
97
97
98
98
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
+ } )
99
105
. then ( function ( ) {
100
106
const requestedScope = this . getScope ( request ) ;
101
107
@@ -107,7 +113,6 @@ AuthorizeHandler.prototype.handle = function(request, response) {
107
113
return this . generateAuthorizationCode ( client , user , scope ) ;
108
114
} )
109
115
. then ( function ( authorizationCode ) {
110
- state = this . getState ( request ) ;
111
116
ResponseType = this . getResponseType ( request ) ;
112
117
113
118
return this . saveAuthorizationCode ( authorizationCode , expiresAt , scope , client , uri , user ) ;
@@ -167,13 +172,13 @@ AuthorizeHandler.prototype.getClient = function(request) {
167
172
throw new InvalidRequestError ( 'Missing parameter: `client_id`' ) ;
168
173
}
169
174
170
- if ( ! is . vschar ( clientId ) ) {
175
+ if ( ! isFormat . vschar ( clientId ) ) {
171
176
throw new InvalidRequestError ( 'Invalid parameter: `client_id`' ) ;
172
177
}
173
178
174
179
const redirectUri = request . body . redirect_uri || request . query . redirect_uri ;
175
180
176
- if ( redirectUri && ! is . uri ( redirectUri ) ) {
181
+ if ( redirectUri && ! isFormat . uri ( redirectUri ) ) {
177
182
throw new InvalidRequestError ( 'Invalid request: `redirect_uri` is not a valid URI' ) ;
178
183
}
179
184
return promisify ( this . model . getClient , 2 ) . call ( this . model , clientId , null )
@@ -233,7 +238,7 @@ AuthorizeHandler.prototype.validateScope = function(user, client, scope) {
233
238
AuthorizeHandler . prototype . getScope = function ( request ) {
234
239
const scope = request . body . scope || request . query . scope ;
235
240
236
- if ( ! is . nqschar ( scope ) ) {
241
+ if ( ! isFormat . nqschar ( scope ) ) {
237
242
throw new InvalidScopeError ( 'Invalid parameter: `scope`' ) ;
238
243
}
239
244
@@ -246,13 +251,14 @@ AuthorizeHandler.prototype.getScope = function(request) {
246
251
247
252
AuthorizeHandler . prototype . getState = function ( request ) {
248
253
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\`` ) ;
256
262
}
257
263
258
264
return state ;
0 commit comments