diff --git a/codex-rs/rmcp-client/src/auth_status.rs b/codex-rs/rmcp-client/src/auth_status.rs index 58c0a1c455..de312c66c2 100644 --- a/codex-rs/rmcp-client/src/auth_status.rs +++ b/codex-rs/rmcp-client/src/auth_status.rs @@ -1,7 +1,5 @@ use std::collections::HashMap; use std::time::Duration; -use std::time::SystemTime; -use std::time::UNIX_EPOCH; use anyhow::Error; use anyhow::Result; @@ -17,6 +15,7 @@ use tracing::debug; use crate::oauth::StoredOAuthTokens; use crate::oauth::load_oauth_tokens; +use crate::oauth::token_needs_refresh; use crate::utils::apply_default_headers; use crate::utils::build_default_headers; use codex_config::types::OAuthCredentialsStoreMode; @@ -73,15 +72,7 @@ fn stored_oauth_tokens_are_locally_usable(tokens: &StoredOAuthTokens) -> bool { return false; } - let now_ms = SystemTime::now() - .duration_since(UNIX_EPOCH) - .unwrap_or_else(|_| Duration::from_secs(0)) - .as_millis() as u64; - let is_expired = tokens - .expires_at - .is_some_and(|expires_at| expires_at <= now_ms); - - if is_expired { + if token_needs_refresh(tokens.expires_at) { return tokens .token_response .0 diff --git a/codex-rs/rmcp-client/src/oauth.rs b/codex-rs/rmcp-client/src/oauth.rs index 6094888a58..99456cf6d8 100644 --- a/codex-rs/rmcp-client/src/oauth.rs +++ b/codex-rs/rmcp-client/src/oauth.rs @@ -499,7 +499,7 @@ fn expires_in_from_timestamp(expires_at: u64) -> Option { } } -fn token_needs_refresh(expires_at: Option) -> bool { +pub(crate) fn token_needs_refresh(expires_at: Option) -> bool { let Some(expires_at) = expires_at else { return false; }; diff --git a/codex-rs/rmcp-client/tests/oauth_auth_status.rs b/codex-rs/rmcp-client/tests/oauth_auth_status.rs index 17ac1eaf92..fb6c48a19b 100644 --- a/codex-rs/rmcp-client/tests/oauth_auth_status.rs +++ b/codex-rs/rmcp-client/tests/oauth_auth_status.rs @@ -3,6 +3,8 @@ mod streamable_http_test_support; use std::fs; use std::net::TcpListener; use std::time::Duration; +use std::time::SystemTime; +use std::time::UNIX_EPOCH; use codex_config::types::OAuthCredentialsStoreMode; use codex_exec_server::Environment; @@ -46,7 +48,8 @@ async fn blank_client_ids_are_not_logged_in_without_discovery() -> anyhow::Resul } #[tokio::test(flavor = "multi_thread", worker_threads = 1)] -async fn blank_refresh_tokens_do_not_make_expired_credentials_usable() -> anyhow::Result<()> { +async fn expired_credentials_require_nonblank_refresh_token() -> anyhow::Result<()> { + run_child("expired_without_refresh", "not-a-valid-url").await?; run_child("expired_with_empty_refresh", "not-a-valid-url").await?; run_child("expired_with_whitespace_refresh", "not-a-valid-url").await } @@ -56,11 +59,22 @@ async fn refreshable_expired_credentials_report_oauth() -> anyhow::Result<()> { run_child("expired_with_refresh", "not-a-valid-url").await } +#[tokio::test(flavor = "multi_thread", worker_threads = 1)] +async fn refresh_window_boundary_matches_startup_refresh() -> anyhow::Result<()> { + run_child("inside_refresh_window", "not-a-valid-url").await?; + run_child("outside_refresh_window", "not-a-valid-url").await +} + #[tokio::test(flavor = "multi_thread", worker_threads = 1)] async fn future_credentials_without_refresh_report_oauth_without_discovery() -> anyhow::Result<()> { run_child("future_without_refresh", "not-a-valid-url").await } +#[tokio::test(flavor = "multi_thread", worker_threads = 1)] +async fn legacy_credentials_without_expiry_report_oauth_without_discovery() -> anyhow::Result<()> { + run_child("legacy_without_expiry", "not-a-valid-url").await +} + #[tokio::test(flavor = "multi_thread", worker_threads = 1)] async fn server_rejected_credentials_still_report_oauth() -> anyhow::Result<()> { let server = MockServer::start().await; @@ -147,6 +161,35 @@ async fn oauth_auth_status_child() -> anyhow::Result<()> { return Ok(()); } + if scenario == "legacy_without_expiry" { + let response = OAuthTokenResponse::new( + AccessToken::new("access-token".to_string()), + BasicTokenType::Bearer, + VendorExtraTokenFields::default(), + ); + save_oauth_tokens( + SERVER_NAME, + &StoredOAuthTokens { + server_name: SERVER_NAME.to_string(), + url: server_url.clone(), + client_id: "client-id".to_string(), + token_response: WrappedOAuthTokenResponse(response), + expires_at: None, + }, + OAuthCredentialsStoreMode::File, + )?; + + assert_eq!( + determine_auth_status(&server_url).await?, + McpAuthStatus::OAuth + ); + return Ok(()); + } + + let now_ms = SystemTime::now() + .duration_since(UNIX_EPOCH) + .unwrap_or_else(|_| Duration::from_secs(0)) + .as_millis() as u64; let (access_token, client_id, refresh_token, expires_at, expected_status) = match scenario.as_str() { "empty_access_token" => ( @@ -177,6 +220,13 @@ async fn oauth_auth_status_child() -> anyhow::Result<()> { /*expires_at*/ None, McpAuthStatus::NotLoggedIn, ), + "expired_without_refresh" => ( + "expired-access-token", + "client-id", + /*refresh_token*/ None, + Some(0), + McpAuthStatus::NotLoggedIn, + ), "expired_with_empty_refresh" => ( "expired-access-token", "client-id", @@ -212,6 +262,20 @@ async fn oauth_auth_status_child() -> anyhow::Result<()> { Some(0), McpAuthStatus::OAuth, ), + "inside_refresh_window" => ( + "", + "client-id", + Some("refresh-token"), + Some(now_ms.saturating_add(29_000)), + McpAuthStatus::OAuth, + ), + "outside_refresh_window" => ( + "", + "client-id", + Some("refresh-token"), + Some(now_ms.saturating_add(31_000)), + McpAuthStatus::NotLoggedIn, + ), "future_without_refresh" => ( "access-token", "client-id", @@ -251,9 +315,12 @@ async fn oauth_auth_status_child() -> anyhow::Result<()> { | "whitespace_access_token" | "empty_client_id" | "whitespace_client_id" + | "expired_without_refresh" | "expired_with_empty_refresh" | "expired_with_whitespace_refresh" | "expired_with_refresh" + | "inside_refresh_window" + | "outside_refresh_window" | "future_without_refresh" => {} unexpected => panic!("unexpected child scenario: {unexpected}"), }