# `AttestoClient.WWWAuthenticate`
[🔗](https://github.com/XukuLLC/attesto_client/blob/v2.26.0/lib/attesto_client/www_authenticate.ex#L1)

Read `WWW-Authenticate` challenges (RFC 9110 §11.6.1).

A resource server refuses a request with one or more challenges: an
authentication scheme followed by auth-params, or by a single token68 value.
A client reads them to learn which scheme the resource accepts, why a
credential was refused (RFC 6750 §3), whether a DPoP nonce must be fetched
(RFC 9449 §8), or where the resource publishes its metadata (RFC 9728 §5.1,
the `resource_metadata` parameter).

`parse/1` takes one header value, or the list `Plug.Conn.get_resp_header/2`
and `Req.Response.get_header/2` return, and reads every challenge in it.
Scheme and parameter names are case-insensitive, so they are returned
lowercased; quoted parameter values are returned unquoted. A malformed list
member is skipped rather than failing the header, and a value over 8 KiB is
read as no challenges at all.

# `challenge`

```elixir
@type challenge() :: %{
  scheme: String.t(),
  params: %{optional(String.t()) =&gt; String.t()},
  token68: String.t() | nil
}
```

One challenge: its scheme, its auth-params, and the token68 value it carries
when it has one instead of parameters.

# `bearer_scopes`

```elixir
@spec bearer_scopes(String.t() | [String.t()] | [challenge()]) ::
  {:ok, [String.t()]} | :none | {:error, :invalid_scope}
```

Return the scope tokens from the first Bearer challenge that carries a
`scope` parameter.

The result is `:none` when no Bearer challenge names a scope and
`{:error, :invalid_scope}` when the named value is empty or contains a value
that is not an OAuth scope token. Repeated tokens are removed while retaining
their first-seen order.

    iex> AttestoClient.WWWAuthenticate.bearer_scopes(
    ...>   ~s(Bearer error="insufficient_scope", scope="files:read files:write")
    ...> )
    {:ok, ["files:read", "files:write"]}

# `parse`

```elixir
@spec parse(String.t() | [String.t()] | term()) :: [challenge()]
```

Parse one `WWW-Authenticate` value, or a list of them, into challenges.

    iex> AttestoClient.WWWAuthenticate.parse(~s(Bearer realm="api", error="invalid_token", DPoP algs="ES256"))
    [
      %{scheme: "bearer", params: %{"realm" => "api", "error" => "invalid_token"}, token68: nil},
      %{scheme: "dpop", params: %{"algs" => "ES256"}, token68: nil}
    ]

A parameter repeated within one challenge keeps its first value. A parameter
that precedes any scheme belongs to no challenge and is dropped.

# `resource_metadata`

```elixir
@spec resource_metadata(
  String.t() | [String.t()] | [challenge()],
  keyword()
) :: {:ok, String.t()} | :error
```

The RFC 9728 §5.1 `resource_metadata` URL advertised by the first challenge
that carries a valid one, read from a header value, a list of values, or
challenges already parsed.

Only an absolute `https` URL without userinfo or fragment is returned, since
the client will fetch it; a challenge advertising anything else is skipped.
`allow_loopback_http: true` also accepts a loopback HTTP URL for local
development. The fetcher must independently enforce its outbound policy.

    iex> AttestoClient.WWWAuthenticate.resource_metadata(
    ...>   ~s(Bearer resource_metadata="https://rs.example/.well-known/oauth-protected-resource/mcp")
    ...> )
    {:ok, "https://rs.example/.well-known/oauth-protected-resource/mcp"}

---

*Consult [api-reference.md](api-reference.md) for complete listing*
