Skip to content

Commit e32043b

Browse files
committed
make payload.exp and options.expiresIn exclusive
1 parent 53a88ec commit e32043b

File tree

3 files changed

+11
-1
lines changed

3 files changed

+11
-1
lines changed

index.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,9 @@ JWT.sign = function(payload, secretOrPrivateKey, options, callback) {
6666
}
6767
}
6868

69+
if (typeof payload.exp !== 'undefined' && typeof options.expiresIn !== 'undefined') {
70+
throw new Error('Bad "options.expiresIn" option the payload already has an "exp" property.');
71+
}
6972

7073
header.alg = options.algorithm || 'HS256';
7174

test/async_sign.tests.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ describe('signing a token asynchronously', function() {
1010

1111
it('should return the same result as singing synchronously', function(done) {
1212
jwt.sign({ foo: 'bar' }, secret, { algorithm: 'HS256' }, function (err, asyncToken) {
13+
if (err) return done(err);
1314
expect(asyncToken).to.be.a('string');
1415
expect(asyncToken.split('.')).to.have.length(3);
1516
expect(asyncToken).to.equal(syncToken);

test/expires_format.tests.js

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,4 +36,10 @@ describe('expires option', function() {
3636
}).to.throw(/"expiresIn" should be a number of seconds or string representing a timespan/);
3737
});
3838

39-
});
39+
it('should throw an error if expiresIn and exp are provided', function () {
40+
expect(function () {
41+
jwt.sign({ foo: 123, exp: 839218392183 }, '123', { expiresIn: '5h' });
42+
}).to.throw(/Bad "options.expiresIn" option the payload already has an "exp" property./);
43+
});
44+
45+
});

0 commit comments

Comments
 (0)