From 47ae96e0a2c62642b428759c9f4926a944f973c8 Mon Sep 17 00:00:00 2001 From: Pato Istvan Date: Thu, 8 Sep 2016 12:28:45 +0200 Subject: [PATCH 1/2] handling exp default value, if exp comes from claims --- index.js | 4 ++-- test/builder.js | 9 ++++++--- 2 files changed, 8 insertions(+), 5 deletions(-) diff --git a/index.js b/index.js index cbb0acb..1d78836 100644 --- a/index.js +++ b/index.js @@ -431,9 +431,9 @@ var jwtLib = { jwt.setSigningAlgorithm(args.length===3 ? alg : 'HS256'); jwt.setSigningKey(secret); } - jwt.setExpiration((nowEpochSeconds() + (60*60))*1000); // one hour + jwt.setExpiration(claims.exp || (nowEpochSeconds() + (60 * 60)) * 1000); return jwt; } }; -module.exports = jwtLib; \ No newline at end of file +module.exports = jwtLib; diff --git a/test/builder.js b/test/builder.js index c1a20d2..58b2d20 100644 --- a/test/builder.js +++ b/test/builder.js @@ -66,6 +66,12 @@ describe('create()',function(){ assert.equal(nJwt.create({},uuid()).body.exp , oneHourFromNow); }); + it('should not create default exp field, if exp is given',function(){ + var time = 1473333333; + var token = nJwt.create({exp: time},uuid()) + assert.equal(token.body.exp , Math.floor(time/1000)); + }); + it('should not overwrite a defined jti field',function(){ assert.equal(nJwt.create({jti: 1},uuid()).body.jti , 1); }); @@ -108,6 +114,3 @@ describe('base64 URL Encoding',function(){ }); }); - - - From 23f0028e7c3bd15e8035e26b9721ceb068d82c1a Mon Sep 17 00:00:00 2001 From: Pato Istvan Date: Thu, 8 Sep 2016 14:27:36 +0200 Subject: [PATCH 2/2] claims.exp is seconds not ms --- index.js | 2 +- test/builder.js | 4 ++-- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/index.js b/index.js index 1d78836..c19c73f 100644 --- a/index.js +++ b/index.js @@ -431,7 +431,7 @@ var jwtLib = { jwt.setSigningAlgorithm(args.length===3 ? alg : 'HS256'); jwt.setSigningKey(secret); } - jwt.setExpiration(claims.exp || (nowEpochSeconds() + (60 * 60)) * 1000); + jwt.setExpiration(claims.exp * 1000 || (nowEpochSeconds() + (60 * 60)) * 1000); return jwt; } }; diff --git a/test/builder.js b/test/builder.js index 58b2d20..c712d5a 100644 --- a/test/builder.js +++ b/test/builder.js @@ -67,9 +67,9 @@ describe('create()',function(){ }); it('should not create default exp field, if exp is given',function(){ - var time = 1473333333; + var time = Math.floor(Date.now()/1000) var token = nJwt.create({exp: time},uuid()) - assert.equal(token.body.exp , Math.floor(time/1000)); + assert.equal(token.body.exp , time); }); it('should not overwrite a defined jti field',function(){