Skip to content
Myr

basic-auth.ts

This file

Path

src/middleware/basic-auth/basic-auth.ts

64 lines · 1.7 KiB · TypeScript

Last changed by

fix: use `atob` for basic auth (#84)Yusuke Wadabefore adoption4c213e4

History of this file

Take it

feat(basic-auth): basic-auth middleware (#28)Minghebefore adoptioncb4f3d6

line 1why, one step back
1import type { Context } from '../../context'

refactor(utils): better management for utils modules to avoid kitchen sink issue (#59)Minghebefore adoptionc522636

line 2why, one step back
2import { timingSafeEqual } from '../../utils/buffer'

feat(basic-auth): basic-auth middleware (#28)Minghebefore adoptioncb4f3d6

line 3why, one step back
3

fix: basic auth (#83)Yusuke Wadabefore adoption0fe95bc

line 4why, one step back
4const 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 back
5const 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: use `atob` for basic auth (#84)Yusuke Wadabefore adoption4c213e4

lines 35–40why, one step back
35 if (atob && btoa) {
36 return atob(str)37 } else {38 const { Buffer } = require('buffer')39 return Buffer.from(str, 'base64').toString()40 }

feat(basic-auth): basic-auth middleware (#28)Minghebefore adoptioncb4f3d6

lines 41–42why, one step back
41}
42

fix: basic auth (#83)Yusuke Wadabefore adoption0fe95bc

line 43why, one step back
43export const basicAuth = (options: { username: string; password: string; realm?: string }) => {

feat(basic-auth): basic-auth middleware (#28)Minghebefore adoptioncb4f3d6

lines 44–49why, one step back
44 if (!options.realm) {
45 options.realm = 'Secure Area'46 }4748 return async (ctx: Context, next: Function) => {49 const user = auth(ctx.req)

fix: basic auth (#83)Yusuke Wadabefore adoption0fe95bc

lines 50–51why, one step back
50 const usernameEqual = user && (await timingSafeEqual(options.username, user.username))
51 const passwordEqual = user && (await timingSafeEqual(options.password, user.password))

feat(basic-auth): basic-auth middleware (#28)Minghebefore adoptioncb4f3d6

lines 52–56why, one step back
52
53 if (!user || !usernameEqual || !passwordEqual) {54 ctx.res = new Response('Unauthorized', {55 status: 401,56 headers: {

fix: basic auth (#83)Yusuke Wadabefore adoption0fe95bc

line 57why, one step back
57 'WWW-Authenticate': 'Basic realm="' + options.realm.replace(/"/g, '\\"') + '"',

feat(basic-auth): basic-auth middleware (#28)Minghebefore adoptioncb4f3d6

lines 58–64why, one step back
58 },
59 })60 return61 }62 return next()63 }64}