basic-auth.ts
This file
Path
src/utils/basic-auth.ts
26 lines · 787 bytes · TypeScript
Last changed by
Not known.
History of this fileTake it
The walk stopped after 200 commits: lines older than 8f027e5 point at it.
fix(ssg): add `atom+xml` and `rss+xml` to `defaultExtensionMap` (#4899)Yuinbefore adoption8f027e5
lines 1–26why, one step back1import { 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}