Skip to content

Commit 0f19388

Browse files
committed
Fix verification of unsigned tokens (fix #185)
1 parent c548032 commit 0f19388

File tree

2 files changed

+26
-2
lines changed

2 files changed

+26
-2
lines changed

index.js

+8-2
Original file line numberDiff line numberDiff line change
@@ -172,14 +172,20 @@ JWT.verify = function(jwtString, secretOrPublicKey, options, callback) {
172172
return done(new JsonWebTokenError('jwt malformed'));
173173
}
174174

175-
if (parts[2].trim() === '' && secretOrPublicKey){
175+
var hasSignature = parts[2].trim() !== '';
176+
177+
if (!hasSignature && secretOrPublicKey){
176178
return done(new JsonWebTokenError('jwt signature is required'));
177179
}
178180

179-
if (!secretOrPublicKey) {
181+
if (hasSignature && !secretOrPublicKey) {
180182
return done(new JsonWebTokenError('secret or public key must be provided'));
181183
}
182184

185+
if (!hasSignature && !options.algorithms) {
186+
options.algorithms = ['none'];
187+
}
188+
183189
if (!options.algorithms) {
184190
options.algorithms = ~secretOrPublicKey.toString().indexOf('BEGIN CERTIFICATE') ||
185191
~secretOrPublicKey.toString().indexOf('BEGIN PUBLIC KEY') ?

test/verify.tests.js

+18
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,24 @@ describe('verify', function() {
2828
});
2929
});
3030

31+
it('should be able to validate unsigned token', function (done) {
32+
var header = { alg: 'none' };
33+
var payload = { iat: Math.floor(Date.now() / 1000 ) };
34+
35+
var signed = jws.sign({
36+
header: header,
37+
payload: payload,
38+
secret: priv,
39+
encoding: 'utf8'
40+
});
41+
42+
jwt.verify(signed, null, {typ: 'JWT'}, function(err, p) {
43+
assert.isNull(err);
44+
assert.deepEqual(p, payload);
45+
done();
46+
});
47+
});
48+
3149
describe('expiration', function () {
3250
// { foo: 'bar', iat: 1437018582, exp: 1437018583 }
3351
var token = 'eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJmb28iOiJiYXIiLCJpYXQiOjE0MzcwMTg1ODIsImV4cCI6MTQzNzAxODU4M30.NmMv7sXjM1dW0eALNXud8LoXknZ0mH14GtnFclwJv0s';

0 commit comments

Comments
 (0)