Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

RFC 6749#4.1.2.1 - fix order of error handling #565

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 10 additions & 9 deletions lib/handlers/authorize-handler.js
Original file line number Diff line number Diff line change
Expand Up @@ -78,10 +78,6 @@ AuthorizeHandler.prototype.handle = function(request, response) {
throw new InvalidArgumentError('Invalid argument: `response` must be an instance of Response');
}

if ('false' === request.query.allowed) {
return Promise.reject(new AccessDeniedError('Access denied: user denied access to application'));
}

var fns = [
this.getAuthorizationCodeLifetime(),
this.getClient(request),
Expand All @@ -97,14 +93,19 @@ AuthorizeHandler.prototype.handle = function(request, response) {
var ResponseType;

return Promise.bind(this)
.then(function() {
.then(function() {
scope = this.getScope(request);

return this.generateAuthorizationCode(client, user, scope);
})
.then(function(authorizationCode) {
state = this.getState(request);
ResponseType = this.getResponseType(request);
})
.then(function() {
if ('false' === request.query.allowed) {
return Promise.reject(new AccessDeniedError('Access denied: user denied access to application'));
}

return this.generateAuthorizationCode(client, user, scope);
})
.then(function(authorizationCode) {

return this.saveAuthorizationCode(authorizationCode, expiresAt, scope, client, uri, user);
})
Expand Down
33 changes: 26 additions & 7 deletions test/integration/handlers/authorize-handler_test.js
Original file line number Diff line number Diff line change
Expand Up @@ -159,21 +159,40 @@ describe('AuthorizeHandler integration', function() {
}
});

it('should throw an error if `allowed` is `false`', function() {
it('should redirect to an error response if user denied access', function() {
var model = {
getAccessToken: function() {},
getClient: function() {},
getAccessToken: function() {
return {
user: {},
accessTokenExpiresAt: new Date(new Date().getTime() + 10000)
};
},
getClient: function() {
return { grants: ['authorization_code'], redirectUris: ['http://example.com/cb'] };
},
saveAuthorizationCode: function() {}
};
var handler = new AuthorizeHandler({ authorizationCodeLifetime: 120, model: model });
var request = new Request({ body: {}, headers: {}, method: {}, query: { allowed: 'false' } });
var request = new Request({
body: {
client_id: 12345,
response_type: 'code'
},
method: {},
headers: {
'Authorization': 'Bearer foo'
},
query: {
state: 'foobar',
allowed: 'false'
}
});
var response = new Response({ body: {}, headers: {} });

return handler.handle(request, response)
.then(should.fail)
.catch(function(e) {
e.should.be.an.instanceOf(AccessDeniedError);
e.message.should.equal('Access denied: user denied access to application');
.catch(function() {
response.get('location').should.equal('http://example.com/cb?error=access_denied&error_description=Access%20denied%3A%20user%20denied%20access%20to%20application&state=foobar');
});
});

Expand Down