Skip to content
Myr

basic-auth.ts

This file

Path

src/utils/basic-auth.ts

26 lines · 787 bytes · TypeScript

Last changed by

Not known.

History of this file

Take it

The walk stopped after 200 commits: lines older than e43f203 point at it.

perf(helper/cookie): fast-path for name specified (#3608)TATSUNO “Taz” Yasuhirobefore adoptione43f203

lines 1–26why, one step back
1import { decodeBase64 } from './encode'
23const CREDENTIALS_REGEXP = /^ *(?:[Bb][Aa][Ss][Ii][Cc]) +([A-Za-z0-9._~+/-]+=*) *$/4const USER_PASS_REGEXP = /^([^:]*):(.*)$/5const utf8Decoder = new TextDecoder()67export type Auth = (req: Request) => { username: string; password: string } | undefined89export const auth: Auth = (req: Request) => {10 const match = CREDENTIALS_REGEXP.exec(req.headers.get('Authorization') || '')11 if (!match) {12 return undefined13 }1415 let userPass = undefined16 // If an invalid string is passed to atob(), it throws a `DOMException`.17 try {18 userPass = USER_PASS_REGEXP.exec(utf8Decoder.decode(decodeBase64(match[1])))19 } catch {} // Do nothing2021 if (!userPass) {22 return undefined23 }2425 return { username: userPass[1], password: userPass[2] }26}