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

Scope is optional with authorization code grant #103

Closed
wants to merge 5 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 1 addition & 1 deletion lib/grant-types/abstract-grant-type.js
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@ AbstractGrantType.prototype.validateScope = async function(user, client, scope)

return validatedScope;
} else {
return scope;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

not necessary anymore since the function is async now.

return Promise.resolve(scope);
}
};

Expand Down
1 change: 1 addition & 0 deletions lib/grant-types/authorization-code-grant-type.js
Original file line number Diff line number Diff line change
Expand Up @@ -207,6 +207,7 @@ class AuthorizationCodeGrantType extends AbstractGrantType {
}
}


/**
* Export constructor.
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ describe('AuthorizationCodeGrantType integration', function() {
e.message.should.equal('Missing parameter: `request`');
}
});

it('should throw an error if `client` is invalid', function() {
const client = {};
const model = {
Expand All @@ -108,7 +108,7 @@ describe('AuthorizationCodeGrantType integration', function() {
});

it('should throw an error if `client` is missing', function() {

const model = {
getAuthorizationCode: function() { return { authorizationCode: 12345, expiresAt: new Date(new Date() * 2), user: {} }; },
revokeAuthorizationCode: function() {},
Expand Down
33 changes: 32 additions & 1 deletion test/unit/grant-types/authorization-code-grant-type_test.js
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ describe('AuthorizationCodeGrantType', function() {
};
const handler = new AuthorizationCodeGrantType({ accessTokenLifetime: 120, model: model });

sinon.stub(handler, 'validateScope').returns('foobiz');
sinon.stub(handler, 'validateScope').returns(Promise.resolve('foobiz'));
sinon.stub(handler, 'generateAccessToken').returns(Promise.resolve('foo'));
sinon.stub(handler, 'generateRefreshToken').returns(Promise.resolve('bar'));
sinon.stub(handler, 'getAccessTokenExpiresAt').returns(Promise.resolve('biz'));
Expand All @@ -86,6 +86,37 @@ describe('AuthorizationCodeGrantType', function() {
model.saveToken.firstCall.args[1].should.equal(client);
model.saveToken.firstCall.args[2].should.equal(user);
model.saveToken.firstCall.thisValue.should.equal(model);
handler.validateScope.callCount.should.equal(1);
})
.catch(should.fail);
});
});

describe('saveToken() - no scope', function() {
it('should call `model.saveToken()`', function() {
const client = {};
const user = {};
const model = {
getAuthorizationCode: function() {},
revokeAuthorizationCode: function() {},
saveToken: sinon.stub().returns(true),
validateScope: function(u, c, s){ return null; }
};
const handler = new AuthorizationCodeGrantType({ accessTokenLifetime: 120, model: model });

sinon.stub(handler, 'generateAccessToken').returns(Promise.resolve('foo'));
sinon.stub(handler, 'generateRefreshToken').returns(Promise.resolve('bar'));
sinon.stub(handler, 'getAccessTokenExpiresAt').returns(Promise.resolve('biz'));
sinon.stub(handler, 'getRefreshTokenExpiresAt').returns(Promise.resolve('baz'));

return handler.saveToken(user, client, 'foobar', null)
.then(function() {
model.saveToken.callCount.should.equal(1);
model.saveToken.firstCall.args.should.have.length(3);
model.saveToken.firstCall.args[0].should.eql({ accessToken: 'foo', authorizationCode: 'foobar', accessTokenExpiresAt: 'biz', refreshToken: 'bar', refreshTokenExpiresAt: 'baz', scope: null });
model.saveToken.firstCall.args[1].should.equal(client);
model.saveToken.firstCall.args[2].should.equal(user);
model.saveToken.firstCall.thisValue.should.equal(model);
})
.catch(should.fail);
});
Expand Down