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

fix: local sign options overwrites global sign options #310

Merged
merged 2 commits into from
Oct 7, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
4 changes: 2 additions & 2 deletions jwt.js
Original file line number Diff line number Diff line change
Expand Up @@ -369,12 +369,12 @@ function fastifyJwt (fastify, options, next) {
const localSignOptions = convertTemporalProps(options.sign)
// New supported contract, options supports sign and can expand
options = {
sign: mergeOptionsWithKey(Object.assign(signOptions, localSignOptions), true)
sign: mergeOptionsWithKey(Object.assign({ ...signOptions }, localSignOptions), true)
mattiapv marked this conversation as resolved.
Show resolved Hide resolved
}
} else {
const localOptions = convertTemporalProps(options)
// Original contract, options supports only sign
options = mergeOptionsWithKey(Object.assign(signOptions, localOptions), true)
options = mergeOptionsWithKey(Object.assign({ ...signOptions }, localOptions), true)
mattiapv marked this conversation as resolved.
Show resolved Hide resolved
}

if (!payload) {
Expand Down
41 changes: 41 additions & 0 deletions test/jwt.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -3008,3 +3008,44 @@ test('decorator name should work after being changed in the options', async func
t.equal(user.baz, undefined)
t.equal(user.foo, 'bar')
})

test('local sign options should not overwrite global sign options', async function (t) {
t.plan(2)

const options = {
secret: 'test',
sign: {
expiresIn: '15m'
}
}

const fastify = Fastify()
fastify.register(jwt, options)

const tokensDifference = 85500

fastify.post('/sign', async function (request, reply) {
const { token, refreshToken } = request.body
const refreshTokenSigned = await reply.jwtSign(refreshToken, { expiresIn: '1d' })
const tokenSigned = await reply.jwtSign(token)
return reply.send({ tokenSigned, refreshTokenSigned })
})

await fastify.ready()

const signResponse = await fastify.inject({
method: 'post',
url: '/sign',
payload: { token: { foo: 'bar' }, refreshToken: { bar: 'foo' } }
})

const token = JSON.parse(signResponse.payload).tokenSigned
const refreshToken = JSON.parse(signResponse.payload).refreshTokenSigned
const decodedToken = fastify.jwt.verify(token)
const decodedRefreshToken = fastify.jwt.verify(refreshToken)
const calculatedDifference = decodedRefreshToken.exp - decodedToken.exp
// max 5 seconds of difference for safety
t.ok(calculatedDifference >= tokensDifference && calculatedDifference <= tokensDifference + 5)

t.equal(fastify.jwt.options.sign.expiresIn, '15m')
})