Skip to content

Commit

Permalink
Merge pull request #374 from ziluvatar/add-check-for-empty-secrets
Browse files Browse the repository at this point in the history
sign: add check to be sure secret has a value
  • Loading branch information
jfromaniello committed Aug 4, 2017
2 parents 43739dc + c584d1c commit c6a7026
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 0 deletions.
3 changes: 3 additions & 0 deletions sign.js
Expand Up @@ -66,6 +66,9 @@ module.exports = function (payload, secretOrPrivateKey, options, callback) {
throw err;
}

if (!secretOrPrivateKey) {
return failure(new Error('secretOrPrivateKey must have a value'));
}

if (typeof payload === 'undefined') {
return failure(new Error('payload is required'));
Expand Down
14 changes: 14 additions & 0 deletions test/async_sign.tests.js
Expand Up @@ -63,5 +63,19 @@ describe('signing a token asynchronously', function() {
done();
});
});

describe('secret must have a value', function(){
[undefined, '', 0].forEach(function(secret){
it('should return an error if the secret is falsy: ' + (typeof secret === 'string' ? '(empty string)' : secret), function(done) {
// This is needed since jws will not answer for falsy secrets
jwt.sign('string', secret, {}, function(err, token) {
expect(err).to.be.exist();
expect(err.message).to.equal('secretOrPrivateKey must have a value');
expect(token).to.not.exist;
done();
});
});
});
});
});
});

0 comments on commit c6a7026

Please sign in to comment.