Skip to content
Myr

feat(basic-auth): add `verifyUser` option (#2450)

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

* feat(basic-auth): add `verifyUser` option * denoify

authored by Yusuke Wada · committed by GitHub · parent 7425ffb · Browse files · 3 files+120 added−20 removed
This commit

State

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

Diff stats

3 files+120 added−20 removed

  • tests:+42 added−0 removed
  • other:+78 added−20 removed

3 files+120 added−20 removed

modified:deno_dist/middleware/basic-auth/index.ts+39 added-10 removed
modified:src/middleware/basic-auth/index.test.ts+42 added
68 unmodified lines
69 const auth = basicAuth({ username: username, password: password })70 return auth(c, next)71 })7273 app.use('/verify-user/*', async (c, next) => {74 const auth = basicAuth({75 verifyUser: (username, password, c) => {76 return (77 c.req.path === '/verify-user' &&78 username === 'dynamic-user' &&79 password === 'hono-password'80 )81 },82 })83 return auth(c, next)84 })8586 app.get('/auth/*', (c) => {87 handlerExecuted = true88 return c.text('auth')89 })
14 unmodified lines
104 handlerExecuted = true105 return c.text('nested')106 })107108 app.get('/verify-user', (c) => {109 handlerExecuted = true110 return c.text('verify-user')111 })112113 it('Should not authorize', async () => {114 const req = new Request('http://localhost/auth/a')115 const res = await app.request(req)116 expect(res).not.toBeNull()
84 unmodified lines
201 expect(res).not.toBeNull()202 expect(res.status).toBe(401)203 expect(await res.text()).toBe('Unauthorized')204 })205206 it('Should authorize - verifyUser', async () => {207 const credential = Buffer.from('dynamic-user' + ':' + 'hono-password').toString('base64')208209 const req = new Request('http://localhost/verify-user')210 req.headers.set('Authorization', `Basic ${credential}`)211 const res = await app.request(req)212 expect(handlerExecuted).toBeTruthy()213 expect(res).not.toBeNull()214 expect(res.status).toBe(200)215 expect(await res.text()).toBe('verify-user')216 })217218 it('Should not authorize - verifyUser', async () => {219 const credential = Buffer.from('foo' + ':' + 'bar').toString('base64')220221 const req = new Request('http://localhost/verify-user')222 req.headers.set('Authorization', `Basic ${credential}`)223 const res = await app.request(req)224 expect(handlerExecuted).toBeFalsy()225 expect(res).not.toBeNull()226 expect(res.status).toBe(401)227 expect(await res.text()).toBe('Unauthorized')228 })229})
modified:src/middleware/basic-auth/index.ts+39 added-10 removed