basic-auth.ts
This file
Path
src/middleware/basic-auth/basic-auth.ts
74 lines · 1.9 KiB · TypeScript
Last changed by
fix: basic auth (#83)Yusuke Wadabefore adoption0fe95bc
History of this fileTake it
feat(basic-auth): basic-auth middleware (#28)Minghebefore adoptioncb4f3d6
line 1why, one step back1import type { Context } from '../../context'refactor(utils): better management for utils modules to avoid kitchen sink issue (#59)Minghebefore adoptionc522636
line 2why, one step back2import { timingSafeEqual } from '../../utils/buffer'feat(basic-auth): basic-auth middleware (#28)Minghebefore adoptioncb4f3d6
line 3why, one step back3fix: basic auth (#83)Yusuke Wadabefore adoption0fe95bc
line 4why, one step back4const CREDENTIALS_REGEXP = /^ *(?:[Bb][Aa][Ss][Ii][Cc]) +([A-Za-z0-9._~+/-]+=*) *$/feat(basic-auth): basic-auth middleware (#28)Minghebefore adoptioncb4f3d6
lines 5–33why, one step back5const USER_PASS_REGEXP = /^([^:]*):(.*)$/67const auth = (req: Request) => {8 if (!req) {9 throw new TypeError('argument req is required')10 }1112 if (typeof req !== 'object') {13 throw new TypeError('argument req is required to be an object')14 }1516 if (!req.headers || typeof req.headers !== 'object') {17 throw new TypeError('argument req is required to have headers property')18 }1920 const match = CREDENTIALS_REGEXP.exec(req.headers.get('Authorization'))21 if (!match) {22 return undefined23 }2425 const userPass = USER_PASS_REGEXP.exec(decodeBase64(match[1]))2627 if (!userPass) {28 return undefined29 }3031 return { username: userPass[1], password: userPass[2] }32}33fix: basic auth (#83)Yusuke Wadabefore adoption0fe95bc
lines 34–35why, one step back34let b: BufferConstructor35feat(basic-auth): basic-auth middleware (#28)Minghebefore adoptioncb4f3d6
line 36why, one step back36function decodeBase64(str: string) {fix: basic auth (#83)Yusuke Wadabefore adoption0fe95bc
line 37why, one step back37 return b.from(str, 'base64').toString()feat(basic-auth): basic-auth middleware (#28)Minghebefore adoptioncb4f3d6
lines 38–39why, one step back38}39fix: basic auth (#83)Yusuke Wadabefore adoption0fe95bc
lines 40–53why, one step back40export const basicAuth = (options: { username: string; password: string; realm?: string }) => {41 try {42 b = Buffer43 } catch {}4445 if (b === undefined) {46 try {47 const { Buffer } = require('buffer')48 b = Buffer49 } catch (e) {50 throw new Error('If you want to use Basic Auth Middleware, install "buffer" package.')51 }52 }53feat(basic-auth): basic-auth middleware (#28)Minghebefore adoptioncb4f3d6
lines 54–59why, one step back54 if (!options.realm) {55 options.realm = 'Secure Area'56 }5758 return async (ctx: Context, next: Function) => {59 const user = auth(ctx.req)fix: basic auth (#83)Yusuke Wadabefore adoption0fe95bc
lines 60–61why, one step back60 const usernameEqual = user && (await timingSafeEqual(options.username, user.username))61 const passwordEqual = user && (await timingSafeEqual(options.password, user.password))feat(basic-auth): basic-auth middleware (#28)Minghebefore adoptioncb4f3d6
lines 62–66why, one step back6263 if (!user || !usernameEqual || !passwordEqual) {64 ctx.res = new Response('Unauthorized', {65 status: 401,66 headers: {fix: basic auth (#83)Yusuke Wadabefore adoption0fe95bc
line 67why, one step back67 'WWW-Authenticate': 'Basic realm="' + options.realm.replace(/"/g, '\\"') + '"',feat(basic-auth): basic-auth middleware (#28)Minghebefore adoptioncb4f3d6
lines 68–74why, one step back68 },69 })70 return71 }72 return next()73 }74}