Skip to content
Myr

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

6 files+27 added−18 removed

modified:deno_dist/middleware/basic-auth/index.ts+1 added-1 removed
modified:deno_dist/middleware/bearer-auth/index.ts+1 added-4 removed
modified:src/middleware/basic-auth/index.test.ts+12 added-5 removed
28 unmodified lines
29 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 })3839 app.use(40 '/auth-unicode/*',41 basicAuth({
29 unmodified lines
71 const auth = basicAuth({ username: username, password: password })72 await auth(c, next)73 })7470 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'))7975 app.get('/nested/*', () => new Response('nested'))80 app.get('/nested/*', (c) => c.text('nested'))8182 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 })9091 it('Should authorize', async () => {92 const credential = Buffer.from(username + ':' + password).toString('base64')
3 unmodified lines
96 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 })102103 it('Should authorize Unicode', async () => {104 const credential = Buffer.from(username + ':' + unicodePassword).toString('base64')
61 unmodified lines
modified:src/middleware/basic-auth/index.ts+1 added-1 removed
modified:src/middleware/bearer-auth/index.test.ts+11 added-3 removed
5 unmodified lines
67 const token = 'abcdefg12345-._~+/='89 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'))1516 app.use('/authBot/*', bearerAuth({ token, prefix: 'Bot' }))17 app.get('/authBot/*', (c) => c.text('auth bot'))1812 app.get('/auth/*', () => new Response('auth'))13 app.get('/authBot/*', () => new Response('auth bot'))1419 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 })2829 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 })3738 it('Should not authorize - invalid request', async () => {39 const req = new Request('http://localhost/auth/a')
1 unmodified line
41 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 })4748 it('Should not authorize - invalid token', async () => {49 const req = new Request('http://localhost/auth/a')
1 unmodified line
51 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 })5758 it('Should authorize', async () => {59 const req = new Request('http://localhost/authBot/a')
16 unmodified lines
modified:src/middleware/bearer-auth/index.ts+1 added-4 removed
53 unmodified lines
54 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}