Renaming header.alg mismatch exception to invalid algorithm and adding more mismatch tests.

This commit is contained in:
Alberto Pose
2015-04-10 14:53:42 -03:00
parent 19e6cc6a1f
commit 9f24ffd579
2 changed files with 29 additions and 7 deletions
+1 -1
View File
@@ -126,7 +126,7 @@ module.exports.verify = function(jwtString, secretOrPublicKey, options, callback
var header = decodedToken.header;
if (!~options.algorithms.indexOf(header.alg)) {
return done(new JsonWebTokenError('invalid signature'));
return done(new JsonWebTokenError('invalid algorithm'));
}
var valid;
+28 -6
View File
@@ -11,10 +11,32 @@ var pub = fs.readFileSync(path.join(__dirname, 'pub.pem'), 'utf8');
var TOKEN = 'eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJmb28iOiJiYXIiLCJpYXQiOjE0MjY1NDY5MTl9.ETgkTn8BaxIX4YqvUWVFPmum3moNZ7oARZtSBXb_vP4';
describe('signing with pub key as symmetric', function () {
it('should not verify', function () {
expect(function () {
jwt.verify(TOKEN, pub);
}).to.throw(JsonWebTokenError, /invalid signature/);
describe('when setting a wrong `header.alg`', function () {
describe('signing with pub key as symmetric', function () {
it('should not verify', function () {
expect(function () {
jwt.verify(TOKEN, pub);
}).to.throw(JsonWebTokenError, /invalid algorithm/);
});
});
});
describe('signing with pub key as HS256 and whitelisting only RS256', function () {
it('should not verify', function () {
expect(function () {
jwt.verify(TOKEN, pub, {algorithms: ['RS256']});
}).to.throw(JsonWebTokenError, /invalid algorithm/);
});
});
describe('signing with HS256 and checking with HS384', function () {
it('should not verify', function () {
expect(function () {
var token = jwt.sign({foo: 'bar'}, 'secret', {algorithm: 'HS256'});
jwt.verify(token, 'some secret', {algorithms: ['HS384']});
}).to.throw(JsonWebTokenError, /invalid algorithm/);
});
});
});