Skip to content
Myr

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

2 files+36 added−8 removed

modified:src/middleware/basic-auth/index.test.ts+29 added
modified:src/middleware/basic-auth/index.ts+7 added-8 removed
46 unmodified lines
47 users.unshift({ username: options.username, password: options.password })4849 return async (ctx: Context, next: Next) => {50 const requestUser = auth(ctx.req)5151 if (requestUser) {52 for (const user of users) {53 const usernameEqual = await timingSafeEqual(54 user.username,
7 unmodified lines
62 )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}