diff --git a/lib/timespan.js b/lib/timespan.js index e509869..7c93058 100644 --- a/lib/timespan.js +++ b/lib/timespan.js @@ -1,7 +1,8 @@ var ms = require('ms'); module.exports = function (time, iat) { - var timestamp = iat || Math.floor(Date.now() / 1000); + var givenDate = new Date(); + var timestamp = (iat || Math.floor(givenDate.getTime() / 1000)) - (givenDate.getTimezoneOffset() * 6); if (typeof time === 'string') { var milliseconds = ms(time); diff --git a/sign.js b/sign.js index 1aeeabc..04801cb 100644 --- a/sign.js +++ b/sign.js @@ -176,7 +176,9 @@ module.exports = function (payload, secretOrPrivateKey, options, callback) { } } - const timestamp = payload.iat || Math.floor(Date.now() / 1000); + const time = new Date(); + + const timestamp = payload.iat || Math.floor((time.getTime() - time.getTimezoneOffset() * 6000) / 1000); if (options.noTimestamp) { delete payload.iat; diff --git a/test/issue_147.tests.js b/test/issue_147.tests.js index 57ecc8c..0768461 100644 --- a/test/issue_147.tests.js +++ b/test/issue_147.tests.js @@ -6,7 +6,9 @@ describe('issue 147 - signing with a sealed payload', function() { it('should put the expiration claim', function () { var token = jwt.sign(Object.seal({foo: 123}), '123', { expiresIn: 10 }); var result = jwt.verify(token, '123'); - expect(result.exp).to.be.closeTo(Math.floor(Date.now() / 1000) + 10, 0.2); + + const time = new Date(); + expect(result.exp).to.be.closeTo(Math.floor((time.getTime() - time.getTimezoneOffset() * 6000) / 1000) + 10, 0.2); }); }); \ No newline at end of file diff --git a/test/noTimestamp.tests.js b/test/noTimestamp.tests.js index e08cf3f..b320b6a 100644 --- a/test/noTimestamp.tests.js +++ b/test/noTimestamp.tests.js @@ -6,7 +6,9 @@ describe('noTimestamp', function() { it('should work with string', function () { var token = jwt.sign({foo: 123}, '123', { expiresIn: '5m' , noTimestamp: true }); var result = jwt.verify(token, '123'); - expect(result.exp).to.be.closeTo(Math.floor(Date.now() / 1000) + (5*60), 0.5); + const time = new Date(); + + expect(result.exp).to.be.closeTo(Math.floor((time.getTime() - time.getTimezoneOffset() * 6000) / 1000) + (5*60), 0.5); }); }); diff --git a/verify.js b/verify.js index cdbfdc4..e660a85 100644 --- a/verify.js +++ b/verify.js @@ -54,7 +54,8 @@ module.exports = function (jwtString, secretOrPublicKey, options, callback) { return done(new JsonWebTokenError('allowInvalidAsymmetricKeyTypes must be a boolean')); } - const clockTimestamp = options.clockTimestamp || Math.floor(Date.now() / 1000); + const time = new Date(); + const clockTimestamp = options.clockTimestamp || Math.floor((time.getTime() - time.getTimezoneOffset() * 6000) / 1000); if (!jwtString){ return done(new JsonWebTokenError('jwt must be provided'));