1 /* This Source Code Form is subject to the terms of the Mozilla Public
2 * License, v. 2.0. If a copy of the MPL was not distributed with this file,
3 * You can obtain one at http://mozilla.org/MPL/2.0/. */
6 * Parse the parameter in a name/value pair and remove quotes.
8 * @param {string} paramValue
9 * A string representing a challenge parameter.
12 * An object with name and value string properties.
14 function parseChallengeParameter(paramValue) {
15 const [name, value] = paramValue.split("=");
16 return { name, value: value?.replace(/["']/g, "") };
20 * Simple parser for authenticate (WWW-Authenticate or Proxy-Authenticate)
23 * Bug 1857847: Replace with Necko's ChallengeParser once exposed to JS.
25 * @param {string} headerValue
26 * The value of an authenticate header.
28 * @returns {Array<object>}
29 * Array of challenge objects containing two properties:
30 * - {string} scheme: The scheme for the challenge
31 * - {Array<object>} params: Array of { name, value } objects representing
32 * all the parameters of the challenge.
34 export function parseChallengeHeader(headerValue) {
35 const challenges = [];
36 const parts = headerValue.split(",").map(part => part.trim());
41 const schemeRegex = /^(\w+)(?:\s+(.*))?$/;
42 for (const part of parts) {
43 const matches = part.match(schemeRegex);
44 if (matches !== null) {
45 // This is a new scheme.
46 if (scheme !== null) {
47 // If we have a challenge recorded, add it to the array.
48 challenges.push({ scheme, params });
51 // Reset the state for a new scheme.
55 params.push(parseChallengeParameter(matches[2]));
58 if (scheme === null) {
59 // A scheme should always be found before parameters, this header
60 // probably needs a more careful parsing solution.
64 params.push(parseChallengeParameter(part));
68 if (scheme !== null) {
69 // If we have a challenge recorded, add it to the array.
70 challenges.push({ scheme, params });