diff --git a/codex-rs/cloud-config/src/discovery.rs b/codex-rs/cloud-config/src/discovery.rs index ee5aa6f542..9ffcefad8c 100644 --- a/codex-rs/cloud-config/src/discovery.rs +++ b/codex-rs/cloud-config/src/discovery.rs @@ -80,6 +80,12 @@ async fn resolve_oauth( return Ok(None); }; let overrides = overrides.cloned().unwrap_or_default(); + let client_id = overrides.client_id.ok_or_else(|| { + io::Error::new( + io::ErrorKind::InvalidInput, + "OAuth discovery requires a client ID. Set gateway.auth.client_id or config_server.oauth.client_id in requirements.toml.", + ) + })?; let device_authorization_url = match overrides.device_authorization_url { Some(url) => Some(url), None => metadata @@ -95,7 +101,7 @@ async fn resolve_oauth( authorization_url: metadata.authorization_endpoint, token_url: metadata.token_endpoint, device_authorization_url, - client_id: overrides.client_id.unwrap_or_else(|| String::from("codex")), + client_id, resource: overrides.resource, scopes: overrides.scopes, redirect_port: overrides.redirect_port, diff --git a/codex-rs/cloud-config/src/discovery_tests.rs b/codex-rs/cloud-config/src/discovery_tests.rs index 9f3c0351c6..526bad2734 100644 --- a/codex-rs/cloud-config/src/discovery_tests.rs +++ b/codex-rs/cloud-config/src/discovery_tests.rs @@ -1,6 +1,6 @@ #![cfg(test)] -//! Tests config-server OAuth defaults, discovery activation, and routing. +//! Tests config-server OAuth discovery, client settings, and routing. use super::DISCOVERY_TIMEOUT; use super::resolve_config_server; @@ -31,8 +31,15 @@ fn metadata(issuer: &str) -> Value { }) } +fn client_id_only() -> ConfigServerOAuth { + ConfigServerOAuth::Discover(OAuthDiscoveryOverrides { + client_id: Some("codex".into()), + ..Default::default() + }) +} + #[tokio::test] -async fn applies_company_defaults_and_optional_device_endpoint() { +async fn preserves_explicit_codex_client_id_and_optional_device_endpoint() { let server = MockServer::start().await; for device_url in [None, Some("https://login.example/device")] { server.reset().await; @@ -56,7 +63,7 @@ async fn applies_company_defaults_and_optional_device_endpoint() { resolve_oauth( &server.uri(), &transport_default_auth_route_config(), - /*configured*/ None + Some(&client_id_only()) ) .await .unwrap(), @@ -108,11 +115,18 @@ async fn discovers_endpoints_and_uses_configured_scopes() { .respond_with(ResponseTemplate::new(200).set_body_json(metadata(&server.uri()))) .mount(&server) .await; - let default_session = resolve_oauth(&server.uri(), &routes, /*configured*/ None) - .await - .unwrap() - .unwrap(); - assert!(default_session.scopes.is_empty()); + let scopes_only = ConfigServerOAuth::Discover(OAuthDiscoveryOverrides { + scopes: vec!["read".into()], + ..Default::default() + }); + for missing_id in [None, Some(&scopes_only)] { + let error = resolve_oauth(&server.uri(), &routes, missing_id) + .await + .unwrap_err(); + assert_eq!(error.kind(), io::ErrorKind::InvalidInput); + assert!(error.to_string().contains("gateway.auth.client_id")); + assert!(error.to_string().contains("config_server.oauth.client_id")); + } let session = resolve_oauth(&server.uri(), &routes, Some(&configured)) .await .unwrap() @@ -136,7 +150,7 @@ async fn rejects_invalid_device_endpoint() { let error = resolve_oauth( &server.uri(), &transport_default_auth_route_config(), - /*configured*/ None, + Some(&client_id_only()), ) .await .unwrap_err(); @@ -226,7 +240,7 @@ async fn discovery_obeys_local_network_policy_without_requiring_fetched_policy() .mount(&server) .await; assert!( - resolve_oauth(&server.uri(), &routes, /*configured*/ None) + resolve_oauth(&server.uri(), &routes, Some(&client_id_only())) .await .unwrap() .is_some() @@ -236,7 +250,7 @@ async fn discovery_obeys_local_network_policy_without_requiring_fetched_policy() let routes = AuthRouteConfig::from_http_client_factory(allowed).with_local_bootstrap_factory(blocked); assert!( - resolve_oauth(&server.uri(), &routes, /*configured*/ None) + resolve_oauth(&server.uri(), &routes, Some(&client_id_only())) .await .is_err() );