AttestoClient.MCPAuthorization (AttestoClient v2.26.0)

Copy Markdown View Source

MCP authorization policy over the generic OAuth client primitives.

This module implements the authorization requirements of MCP 2026-07-28 without introducing an MCP authorization profile. The existing profiles bind OAuth security bundles such as HAIP and FAPI; MCP instead coordinates protected-resource discovery, authorization-server discovery, registration selection, scope escalation, and Bearer token use.

discover/2 follows an RFC 9728 resource_metadata challenge pointer when present. Otherwise it tries the path-specific Protected Resource Metadata URL and then the origin-root URL. It follows the advertised issuer through all required RFC 8414 / OpenID Connect metadata candidates, validates exact resource and issuer identity, and requires an explicit S256 PKCE capability.

Client registrations remain application data. select_registration/2 chooses, in order, a registration already bound to the selected issuer, a pre-registration for that issuer, a validated Client ID Metadata Document, opt-in Dynamic Client Registration, then a host prompt callback. It never sends credentials to an issuer other than the one they are bound to. The returned Registration is what the host persists.

start/4, callback/5, and refresh/6 delegate the OAuth wire operations to AttestoClient.AuthorizationCode and AttestoClient.Token. They pin the discovered issuer and metadata, send the MCP resource on authorization and token requests, retain requested scopes when the token response omits them, and accept only Bearer access tokens. This package does not implement the MCP JSON-RPC transport; authorization_header/2 and step_up/3 provide the transport-facing policy points.

Summary

Functions

Build the only Authorization header permitted by core MCP authorization.

Complete MCP authorization and require a Bearer access token.

Discover and validate the authorization context for an MCP resource.

Refresh an MCP token through the generic single-flight coordinator.

Select client registration for a discovered authorization server.

Start MCP authorization with the selected registration.

Apply one MCP insufficient-scope challenge to an authorization context.

Types

error()

@type error() ::
  :authorization_server_changed
  | :client_registration_required
  | :invalid_client_id_metadata
  | :invalid_client_metadata
  | :invalid_dynamic_registration
  | :invalid_offline_access
  | :invalid_pre_registered_clients
  | :invalid_prompt
  | :invalid_registration
  | :invalid_registration_response
  | :invalid_scope_retry_limit
  | :invalid_scopes
  | :missing_pkce_support
  | :not_insufficient_scope
  | :scope_retry_limit
  | :token_binding_mismatch
  | :unsupported_token_endpoint_auth_method
  | :unsupported_token_type
  | AttestoClient.Discovery.error()
  | term()

t()

@type t() :: %AttestoClient.MCPAuthorization{
  authorization_server: map(),
  issuer: String.t(),
  resource: String.t(),
  resource_metadata: map(),
  scope_attempts: non_neg_integer(),
  scopes: [String.t()]
}

Functions

authorization_header(context, tokens)

@spec authorization_header(t(), AttestoClient.TokenSet.t()) ::
  {:ok, {String.t(), String.t()}}
  | {:error, :unsupported_token_type | :token_binding_mismatch}

Build the only Authorization header permitted by core MCP authorization.

The token type is compared case-insensitively as OAuth requires, while the emitted scheme uses the registered Bearer spelling. The token set must be retained for this context's exact issuer and sole MCP resource; this prevents accidentally sending one server's token to another server.

callback(context, registration, store, response, opts)

@spec callback(
  t(),
  AttestoClient.MCPAuthorization.Registration.t(),
  AttestoClient.AuthorizationCode.transaction_target(),
  map() | String.t(),
  keyword()
) ::
  {:ok, %{tokens: AttestoClient.TokenSet.t(), id_token_claims: nil}}
  | {:error, term()}

Complete MCP authorization and require a Bearer access token.

Callback validation, including exact authorization-response issuer checking before code redemption, is performed by AuthorizationCode.callback/3.

discover(resource, opts \\ [])

@spec discover(
  String.t(),
  keyword()
) :: {:ok, t()} | {:error, error()}

Discover and validate the authorization context for an MCP resource.

Discovery and transport options are those of AttestoClient.Discovery.authorization_server/2. :www_authenticate may carry both the Protected Resource Metadata pointer and an initial scope. offline_access: true requests refresh-token access only when the selected authorization server advertises that scope; otherwise it is omitted.

refresh(context, registration, coordinator, key, tokens, opts)

Refresh an MCP token through the generic single-flight coordinator.

The plain OAuth mode skips OIDC subject and JWKS processing. The retained MCP resource is repeated, the registration stays issuer-bound, and a non-Bearer result is refused.

select_registration(context, opts \\ [])

@spec select_registration(
  t(),
  keyword()
) :: {:ok, AttestoClient.MCPAuthorization.Registration.t()} | {:error, error()}

Select client registration for a discovered authorization server.

Options:

  • :registration - a previously persisted Registration.
  • :pre_registered - a map keyed by exact issuer. Values carry :client_id and optional :client_auth (default :none).
  • :client_id_metadata - a map carrying :client_id, the decoded :document, and optional :client_auth. It is considered only when the server advertises client_id_metadata_document_supported: true.
  • :dynamic_registration - opt in with a keyword list or map containing :client_name, non-empty :redirect_uris, and :application_type (:native, :web, or the corresponding string). The registration endpoint is used only when advertised by the selected server.
  • :prompt - a two-arity callback receiving issuer and validated server metadata. It may return {:ok, credentials}, credentials, or :error.

The priority is existing/current pre-registration, configured pre-registration, CIMD, dynamic registration, then prompt. Credentials are checked against the server's advertised token-endpoint authentication methods. Dynamic registration is intentionally opt-in because it creates state at the authorization server.

start(context, registration, store, opts)

Start MCP authorization with the selected registration.

The context's resource, issuer, metadata and scopes, and the registration's client identity and authentication are authoritative. Remaining options are forwarded to AttestoClient.AuthorizationCode.start/2; notably the host supplies :redirect_uri, :browser_binding, transaction lifetime, storage, and transport configuration.

step_up(context, www_authenticate, opts \\ [])

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

Apply one MCP insufficient-scope challenge to an authorization context.

The challenged scopes are unioned with every scope previously requested. :max_scope_retries defaults to 2; once reached, the result is {:error, :scope_retry_limit}. A challenge without error="insufficient_scope" and a valid non-empty scope is not acted on.