fix: fix basic auth middleware (#227)
Before adoption: this commit came in with the history the project adopted, so no landing records it.
authored by Yusuke Wada · committed by GitHub · parent d3a7df3 · Browse files · 2 files·+36 added−8 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+36 added−8 removed
- tests:+29 added−0 removed
- other:+7 added−8 removed
Provenance
- Commit
62b9238- Tree
d73b01e- Author
Yusuke Wada <yusuke@kamawada.com>- Committer
GitHub <noreply@github.com>- Parent
d3a7df3
2 files changed+36 added−8 removed
modified:src/middleware/basic-auth/index.test.ts+29 added
60 unmodified lines616263646566676869707172737475767778798051 unmodified lines13213313413513613713813914014114214314414514614714814915015115215315415515615715860 unmodified lines hashFunction: (data: string) => SHA256(data).toString(), }) )
app.use('/nested/*', async (c, next) => { const auth = basicAuth({ username: username, password: password }) await auth(c, next) })
app.get('/auth/*', () => new Response('auth')) app.get('/auth-unicode/*', () => new Response('auth')) app.get('/auth-multi/*', () => new Response('auth')) app.get('/auth-override-func/*', () => new Response('auth'))
app.get('/nested/*', () => new Response('nested'))
it('Should not authorize', async () => { const req = new Request('http://localhost/auth/a') const res = await app.request(req) expect(res).not.toBeNull()51 unmodified lines expect(res).not.toBeNull() expect(res.status).toBe(200) expect(await res.text()).toBe('auth') })
it('Should authorize - nested', async () => { const credential = Buffer.from(username + ':' + password).toString('base64')
const req = new Request('http://localhost/nested') req.headers.set('Authorization', `Basic ${credential}`) const res = await app.request(req) expect(res).not.toBeNull() expect(res.status).toBe(200) expect(await res.text()).toBe('nested') })
it('Should not authorize - nested', async () => { const credential = Buffer.from('foo' + ':' + 'bar').toString('base64')
const req = new Request('http://localhost/nested') req.headers.set('Authorization', `Basic ${credential}`) const res = await app.request(req) expect(res).not.toBeNull() expect(res.status).toBe(401) expect(await res.text()).toBe('Unauthorized') })})modified:src/middleware/basic-auth/index.ts+7 added-8 removed
46 unmodified lines47 users.unshift({ username: options.username, password: options.password })48 49 return async (ctx: Context, next: Next) => {50 const requestUser = auth(ctx.req)51-51 if (requestUser) {52 for (const user of users) {53 const usernameEqual = await timingSafeEqual(54 user.username,7 unmodified lines62 )63 if (usernameEqual && passwordEqual) {64 // Authorized OK65 await next()66+ return67 }68 }69- } else {70- ctx.res = new Response('Unauthorized', {71- status: 401,72- headers: {73- 'WWW-Authenticate': 'Basic realm="' + options.realm.replace(/"/g, '\\"') + '"',74- },75- })69 }70+ ctx.res = new Response('Unauthorized', {71+ status: 401,72+ headers: {73+ 'WWW-Authenticate': 'Basic realm="' + options.realm.replace(/"/g, '\\"') + '"',74+ },75+ })76 }77 }