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: Allow local options to override signing key #328

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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
34 changes: 34 additions & 0 deletions test/jwt.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -3047,3 +3047,37 @@ test('local sign options should not overwrite global sign options', async functi

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

test('local sign options.key should take effect', async function (t) {
t.plan(1)

const options = {
secret: 'test'
}

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

fastify.post('/sign', async function (request, reply) {
const { token, refreshToken } = request.body
const refreshTokenSigned = await reply.jwtSign(refreshToken, { key: 'something-else' }) // signing by different key
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

fastify.jwt.verify(token, { key: 'test' })
fastify.jwt.verify(refreshToken, { key: 'something-else' }) // should not throw

t.ok(true)
})
Loading