mirror of
https://github.com/openai/codex.git
synced 2026-11-20 22:40:37 +00:00
## What changed Allow config server bundle and auth loaders to discover authorization and token endpoints, plus an optional device authorization endpoint, from OAuth metadata when callers enable discovery and omit `config_server.oauth`. Discovery uses the bootstrap HTTP policy, times out after five seconds, and keeps the discovered settings in memory. Existing managed-config entry points pass `ConfigServerDiscovery::Disabled`. If bundle loader initialization fails, report the error on the first read and retry on later reads until a loader and its refresh worker can start. ## Testing Add tests for endpoint parsing and defaults, the discovery timeout, explicit OAuth precedence, disabled discovery, and local network policy. GitOrigin-RevId: 7cb72054147ec34f985d55dfe708fccfafcb4ba3
84 lines
2.5 KiB
Rust
84 lines
2.5 KiB
Rust
//! Discovers OAuth endpoints for the config server through RMCP.
|
|
//! Explicit OAuth settings take precedence, and discovered settings stay in memory.
|
|
|
|
use codex_config::ConfigServer;
|
|
use codex_exec_server::RouteAwareHttpClient;
|
|
use codex_login::AuthRouteConfig;
|
|
use codex_login::OAuthSessionConfig;
|
|
use codex_rmcp_client::discover_authorization_metadata;
|
|
use serde::Deserialize;
|
|
use std::io;
|
|
use std::sync::Arc;
|
|
use std::time::Duration;
|
|
|
|
const DISCOVERY_TIMEOUT: Duration = Duration::from_secs(5);
|
|
|
|
/// Controls OAuth discovery when the config server has no explicit OAuth settings.
|
|
#[derive(Clone, Copy, Debug, Default, PartialEq, Eq)]
|
|
pub enum ConfigServerDiscovery {
|
|
#[default]
|
|
Disabled,
|
|
Enabled,
|
|
}
|
|
|
|
pub(crate) async fn resolve_config_server(
|
|
config: &ConfigServer,
|
|
routes: &AuthRouteConfig,
|
|
discovery: ConfigServerDiscovery,
|
|
) -> io::Result<ConfigServer> {
|
|
config.validated_base_url().map_err(io::Error::other)?;
|
|
let mut resolved = config.clone();
|
|
|
|
if discovery == ConfigServerDiscovery::Enabled
|
|
&& config.oauth.is_none()
|
|
&& let Some(oauth) = discover_oauth(&config.base_url, routes).await?
|
|
{
|
|
resolved.oauth = Some(Box::new(oauth));
|
|
resolved.validated_base_url().map_err(io::Error::other)?;
|
|
}
|
|
|
|
Ok(resolved)
|
|
}
|
|
|
|
async fn discover_oauth(
|
|
issuer: &str,
|
|
routes: &AuthRouteConfig,
|
|
) -> io::Result<Option<OAuthSessionConfig>> {
|
|
let client = Arc::new(RouteAwareHttpClient::new(
|
|
routes.bootstrap_http_client_factory().clone(),
|
|
));
|
|
let metadata = tokio::time::timeout(
|
|
DISCOVERY_TIMEOUT,
|
|
discover_authorization_metadata(issuer, client),
|
|
)
|
|
.await
|
|
.map_err(|_| io::Error::new(io::ErrorKind::TimedOut, "OAuth discovery timed out"))?
|
|
.map_err(|_| io::Error::other("OAuth discovery failed"))?;
|
|
let Some(metadata) = metadata else {
|
|
return Ok(None);
|
|
};
|
|
|
|
let device_authorization_url = metadata
|
|
.additional_fields
|
|
.get("device_authorization_endpoint")
|
|
.map(Option::<String>::deserialize)
|
|
.transpose()
|
|
.map_err(|_| io::Error::other("OAuth discovery device endpoint is invalid"))?
|
|
.flatten();
|
|
|
|
Ok(Some(OAuthSessionConfig {
|
|
authorization_url: metadata.authorization_endpoint,
|
|
token_url: metadata.token_endpoint,
|
|
device_authorization_url,
|
|
client_id: String::from("codex"),
|
|
resource: None,
|
|
scopes: Vec::new(),
|
|
redirect_port: None,
|
|
credentials_store: Default::default(),
|
|
}))
|
|
}
|
|
|
|
#[cfg(test)]
|
|
#[path = "discovery_tests.rs"]
|
|
mod tests;
|