Skip to content

A cross-platform desktop All-in-One assistant for Claude Code, Codex, OpenCode,DeepSeek Harness and more. Only official website: ccswitch.io

fix(codex): keep a codex login --with-api-key login across third-party switches

Jason4c3aab4cfab6
diff --git a/src-tauri/src/proxy/providers/codex.rs b/src-tauri/src/proxy/providers/codex.rs
index 2ca465950..867bb2b51 100644
--- a/src-tauri/src/proxy/providers/codex.rs
+++ b/src-tauri/src/proxy/providers/codex.rs
@@ -267,7 +267,7 @@ pub fn provider_needs_responses_late_arguments_repair(provider: &Provider) -> bo
!is_codex_official_provider(provider)
}
-fn has_explicit_codex_third_party_upstream(provider: &Provider) -> bool {
+pub(crate) fn has_explicit_codex_third_party_upstream(provider: &Provider) -> bool {
let non_empty_setting = |key: &str| {
provider
.settings_config
diff --git a/src-tauri/src/proxy/providers/mod.rs b/src-tauri/src/proxy/providers/mod.rs
index 41129333e..e47420ee0 100644
--- a/src-tauri/src/proxy/providers/mod.rs
+++ b/src-tauri/src/proxy/providers/mod.rs
@@ -88,6 +88,7 @@ pub use claude::{
normalize_anthropic_messages_for_provider, transform_claude_request_for_api_format,
ClaudeAdapter,
};
+pub(crate) use codex::has_explicit_codex_third_party_upstream;
pub use codex::CodexAdapter;
pub use codex::{
apply_codex_chat_upstream_model, apply_codex_upstream_model, codex_provider_upstream_model,
diff --git a/src-tauri/src/services/provider/codex_direct.rs b/src-tauri/src/services/provider/codex_direct.rs
index ff710343a..efe2034c5 100644
--- a/src-tauri/src/services/provider/codex_direct.rs
+++ b/src-tauri/src/services/provider/codex_direct.rs
@@ -491,8 +491,23 @@ fn row_facts(db: &Database) -> Result<RowFacts, AppError> {
}
continue;
}
- if let Some(key) = auth.and_then(extract_codex_auth_api_key) {
- facts.third_party_keys.push(key);
+ // 只收真的指向第三方上游的行的 Key(路由表,或旧版顶层的 `openai_base_url`)。首启从
+ // `codex login --with-api-key` 导入的 default 行也是非官方,但它的 config 没有路由,
+ // Key 就是用户自己的登录:收进来会把 live 里这份登录认成残留,切到第三方时删掉、不进
+ // 暂存,切回官方也还不回来(#10195)。
+ let routes_elsewhere =
+ crate::proxy::providers::has_explicit_codex_third_party_upstream(provider)
+ || provider
+ .settings_config
+ .get("config")
+ .and_then(Value::as_str)
+ .and_then(|text| text.parse::<toml_edit::DocumentMut>().ok())
+ .and_then(|doc| doc.get("openai_base_url")?.as_str().map(str::to_string))
+ .is_some_and(|url| !url.trim().is_empty());
+ if routes_elsewhere {
+ if let Some(key) = auth.and_then(extract_codex_auth_api_key) {
+ facts.third_party_keys.push(key);
+ }
}
let Some(doc) = provider
.settings_config
diff --git a/src-tauri/tests/integration/provider_service.rs b/src-tauri/tests/integration/provider_service.rs
index ef0269aaa..176794363 100644
--- a/src-tauri/tests/integration/provider_service.rs
+++ b/src-tauri/tests/integration/provider_service.rs
@@ -1318,6 +1318,159 @@ requires_openai_auth = true
);
}
+/// #10195: a login made with `codex login --with-api-key` is imported as the
+/// non-official `default` row. Its key is the user's own login, not a key a
+/// third-party route ever wrote, so the first switch to a relay must stash it
+/// (or keep it with preservation on) and switching back to the official card
+/// must restore it.
+fn seed_api_key_login_and_switch_to_relay(preserve: bool) -> cc_switch_lib::AppState {
+ let live_auth = json!({ "OPENAI_API_KEY": "sk-user-own-login" });
+ write_codex_live_atomic(&live_auth, Some("model = \"gpt-5.4\"\n")).expect("seed API-key login");
+ if preserve {
+ enable_codex_official_auth_preservation();
+ }
+
+ let state = create_test_state().expect("create test state");
+ assert!(import_default_config_test_hook(&state, AppType::Codex).expect("import live"));
+ let imported = state
+ .db
+ .get_provider_by_id("default", &AppType::Codex)
+ .expect("read default row")
+ .expect("default row imported");
+ assert_eq!(imported.category.as_deref(), Some("custom"));
+
+ let relay = Provider::with_id(
+ "relay".to_string(),
+ "Relay".to_string(),
+ json!({
+ "auth": {"OPENAI_API_KEY": "sk-relay"},
+ "config": "model_provider = \"relay\"\nmodel = \"gpt-5.4\"\n\n[model_providers.relay]\nname = \"Relay\"\nbase_url = \"https://relay.example/v1\"\nwire_api = \"responses\"\n"
+ }),
+ None,
+ );
+ state
+ .db
+ .save_provider(&AppType::Codex, &relay)
+ .expect("save relay");
+ let mut official = Provider::with_id(
+ "official-provider".to_string(),
+ "OpenAI Official".to_string(),
+ json!({ "auth": {}, "config": "" }),
+ None,
+ );
+ official.category = Some("official".to_string());
+ state
+ .db
+ .save_provider(&AppType::Codex, &official)
+ .expect("save official");
+
+ ProviderService::switch(&state, AppType::Codex, "relay").expect("switch to relay");
+ state
+}
+
+fn live_codex_api_key() -> Option<String> {
+ let path = cc_switch_lib::get_codex_auth_path();
+ if !path.exists() {
+ return None;
+ }
+ let auth: serde_json::Value = read_json_file(&path).expect("read auth.json");
+ auth.get("OPENAI_API_KEY")
+ .and_then(|v| v.as_str())
+ .map(str::to_string)
+}
+
+#[test]
+fn provider_service_codex_api_key_login_is_stashed_and_restored() {
+ let _guard = test_mutex().lock().unwrap_or_else(|e| e.into_inner());
+ reset_test_fs();
+ let _home = ensure_test_home();
+
+ let state = seed_api_key_login_and_switch_to_relay(false);
+ assert_eq!(
+ live_codex_api_key(),
+ None,
+ "preservation off: no auth.json next to the third-party route"
+ );
+
+ ProviderService::switch(&state, AppType::Codex, "official-provider")
+ .expect("switch to official");
+ assert_eq!(
+ live_codex_api_key().as_deref(),
+ Some("sk-user-own-login"),
+ "the stashed API-key login comes back on the official card"
+ );
+}
+
+#[test]
+fn provider_service_switch_codex_official_clears_legacy_reroute_residue() {
+ let _guard = test_mutex().lock().unwrap_or_else(|e| e.into_inner());
+ reset_test_fs();
+ let _home = ensure_test_home();
+
+ // A row that reroutes the built-in provider through a top-level
+ // `openai_base_url` is a third-party route too: older versions wrote its
+ // key into auth.json, so that key is residue, not the user's login.
+ let legacy_config = "model = \"gpt-5.4\"\nopenai_base_url = \"https://relay.example/v1\"\n";
+ write_codex_live_atomic(
+ &json!({ "OPENAI_API_KEY": "legacy-key" }),
+ Some(legacy_config),
+ )
+ .expect("seed legacy live config");
+
+ let mut initial_config = MultiAppConfig::default();
+ {
+ let manager = initial_config
+ .get_manager_mut(&AppType::Codex)
+ .expect("codex manager");
+ manager.current = "legacy".to_string();
+ manager.providers.insert(
+ "legacy".to_string(),
+ Provider::with_id(
+ "legacy".to_string(),
+ "Legacy relay".to_string(),
+ json!({
+ "auth": {"OPENAI_API_KEY": "legacy-key"},
+ "config": legacy_config
+ }),
+ None,
+ ),
+ );
+ let mut official = Provider::with_id(
+ "official-provider".to_string(),
+ "OpenAI Official".to_string(),
+ json!({ "auth": {}, "config": "" }),
+ None,
+ );
+ official.category = Some("official".to_string());
+ manager
+ .providers
+ .insert("official-provider".to_string(), official);
+ }
+ let state = create_test_state_with_config(&initial_config).expect("create test state");
+
+ ProviderService::switch(&state, AppType::Codex, "official-provider")
+ .expect("switch to official");
+ assert_eq!(
+ live_codex_api_key(),
+ None,
+ "the legacy relay key must not be kept as the official login"
+ );
+}
+
+#[test]
+fn provider_service_codex_api_key_login_survives_relay_switch_with_preservation() {
+ let _guard = test_mutex().lock().unwrap_or_else(|e| e.into_inner());
+ reset_test_fs();
+ let _home = ensure_test_home();
+
+ let _state = seed_api_key_login_and_switch_to_relay(true);
+ assert_eq!(
+ live_codex_api_key().as_deref(),
+ Some("sk-user-own-login"),
+ "preservation on: the user's own login stays in place"
+ );
+}
+
#[test]
fn provider_service_reswitch_current_official_keeps_live_auth() {
let _guard = test_mutex().lock().unwrap_or_else(|e| e.into_inner());