basic-auth.ts
This file
Path
src/middleware/basic-auth/basic-auth.ts
71 lines · 1.9 KiB · TypeScript
Last changed by
fix: Unicode problem about atob/btoa (#85)Yusuke Wadabefore adoption04bb4bb
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–34why, 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}3334function decodeBase64(str: string) {fix: Unicode problem about atob/btoa (#85)Yusuke Wadabefore adoption04bb4bb
lines 35–43why, one step back35 if (atob) {36 const text = atob(str)37 const length = text.length38 const bytes = new Uint8Array(length)39 for (let i = 0; i < length; i++) {40 bytes[i] = text.charCodeAt(i)41 }42 const decoder = new TextDecoder()43 return decoder.decode(bytes)fix: use `atob` for basic auth (#84)Yusuke Wadabefore adoption4c213e4
lines 44–47why, one step back44 } else {45 const { Buffer } = require('buffer')46 return Buffer.from(str, 'base64').toString()47 }feat(basic-auth): basic-auth middleware (#28)Minghebefore adoptioncb4f3d6
lines 48–49why, one step back48}49fix: basic auth (#83)Yusuke Wadabefore adoption0fe95bc
line 50why, one step back50export const basicAuth = (options: { username: string; password: string; realm?: string }) => {feat(basic-auth): basic-auth middleware (#28)Minghebefore adoptioncb4f3d6
lines 51–56why, one step back51 if (!options.realm) {52 options.realm = 'Secure Area'53 }5455 return async (ctx: Context, next: Function) => {56 const user = auth(ctx.req)fix: basic auth (#83)Yusuke Wadabefore adoption0fe95bc
lines 57–58why, one step back57 const usernameEqual = user && (await timingSafeEqual(options.username, user.username))58 const passwordEqual = user && (await timingSafeEqual(options.password, user.password))feat(basic-auth): basic-auth middleware (#28)Minghebefore adoptioncb4f3d6
lines 59–63why, one step back5960 if (!user || !usernameEqual || !passwordEqual) {61 ctx.res = new Response('Unauthorized', {62 status: 401,63 headers: {fix: basic auth (#83)Yusuke Wadabefore adoption0fe95bc
line 64why, one step back64 'WWW-Authenticate': 'Basic realm="' + options.realm.replace(/"/g, '\\"') + '"',feat(basic-auth): basic-auth middleware (#28)Minghebefore adoptioncb4f3d6
lines 65–71why, one step back65 },66 })67 return68 }69 return next()70 }71}