basic-auth.ts
This file
Path
src/middleware/basic-auth/basic-auth.ts
78 lines · 2.1 KiB · TypeScript
Last changed by
feat: basic auth middleware supports overriding `hashFunction` (#128)Yusuke Wadabefore adoption7a31de5
History of this fileTake it
12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879import type { Context } from '../../context'import { timingSafeEqual } from '../../utils/buffer'import { decodeBase64 } from '../../utils/crypto'
const CREDENTIALS_REGEXP = /^ *(?:[Bb][Aa][Ss][Ii][Cc]) +([A-Za-z0-9._~+/-]+=*) *$/const USER_PASS_REGEXP = /^([^:]*):(.*)$/
const auth = (req: Request) => { if (!req) { throw new TypeError('argument req is required') }
if (typeof req !== 'object') { throw new TypeError('argument req is required to be an object') }
if (!req.headers || typeof req.headers !== 'object') { throw new TypeError('argument req is required to have headers property') }
const match = CREDENTIALS_REGEXP.exec(req.headers.get('Authorization')) if (!match) { return undefined }
const userPass = USER_PASS_REGEXP.exec(decodeBase64(match[1]))
if (!userPass) { return undefined }
return { username: userPass[1], password: userPass[2] }}
export const basicAuth = ( options: { username: string; password: string; realm?: string; hashFunction?: Function }, ...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 requestUser = auth(ctx.req)
if (requestUser) { for (const user of users) { const usernameEqual = await timingSafeEqual( user.username, requestUser.username, options.hashFunction ) const passwordEqual = await timingSafeEqual( user.password, requestUser.password, options.hashFunction ) if (usernameEqual && passwordEqual) { // Authorized OK return next() } } }
ctx.res = new Response('Unauthorized', { status: 401, headers: { 'WWW-Authenticate': 'Basic realm="' + options.realm.replace(/"/g, '\\"') + '"', }, }) return }}