WWW-Authenticate Header Parser
Parse WWW-Authenticate challenges and quoted auth parameters, including multiple challenges in one header.
Code
export interface AuthChallenge {
scheme: string;
params: Record<string, string>;
}
function skipWhitespace(header: string, index: number): number {
while (index < header.length && /\s/.test(header[index]!)) index += 1;
return index;
}
function readToken(header: string, index: number): { value: string; end: number } {
const match = /^[^\s,="]+/.exec(header.slice(index));
if (!match) throw new SyntaxError("expected token at position " + index);
return { value: match[0], end: index + match[0].length };
}
function readQuoted(header: string, index: number): { value: string; end: number } {
let value = "";
index += 1;
while (index < header.length) {
const character = header[index]!;
if (character === "\\" && index + 1 < header.length) {
value += header[index + 1];
index += 2;
continue;
}
if (character === '"') return { value, end: index + 1 };
value += character;
index += 1;
}
throw new SyntaxError("unterminated quoted string");
}
function readValue(header: string, index: number): { value: string; end: number } {
return header[index] === '"' ? readQuoted(header, index) : readToken(header, index);
}
export function parseWwwAuthenticate(header: string): AuthChallenge[] {
if (typeof header !== "string" || header.trim() === "") {
throw new TypeError("header must be a non-empty string");
}
const challenges: AuthChallenge[] = [];
let index = 0;
while (index < header.length) {
index = skipWhitespace(header, index);
if (header[index] === ",") {
index = skipWhitespace(header, index + 1);
continue;
}
if (index >= header.length) break;
const schemeToken = readToken(header, index);
const scheme = schemeToken.value;
index = skipWhitespace(header, schemeToken.end);
const params: Record<string, string> = {};
if (index < header.length && header[index] !== ",") {
const name = readToken(header, index);
index = skipWhitespace(header, name.end);
if (header[index] !== "=") {
throw new SyntaxError("expected = after parameter " + name.value);
}
index = skipWhitespace(header, index + 1);
const value = readValue(header, index);
params[name.value.toLowerCase()] = value.value;
index = value.end;
}
while (true) {
index = skipWhitespace(header, index);
if (header[index] !== ",") break;
const afterComma = skipWhitespace(header, index + 1);
if (afterComma >= header.length) {
index = afterComma;
break;
}
const lookahead = readToken(header, afterComma);
const afterName = skipWhitespace(header, lookahead.end);
if (header[afterName] !== "=") break;
index = skipWhitespace(header, afterName + 1);
const value = readValue(header, index);
params[lookahead.value.toLowerCase()] = value.value;
index = value.end;
}
challenges.push({ scheme, params });
}
return challenges;
}How it works
WWW-Authenticate Header Parser keeps everything in the functions skipWhitespace, readToken, readQuoted and readValue.
Iteration is nested: while (index < header.length && /\s/.test(header[index]!)) index += 1; frames the pass, while (index < header.length) does the per-item step. The SyntaxError and TypeError throw is the give-up path: it fires only after the loop above it has gone through every attempt without the early return firing.
The return value is newly constructed — inputs are never handed back.
Caveats
- The guard
!matchshort-circuits the flow with an earlythrowwhen its condition holds.
Applying it
The entry-point signature is export function parseWwwAuthenticate(header: string)
parseWwwAuthenticate is what another module would consume; nothing else here is load-bearing for callers.
Statements that matter
return header[index] === '"' ? readQuoted(header, index) : readToken(header, index);
if (!match) throw new SyntaxError("expected token at position " + index);
while (index < header.length && /\s/.test(header[index]!)) index += 1;
Interactive Tools for This Topic
FAQ
- Does `parseWwwAuthenticate` modify its input?
- It does not. Inputs are only read; the return value is freshly built.
- When does the code throw?
- On the exhaustion path: `SyntaxError` and `TypeError` is thrown after the loop has gone through every attempt without the early return firing.
- Do I need to install anything before running this?
- No — every API used here ships with the language runtime, so there is nothing to install. The file defines `skipWhitespace`, `readToken`, `readQuoted` at load time.
Report a problem with this exampleOpens a pre-filled issue on GitHub.