Skip to content
Myr

fix(bearer-auth): make auth-scheme case-insensitive (#4659)

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

* fix(bearer-auth): make auth-scheme case-insensitive * refactor: adjust test case order

authored by Taesu · committed by GitHub · parent cea7b7b · Browse files · 2 files+22 added−1 removed
This commit

State

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

Diff stats

2 files+22 added−1 removed

  • tests:+21 added−0 removed
  • other:+1 added−1 removed

2 files+22 added−1 removed

modified:src/middleware/bearer-auth/index.test.ts+21 added
448 unmodified lines
449 expect(await res.text()).toBe('auth')450 expect(res.headers.get('x-custom')).toBe('foo')451 })452453 it.each([['bearer'], ['BEARER'], ['BeArEr']])(454 'Should authorize - prefix is case-insensitive: %s',455 async (prefix) => {456 const req = new Request('http://localhost/auth/a')457 req.headers.set('Authorization', `${prefix} ${token}`)458 const res = await app.request(req)459 expect(res).not.toBeNull()460 expect(res.status).toBe(200)461 expect(handlerExecuted).toBeTruthy()462 }463 )464465 it('Should not authorize - no authorization header', async () => {466 const req = new Request('http://localhost/auth/a')467 const res = await app.request(req)468 expect(res).not.toBeNull()
23 unmodified lines
492 expect(await res.text()).toBe('Unauthorized')493 expect(res.headers.get('x-custom')).toBeNull()494 })495496 it('Should not authorize - token is case-sensitive', async () => {497 const req = new Request('http://localhost/auth/a')498 req.headers.set('Authorization', `Bearer ${token.toUpperCase()}`)499 const res = await app.request(req)500 expect(res).not.toBeNull()501 expect(res.status).toBe(401)502 expect(await res.text()).toBe('Unauthorized')503 })504505 it('Should authorize', async () => {506 const req = new Request('http://localhost/authBot/a')507 req.headers.set('Authorization', 'Bot abcdefg12345-._~+/=')508 const res = await app.request(req)
417 unmodified lines
modified:src/middleware/bearer-auth/index.ts+1 added-1 removed
111 unmodified lines
112 }113114 const realm = options.realm?.replace(/"/g, '\\"')115 const prefixRegexStr = options.prefix === '' ? '' : `${options.prefix} +`116 const regexp = new RegExp(`^${prefixRegexStr}(${TOKEN_STRINGS}) *$`)116 const regexp = new RegExp(`^${prefixRegexStr}(${TOKEN_STRINGS}) *$`, 'i')117 const wwwAuthenticatePrefix = options.prefix === '' ? '' : `${options.prefix} `118119 const throwHTTPException = async (120 c: Context,
87 unmodified lines