Skip to content

Commit 848b0bb

Browse files
authored
Merge pull request #112 from FStefanni/89_11_565
Fixed order of checks in authorize-handler
2 parents c2d108d + 7a61930 commit 848b0bb

File tree

2 files changed

+10
-34
lines changed

2 files changed

+10
-34
lines changed

lib/handlers/authorize-handler.js

+1-5
Original file line numberDiff line numberDiff line change
@@ -77,10 +77,6 @@ AuthorizeHandler.prototype.handle = function(request, response) {
7777
throw new InvalidArgumentError('Invalid argument: `response` must be an instance of Response');
7878
}
7979

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-
8480
const fns = [
8581
this.getAuthorizationCodeLifetime(),
8682
this.getClient(request),
@@ -98,7 +94,7 @@ AuthorizeHandler.prototype.handle = function(request, response) {
9894
return Promise.bind(this)
9995
.then(function() {
10096
state = this.getState(request);
101-
if(request.query.allowed === 'false') {
97+
if (request.query.allowed === 'false' || request.body.allowed === 'false') {
10298
throw new AccessDeniedError('Access denied: user denied access to application');
10399
}
104100
})

test/integration/handlers/authorize-handler_test.js

+9-29
Original file line numberDiff line numberDiff line change
@@ -159,7 +159,7 @@ describe('AuthorizeHandler integration', function() {
159159
}
160160
});
161161

162-
it('should throw an error if `allowed` is `false`', function() {
162+
it('should redirect to an error response if user denied access', function() {
163163
const model = {
164164
getAccessToken: function() {
165165
return {
@@ -170,49 +170,29 @@ describe('AuthorizeHandler integration', function() {
170170
getClient: function() {
171171
return { grants: ['authorization_code'], redirectUris: ['http://example.com/cb'] };
172172
},
173-
saveAuthorizationCode: function() {
174-
throw new Error('Unhandled exception');
175-
}
173+
saveAuthorizationCode: function() {}
176174
};
177175
const handler = new AuthorizeHandler({ authorizationCodeLifetime: 120, model: model });
178176
const request = new Request({
179177
body: {
180-
client_id: 'test'
178+
client_id: 12345,
179+
response_type: 'code'
181180
},
181+
method: {},
182182
headers: {
183183
'Authorization': 'Bearer foo'
184184
},
185-
method: {},
186185
query: {
187-
allowed: 'false',
188-
state: 'foobar'
186+
state: 'foobar',
187+
allowed: 'false'
189188
}
190189
});
191190
const response = new Response({ body: {}, headers: {} });
192191

193192
return handler.handle(request, response)
194193
.then(should.fail)
195-
.catch(function(e) {
196-
e.should.be.an.instanceOf(AccessDeniedError);
197-
e.message.should.equal('Access denied: user denied access to application');
198-
});
199-
});
200-
201-
it('should throw an error if `allowed` is `false` body', function() {
202-
const model = {
203-
getAccessToken: function() {},
204-
getClient: function() {},
205-
saveAuthorizationCode: function() {}
206-
};
207-
const handler = new AuthorizeHandler({ authorizationCodeLifetime: 120, model: model });
208-
const request = new Request({ body: { allowed: 'false' }, headers: {}, method: {}, query: {} });
209-
const response = new Response({ body: {}, headers: {} });
210-
211-
return handler.handle(request, response)
212-
.then(should.fail)
213-
.catch(function(e) {
214-
e.should.be.an.instanceOf(AccessDeniedError);
215-
e.message.should.equal('Access denied: user denied access to application');
194+
.catch(function() {
195+
response.get('location').should.equal('http://example.com/cb?error=access_denied&error_description=Access%20denied%3A%20user%20denied%20access%20to%20application&state=foobar');
216196
});
217197
});
218198

0 commit comments

Comments
 (0)