Skip to content
Myr

Merge commit from fork

Before adoption: this commit came in with the history the project adopted, so no landing records it.

authored by Taku Amano · committed by GitHub · parent c657a39 · Browse files · 4 files+169 added−28 removed
This commit

State

Before adoption: this commit came in with the history the project adopted, so no landing records it.

Diff stats

4 files+169 added−28 removed

  • tests:+167 added−26 removed
  • other:+2 added−2 removed

4 files+169 added−28 removed

modified:src/middleware/jwk/index.test.ts+101 added-18 removed
78 unmodified lines
79 expect(await res.json()).toEqual({ message: 'hello world' })80 expect(handlerExecuted).toBeTruthy()81 })8283 it('Should not authorize if bad token is present', async () => {83 it('Should not authorize if a non-Bearer scheme is present', async () => {84 const credential = await Jwt.sign({ message: 'hello world' }, test_keys.private_keys[0])85 const url = 'http://localhost/backend-auth-or-anon/a'86 const req = new Request(url)87 req.headers.set('Authorization', `Basic ${credential}`)88 const res = await app.request(req)89 expect(res).not.toBeNull()90 expect(res.status).toBe(401)91 expect(res.headers.get('www-authenticate')).toEqual(92 `Bearer realm="${url}",error="invalid_request",error_description="invalid credentials structure"`93 )94 expect(handlerExecuted).toBeFalsy()95 })9697 it('Should not authorize if an invalid Bearer token is present', async () => {98 const invalidToken =99 'ssyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJtZXNzYWdlIjoiaGVsbG8gd29ybGQifQ.B54pAqIiLbu170tGQ1rY06Twv__0qSHTA0ioQPIOvFE'100 const url = 'http://localhost/backend-auth-or-anon/a'101 const req = new Request(url)88 req.headers.set('Authorization', `Basic ${invalidToken}`)102 req.headers.set('Authorization', `Bearer ${invalidToken}`)103 const res = await app.request(req)104 expect(res).not.toBeNull()105 expect(res.status).toBe(401)106 expect(res.headers.get('www-authenticate')).toEqual(
132 unmodified lines
239240 it('Should return a server error if options.jwks_uri returns a 404', async () => {241 const credential = await Jwt.sign({ message: 'hello world' }, test_keys.private_keys[0])242 const req = new Request('http://localhost/auth-with-404-jwks_uri/a')229 req.headers.set('Authorization', `Basic ${credential}`)243 req.headers.set('Authorization', `Bearer ${credential}`)244 const res = await app.request(req)245 expect(res).not.toBeNull()246 expect(res.status).toBe(500)247 })248249 it('Should return a server error if the remotely fetched keys from options.jwks_uri are missing', async () => {250 const credential = await Jwt.sign({ message: 'hello world' }, test_keys.private_keys[0])251 const req = new Request('http://localhost/auth-with-missing-jwks_uri/a')238 req.headers.set('Authorization', `Basic ${credential}`)252 req.headers.set('Authorization', `Bearer ${credential}`)253 const res = await app.request(req)254 expect(res).not.toBeNull()255 expect(res.status).toBe(500)256 })257258 it('Should return a server error if the remotely fetched keys from options.jwks_uri are malformed', async () => {259 const credential = await Jwt.sign({ message: 'hello world' }, test_keys.private_keys[0])260 const req = new Request('http://localhost/auth-with-bad-jwks_uri/a')247 req.headers.set('Authorization', `Basic ${credential}`)261 req.headers.set('Authorization', `Bearer ${credential}`)262 const res = await app.request(req)263 expect(res).not.toBeNull()264 expect(res.status).toBe(500)265 })
99 unmodified lines
365366 it('Should authorize with Unicode payload from a static array passed to options.keys', async () => {367 const credential = await Jwt.sign({ message: 'hello world' }, test_keys.private_keys[0])368 const req = new Request('http://localhost/auth-with-keys-unicode/a')355 req.headers.set('Authorization', `Basic ${credential}`)369 req.headers.set('Authorization', `bearer ${credential}`)370 const res = await app.request(req)371 expect(res).not.toBeNull()372 expect(res.status).toBe(200)373 expect(await res.json()).toEqual({ message: 'hello world' })374 expect(handlerExecuted).toBeTruthy()375 })376377 it('Should authorize with mixed-case Bearer scheme', async () => {378 const credential = await Jwt.sign({ message: 'hello world' }, test_keys.private_keys[0])379 const req = new Request('http://localhost/auth-with-keys-unicode/a')380 req.headers.set('Authorization', `bEaReR ${credential}`)381 const res = await app.request(req)382 expect(res).not.toBeNull()383 expect(res.status).toBe(200)384 expect(await res.json()).toEqual({ message: 'hello world' })385 expect(handlerExecuted).toBeTruthy()386 })387388 it('Should authorize from a function passed to options.keys', async () => {389 const credential = await Jwt.sign({ message: 'hello world' }, test_keys.private_keys[0])390 const req = new Request('http://localhost/auth-with-keys-fn/a')366 req.headers.set('Authorization', `Basic ${credential}`)391 req.headers.set('Authorization', `Bearer ${credential}`)392 const res = await app.request(req)393 expect(res).not.toBeNull()394 expect(res.status).toBe(200)395 expect(await res.json()).toEqual({ message: 'hello world' })
2 unmodified lines
398399 it('Should authorize from keys remotely fetched from options.jwks_uri', async () => {400 const credential = await Jwt.sign({ message: 'hello world' }, test_keys.private_keys[0])401 const req = new Request('http://localhost/auth-with-jwks_uri/a')377 req.headers.set('Authorization', `Basic ${credential}`)402 req.headers.set('Authorization', `Bearer ${credential}`)403 const res = await app.request(req)404 expect(res).not.toBeNull()405 expect(res.status).toBe(200)406 expect(await res.json()).toEqual({ message: 'hello world' })
2 unmodified lines
409410 it('Should authorize from keys and hard-coded and remotely fetched from options.jwks_uri', async () => {411 const credential = await Jwt.sign({ message: 'hello world' }, test_keys.private_keys[0])412 const req = new Request('http://localhost/auth-with-keys-and-jwks_uri/a')388 req.headers.set('Authorization', `Basic ${credential}`)413 req.headers.set('Authorization', `Bearer ${credential}`)414 const res = await app.request(req)415 expect(res).not.toBeNull()416 expect(res.status).toBe(200)417 expect(await res.json()).toEqual({ message: 'hello world' })418 expect(handlerExecuted).toBeTruthy()419 })420396 it('Should not authorize requests with invalid Unicode payload in header', async () => {421 it('Should not authorize requests with non-Bearer scheme in header', async () => {422 const credential = await Jwt.sign({ message: 'hello world' }, test_keys.private_keys[0])423 const url = 'http://localhost/auth-with-keys-unicode/a'424 const req = new Request(url)425 req.headers.set('Authorization', `Basic ${credential}`)426 const res = await app.request(req)427 expect(res).not.toBeNull()428 expect(res.status).toBe(401)429 expect(res.headers.get('www-authenticate')).toEqual(430 `Bearer realm="${url}",error="invalid_request",error_description="invalid credentials structure"`431 )432 expect(handlerExecuted).toBeFalsy()433 })434435 it('Should not authorize requests with invalid token in Bearer header', async () => {436 const invalidToken =437 'ssyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJtZXNzYWdlIjoiaGVsbG8gd29ybGQifQ.B54pAqIiLbu170tGQ1rY06Twv__0qSHTA0ioQPIOvFE'438439 const url = 'http://localhost/auth-with-keys-unicode/a'440 const req = new Request(url)401 req.headers.set('Authorization', `Basic ${invalidToken}`)441 req.headers.set('Authorization', `Bearer ${invalidToken}`)442 const res = await app.request(req)443 expect(res).not.toBeNull()444 expect(res.status).toBe(401)445 expect(res.headers.get('www-authenticate')).toEqual(
88 unmodified lines
534 expect(res.status).toBe(200)535 expect(await res.json()).toEqual({ message: 'hello world' })536 expect(handlerExecuted).toBeTruthy()537 })538539 it('Should authorize with mixed-case Bearer scheme in custom header', async () => {540 const credential = await Jwt.sign({ message: 'hello world' }, test_keys.private_keys[1])541542 const req = new Request('http://localhost/auth-with-keys/a')543 req.headers.set('x-custom-auth-header', `bEaReR ${credential}`)544 const res = await app.request(req)545 expect(res).not.toBeNull()546 expect(res.status).toBe(200)547 expect(await res.json()).toEqual({ message: 'hello world' })548 expect(handlerExecuted).toBeTruthy()549 })550551 it('Should not authorize with non-Bearer scheme in custom header', async () => {552 const credential = await Jwt.sign({ message: 'hello world' }, test_keys.private_keys[1])553 const url = 'http://localhost/auth-with-keys/a'554555 const req = new Request(url)556 req.headers.set('x-custom-auth-header', `Basic ${credential}`)557 const res = await app.request(req)558 expect(res).not.toBeNull()559 expect(res.status).toBe(401)560 expect(res.headers.get('www-authenticate')).toEqual(561 `Bearer realm="${url}",error="invalid_request",error_description="invalid credentials structure"`562 )563 expect(handlerExecuted).toBeFalsy()564 })565566 it('Should not authorize invalid token with Bearer scheme in custom header', async () => {567 const invalidToken =568 'ssyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJtZXNzYWdlIjoiaGVsbG8gd29ybGQifQ.B54pAqIiLbu170tGQ1rY06Twv__0qSHTA0ioQPIOvFE'569 const url = 'http://localhost/auth-with-keys/a'570571 const req = new Request(url)572 req.headers.set('x-custom-auth-header', `Bearer ${invalidToken}`)573 const res = await app.request(req)574 expect(res).not.toBeNull()575 expect(res.status).toBe(401)576 expect(res.headers.get('www-authenticate')).toEqual(577 `Bearer realm="${url}",error="invalid_token",error_description="token verification failure"`578 )579 expect(handlerExecuted).toBeFalsy()580 })581 })582583 describe('Credentials in cookie', () => {584 let handlerExecuted: boolean
308 unmodified lines
893 iss: 'http://not-checked.test',894 }895 const credential = await Jwt.sign(payload, test_keys.private_keys[0])896 const req = new Request('http://localhost/auth-with-keys-default/a')814 req.headers.set('Authorization', `Basic ${credential}`)897 req.headers.set('Authorization', `Bearer ${credential}`)898 const res = await app.request(req)899 expect(res).not.toBeNull()900 expect(res.status).toBe(200)901 expect(await res.json()).toEqual(payload)
5 unmodified lines
907 exp: inPast(),908 }909 const credential = await Jwt.sign(payload, test_keys.private_keys[0])910 const req = new Request('http://localhost/auth-with-keys-default/a')828 req.headers.set('Authorization', `Basic ${credential}`)911 req.headers.set('Authorization', `Bearer ${credential}`)912 const res = await app.request(req)913 expect(res).not.toBeNull()914 expect(res.status).toBe(401)915 expect(handlerExecuted).toBeFalsy()
4 unmodified lines
920 nbf: inFuture(),921 }922 const credential = await Jwt.sign(payload, test_keys.private_keys[0])923 const req = new Request('http://localhost/auth-with-keys-default/a')841 req.headers.set('Authorization', `Basic ${credential}`)924 req.headers.set('Authorization', `Bearer ${credential}`)925 const res = await app.request(req)926 expect(res).not.toBeNull()927 expect(res.status).toBe(401)928 expect(handlerExecuted).toBeFalsy()
4 unmodified lines
933 iat: inFuture(),934 }935 const credential = await Jwt.sign(payload, test_keys.private_keys[0])936 const req = new Request('http://localhost/auth-with-keys-default/a')854 req.headers.set('Authorization', `Basic ${credential}`)937 req.headers.set('Authorization', `Bearer ${credential}`)938 const res = await app.request(req)939 expect(res).not.toBeNull()940 expect(res.status).toBe(401)941 expect(handlerExecuted).toBeFalsy()
4 unmodified lines
946 iss: 'http://issuer.test',947 }948 const credential = await Jwt.sign(payload, test_keys.private_keys[0])949 const req = new Request('http://localhost/auth-with-keys-and-issuer/a')867 req.headers.set('Authorization', `Basic ${credential}`)950 req.headers.set('Authorization', `Bearer ${credential}`)951 const res = await app.request(req)952 expect(res).not.toBeNull()953 expect(res.status).toBe(200)954 expect(await res.json()).toEqual(payload)
5 unmodified lines
960 // Nothing961 }962 const credential = await Jwt.sign(payload, test_keys.private_keys[0])963 const req = new Request('http://localhost/auth-with-keys-and-issuer/a')881 req.headers.set('Authorization', `Basic ${credential}`)964 req.headers.set('Authorization', `Bearer ${credential}`)965 const res = await app.request(req)966 expect(res).not.toBeNull()967 expect(res.status).toBe(401)968 expect(handlerExecuted).toBeFalsy()
4 unmodified lines
973 iss: 'http://bad-issuer.test',974 }975 const credential = await Jwt.sign(payload, test_keys.private_keys[0])976 const req = new Request('http://localhost/auth-with-keys-and-issuer/a')894 req.headers.set('Authorization', `Basic ${credential}`)977 req.headers.set('Authorization', `Bearer ${credential}`)978 const res = await app.request(req)979 expect(res).not.toBeNull()980 expect(res.status).toBe(401)981 expect(handlerExecuted).toBeFalsy()
99 unmodified lines
modified:src/middleware/jwk/jwk.ts+1 added-1 removed
modified:src/middleware/jwt/index.test.ts+66 added-8 removed
56 unmodified lines
57 expect(await res.json()).toEqual({ message: 'hello world' })58 expect(handlerExecuted).toBeTruthy()59 })6061 it('Should authorize Unicode', async () => {61 it('Should authorize with lowercase bearer scheme', async () => {62 const credential =63 'eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJtZXNzYWdlIjoiaGVsbG8gd29ybGQifQ.B54pAqIiLbu170tGQ1rY06Twv__0qSHTA0ioQPIOvFE'6465 const req = new Request('http://localhost/auth-unicode/a')66 req.headers.set('Authorization', `Basic ${credential}`)66 req.headers.set('Authorization', `bearer ${credential}`)67 const res = await app.request(req)68 expect(res).not.toBeNull()69 expect(res.status).toBe(200)70 expect(await res.json()).toEqual({ message: 'hello world' })71 expect(handlerExecuted).toBeTruthy()72 })7374 it('Should not authorize Unicode', async () => {74 it('Should authorize with mixed-case Bearer scheme', async () => {75 const credential =76 'eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJtZXNzYWdlIjoiaGVsbG8gd29ybGQifQ.B54pAqIiLbu170tGQ1rY06Twv__0qSHTA0ioQPIOvFE'7778 const req = new Request('http://localhost/auth-unicode/a')79 req.headers.set('Authorization', `bEaReR ${credential}`)80 const res = await app.request(req)81 expect(res).not.toBeNull()82 expect(res.status).toBe(200)83 expect(await res.json()).toEqual({ message: 'hello world' })84 expect(handlerExecuted).toBeTruthy()85 })8687 it('Should not authorize with non-Bearer scheme', async () => {88 const credential =89 'eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJtZXNzYWdlIjoiaGVsbG8gd29ybGQifQ.B54pAqIiLbu170tGQ1rY06Twv__0qSHTA0ioQPIOvFE'9091 const url = 'http://localhost/auth-unicode/a'92 const req = new Request(url)93 req.headers.set('Authorization', `Basic ${credential}`)94 const res = await app.request(req)95 expect(res).not.toBeNull()96 expect(res.status).toBe(401)97 expect(res.headers.get('www-authenticate')).toEqual(98 `Bearer realm="${url}",error="invalid_request",error_description="invalid credentials structure"`99 )100 expect(handlerExecuted).toBeFalsy()101 })102103 it('Should not authorize invalid token with Bearer scheme', async () => {104 const invalidToken =105 'ssyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJtZXNzYWdlIjoiaGVsbG8gd29ybGQifQ.B54pAqIiLbu170tGQ1rY06Twv__0qSHTA0ioQPIOvFE'106107 const url = 'http://localhost/auth-unicode/a'108 const req = new Request(url)80 req.headers.set('Authorization', `Basic ${invalidToken}`)109 req.headers.set('Authorization', `Bearer ${invalidToken}`)110 const res = await app.request(req)111 expect(res).not.toBeNull()112 expect(res.status).toBe(401)113 expect(res.headers.get('www-authenticate')).toEqual(
108 unmodified lines
222 expect(await res.json()).toEqual({ message: 'hello world' })223 expect(handlerExecuted).toBeTruthy()224 })225197 it('Should authorize Unicode', async () => {226 it('Should authorize with lowercase bearer scheme', async () => {227 const credential =228 'eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJtZXNzYWdlIjoiaGVsbG8gd29ybGQifQ.B54pAqIiLbu170tGQ1rY06Twv__0qSHTA0ioQPIOvFE'229230 const req = new Request('http://localhost/auth-unicode/a')202 req.headers.set('x-custom-auth-header', `Basic ${credential}`)231 req.headers.set('x-custom-auth-header', `bearer ${credential}`)232 const res = await app.request(req)233 expect(res).not.toBeNull()234 expect(res.status).toBe(200)235 expect(await res.json()).toEqual({ message: 'hello world' })236 expect(handlerExecuted).toBeTruthy()237 })238210 it('Should not authorize Unicode', async () => {239 it('Should authorize with mixed-case Bearer scheme in custom header', async () => {240 const credential =241 'eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJtZXNzYWdlIjoiaGVsbG8gd29ybGQifQ.B54pAqIiLbu170tGQ1rY06Twv__0qSHTA0ioQPIOvFE'242243 const req = new Request('http://localhost/auth-unicode/a')244 req.headers.set('x-custom-auth-header', `bEaReR ${credential}`)245 const res = await app.request(req)246 expect(res).not.toBeNull()247 expect(res.status).toBe(200)248 expect(await res.json()).toEqual({ message: 'hello world' })249 expect(handlerExecuted).toBeTruthy()250 })251252 it('Should not authorize with non-Bearer scheme in custom header', async () => {253 const credential =254 'eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJtZXNzYWdlIjoiaGVsbG8gd29ybGQifQ.B54pAqIiLbu170tGQ1rY06Twv__0qSHTA0ioQPIOvFE'255256 const url = 'http://localhost/auth-unicode/a'257 const req = new Request(url)258 req.headers.set('x-custom-auth-header', `Basic ${credential}`)259 const res = await app.request(req)260 expect(res).not.toBeNull()261 expect(res.status).toBe(401)262 expect(res.headers.get('www-authenticate')).toEqual(263 `Bearer realm="${url}",error="invalid_request",error_description="invalid credentials structure"`264 )265 expect(handlerExecuted).toBeFalsy()266 })267268 it('Should not authorize invalid token with Bearer scheme in custom header', async () => {269 const invalidToken =270 'ssyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJtZXNzYWdlIjoiaGVsbG8gd29ybGQifQ.B54pAqIiLbu170tGQ1rY06Twv__0qSHTA0ioQPIOvFE'271272 const url = 'http://localhost/auth-unicode/a'273 const req = new Request(url)216 req.headers.set('x-custom-auth-header', `Basic ${invalidToken}`)274 req.headers.set('x-custom-auth-header', `Bearer ${invalidToken}`)275 const res = await app.request(req)276 expect(res).not.toBeNull()277 expect(res.status).toBe(401)278 expect(res.headers.get('www-authenticate')).toEqual(
469 unmodified lines
modified:src/middleware/jwt/jwt.ts+1 added-1 removed
78 unmodified lines
79 const credentials = ctx.req.raw.headers.get(headerName)80 let token81 if (credentials) {82 const parts = credentials.split(/\s+/)83 if (parts.length !== 2) {83 if (parts.length !== 2 || parts[0].toLowerCase() !== 'bearer') {84 const errDescription = 'invalid credentials structure'85 throw new HTTPException(401, {86 message: errDescription,87 res: unauthorizedResponse({
91 unmodified lines