Skip to content

Commit

Permalink
Merge pull request oauthjs#522 from adieuadieu/fix/unauthorized_clien…
Browse files Browse the repository at this point in the history
…t_when_no_auth_code_grant

fix: authorization_code grant should not be required in implicit flow
  • Loading branch information
mjsalinger authored Sep 5, 2018
2 parents c516c16 + a950bc9 commit 4a77277
Show file tree
Hide file tree
Showing 3 changed files with 50 additions and 1 deletion.
7 changes: 6 additions & 1 deletion lib/handlers/authorize-handler.js
Original file line number Diff line number Diff line change
Expand Up @@ -147,6 +147,7 @@ AuthorizeHandler.prototype.getClient = function(request) {
if (redirectUri && !is.uri(redirectUri)) {
throw new InvalidRequestError('Invalid request: `redirect_uri` is not a valid URI');
}

return promisify(this.model.getClient, 2).call(this.model, clientId, null)
.then(function(client) {
if (!client) {
Expand All @@ -157,7 +158,10 @@ AuthorizeHandler.prototype.getClient = function(request) {
throw new InvalidClientError('Invalid client: missing client `grants`');
}

if (!_.includes(client.grants, 'authorization_code')) {
var responseType = request.body.response_type || request.query.response_type;
var requestedGrantType = responseType === 'token' ? 'implicit' : 'authorization_code';

if (!_.includes(client.grants, requestedGrantType)) {
throw new UnauthorizedClientError('Unauthorized client: `grant_type` is invalid');
}

Expand All @@ -168,6 +172,7 @@ AuthorizeHandler.prototype.getClient = function(request) {
if (redirectUri && !_.includes(client.redirectUris, redirectUri)) {
throw new InvalidClientError('Invalid client: `redirect_uri` does not match client value');
}

return client;
});
};
Expand Down
4 changes: 4 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,10 @@
{
"name": "Jonathon Hill",
"email": "[email protected]"
},
{
"name": "Marco Lüthy",
"email": "[email protected]"
}
],
"main": "index.js",
Expand Down
40 changes: 40 additions & 0 deletions test/integration/handlers/authorize-handler_test.js
Original file line number Diff line number Diff line change
Expand Up @@ -249,6 +249,46 @@ describe('AuthorizeHandler integration', function() {
.catch(should.fail);
});


it('given an implicit grant flow, should redirect to a successful response with `token` and `state` if successful', function() {
var client = { grants: ['implicit'], redirectUris: ['http://example.com/cb'] };
var token = { accessToken: 'foobar-token' }
var model = {
getAccessToken: function() {
return {
client: client,
user: {},
accessTokenExpiresAt: new Date(new Date().getTime() + 10000)
};
},
getClient: function() {
return client;
},
saveToken: function() { return token; }
};
var handler = new AuthorizeHandler({ accessTokenLifetime: 120, model: model });
var request = new Request({
body: {
},
headers: {
'Authorization': 'Bearer foo'
},
method: {},
query: {
client_id: 12345,
response_type: 'token',
state: 'foobar'
}
});
var response = new Response({ body: {}, headers: {} });

return handler.handle(request, response)
.then(function() {
response.get('location').should.equal('http://example.com/cb#access_token=foobar-token&state=foobar');
})
.catch(should.fail);
});

it('should redirect to an error response if `scope` is invalid', function() {
var model = {
getAccessToken: function() {
Expand Down

0 comments on commit 4a77277

Please sign in to comment.