Skip to content

Commit

Permalink
add test & modify guard code
Browse files Browse the repository at this point in the history
  • Loading branch information
ziluvatar committed Aug 17, 2017
1 parent 91ba14d commit 2a3404f
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 7 deletions.
8 changes: 2 additions & 6 deletions sign.js
Expand Up @@ -66,12 +66,8 @@ module.exports = function (payload, secretOrPrivateKey, options, callback) {
throw err;
}

if (!secretOrPrivateKey) {
if (options.algorithm === 'none') {
secretOrPrivateKey = 'Fix for https://github.com/auth0/node-jsonwebtoken/issues/381';
} else {
return failure(new Error('secretOrPrivateKey must have a value'));
}
if (!secretOrPrivateKey && options.algorithm !== 'none') {
return failure(new Error('secretOrPrivateKey must have a value'));
}

if (typeof payload === 'undefined') {
Expand Down
4 changes: 3 additions & 1 deletion test/async_sign.tests.js
Expand Up @@ -40,7 +40,9 @@ describe('signing a token asynchronously', function() {
});
});

it('should work with none algorithm where secret is falsy', function(done) {
//Known bug: https://github.com/brianloveswords/node-jws/issues/62
//If you need this use case, you need to go for the non-callback-ish code style.
it.skip('should work with none algorithm where secret is falsy', function(done) {
jwt.sign({ foo: 'bar' }, undefined, { algorithm: 'none' }, function(err, token) {
expect(token).to.be.a('string');
expect(token.split('.')).to.have.length(3);
Expand Down
10 changes: 10 additions & 0 deletions test/jwt.hs.tests.js
Expand Up @@ -51,6 +51,16 @@ describe('HS256', function() {
});
});

it('should work with falsy secret and token not signed', function(done) {
var signed = jwt.sign({ foo: 'bar' }, null, { algorithm: 'none' });
var unsigned = signed.split('.')[0] + '.' + signed.split('.')[1] + '.';
jwt.verify(unsigned, 'secret', function(err, decoded) {
assert.isUndefined(decoded);
assert.isNotNull(err);
done();
});
});

it('should throw when verifying null', function(done) {
jwt.verify(null, 'secret', function(err, decoded) {
assert.isUndefined(decoded);
Expand Down

0 comments on commit 2a3404f

Please sign in to comment.