parse static method

WwwAuthenticateChallenge? parse(
  1. String? header
)

Parse a raw WWW-Authenticate header value.

Tolerant of a single header carrying one challenge (the common MCP case: Bearer resource_metadata="…", error="…", scope="…"). Returns null when the value is empty. Non-Bearer schemes still parse (scheme captured) but carry whatever params are present.

Implementation

static WwwAuthenticateChallenge? parse(String? header) {
  if (header == null) return null;
  final trimmed = header.trim();
  if (trimmed.isEmpty) return null;

  // Split scheme from the auth-param list.
  final spaceIdx = trimmed.indexOf(' ');
  if (spaceIdx < 0) {
    return WwwAuthenticateChallenge(
      scheme: trimmed.toLowerCase(),
      params: const {},
    );
  }
  final scheme = trimmed.substring(0, spaceIdx).toLowerCase();
  final rest = trimmed.substring(spaceIdx + 1);

  return WwwAuthenticateChallenge(
    scheme: scheme,
    params: _parseAuthParams(rest),
  );
}