# `Attesto.Discovery`
[🔗](https://github.com/XukuLLC/attesto/blob/v2.33.0/lib/attesto/discovery.ex#L1)

RFC 8414 - OAuth 2.0 Authorization Server Metadata.

Build the JSON document a client fetches from
`/.well-known/oauth-authorization-server` (or the OIDC
`/.well-known/openid-configuration`) to discover how to talk to the
issuer: where the token and JWKS endpoints are, which grants and
challenge methods are supported, and which DPoP algorithms the server
accepts.

Attesto fills the fields it can derive or fix by protocol:

  * `issuer` and `token_endpoint` from the `Attesto.Config`.
  * `jwks_uri` derived from the issuer (overridable).
  * `code_challenge_methods_supported` is `["S256"]` - Attesto's PKCE is
    S256 only.
  * `dpop_signing_alg_values_supported` from `Attesto.DPoP.allowed_algs/0`.
  * `grant_types_supported` defaults to `["client_credentials"]`.

Everything host-specific (the authorization, revocation, introspection,
and registration endpoints; the supported scopes, response types, and
client-authentication methods) is supplied through `opts` and merged in.
`nil` opt values are dropped so the document only advertises what the
host actually implements.

The result is a string-keyed map ready to serialise as the endpoint's
JSON body.

# `metadata_candidate`

```elixir
@type metadata_candidate() ::
  {:oauth_authorization_server | :openid_configuration, String.t()}
```

An authorization-server metadata protocol and its discovery URL.

# `metadata`

```elixir
@spec metadata(
  Attesto.Config.t(),
  keyword()
) :: %{required(String.t()) =&gt; term()}
```

Build the authorization-server metadata document for `config`.

Options:

  * `:jwks_uri` - the full JWKS URL. Defaults to the issuer merged with
    `/.well-known/jwks.json`.
  * `:grant_types_supported` - defaults to `["client_credentials"]`.
  * `:authorization_endpoint`, `:revocation_endpoint`,
    `:introspection_endpoint`, `:registration_endpoint`,
    `:userinfo_endpoint` - host endpoint URLs, included only if given.
  * `:scopes_supported`, `:response_types_supported`,
    `:response_modes_supported`, `:token_endpoint_auth_methods_supported`,
    `:service_documentation`, `:ui_locales_supported` - included only if
    given.
  * `:pushed_authorization_request_endpoint` (RFC 9126),
    `:require_pushed_authorization_requests` - the PAR endpoint URL and
    whether the server mandates PAR; included only if given.
  * `:client_id_metadata_document_supported`
    (`draft-ietf-oauth-client-id-metadata-document-03` §8) - a boolean
    advertising whether the server dereferences an HTTPS `client_id` URL to a
    client metadata document; included only if given.
  * `:identity_chaining_requested_token_types_supported`
    (`draft-ietf-oauth-identity-chaining-17` §3, RFC 10092) - a list of
    requested token type URIs supported at the token exchange endpoint;
    included only if given.
  * `:backchannel_authentication_endpoint`,
    `:backchannel_token_delivery_modes_supported` (e.g. `["poll", "ping"]` -
    FAPI-CIBA forbids `"push"`),
    `:backchannel_authentication_request_signing_alg_values_supported`,
    `:backchannel_user_code_parameter_supported` (CIBA Core §4) - the CIBA
    endpoint and capability advertisement; included only if given.
  * `:backchannel_endpoint_login_hint_token_types_supported` (FAPI-CIBA
    Draft 04 §4.3) - the ecosystem-specific `login_hint_token` types accepted
    by the backchannel authentication endpoint; included only if given.

The accepted host fields are the RFC 8414 §2 allowlist in
`@host_fields`; the enumeration above is illustrative. Any other opt key
is ignored.

# `metadata_candidates`

```elixir
@spec metadata_candidates(
  term(),
  keyword()
) :: {:ok, [metadata_candidate()]} | {:error, :invalid_issuer}
```

Return the authorization-server metadata URLs to try for `issuer`, in
interoperability order.

For an issuer with a path, the candidates are RFC 8414 path insertion,
OpenID Connect path insertion, then OpenID Connect path appending. For an
issuer without a path, the two OpenID Connect constructions are identical
and are returned once. This is the sequence MCP clients use, and is also
suitable for any OAuth client that supports both metadata formats.

The issuer must be an HTTPS issuer identifier: an absolute URL without a
query, fragment, or userinfo component. The issuer value itself is not
normalized; consumers must still compare a retrieved document's `issuer`
member to it by exact string comparison.

Options are passed to `Attesto.URI.issuer_identifier?/2`. In particular,
`allow_loopback_http: true` supports a local development issuer without
weakening the default HTTPS requirement.

    iex> Attesto.Discovery.metadata_candidates("https://auth.example/tenant")
    {:ok,
     [
       {:oauth_authorization_server,
        "https://auth.example/.well-known/oauth-authorization-server/tenant"},
       {:openid_configuration,
        "https://auth.example/.well-known/openid-configuration/tenant"},
       {:openid_configuration,
        "https://auth.example/tenant/.well-known/openid-configuration"}
     ]}

---

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