Skip to content

Commit b1ff632

Browse files
author
Rhys Anthony McCaig
committed
Fix for #381. Set secret string before using jws when alg is none
1 parent e56f904 commit b1ff632

File tree

2 files changed

+22
-2
lines changed

2 files changed

+22
-2
lines changed

sign.js

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,11 @@ module.exports = function (payload, secretOrPrivateKey, options, callback) {
6767
}
6868

6969
if (!secretOrPrivateKey) {
70-
return failure(new Error('secretOrPrivateKey must have a value'));
70+
if (options.algorithm === 'none') {
71+
secretOrPrivateKey = 'Fix for https://github.com/auth0/node-jsonwebtoken/issues/381';
72+
} else {
73+
return failure(new Error('secretOrPrivateKey must have a value'));
74+
}
7175
}
7276

7377
if (typeof payload === 'undefined') {

test/async_sign.tests.js

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,22 @@ describe('signing a token asynchronously', function() {
3232
});
3333
});
3434

35+
it('should work with none algorithm where secret is set', function(done) {
36+
jwt.sign({ foo: 'bar' }, undefined, { algorithm: 'none' }, function(err, token) {
37+
expect(token).to.be.a('string');
38+
expect(token.split('.')).to.have.length(3);
39+
done();
40+
});
41+
});
42+
43+
it('should work with none algorithm where secret is falsy', function(done) {
44+
jwt.sign({ foo: 'bar' }, undefined, { algorithm: 'none' }, function(err, token) {
45+
expect(token).to.be.a('string');
46+
expect(token.split('.')).to.have.length(3);
47+
done();
48+
});
49+
});
50+
3551
it('should return error when secret is not a cert for RS256', function(done) {
3652
//this throw an error because the secret is not a cert and RS256 requires a cert.
3753
jwt.sign({ foo: 'bar' }, secret, { algorithm: 'RS256' }, function (err) {
@@ -66,7 +82,7 @@ describe('signing a token asynchronously', function() {
6682

6783
describe('secret must have a value', function(){
6884
[undefined, '', 0].forEach(function(secret){
69-
it('should return an error if the secret is falsy: ' + (typeof secret === 'string' ? '(empty string)' : secret), function(done) {
85+
it('should return an error if the secret is falsy and algorithm is not set to none: ' + (typeof secret === 'string' ? '(empty string)' : secret), function(done) {
7086
// This is needed since jws will not answer for falsy secrets
7187
jwt.sign('string', secret, {}, function(err, token) {
7288
expect(err).to.be.exist();

0 commit comments

Comments
 (0)