add clock tolerance
This commit is contained in:
@@ -119,4 +119,30 @@ describe.each(Object.entries(data) as [JwtAlgorithm, Dataset][])('%s', (algorith
|
||||
const verified = await jwt.verify(token, data.public, algorithm)
|
||||
expect(verified).toBeTruthy()
|
||||
})
|
||||
})
|
||||
|
||||
describe('Verify', async () => {
|
||||
const secret = 'super-secret'
|
||||
|
||||
const now = Math.floor(Date.now() / 1000)
|
||||
const off = 30 // 30 seconds
|
||||
const nbf = now + off // Not valid before 30 seconds from now
|
||||
const exp = now - off // Expired 30 seconds ago
|
||||
|
||||
const notYetValidToken = await jwt.sign({ sub: 'me', nbf }, secret)
|
||||
const expiredToken = await jwt.sign({ sub: 'me', exp }, secret)
|
||||
|
||||
test('Not yet valid', () => {
|
||||
expect(jwt.verify(notYetValidToken, secret, { throwError: true })).rejects.toThrowError('NOT_YET_VALID')
|
||||
})
|
||||
|
||||
test('Expired', () => {
|
||||
console.log({ exp, now: Math.floor(Date.now() / 1000) })
|
||||
expect(jwt.verify(expiredToken, secret, { throwError: true })).rejects.toThrowError('EXPIRED')
|
||||
})
|
||||
|
||||
test('Clock offset', () => {
|
||||
expect(jwt.verify(notYetValidToken, secret, { clockTolerance: off, throwError: true })).resolves.toBe(true)
|
||||
expect(jwt.verify(expiredToken, secret, { clockTolerance: off, throwError: true })).resolves.toBe(true)
|
||||
})
|
||||
})
|
||||
Reference in New Issue
Block a user