Skip to content
Merged
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
2 changes: 1 addition & 1 deletion src/core/Jwt.js
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ class Jwt {
}

get expired () {
return Date.now() > this.expiresAt;
return Math.round(Date.now() / 1000) > this.expiresAt;
}

_decode () {
Expand Down
4 changes: 2 additions & 2 deletions test/core/Jwt.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ describe('Jwt', () => {
describe('#constructor', () => {
it('should construct a Jwt instance and decode the payload', () => {
const
expiresAt = Date.now() + 3600 * 1000,
expiresAt = Math.round(Date.now() / 1000) + 3600 * 1000,
encodedJwt = generateJwt('user-gordon', expiresAt);

authenticationToken = new Jwt(encodedJwt);
Expand All @@ -41,7 +41,7 @@ describe('Jwt', () => {

describe('#get expired', () => {
it('should return a boolean according to expiresAt', () => {
const encodedJwt = generateJwt('user-gordon', Date.now() - 1000);
const encodedJwt = generateJwt('user-gordon', Math.round(Date.now() / 1000) - 1000);

authenticationToken = new Jwt(encodedJwt);

Expand Down
4 changes: 2 additions & 2 deletions test/mocks/generateJwt.mock.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
function generateJwt (userId = 'test-user', expiresAt = null) {
const
header = { alg: 'HS256', typ: 'JWT' },
payload = { _id: userId, iat: Date.now(), exp: expiresAt },
payload = { _id: userId, iat: Math.round(Date.now() / 1000), exp: expiresAt },
signature = 'who cares?';

expiresAt = expiresAt || Date.now() + 3600 * 1000;
expiresAt = expiresAt || Math.round(Date.now() / 1000) + 3600 * 1000;

return [ header, payload, signature]
.map(data => Buffer.from(JSON.stringify(data)).toString('base64'))
Expand Down