Skip to content

Commit

Permalink
Merge branch 'jonekdahl-verify-unsigned-tokens'
Browse files Browse the repository at this point in the history
  • Loading branch information
jfromaniello committed Apr 27, 2016
2 parents afb3285 + 7b0ba50 commit ec88079
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 2 deletions.
10 changes: 8 additions & 2 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -71,14 +71,20 @@ JWT.verify = function(jwtString, secretOrPublicKey, options, callback) {
return done(new JsonWebTokenError('jwt malformed'));
}

if (parts[2].trim() === '' && secretOrPublicKey){
var hasSignature = parts[2].trim() !== '';

if (!hasSignature && secretOrPublicKey){
return done(new JsonWebTokenError('jwt signature is required'));
}

if (!secretOrPublicKey) {
if (hasSignature && !secretOrPublicKey) {
return done(new JsonWebTokenError('secret or public key must be provided'));
}

if (!hasSignature && !options.algorithms) {
options.algorithms = ['none'];
}

if (!options.algorithms) {
options.algorithms = ~secretOrPublicKey.toString().indexOf('BEGIN CERTIFICATE') ||
~secretOrPublicKey.toString().indexOf('BEGIN PUBLIC KEY') ?
Expand Down
18 changes: 18 additions & 0 deletions test/verify.tests.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,24 @@ describe('verify', function() {
});
});

it('should be able to validate unsigned token', function (done) {
var header = { alg: 'none' };
var payload = { iat: Math.floor(Date.now() / 1000 ) };

var signed = jws.sign({
header: header,
payload: payload,
secret: priv,
encoding: 'utf8'
});

jwt.verify(signed, null, {typ: 'JWT'}, function(err, p) {
assert.isNull(err);
assert.deepEqual(p, payload);
done();
});
});

describe('expiration', function () {
// { foo: 'bar', iat: 1437018582, exp: 1437018583 }
var token = 'eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJmb28iOiJiYXIiLCJpYXQiOjE0MzcwMTg1ODIsImV4cCI6MTQzNzAxODU4M30.NmMv7sXjM1dW0eALNXud8LoXknZ0mH14GtnFclwJv0s';
Expand Down

0 comments on commit ec88079

Please sign in to comment.