Files
codex/codex-rs/rmcp-client/src/oauth_discovery.rs
T
vishnu-oai b32ada5320 Add published OAuth metadata discovery to the RMCP client (#60578)
## What changed

Expose `discover_authorization_metadata` using RMCP and the existing OAuth HTTP adapter with a caller-supplied HTTP client. Return published authorization-server metadata, including discovery through protected-resource metadata, while excluding synthesized legacy endpoints. Return `None` when authorization metadata is unavailable and propagate discovery errors.

## Testing

Add tests for published versus synthesized endpoints, discovery failures and issuer mismatches, and discovery through a separate authorization server without sending credentials.

GitOrigin-RevId: b273067bb3b33d11301ae0e2017b28cd8e25b548
2026-11-10 11:45:44 +00:00

44 lines
1.6 KiB
Rust

//! Resolves published OAuth metadata through RMCP and Codex's existing HTTP adapter.
//! The caller supplies network policy and an overall discovery deadline.
use std::sync::Arc;
use anyhow::Result;
use codex_exec_server::HttpClient;
use http::HeaderMap;
use rmcp::transport::AuthorizationManager;
use rmcp::transport::auth::AuthError;
use rmcp::transport::auth::AuthorizationMetadata;
use crate::http_client_adapter::StreamableHttpRedirectMode;
use crate::oauth_http_client::OAuthHttpClientAdapter;
/// Discovers published authorization-server metadata with browser and token endpoints.
///
/// Returns `Ok(None)` when RMCP finds no usable document, for example when a
/// direct issuer publishes only device and token endpoints. Discards synthesized
/// legacy endpoints and propagates discovery errors.
pub async fn discover_authorization_metadata(
url: &str,
http_client: Arc<dyn HttpClient>,
) -> Result<Option<AuthorizationMetadata>> {
let client = OAuthHttpClientAdapter::new_with_redirect_mode(
http_client,
HeaderMap::new(),
url,
/*has_configured_headers*/ false,
StreamableHttpRedirectMode::Legacy,
)?;
let manager = AuthorizationManager::new_with_oauth_http_client(url, Arc::new(client)).await?;
match manager.resolve_metadata().await {
Ok(resolution) if !resolution.source.is_discovered() => Ok(None),
Ok(resolution) => Ok(Some(resolution.metadata)),
Err(AuthError::NoAuthorizationSupport) => Ok(None),
Err(error) => Err(error.into()),
}
}
#[cfg(test)]
#[path = "oauth_discovery_tests.rs"]
mod tests;