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/signer.js
Original file line number Diff line number Diff line change
Expand Up @@ -235,7 +235,7 @@ module.exports = function createSigner(options) {
if (typeof expiresIn === 'string') {
expiresIn = parseMs(expiresIn)
}
if (typeof expiresIn !== 'number' || expiresIn < 0) {
if (typeof expiresIn !== 'number') {
throw new TokenError(
TokenError.codes.invalidOption,
'The expiresIn option must be a positive number or a valid string.'
Expand Down
11 changes: 7 additions & 4 deletions test/signer.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -297,6 +297,13 @@ test('it supports expiresIn as a string', async t => {
)
})

test('it supports negative expiresIn value', async t => {
t.assert.equal(
sign({ a: 1, iat: 100 }, { expiresIn: -1 }),
'eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJhIjoxLCJpYXQiOjEwMCwiZXhwIjo5OX0.KqZa0DfwU41PqeDTct_kWCUKXeyQpzJJSZhGd76TR40'
)
})

test('it adds the payload exp claim', async t => {
t.assert.equal(
sign({ a: 1, iat: 100, exp: 200 }, {}),
Expand Down Expand Up @@ -598,10 +605,6 @@ test('options validation - expiresIn', async t => {
t.assert.throws(() => createSigner({ key: 'secret', expiresIn: 'invalid string' }), {
message: 'The expiresIn option must be a positive number or a valid string.'
})

t.assert.throws(() => createSigner({ key: 'secret', expiresIn: -1 }), {
message: 'The expiresIn option must be a positive number or a valid string.'
})
})

test('options validation - notBefore', async t => {
Expand Down
7 changes: 7 additions & 0 deletions test/verifier.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -469,6 +469,13 @@ test('it validates if the token has not expired including the clock tolerance',
)
})

test('rejects token with negative exp', t => {
t.mock.timers.enable({ now: 0 })
const token = createSigner({ key: 'secret', expiresIn: -120000 })({ a: 1 })

t.assert.throws(() => verify(token), { message: 'The token has expired at 1969-12-31T23:58:00.000Z.' })
})

test('it validates the jti claim only if explicitily enabled', t => {
t.mock.timers.enable({ now: 100000 })
const sign = createSigner({ key: 'secret' })
Expand Down
Loading