fix(middleware): support multiple middleware on bearer/basic auth middleware (#513)
Before adoption: this commit came in with the history the project adopted, so no landing records it.
Fix #509
authored by Yusuke Wada · committed by GitHub · parent 058e311 · Browse files · 6 files·+27 added−18 removed
This commit
State
Before adoption: this commit came in with the history the project adopted, so no landing records it.
Diff stats
6 files+27 added−18 removed
- tests:+23 added−8 removed
- other:+4 added−10 removed
Provenance
- Commit
3697e15- Tree
65a176c- Author
Yusuke Wada <yusuke@kamawada.com>- Committer
GitHub <noreply@github.com>- Parent
058e311
6 files changed+27 added−18 removed
modified:deno_dist/middleware/basic-auth/index.ts+1 added-1 removed
47 unmodified lines4849505152525354553 unmodified lines5960616263646547 unmodified lines requestUser.password, options.hashFunction ) if (usernameEqual && passwordEqual) { // Authorized OK await next() return } }3 unmodified lines headers: { 'WWW-Authenticate': 'Basic realm="' + options.realm?.replace(/"/g, '\\"') + '"', }, }) await next() }}modified:deno_dist/middleware/bearer-auth/index.ts+1 added-4 removed
53 unmodified lines545556575859606158596061626353 unmodified lines headers: { 'WWW-Authenticate': `${options.prefix} error="invalid_token"`, }, }) } else { // Authorize OK await next() return } } } await next() }}modified:src/middleware/basic-auth/index.test.ts+12 added-5 removed
28 unmodified lines29 username,30 password,31 })32 )33+ // Test multiple handlers34+ app.use('/auth/*', async (c, next) => {35+ c.header('x-custom', 'foo')36+ await next()37+ })38 39 app.use(40 '/auth-unicode/*',41 basicAuth({29 unmodified lines71 const auth = basicAuth({ username: username, password: password })72 await auth(c, next)73 })74 70- app.get('/auth/*', () => new Response('auth'))71- app.get('/auth-unicode/*', () => new Response('auth'))72- app.get('/auth-multi/*', () => new Response('auth'))73- app.get('/auth-override-func/*', () => new Response('auth'))75+ app.get('/auth/*', (c) => c.text('auth'))76+ app.get('/auth-unicode/*', (c) => c.text('auth'))77+ app.get('/auth-multi/*', (c) => c.text('auth'))78+ app.get('/auth-override-func/*', (c) => c.text('auth'))79 75- app.get('/nested/*', () => new Response('nested'))80+ app.get('/nested/*', (c) => c.text('nested'))81 82 it('Should not authorize', async () => {83 const req = new Request('http://localhost/auth/a')84 const res = await app.request(req)85 expect(res).not.toBeNull()86 expect(res.status).toBe(401)87 expect(await res.text()).toBe('Unauthorized')88+ expect(res.headers.get('x-custom')).toBe('foo')89 })90 91 it('Should authorize', async () => {92 const credential = Buffer.from(username + ':' + password).toString('base64')3 unmodified lines96 const res = await app.request(req)97 expect(res).not.toBeNull()98 expect(res.status).toBe(200)99 expect(await res.text()).toBe('auth')100+ expect(res.headers.get('x-custom')).toBe('foo')101 })102 103 it('Should authorize Unicode', async () => {104 const credential = Buffer.from(username + ':' + unicodePassword).toString('base64')61 unmodified linesmodified:src/middleware/basic-auth/index.ts+1 added-1 removed
47 unmodified lines4849505152525354553 unmodified lines5960616263646547 unmodified lines requestUser.password, options.hashFunction ) if (usernameEqual && passwordEqual) { // Authorized OK await next() return } }3 unmodified lines headers: { 'WWW-Authenticate': 'Basic realm="' + options.realm?.replace(/"/g, '\\"') + '"', }, }) await next() }}modified:src/middleware/bearer-auth/index.test.ts+11 added-3 removed
5 unmodified lines6 7 const token = 'abcdefg12345-._~+/='8 9 app.use('/auth/*', bearerAuth({ token }))10+ app.use('/auth/*', async (c, next) => {11+ c.header('x-custom', 'foo')12+ await next()13+ })14+ app.get('/auth/*', (c) => c.text('auth'))15+16 app.use('/authBot/*', bearerAuth({ token, prefix: 'Bot' }))17+ app.get('/authBot/*', (c) => c.text('auth bot'))18 12- app.get('/auth/*', () => new Response('auth'))13- app.get('/authBot/*', () => new Response('auth bot'))14-19 it('Should authorize', async () => {20 const req = new Request('http://localhost/auth/a')21 req.headers.set('Authorization', 'Bearer abcdefg12345-._~+/=')22 const res = await app.request(req)23 expect(res).not.toBeNull()24 expect(res.status).toBe(200)25 expect(await res.text()).toBe('auth')26+ expect(res.headers.get('x-custom')).toBe('foo')27 })28 29 it('Should not authorize - no authorization header', async () => {30 const req = new Request('http://localhost/auth/a')31 const res = await app.request(req)32 expect(res).not.toBeNull()33 expect(res.status).toBe(401)34 expect(await res.text()).toBe('Unauthorized')35+ expect(res.headers.get('x-custom')).toBe('foo')36 })37 38 it('Should not authorize - invalid request', async () => {39 const req = new Request('http://localhost/auth/a')1 unmodified line41 const res = await app.request(req)42 expect(res).not.toBeNull()43 expect(res.status).toBe(400)44 expect(await res.text()).toBe('Bad Request')45+ expect(res.headers.get('x-custom')).toBe('foo')46 })47 48 it('Should not authorize - invalid token', async () => {49 const req = new Request('http://localhost/auth/a')1 unmodified line51 const res = await app.request(req)52 expect(res).not.toBeNull()53 expect(res.status).toBe(401)54 expect(await res.text()).toBe('Unauthorized')55+ expect(res.headers.get('x-custom')).toBe('foo')56 })57 58 it('Should authorize', async () => {59 const req = new Request('http://localhost/authBot/a')16 unmodified linesmodified:src/middleware/bearer-auth/index.ts+1 added-4 removed
53 unmodified lines54 headers: {55 'WWW-Authenticate': `${options.prefix} error="invalid_token"`,56 },57 })58- } else {59- // Authorize OK60- await next()61- return58 }59 }60 }61+ await next()62 }63 }