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
123456789101112131415161718192021222324252627import { decodeBase64 } from './encode'
const CREDENTIALS_REGEXP = /^ *(?:[Bb][Aa][Ss][Ii][Cc]) +([A-Za-z0-9._~+/-]+=*) *$/const USER_PASS_REGEXP = /^([^:]*):(.*)$/const utf8Decoder = new TextDecoder()
export type Auth = (req: Request) => { username: string; password: string } | undefined
export const auth: Auth = (req: Request) => { const match = CREDENTIALS_REGEXP.exec(req.headers.get('Authorization') || '') if (!match) { return undefined }
let userPass = undefined // If an invalid string is passed to atob(), it throws a `DOMException`. try { userPass = USER_PASS_REGEXP.exec(utf8Decoder.decode(decodeBase64(match[1]))) } catch {} // Do nothing
if (!userPass) { return undefined }
return { username: userPass[1], password: userPass[2] }}