test(jwk): Add tests of JWK token verification (#4402)
Before adoption: this commit came in with the history the project adopted, so no landing records it.
Check that by default we verify the token timestamp attributes and when specified an issuer (iss) is also verified.
authored by Matthew Buckett · committed by GitHub · parent 2f489b3 · Browse files · 1 file·+131 added−0 removed
This commit
State
Before adoption: this commit came in with the history the project adopted, so no landing records it.
Diff stats
1 file+131 added−0 removed
- tests:+131 added−0 removed
Provenance
- Commit
6792789- Tree
2643a2e- Author
Matthew Buckett <buckett@bumph.org>- Committer
GitHub <noreply@github.com>- Parent
2f489b3
1 file changed+131 added−0 removed
modified:src/middleware/jwk/index.test.ts+131 added
741 unmodified lines742 message: `invalid JWT token: ${credential}`,743 })744 })745 })746+747+ describe('Verification of token attributes', () => {748+ let handlerExecuted: boolean749+750+ beforeEach(() => {751+ handlerExecuted = false752+ })753+754+ function inFuture() {755+ return Date.now() / 1000 + 100756+ }757+758+ function inPast() {759+ return Date.now() / 1000 - 100760+ }761+762+ const app = new Hono()763+764+ app.use('/auth-with-keys-default/*', jwk({ keys: verify_keys }))765+ app.use(766+ '/auth-with-keys-and-issuer/*',767+ jwk({ keys: verify_keys, verification: { iss: 'http://issuer.test' } })768+ )769+770+ app.get('/auth-with-keys-default/*', (c) => {771+ handlerExecuted = true772+ const payload = c.get('jwtPayload')773+ return c.json(payload)774+ })775+ app.get('/auth-with-keys-and-issuer/*', (c) => {776+ handlerExecuted = true777+ const payload = c.get('jwtPayload')778+ return c.json(payload)779+ })780+781+ it('Should validate exp/nbf/iat and pass when good by default', async () => {782+ const payload = {783+ exp: inFuture(),784+ nbf: inPast(),785+ iat: inPast(),786+ iss: 'http://not-checked.test',787+ }788+ const credential = await Jwt.sign(payload, test_keys.private_keys[0])789+ const req = new Request('http://localhost/auth-with-keys-default/a')790+ req.headers.set('Authorization', `Basic ${credential}`)791+ const res = await app.request(req)792+ expect(res).not.toBeNull()793+ expect(res.status).toBe(200)794+ expect(await res.json()).toEqual(payload)795+ expect(handlerExecuted).toBeTruthy()796+ })797+798+ it('Should validate exp and fail when bad', async () => {799+ const payload = {800+ exp: inPast(),801+ }802+ const credential = await Jwt.sign(payload, test_keys.private_keys[0])803+ const req = new Request('http://localhost/auth-with-keys-default/a')804+ req.headers.set('Authorization', `Basic ${credential}`)805+ const res = await app.request(req)806+ expect(res).not.toBeNull()807+ expect(res.status).toBe(401)808+ expect(handlerExecuted).toBeFalsy()809+ })810+811+ it('Should validate nbf and fail when bad', async () => {812+ const payload = {813+ nbf: inFuture(),814+ }815+ const credential = await Jwt.sign(payload, test_keys.private_keys[0])816+ const req = new Request('http://localhost/auth-with-keys-default/a')817+ req.headers.set('Authorization', `Basic ${credential}`)818+ const res = await app.request(req)819+ expect(res).not.toBeNull()820+ expect(res.status).toBe(401)821+ expect(handlerExecuted).toBeFalsy()822+ })823+824+ it('Should validate iat and fail when bad', async () => {825+ const payload = {826+ iat: inFuture(),827+ }828+ const credential = await Jwt.sign(payload, test_keys.private_keys[0])829+ const req = new Request('http://localhost/auth-with-keys-default/a')830+ req.headers.set('Authorization', `Basic ${credential}`)831+ const res = await app.request(req)832+ expect(res).not.toBeNull()833+ expect(res.status).toBe(401)834+ expect(handlerExecuted).toBeFalsy()835+ })836+837+ it('Should validate iss when supplied', async () => {838+ const payload = {839+ iss: 'http://issuer.test',840+ }841+ const credential = await Jwt.sign(payload, test_keys.private_keys[0])842+ const req = new Request('http://localhost/auth-with-keys-and-issuer/a')843+ req.headers.set('Authorization', `Basic ${credential}`)844+ const res = await app.request(req)845+ expect(res).not.toBeNull()846+ expect(res.status).toBe(200)847+ expect(await res.json()).toEqual(payload)848+ expect(handlerExecuted).toBeTruthy()849+ })850+851+ it('Should reject missing iss when required', async () => {852+ const payload = {853+ // Nothing854+ }855+ const credential = await Jwt.sign(payload, test_keys.private_keys[0])856+ const req = new Request('http://localhost/auth-with-keys-and-issuer/a')857+ req.headers.set('Authorization', `Basic ${credential}`)858+ const res = await app.request(req)859+ expect(res).not.toBeNull()860+ expect(res.status).toBe(401)861+ expect(handlerExecuted).toBeFalsy()862+ })863+864+ it('Should reject iss when different', async () => {865+ const payload = {866+ iss: 'http://bad-issuer.test',867+ }868+ const credential = await Jwt.sign(payload, test_keys.private_keys[0])869+ const req = new Request('http://localhost/auth-with-keys-and-issuer/a')870+ req.headers.set('Authorization', `Basic ${credential}`)871+ const res = await app.request(req)872+ expect(res).not.toBeNull()873+ expect(res.status).toBe(401)874+ expect(handlerExecuted).toBeFalsy()875+ })876+ })877 })