mirror of
https://github.com/hanzoai/node-jsonwebtoken.git
synced 2026-09-24 17:00:48 +00:00
Renaming header.alg mismatch exception to invalid algorithm and adding more mismatch tests.
This commit is contained in:
@@ -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
@@ -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/);
|
||||
});
|
||||
});
|
||||
|
||||
|
||||
});
|
||||
|
||||
Reference in New Issue
Block a user