feat: basic auth middleware supports multiple users (#112)
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 2f08c0f · Browse files · 2 files·+68 added−13 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+68 added−13 removed
- tests:+40 added−0 removed
- other:+28 added−13 removed
Provenance
- Commit
2b76987- Tree
ac25509- Author
Yusuke Wada <yusuke@kamawada.com>- Committer
GitHub <noreply@github.com>- Parent
2f08c0f
2 files changed+68 added−13 removed
modified:src/middleware/basic-auth/basic-auth.test.ts+40 added
14 unmodified lines15161718192021222324252627288 unmodified lines3738394041424344454647484950515253545556575859606122 unmodified lines8485868788899091929394959697989910010110210310410510610714 unmodified lines const username = 'hono-user-a' const password = 'hono-password-a' const unicodePassword = '炎'
const usernameB = 'hono-user-b' const passwordB = 'hono-password-b'
const usernameC = 'hono-user-c' const passwordC = 'hono-password-c'
app.use( '/auth/*', basicAuth({ username,8 unmodified lines password: unicodePassword, }) )
app.use( '/auth-multi/*', basicAuth( { username: usernameB, password: passwordB, }, { username: usernameC, password: passwordC, } ) )
app.get('/auth/*', () => new Response('auth')) app.get('/auth-unicode/*', () => new Response('auth')) app.get('/auth-multi/*', () => new Response('auth'))
it('Unauthorized', async () => { const req = new Request('http://localhost/auth/a') const res = await app.dispatch(req)22 unmodified lines expect(res).not.toBeNull() expect(res.status).toBe(200) expect(await res.text()).toBe('auth') })
it('Authorizated multiple users', async () => { let credential = Buffer.from(usernameB + ':' + passwordB).toString('base64')
let req = new Request('http://localhost/auth-multi/b') req.headers.set('Authorization', `Basic ${credential}`) let res = await app.dispatch(req) expect(res).not.toBeNull() expect(res.status).toBe(200) expect(await res.text()).toBe('auth')
credential = Buffer.from(usernameC + ':' + passwordC).toString('base64') req = new Request('http://localhost/auth-multi/c') req.headers.set('Authorization', `Basic ${credential}`) res = await app.dispatch(req) expect(res).not.toBeNull() expect(res.status).toBe(200) expect(await res.text()).toBe('auth') })})modified:src/middleware/basic-auth/basic-auth.ts+28 added-13 removed
29 unmodified lines30313233343435363738394041424344454647484041424950444546474849505151525354555657585960536162636465666768697029 unmodified lines
return { username: userPass[1], password: userPass[2] }}
export const basicAuth = (options: { username: string; password: string; realm?: string }) => {export const basicAuth = ( options: { username: string; password: string; realm?: string }, ...users: { username: string; password: string }[]) => { if (!options) { throw new Error('basic auth middleware requires options for "username and password"') }
if (!options.realm) { options.realm = 'Secure Area' }
users.unshift({ username: options.username, password: options.password })
return async (ctx: Context, next: Function) => { const user = auth(ctx.req) const usernameEqual = user && (await timingSafeEqual(options.username, user.username)) const passwordEqual = user && (await timingSafeEqual(options.password, user.password)) const requestUser = auth(ctx.req)
if (!user || !usernameEqual || !passwordEqual) { ctx.res = new Response('Unauthorized', { status: 401, headers: { 'WWW-Authenticate': 'Basic realm="' + options.realm.replace(/"/g, '\\"') + '"', }, }) return if (requestUser) { for (const user of users) { const usernameEqual = await timingSafeEqual(user.username, requestUser.username) const passwordEqual = await timingSafeEqual(user.password, requestUser.password) if (usernameEqual && passwordEqual) { // Authorized OK return next() } } } return next()
ctx.res = new Response('Unauthorized', { status: 401, headers: { 'WWW-Authenticate': 'Basic realm="' + options.realm.replace(/"/g, '\\"') + '"', }, }) return }}