ExAgent.MCP.OAuth (ExAgent v3.0.0)

View Source

Pure OAuth helpers for the MCP client host.

Stability — Candidate.

Select an issuer from trusted protected resource metadata, then fetch metadata_urls/1 in order and validate the document with validate_metadata/2. The host owns HTTP policy, authorization redirects, OAuth state, the code exchange, token custody and token refresh. Keep the validated issuer and PKCE verifier together in the host's per-authorization record.

Summary

Functions

Return the RFC 8414 and OpenID Connect discovery URLs in MCP priority order.

Derive the unpadded base64url S256 challenge from an RFC 7636 verifier.

Encode 32 to 96 bytes of host-generated random entropy as a PKCE verifier.

Check the decoded authorization-response iss against the issuer recorded before redirect. Use for both success and error callbacks, before consuming their contents. The host also verifies the callback's binding and state.

Validate issuer identity, HTTPS endpoints and advertised PKCE S256 support for the authorization code flow. Pass the issuer used to construct the discovery URL and the decoded document fetched over trusted HTTPS.

Types

metadata()

@type metadata() :: %{
  issuer: String.t(),
  authorization_endpoint: String.t(),
  token_endpoint: String.t(),
  iss_required?: boolean()
}

Functions

metadata_urls(issuer)

@spec metadata_urls(String.t()) :: {:ok, [String.t()]} | {:error, :invalid_issuer}

Return the RFC 8414 and OpenID Connect discovery URLs in MCP priority order.

pkce_challenge(verifier)

@spec pkce_challenge(String.t()) :: {:ok, String.t()} | {:error, :invalid_verifier}

Derive the unpadded base64url S256 challenge from an RFC 7636 verifier.

pkce_verifier(entropy)

@spec pkce_verifier(binary()) :: {:ok, String.t()} | {:error, :invalid_entropy}

Encode 32 to 96 bytes of host-generated random entropy as a PKCE verifier.

validate_issuer(arg1, params)

@spec validate_issuer(metadata(), map()) :: :ok | {:error, atom()}

Check the decoded authorization-response iss against the issuer recorded before redirect. Use for both success and error callbacks, before consuming their contents. The host also verifies the callback's binding and state.

validate_metadata(issuer, document)

@spec validate_metadata(String.t(), map()) :: {:ok, metadata()} | {:error, atom()}

Validate issuer identity, HTTPS endpoints and advertised PKCE S256 support for the authorization code flow. Pass the issuer used to construct the discovery URL and the decoded document fetched over trusted HTTPS.

Returns {:error, :pkce_unsupported} unless code_challenge_methods_supported is an array of strings containing "S256". MCP Authorization Code Protection