Skip to content

Commit

Permalink
Updating to jws@^3.0.0
Browse files Browse the repository at this point in the history
As `jws@3.0.0` changed the verify method signature to be `jws.verify(signature, algorithm, secretOrKey)`, the token header must be decoded first in order to make sure that the `alg` field matches one of the allowed `options.algorithms`. After that, the now validated `header.alg` is passed to `jws.verify`

As the order of steps has changed, the error that was thrown when the JWT was invalid is no longer the `jws` one:

{ [Error: Invalid token: no header in signature 'a.b.c'] code: 'MISSING_HEADER', signature: 'a.b.c' }

That old error (removed from jws) has been replaced by a `JsonWebTokenError` with message `invalid token`. That's why this change will bump be a major.
  • Loading branch information
pose committed Apr 10, 2015
1 parent 954bd7a commit 1e46234
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 13 deletions.
21 changes: 13 additions & 8 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -112,15 +112,25 @@ module.exports.verify = function(jwtString, secretOrPublicKey, options, callback
~secretOrPublicKey.toString().indexOf('BEGIN PUBLIC KEY') ?
[ 'RS256','RS384','RS512','ES256','ES384','ES512' ] :
~secretOrPublicKey.toString().indexOf('BEGIN RSA PUBLIC KEY') ?
[ 'RS256','RS384','RS512' ] :
[ 'HS256','HS384','HS512' ];
[ 'RS256','RS384','RS512' ] :
[ 'HS256','HS384','HS512' ];

}

if (!jws.isValid(jwtString)) {
return done(new JsonWebTokenError('invalid token'));
}

var header = jws.decode(jwtString).header;

if (!~options.algorithms.indexOf(header.alg)) {
return done(new JsonWebTokenError('invalid signature'));
}

var valid;

try {
valid = jws.verify(jwtString, secretOrPublicKey);
valid = jws.verify(jwtString, header.alg, secretOrPublicKey);
} catch (e) {
return done(e);
}
Expand All @@ -136,11 +146,6 @@ module.exports.verify = function(jwtString, secretOrPublicKey, options, callback
return done(err);
}

var header = jws.decode(jwtString).header;
if (!~options.algorithms.indexOf(header.alg)) {
return done(new JsonWebTokenError('invalid signature'));
}

if (typeof payload.exp !== 'undefined' && !options.ignoreExpiration) {
if (typeof payload.exp !== 'number') {
return done(new JsonWebTokenError('invalid exp value'));
Expand Down
8 changes: 4 additions & 4 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -19,12 +19,12 @@
"url": "https://github.com/auth0/node-jsonwebtoken/issues"
},
"dependencies": {
"jws": "~2.0.0"
"jws": "^3.0.0"
},
"devDependencies": {
"atob": "~1.1.2",
"chai": "~1.10.0",
"mocha": "~2.1.0"
"atob": "^1.1.2",
"chai": "^1.10.0",
"mocha": "^2.1.0"
},
"engines": {
"npm": ">=1.4.28"
Expand Down
2 changes: 1 addition & 1 deletion test/jwt.rs.tests.js
Original file line number Diff line number Diff line change
Expand Up @@ -241,7 +241,7 @@ describe('RS256', function() {
jwt.verify('fruit.fruit.fruit', pub, function(err, decoded) {
assert.isUndefined(decoded);
assert.isNotNull(err);
assert.equal(err.name, 'Error');
assert.equal(err.name, 'JsonWebTokenError');
done();
});
});
Expand Down

0 comments on commit 1e46234

Please sign in to comment.