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

Don't validate id_token when alg is HS256 and there is no clientSecret #330

Merged
merged 6 commits into from
Feb 5, 2019
Merged
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
18 changes: 14 additions & 4 deletions src/auth/OAUthWithIDTokenValidation.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,9 @@ var Promise = require('bluebird');

var ArgumentError = require('rest-facade').ArgumentError;

var HS256_IGNORE_VALIDATION_MESSAGE =
'Validation of `id_token` requires a `clientSecret` when using the HS256 algorithm. To ensure tokens are validated, please switch the signing algorithm to RS256 or provide a `clientSecret` in the constructor.';
luisrudge marked this conversation as resolved.
Show resolved Hide resolved

/**
* @class
* Abstracts the `oauth.create` method with additional id_token validation
Expand Down Expand Up @@ -58,6 +61,9 @@ OAUthWithIDTokenValidation.prototype.create = function(params, data, cb) {
if (r.id_token) {
function getKey(header, callback) {
if (header.alg === 'HS256') {
if (!_this.clientSecret) {
return callback({ message: HS256_IGNORE_VALIDATION_MESSAGE });
}
return callback(null, Buffer.from(_this.clientSecret, 'base64'));
}
_this._jwksClient.getSigningKey(header.kid, function(err, key) {
Expand All @@ -77,11 +83,15 @@ OAUthWithIDTokenValidation.prototype.create = function(params, data, cb) {
audience: this.clientId,
issuer: 'https://' + this.domain + '/'
},
function(err, payload) {
if (err) {
return rej(err);
function(err) {
if (!err) {
return res(r);
}
if (err.message && err.message.includes(HS256_IGNORE_VALIDATION_MESSAGE)) {
console.warn(HS256_IGNORE_VALIDATION_MESSAGE);
luisrudge marked this conversation as resolved.
Show resolved Hide resolved
return res(r);
}
return res(r);
return rej(err);
}
);
});
Expand Down
23 changes: 22 additions & 1 deletion test/auth/oauth-with-idtoken-validation.tests.js
Original file line number Diff line number Diff line change
Expand Up @@ -128,7 +128,7 @@ describe('OAUthWithIDTokenValidation', function() {
done();
});
});
it('Calls uses secret as key when header.alg === HS256', function(done) {
it('Uses `clientSecret` as key when header.alg === HS256 and there is a user secret', function(done) {
var oauth = {
create: function() {
return new Promise(res => res({ id_token: 'foobar' }));
Expand All @@ -145,6 +145,27 @@ describe('OAUthWithIDTokenValidation', function() {
});
oauthWithValidation.create(PARAMS, DATA);
});
it('Returns unvalidated response when header.alg === HS256 and there is no user secret', function(done) {
var oauth = {
create: function() {
return new Promise(res => res({ id_token: 'foobar' }));
}
};
sinon.stub(jwt, 'verify', function(idtoken, getKey, options, callback) {
getKey({ alg: 'HS256' }, function(err, key) {
expect(err.message).to.contain(
Copy link
Contributor

Choose a reason for hiding this comment

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

I think besides skipping the validation is also important to test that the console.warn was called with the right message

'Validation of `id_token` requires a `clientSecret` when using the HS256 algorithm. To ensure tokens are validated, please switch the signing algorithm to RS256 or provide a `clientSecret` in the constructor.'
);
callback(err, key);
});
});
var oauthWithValidation = new OAUthWithIDTokenValidation(oauth, {});
oauthWithValidation.create(PARAMS, DATA, function(err, response) {
expect(err).to.be.null;
expect(response).to.be.eql({ id_token: 'foobar' });
done();
});
});
describe('when header.alg !== HS256', function() {
it('creates a jwksClient with the correct jwksUri', function(done) {
var oauth = {
Expand Down