Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Updated sign, verify & timespan to account timezone. #924

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion lib/timespan.js
Original file line number Diff line number Diff line change
@@ -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);
Expand Down
4 changes: 3 additions & 1 deletion sign.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
4 changes: 3 additions & 1 deletion test/issue_147.tests.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
});

});
4 changes: 3 additions & 1 deletion test/noTimestamp.tests.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
});

});
3 changes: 2 additions & 1 deletion verify.js
Original file line number Diff line number Diff line change
Expand Up @@ -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'));
Expand Down