icate header.
 *
 * @returns {Array<object>}
 *     Array of challenge objects containing two properties:
 *     - {string} scheme: The scheme for the challenge
 *     - {Array<object>} params: Array of { name, value } objects representing
 *       all the parameters of the challenge.
 */
export function parseChallengeHeader(headerValue) {
  const challenges = [];
  const parts = headerValue.split(",").map(part => part.trim());

  let scheme = null;
  let params = [];

  const schemeRegex = /^(\w+)(?:\s+(.*))?$/;
  for (const part of parts) {
    const matches = part.match(schemeRegex);
    if (matches !== null) {
      // This is a new scheme.
      if (scheme !== null) {
        // If we have a challenge recorded, add it to the array.
        challenges.push({ scheme, params });
      }

      // Reset the state for a new scheme.
      scheme = matches[1];
      params = [];
      if (matches[2]) {
        params.push(parseChallengeParameter(matches[2]));
      }
    } else {
      if (scheme === null) {
        // A scheme should always be found before parameters, this header
        // probably needs a more careful parsing solution.
        return [];
      }

      params.push(parseChallengeParameter(part));
    }
  }

  if (scheme !== null) {
    // If we have a challenge recorded, add it to the array.
    challenges.push({ scheme, params });
  }

  return challenges;
}
PK