From 09ec2451bf9a6750a19f334e9bd0fd9ebe6494b5 Mon Sep 17 00:00:00 2001 From: smurching-oai Date: Thu, 22 Oct 2026 00:57:33 +0000 Subject: [PATCH] Add request-scoped OAuth credentials and session lifecycle APIs (#56036) ## What changed - Add `OAuthSessionRequest` to pair access tokens with their authorization identity and retain rejected-token recovery across transient failures and retries. New sign-ins receive an identity that survives token refresh. - Expose explicit login, logout, and authorization identity lookup, plus a per-manager option to disable automatic browser login while allowing credential reuse and refresh. - Move serializable `OAuthSessionConfig` into `codex-model-provider-info`, add an optional `device_authorization_url` field, and share endpoint URL validation through `codex-http-client`. - Distinguish permission failures, transient token endpoint failures, and invalid responses using `io::ErrorKind`. ## Testing Add coverage for sign-in identity preservation through refresh retries, removal of saved and cached credentials on logout, and suppression of automatic browser login. Extend token endpoint error classification coverage. GitOrigin-RevId: ae9f72e787171e179fe139a3b840a9f7e1e46506 --- codex-rs/Cargo.lock | 2 +- codex-rs/http-client/Cargo.toml | 1 + codex-rs/http-client/src/lib.rs | 26 ++++ codex-rs/login/src/lib.rs | 2 + codex-rs/login/src/oauth_session.rs | 138 +++++++++--------- codex-rs/login/src/oauth_session_login.rs | 36 +++++ .../login/src/oauth_session_login_tests.rs | 44 ++++++ codex-rs/login/src/oauth_session_request.rs | 70 +++++++++ .../login/src/oauth_session_request_tests.rs | 56 +++++++ codex-rs/login/src/oauth_session_storage.rs | 6 + codex-rs/login/src/oauth_session_tests.rs | 45 ++++-- codex-rs/login/src/oauth_session_token.rs | 35 ++++- codex-rs/model-provider-info/Cargo.toml | 2 +- .../model-provider-info/src/gateway_oauth.rs | 30 +--- codex-rs/model-provider-info/src/lib.rs | 2 + .../model-provider-info/src/oauth_session.rs | 27 ++++ codex-rs/model-provider/src/shared_state.rs | 1 + .../src/shared_state_test_support.rs | 1 + 18 files changed, 409 insertions(+), 115 deletions(-) create mode 100644 codex-rs/login/src/oauth_session_request.rs create mode 100644 codex-rs/login/src/oauth_session_request_tests.rs create mode 100644 codex-rs/model-provider-info/src/oauth_session.rs diff --git a/codex-rs/Cargo.lock b/codex-rs/Cargo.lock index d980227377..bb178d3a09 100644 --- a/codex-rs/Cargo.lock +++ b/codex-rs/Cargo.lock @@ -4144,6 +4144,7 @@ name = "codex-model-provider-info" version = "0.0.0" dependencies = [ "codex-client", + "codex-http-client", "codex-protocol", "codex-utils-absolute-path", "codex-utils-redacted-string", @@ -4154,7 +4155,6 @@ dependencies = [ "serde", "tempfile", "toml 0.9.11+spec-1.1.0", - "url", ] [[package]] diff --git a/codex-rs/http-client/Cargo.toml b/codex-rs/http-client/Cargo.toml index 7eb21d3358..fa144cbb70 100644 --- a/codex-rs/http-client/Cargo.toml +++ b/codex-rs/http-client/Cargo.toml @@ -25,6 +25,7 @@ thiserror = { workspace = true } tokio = { workspace = true, features = ["macros", "rt", "time", "sync"] } tracing = { workspace = true } tracing-opentelemetry = { workspace = true } +url = { workspace = true } zstd = { workspace = true } [target.'cfg(target_os = "macos")'.dependencies] diff --git a/codex-rs/http-client/src/lib.rs b/codex-rs/http-client/src/lib.rs index b199a3f676..c982757788 100644 --- a/codex-rs/http-client/src/lib.rs +++ b/codex-rs/http-client/src/lib.rs @@ -1,3 +1,6 @@ +use url::Host; +use url::Url; + mod chatgpt_cloudflare_cookies; mod chatgpt_hosts; mod client; @@ -83,3 +86,26 @@ pub use crate::windows_proxy_auth::WindowsProxyTls; pub use crate::windows_proxy_auth::connect_windows_proxy; #[cfg(windows)] pub use crate::windows_tls::build_windows_platform_tls_config; + +/// Validates HTTPS or loopback HTTP endpoints, rejecting userinfo and fragments. +/// Errors identify the caller-supplied field without including the URL. +pub fn validate_endpoint_url(value: &str, field: &str) -> Result { + let url = Url::parse(value).map_err(|_| format!("invalid {field} URL"))?; + let loopback = match url.host() { + Some(Host::Domain(host)) => host.eq_ignore_ascii_case("localhost"), + Some(Host::Ipv4(host)) => host.is_loopback(), + Some(Host::Ipv6(host)) => host.is_loopback(), + None => false, + }; + if url.host().is_none() + || !url.username().is_empty() + || url.password().is_some() + || url.fragment().is_some() + || !(url.scheme() == "https" || (url.scheme() == "http" && loopback)) + { + return Err(format!( + "{field} must use HTTPS (or loopback HTTP), without userinfo or a fragment" + )); + } + Ok(url) +} diff --git a/codex-rs/login/src/lib.rs b/codex-rs/login/src/lib.rs index b0863ee7c2..39fbb7ead1 100644 --- a/codex-rs/login/src/lib.rs +++ b/codex-rs/login/src/lib.rs @@ -88,9 +88,11 @@ pub use oauth_session::OAuthSessionStatusChange as GatewayAuthStatusChange; pub use oauth_session::subscribe_oauth_session_status as subscribe_gateway_auth_status; pub use oauth_session::OAuthLoginControl; +pub use oauth_session::OAuthRequestCredentials; pub use oauth_session::OAuthSessionConfig; pub use oauth_session::OAuthSessionError; pub use oauth_session::OAuthSessionManager; +pub use oauth_session::OAuthSessionRequest; pub use oauth_session::OAuthSessionStatus; pub use oauth_session::OAuthSessionStatusChange; pub use oauth_session::subscribe_oauth_session_status; diff --git a/codex-rs/login/src/oauth_session.rs b/codex-rs/login/src/oauth_session.rs index 4119aab553..c62fa6135b 100644 --- a/codex-rs/login/src/oauth_session.rs +++ b/codex-rs/login/src/oauth_session.rs @@ -29,22 +29,26 @@ use http::StatusCode; use sha2::Digest; use sha2::Sha256; use tokio::sync::Mutex; -use url::Host; use url::Url; #[path = "oauth_session_callback.rs"] mod callback; #[path = "oauth_session_login.rs"] mod login; +#[path = "oauth_session_request.rs"] +mod request; #[path = "oauth_session_storage.rs"] mod storage; #[path = "oauth_session_token.rs"] mod token; +pub use codex_model_provider_info::OAuthSessionConfig; pub use login::OAuthLoginControl; pub use login::OAuthSessionStatus; pub use login::OAuthSessionStatusChange; pub use login::subscribe_oauth_session_status; +pub use request::OAuthRequestCredentials; +pub use request::OAuthSessionRequest; use callback::CallbackListener; use storage::OAuthSessionStorage; @@ -63,26 +67,21 @@ pub enum OAuthSessionError { LoginInProgress, } -/// Public-client OAuth settings that identify one configured authorization grant. -#[derive(Clone, PartialEq, Eq)] -pub struct OAuthSessionConfig { - pub authorization_url: String, - pub token_url: String, - pub client_id: String, - pub resource: Option, - pub scopes: Vec, - pub redirect_port: Option, -} - /// Resolves and persists an OAuth access token without knowing how a consumer uses it. #[derive(Clone)] pub struct OAuthSessionManager { control: Arc, state: Arc, + // Whether resolving missing/rejected credentials may start browser sign-in. + // Existing credentials can still be reused or refreshed when this is false. + allow_interactive_login: bool, } +#[derive(Clone)] enum RefreshPolicy { WhenExpired, + // Reconcile the cached token with the persisted grant before reusing it. + ObserveStored, AfterRejection(String), } @@ -90,7 +89,7 @@ impl RefreshPolicy { fn can_reuse(&self, token: &StoredToken) -> bool { token_is_usable(token) && match self { - Self::WhenExpired => true, + Self::WhenExpired | Self::ObserveStored => true, Self::AfterRejection(rejected_access_token) => { token.access_token != *rejected_access_token } @@ -99,7 +98,7 @@ impl RefreshPolicy { } enum RefreshOutcome { - AccessToken(String), + Credentials(StoredToken), LoginRequired, } @@ -145,6 +144,7 @@ impl OAuthSessionManager { .map_err(|_| io::Error::other("failed to create provider OAuth HTTP client"))?; Ok(Self { control: OAuthLoginControl::for_host(&codex_home, http_client_factory), + allow_interactive_login: true, state: Arc::new(OAuthSessionState { config, status_tx: login::status_sender_for_host(&codex_home, http_client_factory), @@ -158,6 +158,27 @@ impl OAuthSessionManager { }) } + /// Keeps background credential consumers from opening a browser without + /// changing the login behavior of other managers in the same runtime. + pub fn without_interactive_login(mut self) -> Self { + self.allow_interactive_login = false; + self + } + + fn interactive_login_allowed(&self) -> bool { + self.allow_interactive_login + && !self + .control + .explicit_login + .load(std::sync::atomic::Ordering::Relaxed) + } + + /// Keeps the sent token paired with its sign-in identity across unauthorized + /// recovery and retries, using the existing gateway credential lifecycle. + pub fn begin_request(&self) -> OAuthSessionRequest { + OAuthSessionRequest::new(self.clone()) + } + /// Returns a usable token, refreshing or starting browser login according to the host policy. pub async fn resolve_access_token(&self) -> io::Result { self.resolve_with_browser(RefreshPolicy::WhenExpired, login::open_browser) @@ -181,6 +202,16 @@ impl OAuthSessionManager { policy: RefreshPolicy, open_browser: impl FnOnce(&Url), ) -> io::Result { + self.resolve_credentials_with_browser(policy, open_browser) + .await + .map(|token| token.access_token) + } + + async fn resolve_credentials_with_browser( + &self, + policy: RefreshPolicy, + open_browser: impl FnOnce(&Url), + ) -> io::Result { validate_config(&self.state.config)?; let mut cached = self.lock_cached_token().await?; let terminal_status = self.state.status.borrow().as_ref().is_some_and(|snapshot| { @@ -199,11 +230,7 @@ impl OAuthSessionManager { && cached.login_required.is_some() && cached.token == cached.login_required { - if !self - .control - .explicit_login - .load(std::sync::atomic::Ordering::Relaxed) - { + if self.interactive_login_allowed() { return self.authorize_with_browser(&mut cached, open_browser).await; } return Err(self.login_required_error(cached.token.as_ref())); @@ -222,9 +249,9 @@ impl OAuthSessionManager { if !terminal_matches { self.publish_status(OAuthSessionStatus::Succeeded, Some(token)); } - let access_token = token.access_token.clone(); + let token = token.clone(); cached.login_required = None; - return Ok(access_token); + return Ok(token); } // The provider may rotate its token before the HTTP response arrives. Keep the // refresh, persistence, and cache update alive if the caller drops this future. @@ -232,10 +259,10 @@ impl OAuthSessionManager { let (mut cached, result) = tokio::spawn(async move { let result = match manager.refresh(&mut cached, &policy).await { Err(error) => Err(error), - Ok(RefreshOutcome::AccessToken(access_token)) => { + Ok(RefreshOutcome::Credentials(token)) => { cached.login_required = None; - manager.publish_status(OAuthSessionStatus::Succeeded, cached.token.as_ref()); - Ok(access_token) + manager.publish_status(OAuthSessionStatus::Succeeded, Some(&token)); + Ok(token) } Ok(RefreshOutcome::LoginRequired) => { cached.login_required = cached.token.clone(); @@ -251,10 +278,7 @@ impl OAuthSessionManager { .get_ref() .and_then(|source| source.downcast_ref::()) == Some(&OAuthSessionError::LoginRequired) - }) && !self - .control - .explicit_login - .load(std::sync::atomic::Ordering::Relaxed) + }) && self.interactive_login_allowed() { return self.authorize_with_browser(&mut cached, open_browser).await; } @@ -265,7 +289,7 @@ impl OAuthSessionManager { &self, cached: &mut OAuthSessionCache, credential_lock: &Arc, - ) -> io::Result { + ) -> io::Result { let token = cached .pending .as_ref() @@ -281,10 +305,10 @@ impl OAuthSessionManager { }) .await .map_err(|_| io::Error::other("provider OAuth credential save task failed"))??; - let access_token = token.access_token.clone(); + let token = token.clone(); cached.token = cached.pending.take(); cached.login_required = None; - Ok(access_token) + Ok(token) } async fn refresh( @@ -307,7 +331,7 @@ impl OAuthSessionManager { if let Some(token) = cached.token.as_ref() && policy.can_reuse(token) { - return Ok(RefreshOutcome::AccessToken(token.access_token.clone())); + return Ok(RefreshOutcome::Credentials(token.clone())); } let Some(refresh_token) = cached .token @@ -326,11 +350,11 @@ impl OAuthSessionManager { .await { Ok(response) => { - cached.pending = Some(response.into_stored(Some(refresh_token))?); + cached.pending = Some(response.into_stored(cached.token.as_ref())?); return self .persist_pending(cached, &credential_lock) .await - .map(RefreshOutcome::AccessToken); + .map(RefreshOutcome::Credentials); } Err(OAuthError::Rejected(rejection)) if rejection.status == StatusCode::BAD_REQUEST @@ -365,13 +389,14 @@ impl OAuthSessionManager { if let Some(token) = cached.token.as_ref() && policy.can_reuse(token) { - return Ok(RefreshOutcome::AccessToken(token.access_token.clone())); + return Ok(RefreshOutcome::Credentials(token.clone())); } } Ok(RefreshOutcome::LoginRequired) } - fn credential_id(&self) -> String { + /// Stable, opaque identity of the issuer, client, audience, and scopes. + pub fn credential_id(&self) -> String { let config = &self.state.config; let mut digest = Sha256::new(); digest.update(self.state.codex_home.to_string_lossy().as_bytes()); @@ -420,7 +445,7 @@ impl OAuthSessionManager { &self, cached: &mut OAuthSessionCache, open_browser: impl FnOnce(&Url), - ) -> io::Result { + ) -> io::Result { let pkce = generate_pkce(); let state = generate_state(); let mut listener = CallbackListener::new(self.state.config.redirect_port, state.clone())?; @@ -468,10 +493,10 @@ impl OAuthSessionManager { Some(&redirect_uri), ) })?; - cached.pending = Some(response.into_stored(/*previous_refresh_token*/ None)?); - let access_token = self.persist_pending(cached, &credential_lock).await?; - self.publish_status(OAuthSessionStatus::Succeeded, cached.token.as_ref()); - Ok(access_token) + cached.pending = Some(response.into_stored(/*previous_token*/ None)?); + let token = self.persist_pending(cached, &credential_lock).await?; + self.publish_status(OAuthSessionStatus::Succeeded, Some(&token)); + Ok(token) } fn oauth(&self) -> OAuthClient<'_> { @@ -488,18 +513,6 @@ impl OAuthSessionManager { } } -impl fmt::Debug for OAuthSessionConfig { - fn fmt(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result { - // Configured URLs may contain issuer-specific credentials in arbitrary query keys. - formatter - .debug_struct("OAuthSessionConfig") - .field("client_id", &self.client_id) - .field("scopes", &self.scopes) - .field("redirect_port", &self.redirect_port) - .finish_non_exhaustive() - } -} - impl fmt::Debug for OAuthSessionManager { fn fmt(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result { formatter @@ -552,24 +565,7 @@ fn validate_config(config: &OAuthSessionConfig) -> io::Result<()> { } fn validate_oauth_url(value: &str, description: &str) -> io::Result { - let url = Url::parse(value).map_err(|_| io::Error::other(format!("invalid {description}")))?; - if !url.username().is_empty() || url.password().is_some() || url.fragment().is_some() { - return Err(io::Error::other(format!( - "{description} cannot include embedded credentials or fragments" - ))); - } - let is_loopback = match url.host() { - Some(Host::Ipv4(address)) => address.is_loopback(), - Some(Host::Ipv6(address)) => address.is_loopback(), - Some(Host::Domain(host)) => host.eq_ignore_ascii_case("localhost"), - None => false, - }; - if url.scheme() != "https" && !(url.scheme() == "http" && is_loopback) { - return Err(io::Error::other(format!( - "{description} must use HTTPS unless it is loopback" - ))); - } - Ok(url) + codex_http_client::validate_endpoint_url(value, description).map_err(io::Error::other) } #[cfg(test)] diff --git a/codex-rs/login/src/oauth_session_login.rs b/codex-rs/login/src/oauth_session_login.rs index 443366d2d4..982ab246b7 100644 --- a/codex-rs/login/src/oauth_session_login.rs +++ b/codex-rs/login/src/oauth_session_login.rs @@ -112,6 +112,42 @@ pub(super) fn status_sender_for_host( } impl OAuthSessionManager { + /// Identifies the persisted sign-in without refreshing an expired access token. + /// Refresh preserves this ID; explicit login creates a new one. + pub async fn authorization_id(&self) -> io::Result { + let _lock = storage::lock_credentials(&self.state.codex_home).await?; + self.load_token_async() + .await? + .and_then(|token| token.authorization_id) + .ok_or_else(|| { + io::Error::new( + io::ErrorKind::PermissionDenied, + OAuthSessionError::LoginRequired, + ) + }) + } + + pub async fn login(&self) -> io::Result<()> { + self.login_with_browser(std::future::pending(), open_browser) + .await + } + + /// Removes the persisted grant and its in-memory copy without contacting the issuer. + pub async fn logout(&self) -> io::Result { + let mut cached = self.lock_cached_token().await?; + let credential_lock = storage::lock_credentials(&self.state.codex_home).await?; + let manager = self.clone(); + tokio::task::spawn_blocking(move || { + let _credential_lock = credential_lock; + let removed = manager.state.storage.delete(&manager.credential_id())?; + *cached = OAuthSessionCache::default(); + manager.publish_status(OAuthSessionStatus::NotReady, /*credential*/ None); + Ok(removed) + }) + .await + .map_err(|_| io::Error::other("provider OAuth credential removal task failed"))? + } + /// Returns the OAuth account configuration used to scope status notifications. pub fn config(&self) -> &OAuthSessionConfig { &self.state.config diff --git a/codex-rs/login/src/oauth_session_login_tests.rs b/codex-rs/login/src/oauth_session_login_tests.rs index 95eb6cb565..a813fd5950 100644 --- a/codex-rs/login/src/oauth_session_login_tests.rs +++ b/codex-rs/login/src/oauth_session_login_tests.rs @@ -6,6 +6,37 @@ use crate::OAuthSessionStatus; use pretty_assertions::assert_eq; use tokio::sync::Notify; +#[tokio::test] +async fn logout_removes_saved_and_cached_credentials() { + let server = MockServer::start().await; + let (manager, _home) = client(config(&server), Arc::new(MockKeyringStore::default())); + let manager = manager.without_interactive_login(); + save_token( + &manager, + "access", + Some("refresh"), + Utc::now().timestamp() + 3600, + ); + assert_eq!(manager.resolve_access_token().await.unwrap(), "access"); + assert_eq!( + manager.status().await.unwrap(), + OAuthSessionStatus::Succeeded + ); + + assert!(manager.logout().await.unwrap()); + assert!(manager.load_token().unwrap().is_none()); + assert_eq!( + manager.status().await.unwrap(), + OAuthSessionStatus::NotReady + ); + assert_eq!( + manager.resolve_access_token().await.unwrap_err().kind(), + std::io::ErrorKind::PermissionDenied, + ); + assert!(!manager.logout().await.unwrap()); + assert!(server.received_requests().await.unwrap().is_empty()); +} + #[tokio::test] async fn status_subscriptions_survive_gateway_changes_and_are_isolated_by_home() { let server = MockServer::start().await; @@ -285,6 +316,7 @@ async fn expired_readiness_stays_not_ready_while_refresh_is_blocked() { let server = MockServer::start().await; let (manager, home) = client(config(&server), Arc::new(MockKeyringStore::default())); let expired = StoredToken { + authorization_id: None, access_token: "expired".into(), refresh_token: Some("refresh".into()), expires_at: Some(Utc::now().timestamp() - 1), @@ -526,6 +558,18 @@ async fn legacy_policy_authorizes_missing_and_rejected_credentials() { std::io::ErrorKind::PermissionDenied ); manager.control.allow_automatic_login(); + assert_eq!( + manager + .clone() + .without_interactive_login() + .resolve_with_browser(super::super::RefreshPolicy::WhenExpired, |_| panic!( + "background mode opened browser" + )) + .await + .unwrap_err() + .kind(), + std::io::ErrorKind::PermissionDenied + ); assert_eq!( manager .resolve_with_browser( diff --git a/codex-rs/login/src/oauth_session_request.rs b/codex-rs/login/src/oauth_session_request.rs new file mode 100644 index 0000000000..61db0b291e --- /dev/null +++ b/codex-rs/login/src/oauth_session_request.rs @@ -0,0 +1,70 @@ +//! Coherent request credentials and unauthorized recovery for configured OAuth consumers. +//! The session manager owns observation, refresh, locking, and persistence; a request +//! retains only its rejected token so transport retries cannot forget recovery. + +use super::OAuthSessionManager; +use super::RefreshPolicy; +use super::login; +use std::fmt; +use std::io; + +/// A token and the identity of the exact grant that supplied it. +pub struct OAuthRequestCredentials { + pub access_token: String, + pub authorization_id: Option, +} + +impl fmt::Debug for OAuthRequestCredentials { + fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { + f.debug_struct("OAuthRequestCredentials") + .field("access_token", &"") + .field("authorization_id", &self.authorization_id) + .finish() + } +} + +/// One consumer request and its retries, using the existing session's credential state. +pub struct OAuthSessionRequest { + manager: OAuthSessionManager, + policy: RefreshPolicy, + sent_token: Option, +} + +impl OAuthSessionRequest { + pub(super) fn new(manager: OAuthSessionManager) -> Self { + Self { + manager, + policy: RefreshPolicy::ObserveStored, + sent_token: None, + } + } + + /// Observes persisted credentials using the existing session lifecycle. The returned + /// identity is captured with the token, never read separately after resolution. + pub async fn credentials(&mut self) -> io::Result { + let token = self + .manager + .resolve_credentials_with_browser(self.policy.clone(), login::open_browser) + .await?; + self.sent_token = Some(token.access_token.clone()); + Ok(OAuthRequestCredentials { + access_token: token.access_token, + authorization_id: token.authorization_id, + }) + } + + /// Marks the sent credential as rejected. Callers bound unauthorized replays; + /// transient transport failures retain this state until resolution succeeds. + pub fn reject_credentials(&mut self) -> bool { + let Some(token) = self.sent_token.take() else { + return false; + }; + self.policy = RefreshPolicy::AfterRejection(token); + true + } + + /// Identifies credential failures that belong to unauthorized recovery. + pub fn is_recovering(&self) -> bool { + matches!(self.policy, RefreshPolicy::AfterRejection(_)) + } +} diff --git a/codex-rs/login/src/oauth_session_request_tests.rs b/codex-rs/login/src/oauth_session_request_tests.rs new file mode 100644 index 0000000000..7b7141d580 --- /dev/null +++ b/codex-rs/login/src/oauth_session_request_tests.rs @@ -0,0 +1,56 @@ +//! Request credentials retain their grant identity through unauthorized recovery. + +use super::*; +use pretty_assertions::assert_eq; + +#[tokio::test] +async fn request_refresh_retry_keeps_the_same_sign_in() { + let server = MockServer::start().await; + let (manager, _home) = client(config(&server), Arc::new(MockKeyringStore::default())); + manager + .save_token(&StoredToken { + access_token: "rejected".into(), + authorization_id: Some("grant".into()), + refresh_token: Some("refresh".into()), + expires_at: Some(Utc::now().timestamp() + 3_600), + }) + .unwrap(); + let mut request = manager.begin_request(); + assert_eq!( + request.credentials().await.unwrap().access_token, + "rejected" + ); + assert!(request.reject_credentials()); + Mock::given(method("POST")) + .and(path("/token")) + .and(body_string_contains("refresh_token=refresh")) + .respond_with(ResponseTemplate::new(503)) + .expect(1) + .mount(&server) + .await; + assert!(request.credentials().await.is_err()); + server.verify().await; + server.reset().await; + Mock::given(method("POST")) + .and(path("/token")) + .and(body_string_contains("refresh_token=refresh")) + .respond_with(ResponseTemplate::new(200).set_body_json(json!({ + "access_token": "replacement", + "token_type": "Bearer", + "expires_in": 3600 + }))) + .expect(1) + .mount(&server) + .await; + let credentials = request.credentials().await.unwrap(); + assert_eq!( + (credentials.access_token, credentials.authorization_id), + ("replacement".into(), Some("grant".into())) + ); + assert_eq!(manager.authorization_id().await.unwrap(), "grant"); + // Ordinary transport retries reuse the refreshed token without another exchange. + assert_eq!( + request.credentials().await.unwrap().access_token, + "replacement" + ); +} diff --git a/codex-rs/login/src/oauth_session_storage.rs b/codex-rs/login/src/oauth_session_storage.rs index a82b53fff5..41b34ec4f6 100644 --- a/codex-rs/login/src/oauth_session_storage.rs +++ b/codex-rs/login/src/oauth_session_storage.rs @@ -73,6 +73,12 @@ impl OAuthSessionStorage { .set(&SecretScope::Global, &secret_name(credential_id)?, value) .map_err(|_| io::Error::other("failed to save provider OAuth credentials")) } + + pub(super) fn delete(&self, credential_id: &str) -> io::Result { + self.0 + .delete(&SecretScope::Global, &secret_name(credential_id)?) + .map_err(|_| io::Error::other("failed to remove provider OAuth credentials")) + } } fn secret_name(credential_id: &str) -> io::Result { diff --git a/codex-rs/login/src/oauth_session_tests.rs b/codex-rs/login/src/oauth_session_tests.rs index 7007c0f606..0c16ba7656 100644 --- a/codex-rs/login/src/oauth_session_tests.rs +++ b/codex-rs/login/src/oauth_session_tests.rs @@ -26,6 +26,9 @@ use crate::test_support::transport_default_auth_route_config; #[path = "oauth_session_login_tests.rs"] mod login_tests; +#[path = "oauth_session_request_tests.rs"] +mod request_tests; + #[path = "oauth_session_storage_tests.rs"] mod storage_tests; @@ -48,6 +51,7 @@ fn client( fn config(server: &MockServer) -> OAuthSessionConfig { OAuthSessionConfig { authorization_url: format!("{}/authorize", server.uri()), + device_authorization_url: None, token_url: format!("{}/token", server.uri()), client_id: "codex-test".to_string(), resource: Some("https://gateway.example.test/codex".to_string()), @@ -59,6 +63,7 @@ fn config(server: &MockServer) -> OAuthSessionConfig { fn loopback_config() -> OAuthSessionConfig { OAuthSessionConfig { authorization_url: "http://127.0.0.1:18080/authorize".to_string(), + device_authorization_url: None, token_url: "http://127.0.0.1:18080/token".to_string(), client_id: "codex-test".to_string(), resource: None, @@ -74,6 +79,7 @@ fn save_token( expires_at: i64, ) { let token = StoredToken { + authorization_id: None, access_token: access_token.to_string(), refresh_token: refresh_token.map(str::to_string), expires_at: Some(expires_at), @@ -492,6 +498,7 @@ async fn gateway_credentials_use_a_dedicated_encrypted_file_and_reload_large_tok .expect("gateway auth manager"); client .save_token(&StoredToken { + authorization_id: None, access_token: access_token.clone(), refresh_token: None, expires_at: None, @@ -570,9 +577,18 @@ async fn browser_authorization_exchanges_and_persists_under_the_store_lock() { .await .expect("browser authorization"); - assert_eq!(token, "browser-access-token"); - let expected = - json!({"access_token": "browser-access-token", "refresh_token": "browser-refresh-token"}); + assert_eq!(token.access_token, "browser-access-token"); + let authorization_id = cached + .token + .as_ref() + .and_then(|token| token.authorization_id.as_ref()) + .expect("new browser authorization has a grant ID"); + let expected = json!({ + "access_token": "browser-access-token", + "refresh_token": "browser-refresh-token", + "authorization_id": authorization_id, + }); + assert_eq!( serde_json::to_value(cached.token.as_ref()).expect("cached token"), expected @@ -632,7 +648,8 @@ async fn token_grants_reject_redirects_without_sending_credentials_to_the_target client .authorize_with_browser(&mut cached, complete_browser_authorization) .await - .expect_err("code exchange redirect") + .err() + .expect("code exchange redirect") } }; assert!( @@ -830,6 +847,7 @@ async fn only_explicit_refresh_grant_rejections_request_reauthorization() { (400, Some("invalid_request")), (400, None), (401, Some("unauthorized_client")), + (429, Some("temporarily_unavailable")), (503, Some("unsupported_grant_type")), ] { let server = MockServer::start().await; @@ -859,10 +877,16 @@ async fn only_explicit_refresh_grant_rejections_request_reauthorization() { assert!(matches!(result, Ok(super::RefreshOutcome::LoginRequired))); } _ => { - let message = result - .err() - .expect("surface token endpoint failure") - .to_string(); + let error = result.err().expect("surface token endpoint failure"); + assert_eq!( + error.kind(), + if status >= 500 || status == 429 { + std::io::ErrorKind::Other + } else { + std::io::ErrorKind::PermissionDenied + } + ); + let message = error.to_string(); assert!(message.contains(&format!("returned HTTP {status}"))); if let Some(error_code) = error_code { assert!(message.contains(error_code)); @@ -890,6 +914,7 @@ async fn invalid_grant_recovers_external_credentials_with_a_bounded_retry() { .respond_with(move |_: &wiremock::Request| { writer .save_token(&StoredToken { + authorization_id: None, access_token: "external-access".to_string(), refresh_token: Some("external-refresh".to_string()), expires_at: retry_status.map(|_| Utc::now().timestamp() - 1), @@ -1085,6 +1110,7 @@ async fn failed_save_retains_rotation_without_overwriting_a_new_external_login() if recovery == Recovery::ExternalLogin { manager .save_token(&StoredToken { + authorization_id: None, access_token: "external-access".to_string(), refresh_token: Some("external-refresh".to_string()), expires_at: None, @@ -1105,7 +1131,8 @@ async fn failed_save_retains_rotation_without_overwriting_a_new_external_login() manager .authorize_with_browser(&mut cached, complete_browser_authorization) .await - .expect_err("failed login must preserve the correct recovery baseline"); + .err() + .expect("failed login must preserve the correct recovery baseline"); } if recovery == Recovery::Expired { manager diff --git a/codex-rs/login/src/oauth_session_token.rs b/codex-rs/login/src/oauth_session_token.rs index ecf0086bb2..fe05508626 100644 --- a/codex-rs/login/src/oauth_session_token.rs +++ b/codex-rs/login/src/oauth_session_token.rs @@ -3,6 +3,7 @@ use std::io; use crate::oauth::OAuthError; +use crate::oauth::generate_state; use crate::oauth::sanitize_url_for_logging; use chrono::Utc; use codex_secrets::redact_secrets; @@ -14,6 +15,9 @@ use super::OAuthSessionConfig; #[derive(Clone, Deserialize, Serialize, PartialEq, Eq)] pub(super) struct StoredToken { pub access_token: String, + /// Stable across refreshes, replaced by a new authorization grant. + #[serde(default, skip_serializing_if = "Option::is_none")] + pub authorization_id: Option, #[serde(default, skip_serializing_if = "Option::is_none")] pub refresh_token: Option, #[serde(default, skip_serializing_if = "Option::is_none")] @@ -34,7 +38,7 @@ pub(super) struct TokenResponse { impl TokenResponse { pub(super) fn into_stored( self, - previous_refresh_token: Option<&str>, + previous_token: Option<&StoredToken>, ) -> io::Result { if self.access_token.trim().is_empty() { return Err(io::Error::other( @@ -68,9 +72,13 @@ impl TokenResponse { .transpose()?; Ok(StoredToken { access_token: self.access_token, + authorization_id: match previous_token { + Some(previous) => previous.authorization_id.clone(), + None => Some(generate_state()), + }, refresh_token: self .refresh_token - .or_else(|| previous_refresh_token.map(str::to_string)), + .or_else(|| previous_token.and_then(|previous| previous.refresh_token.clone())), expires_at, }) } @@ -125,15 +133,28 @@ pub(super) fn endpoint_error( } else { "not applicable" }; - io::Error::other(format!( - "provider OAuth token endpoint {endpoint} returned HTTP {} - {detail}{request_id}. Request: grant_type={grant_type}, client_id={client_id}, client_auth=none (public client), pkce={pkce}, redirect_uri={redirect_uri}, resource={resource}, authorization_scopes={scopes}", - rejection.status.as_u16(), - )) + let kind = if rejection.status.is_server_error() + || matches!(rejection.status.as_u16(), 408 | 429) + { + io::ErrorKind::Other + } else { + io::ErrorKind::PermissionDenied + }; + io::Error::new( + kind, + format!( + "provider OAuth token endpoint {endpoint} returned HTTP {} - {detail}{request_id}. Request: grant_type={grant_type}, client_id={client_id}, client_auth=none (public client), pkce={pkce}, redirect_uri={redirect_uri}, resource={resource}, authorization_scopes={scopes}", + rejection.status.as_u16(), + ), + ) } OAuthError::Transport(_) => io::Error::other(format!( "provider OAuth token exchange failed for {endpoint}" )), - OAuthError::InvalidResponse => io::Error::other("provider OAuth token response is invalid"), + OAuthError::InvalidResponse => io::Error::new( + io::ErrorKind::InvalidData, + "provider OAuth token response is invalid", + ), } } diff --git a/codex-rs/model-provider-info/Cargo.toml b/codex-rs/model-provider-info/Cargo.toml index e6a740d25d..51d3026637 100644 --- a/codex-rs/model-provider-info/Cargo.toml +++ b/codex-rs/model-provider-info/Cargo.toml @@ -14,12 +14,12 @@ workspace = true [dependencies] codex-client = { workspace = true } +codex-http-client = { workspace = true } codex-protocol = { workspace = true } codex-utils-redacted-string = { workspace = true } http = { workspace = true } schemars = { workspace = true } serde = { workspace = true, features = ["derive"] } -url = { workspace = true } [dev-dependencies] codex-utils-absolute-path = { workspace = true } diff --git a/codex-rs/model-provider-info/src/gateway_oauth.rs b/codex-rs/model-provider-info/src/gateway_oauth.rs index 21c5901ecf..11ff6ca6da 100644 --- a/codex-rs/model-provider-info/src/gateway_oauth.rs +++ b/codex-rs/model-provider-info/src/gateway_oauth.rs @@ -2,12 +2,11 @@ use std::fmt; +use codex_http_client::validate_endpoint_url; use http::header::HeaderName; use schemars::JsonSchema; use serde::Deserialize; use serde::Serialize; -use url::Host; -use url::Url; use crate::ModelProviderInfo; @@ -69,13 +68,13 @@ impl GatewayOAuthConfig { if provider.aws.is_some() || provider.is_amazon_bedrock() { return Err("provider gateway_oauth cannot be combined with AWS authentication".into()); } - validate_url(&self.authorization_url, "gateway_oauth.authorization_url")?; - validate_url(&self.token_url, "gateway_oauth.token_url")?; + validate_endpoint_url(&self.authorization_url, "gateway_oauth.authorization_url")?; + validate_endpoint_url(&self.token_url, "gateway_oauth.token_url")?; let base_url = provider .base_url .as_deref() .ok_or("gateway_oauth requires base_url")?; - validate_url(base_url, "base_url")?; + validate_endpoint_url(base_url, "base_url")?; if self.client_id.trim().is_empty() || self.redirect_port == Some(0) { return Err( "gateway_oauth requires a nonempty client_id and a nonzero redirect_port".into(), @@ -131,27 +130,6 @@ fn is_token(value: &str) -> bool { .all(|byte| byte.is_ascii_alphanumeric() || b"!#$%&'*+-.^_`|~".contains(&byte)) } -fn validate_url(value: &str, field: &str) -> Result<(), String> { - let url = Url::parse(value).map_err(|_| format!("invalid {field} URL"))?; - let loopback = match url.host() { - Some(Host::Domain(host)) => host.eq_ignore_ascii_case("localhost"), - Some(Host::Ipv4(host)) => host.is_loopback(), - Some(Host::Ipv6(host)) => host.is_loopback(), - None => false, - }; - if url.host().is_none() - || !url.username().is_empty() - || url.password().is_some() - || url.fragment().is_some() - || !(url.scheme() == "https" || (url.scheme() == "http" && loopback)) - { - return Err(format!( - "{field} must use HTTPS (or loopback HTTP), without userinfo or a fragment" - )); - } - Ok(()) -} - #[cfg(test)] #[path = "gateway_oauth_tests.rs"] mod tests; diff --git a/codex-rs/model-provider-info/src/lib.rs b/codex-rs/model-provider-info/src/lib.rs index f020d34c25..a70ab290ea 100644 --- a/codex-rs/model-provider-info/src/lib.rs +++ b/codex-rs/model-provider-info/src/lib.rs @@ -33,10 +33,12 @@ use std::time::Duration; mod capabilities; mod gateway_oauth; +mod oauth_session; pub use capabilities::ModelProviderCapabilities; pub use capabilities::RemoteCompactionSupport; pub use gateway_oauth::GatewayOAuthConfig; pub use gateway_oauth::GatewayOAuthDelivery; +pub use oauth_session::OAuthSessionConfig; pub const RESIDENCY_HEADER_NAME: &str = "x-openai-internal-codex-residency"; diff --git a/codex-rs/model-provider-info/src/oauth_session.rs b/codex-rs/model-provider-info/src/oauth_session.rs new file mode 100644 index 0000000000..cd4bfb2ee6 --- /dev/null +++ b/codex-rs/model-provider-info/src/oauth_session.rs @@ -0,0 +1,27 @@ +//! Public-client OAuth settings shared by managed configuration and service sign-in. + +use serde::Deserialize; +use serde::Serialize; +use std::fmt; + +/// Settings identifying a configured OAuth grant, independent of token delivery. +#[derive(Clone, PartialEq, Eq, Deserialize, Serialize)] +#[serde(deny_unknown_fields)] +pub struct OAuthSessionConfig { + pub authorization_url: String, + pub token_url: String, + #[serde(skip_serializing_if = "Option::is_none")] + pub device_authorization_url: Option, + pub client_id: String, + pub resource: Option, + #[serde(default)] + pub scopes: Vec, + pub redirect_port: Option, +} + +impl fmt::Debug for OAuthSessionConfig { + fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { + // Issuer settings may contain credentials in arbitrary query parameters. + f.debug_struct("OAuthSessionConfig").finish_non_exhaustive() + } +} diff --git a/codex-rs/model-provider/src/shared_state.rs b/codex-rs/model-provider/src/shared_state.rs index 02dae76d79..e6f90597a8 100644 --- a/codex-rs/model-provider/src/shared_state.rs +++ b/codex-rs/model-provider/src/shared_state.rs @@ -40,6 +40,7 @@ impl ModelProviderSharedState { ) -> std::io::Result> { let oauth = GatewayAuthConfig { authorization_url: config.authorization_url.clone(), + device_authorization_url: None, token_url: config.token_url.clone(), client_id: config.client_id.clone(), resource: config.resource.clone(), diff --git a/codex-rs/model-provider/src/shared_state_test_support.rs b/codex-rs/model-provider/src/shared_state_test_support.rs index d86ab79605..d7a60e063e 100644 --- a/codex-rs/model-provider/src/shared_state_test_support.rs +++ b/codex-rs/model-provider/src/shared_state_test_support.rs @@ -73,6 +73,7 @@ pub fn seed_gateway_auth( let runtime = primary.runtime_config(); let oauth = GatewayAuthConfig { authorization_url: config.authorization_url, + device_authorization_url: None, token_url: config.token_url, client_id: config.client_id, resource: config.resource,