-
-
Notifications
You must be signed in to change notification settings - Fork 100
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: Async key provider and errors should be resolved internally #335
base: master
Are you sure you want to change the base?
Changes from all commits
8a83879
89a4964
2046ff5
04288be
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,130 @@ | ||
'use strict' | ||
|
||
const test = require('tap').test | ||
const Fastify = require('fastify') | ||
const jwt = require('../jwt') | ||
|
||
test('Async key provider should be resolved internally', async function (t) { | ||
const fastify = Fastify() | ||
fastify.register(jwt, { | ||
secret: { | ||
private: 'supersecret', | ||
public: async () => Promise.resolve('supersecret') | ||
}, | ||
verify: { | ||
extractToken: (request) => request.headers.jwt, | ||
key: () => Promise.resolve('supersecret') | ||
} | ||
}) | ||
fastify.get('/', async function (request, reply) { | ||
const token = await reply.jwtSign({ user: 'test' }) | ||
request.headers.jwt = token | ||
await request.jwtVerify() | ||
return reply.send(request.user) | ||
}) | ||
const response = await fastify.inject({ | ||
method: 'get', | ||
url: '/', | ||
headers: { | ||
jwt: 'supersecret' | ||
} | ||
}) | ||
t.ok(response) | ||
t.comment("Should be 'undefined'") | ||
t.match(response.json(), { user: 'test' }) | ||
}) | ||
|
||
test('Async key provider errors should be resolved internally', async function (t) { | ||
const fastify = Fastify() | ||
fastify.register(jwt, { | ||
secret: { | ||
public: async () => Promise.resolve('key used per request, false not allowed') | ||
}, | ||
verify: { | ||
extractToken: (request) => request.headers.jwt, | ||
key: () => Promise.resolve('key not used') | ||
} | ||
}) | ||
fastify.get('/', async function (request, reply) { | ||
request.headers.jwt = | ||
'eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJzdWIiOiIxMjM0NTY3ODkwIiwibmFtZSI6IkpvaG4gRG9lIiwiaWF0IjoxNTE2MjM5MDIyfQ.SflKxwRJSMeKKF2QT4fwpMeJf36POk6yJV_adQssw5c' | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. please generate this dynamically in code. |
||
// call to local verifier without cache | ||
await request.jwtVerify() | ||
return reply.send(typeof request.user.then) | ||
}) | ||
const response = await fastify.inject({ | ||
method: 'get', | ||
url: '/' | ||
}) | ||
|
||
t.equal(response.statusCode, 401) | ||
}) | ||
|
||
test('Async key provider should be resolved internally with cache', async function (t) { | ||
const fastify = Fastify() | ||
fastify.register(jwt, { | ||
secret: { | ||
public: async () => false | ||
}, | ||
verify: { | ||
extractToken: (request) => request.headers.jwt, | ||
key: () => Promise.resolve('this secret reused from cache') | ||
} | ||
}) | ||
fastify.get('/', async function (request, reply) { | ||
request.headers.jwt = 'eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJzdWIiOiIxMjM0NTY3ODkwIiwibmFtZSI6IkpvaG4gRG9lIiwiaWF0IjoxNTE2MjM5MDIyfQ.hQOxra1Zo9z61vCqe6_86kVfLqKI0WxDnkiJ_upW0sM' | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Hardcoded There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. please generate this dynamically in code. |
||
await new Promise((resolve, reject) => request.jwtVerify((err, payload) => { | ||
if (err) { | ||
reject(err) | ||
return | ||
} | ||
resolve(payload) | ||
})) | ||
await new Promise((resolve, reject) => request.jwtVerify((err, payload) => { | ||
if (err) { | ||
reject(err) | ||
return | ||
} | ||
resolve(payload) | ||
})) | ||
return reply.send(request.user) | ||
}) | ||
mcollina marked this conversation as resolved.
Show resolved
Hide resolved
|
||
const response = await fastify.inject({ | ||
method: 'get', | ||
url: '/' | ||
}) | ||
t.equal(response.statusCode, 200) | ||
t.match(response.json(), { name: 'John Doe' }) | ||
}) | ||
|
||
test('Async key provider errors should be resolved internally with cache', async function (t) { | ||
const fastify = Fastify() | ||
fastify.register(jwt, { | ||
secret: { | ||
public: async () => false | ||
}, | ||
verify: { | ||
extractToken: (request) => request.headers.jwt, | ||
key: () => Promise.resolve('this secret reused from cache') | ||
} | ||
}) | ||
fastify.get('/', async function (request, reply) { | ||
request.headers.jwt = | ||
'eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJzdWIiOiIxMjM0NTY3ODkwIiwibmFtZSI6IkpvaG4gRG9lIiwiaWF0IjoxNTE2MjM5MDIyfQ.SflKxwRJSMeKKF2QT4fwpMeJf36POk6yJV_adQssw5c' | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. please generate this dynamically in code. |
||
// call to plugin root level verifier | ||
await new Promise((resolve, reject) => request.jwtVerify((err, payload) => { | ||
if (err) { | ||
reject(err) | ||
return | ||
} | ||
resolve(payload) | ||
})) | ||
return reply.send(typeof request.user.then) | ||
}) | ||
const response = await fastify.inject({ | ||
method: 'get', | ||
url: '/' | ||
}) | ||
|
||
t.equal(response.statusCode, 401) | ||
}) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
verifyResult
moved in order to support promise based result.