Skip to content

Commit

Permalink
make payload.exp and options.expiresIn exclusive
Browse files Browse the repository at this point in the history
  • Loading branch information
jfromaniello committed Apr 27, 2016
1 parent 53a88ec commit e32043b
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 1 deletion.
3 changes: 3 additions & 0 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,9 @@ JWT.sign = function(payload, secretOrPrivateKey, options, callback) {
}
}

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

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

Expand Down
1 change: 1 addition & 0 deletions test/async_sign.tests.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ describe('signing a token asynchronously', function() {

it('should return the same result as singing synchronously', function(done) {
jwt.sign({ foo: 'bar' }, secret, { algorithm: 'HS256' }, function (err, asyncToken) {
if (err) return done(err);
expect(asyncToken).to.be.a('string');
expect(asyncToken.split('.')).to.have.length(3);
expect(asyncToken).to.equal(syncToken);
Expand Down
8 changes: 7 additions & 1 deletion test/expires_format.tests.js
Original file line number Diff line number Diff line change
Expand Up @@ -36,4 +36,10 @@ describe('expires option', function() {
}).to.throw(/"expiresIn" should be a number of seconds or string representing a timespan/);
});

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

});

0 comments on commit e32043b

Please sign in to comment.