basic-auth.ts
This file
Path
src/middleware/basic-auth/basic-auth.ts
71 lines · 1.9 KiB · TypeScript
Last changed by
refactor: create `utils/crypto.ts` (#118)Yusuke Wadabefore adoptionbea8aef
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
lines 35–42why, one step back35export const basicAuth = (36 options: { username: string; password: string; realm?: string },37 ...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–46why, one step back43 if (!options.realm) {44 options.realm = 'Secure Area'45 }46feat: basic auth middleware supports multiple users (#112)Yusuke Wadabefore adoption2b76987
lines 47–48why, one step back47 users.unshift({ username: options.username, password: options.password })48feat(basic-auth): basic-auth middleware (#28)Minghebefore adoptioncb4f3d6
line 49why, one step back49 return async (ctx: Context, next: Function) => {feat: basic auth middleware supports multiple users (#112)Yusuke Wadabefore adoption2b76987
line 50why, one step back50 const requestUser = auth(ctx.req)feat(basic-auth): basic-auth middleware (#28)Minghebefore adoptioncb4f3d6
line 51why, one step back51feat: basic auth middleware supports multiple users (#112)Yusuke Wadabefore adoption2b76987
lines 52–60why, one step back52 if (requestUser) {53 for (const user of users) {54 const usernameEqual = await timingSafeEqual(user.username, requestUser.username)55 const passwordEqual = await timingSafeEqual(user.password, requestUser.password)56 if (usernameEqual && passwordEqual) {57 // Authorized OK58 return next()59 }60 }feat(basic-auth): basic-auth middleware (#28)Minghebefore adoptioncb4f3d6
line 61why, one step back61 }feat: basic auth middleware supports multiple users (#112)Yusuke Wadabefore adoption2b76987
lines 62–69why, one step back6263 ctx.res = new Response('Unauthorized', {64 status: 401,65 headers: {66 'WWW-Authenticate': 'Basic realm="' + options.realm.replace(/"/g, '\\"') + '"',67 },68 })69 returnfeat(basic-auth): basic-auth middleware (#28)Minghebefore adoptioncb4f3d6
lines 70–71why, one step back70 }71}