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
feat(basic-auth): basic-auth middleware (#28)Minghebefore adoptioncb4f3d6
line 1why, one step back1import type { Context } from '../../context'refactor: create `utils/crypto.ts` (#118)Yusuke Wadabefore adoptionbea8aef
lines 2–3why, one step back2import { timingSafeEqual } from '../../utils/buffer'3import { decodeBase64 } from '../../utils/crypto'feat(basic-auth): basic-auth middleware (#28)Minghebefore adoptioncb4f3d6
line 4why, one step back4fix: basic auth (#83)Yusuke Wadabefore adoption0fe95bc
line 5why, one step back5const CREDENTIALS_REGEXP = /^ *(?:[Bb][Aa][Ss][Ii][Cc]) +([A-Za-z0-9._~+/-]+=*) *$/feat(basic-auth): basic-auth middleware (#28)Minghebefore adoptioncb4f3d6
lines 6–34why, one step back6const USER_PASS_REGEXP = /^([^:]*):(.*)$/78const auth = (req: Request) => {9 if (!req) {10 throw new TypeError('argument req is required')11 }1213 if (typeof req !== 'object') {14 throw new TypeError('argument req is required to be an object')15 }1617 if (!req.headers || typeof req.headers !== 'object') {18 throw new TypeError('argument req is required to have headers property')19 }2021 const match = CREDENTIALS_REGEXP.exec(req.headers.get('Authorization'))22 if (!match) {23 return undefined24 }2526 const userPass = USER_PASS_REGEXP.exec(decodeBase64(match[1]))2728 if (!userPass) {29 return undefined30 }3132 return { username: userPass[1], password: userPass[2] }33}34feat: basic auth middleware supports multiple users (#112)Yusuke Wadabefore adoption2b76987
line 35why, one step back35export const basicAuth = (feat: basic auth middleware supports overriding `hashFunction` (#128)Yusuke Wadabefore adoption7a31de5
line 36why, one step back36 options: { username: string; password: string; realm?: string; hashFunction?: Function },feat: basic auth middleware supports multiple users (#112)Yusuke Wadabefore adoption2b76987
lines 37–42why, one step back37 ...users: { username: string; password: string }[]38) => {39 if (!options) {40 throw new Error('basic auth middleware requires options for "username and password"')41 }42feat(basic-auth): basic-auth middleware (#28)Minghebefore adoptioncb4f3d6
lines 43–45why, one step back43 if (!options.realm) {44 options.realm = 'Secure Area'45 }feat: basic auth middleware supports multiple users (#112)Yusuke Wadabefore adoption2b76987
lines 46–47why, one step back46 users.unshift({ username: options.username, password: options.password })47feat(basic-auth): basic-auth middleware (#28)Minghebefore adoptioncb4f3d6
line 48why, one step back48 return async (ctx: Context, next: Function) => {feat: basic auth middleware supports multiple users (#112)Yusuke Wadabefore adoption2b76987
line 49why, one step back49 const requestUser = auth(ctx.req)feat(basic-auth): basic-auth middleware (#28)Minghebefore adoptioncb4f3d6
line 50why, one step back50feat: basic auth middleware supports multiple users (#112)Yusuke Wadabefore adoption2b76987
lines 51–52why, one step back51 if (requestUser) {52 for (const user of users) {feat: basic auth middleware supports overriding `hashFunction` (#128)Yusuke Wadabefore adoption7a31de5
lines 53–62why, one step back53 const usernameEqual = await timingSafeEqual(54 user.username,55 requestUser.username,56 options.hashFunction57 )58 const passwordEqual = await timingSafeEqual(59 user.password,60 requestUser.password,61 options.hashFunction62 )feat: basic auth middleware supports multiple users (#112)Yusuke Wadabefore adoption2b76987
lines 63–67why, one step back63 if (usernameEqual && passwordEqual) {64 // Authorized OK65 return next()66 }67 }feat(basic-auth): basic-auth middleware (#28)Minghebefore adoptioncb4f3d6
line 68why, one step back68 }feat: basic auth middleware supports multiple users (#112)Yusuke Wadabefore adoption2b76987
lines 69–76why, one step back6970 ctx.res = new Response('Unauthorized', {71 status: 401,72 headers: {73 'WWW-Authenticate': 'Basic realm="' + options.realm.replace(/"/g, '\\"') + '"',74 },75 })76 returnfeat(basic-auth): basic-auth middleware (#28)Minghebefore adoptioncb4f3d6
lines 77–78why, one step back77 }78}