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

chore: handle invalid token used with RS256 algorithm #10

Merged
merged 1 commit into from
Aug 9, 2023
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
3 changes: 3 additions & 0 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -151,6 +151,9 @@ function getSecret(request, reply, cb) {

// If the algorithm is not using RS256, the encryption key is jwt client secret
if (header.alg.startsWith('HS')) {
if (!request.jwtJwks.secret) {
throw new Unauthorized(errorMessages.invalidAlgorithm)
}
return cb(null, request.jwtJwks.secret)
}

Expand Down
15 changes: 15 additions & 0 deletions test/index.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -702,6 +702,21 @@ describe('RS256 JWT token validation', function () {
})
})

it('should reject an invalid token', async function () {
const response = await server.inject({
method: 'GET',
url: '/verify',
headers: { Authorization: `Bearer ${tokens.hs256Valid}` }
})

expect(response.statusCode).toEqual(401)
expect(response.json()).toEqual({
statusCode: 401,
error: 'Unauthorized',
message: 'Unsupported token.'
})
})

it('should reject a token when is not possible to retrieve the JWK set due to a HTTP error', async function () {
nock.cleanAll()

Expand Down
Loading