Skip to content

Commit 1e46234

Browse files
committed
Updating to jws@^3.0.0
As `[email protected]` 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.
1 parent 954bd7a commit 1e46234

File tree

3 files changed

+18
-13
lines changed

3 files changed

+18
-13
lines changed

index.js

+13-8
Original file line numberDiff line numberDiff line change
@@ -112,15 +112,25 @@ module.exports.verify = function(jwtString, secretOrPublicKey, options, callback
112112
~secretOrPublicKey.toString().indexOf('BEGIN PUBLIC KEY') ?
113113
[ 'RS256','RS384','RS512','ES256','ES384','ES512' ] :
114114
~secretOrPublicKey.toString().indexOf('BEGIN RSA PUBLIC KEY') ?
115-
[ 'RS256','RS384','RS512' ] :
116-
[ 'HS256','HS384','HS512' ];
115+
[ 'RS256','RS384','RS512' ] :
116+
[ 'HS256','HS384','HS512' ];
117117

118118
}
119119

120+
if (!jws.isValid(jwtString)) {
121+
return done(new JsonWebTokenError('invalid token'));
122+
}
123+
124+
var header = jws.decode(jwtString).header;
125+
126+
if (!~options.algorithms.indexOf(header.alg)) {
127+
return done(new JsonWebTokenError('invalid signature'));
128+
}
129+
120130
var valid;
121131

122132
try {
123-
valid = jws.verify(jwtString, secretOrPublicKey);
133+
valid = jws.verify(jwtString, header.alg, secretOrPublicKey);
124134
} catch (e) {
125135
return done(e);
126136
}
@@ -136,11 +146,6 @@ module.exports.verify = function(jwtString, secretOrPublicKey, options, callback
136146
return done(err);
137147
}
138148

139-
var header = jws.decode(jwtString).header;
140-
if (!~options.algorithms.indexOf(header.alg)) {
141-
return done(new JsonWebTokenError('invalid signature'));
142-
}
143-
144149
if (typeof payload.exp !== 'undefined' && !options.ignoreExpiration) {
145150
if (typeof payload.exp !== 'number') {
146151
return done(new JsonWebTokenError('invalid exp value'));

package.json

+4-4
Original file line numberDiff line numberDiff line change
@@ -19,12 +19,12 @@
1919
"url": "https://github.com/auth0/node-jsonwebtoken/issues"
2020
},
2121
"dependencies": {
22-
"jws": "~2.0.0"
22+
"jws": "^3.0.0"
2323
},
2424
"devDependencies": {
25-
"atob": "~1.1.2",
26-
"chai": "~1.10.0",
27-
"mocha": "~2.1.0"
25+
"atob": "^1.1.2",
26+
"chai": "^1.10.0",
27+
"mocha": "^2.1.0"
2828
},
2929
"engines": {
3030
"npm": ">=1.4.28"

test/jwt.rs.tests.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -241,7 +241,7 @@ describe('RS256', function() {
241241
jwt.verify('fruit.fruit.fruit', pub, function(err, decoded) {
242242
assert.isUndefined(decoded);
243243
assert.isNotNull(err);
244-
assert.equal(err.name, 'Error');
244+
assert.equal(err.name, 'JsonWebTokenError');
245245
done();
246246
});
247247
});

0 commit comments

Comments
 (0)