mirror of
https://github.com/farion1231/cc-switch.git
synced 2026-08-02 18:41:35 +08:00
feat(codex): preserve OAuth login state during third-party provider switching
Codex provider switches now only write config.toml for third-party providers, injecting the API key as experimental_bearer_token. The user's auth.json (ChatGPT OAuth tokens) is preserved. Official providers with login material still write auth.json normally. Backfill restores bearer tokens into stored provider auth.OPENAI_API_KEY to maintain canonical shape.
This commit is contained in:
@@ -159,8 +159,9 @@ impl ConfigService {
|
||||
}
|
||||
let cfg_text = settings.get("config").and_then(Value::as_str);
|
||||
|
||||
crate::codex_config::write_codex_live_with_catalog(
|
||||
crate::codex_config::write_codex_provider_live_with_catalog(
|
||||
&provider.settings_config,
|
||||
provider.category.as_deref(),
|
||||
auth,
|
||||
cfg_text,
|
||||
)?;
|
||||
@@ -172,10 +173,30 @@ impl ConfigService {
|
||||
if let Some(manager) = config.get_manager_mut(&AppType::Codex) {
|
||||
if let Some(target) = manager.providers.get_mut(provider_id) {
|
||||
if let Some(obj) = target.settings_config.as_object_mut() {
|
||||
obj.insert(
|
||||
"config".to_string(),
|
||||
serde_json::Value::String(cfg_text_after),
|
||||
);
|
||||
let mut restored = serde_json::json!({
|
||||
"auth": auth.clone(),
|
||||
"config": cfg_text_after,
|
||||
});
|
||||
let restore_provider_token =
|
||||
crate::codex_config::should_restore_codex_provider_token_for_backfill(
|
||||
provider.category.as_deref(),
|
||||
&provider.settings_config,
|
||||
);
|
||||
crate::codex_config::restore_codex_settings_for_backfill(
|
||||
&mut restored,
|
||||
&provider.settings_config,
|
||||
restore_provider_token,
|
||||
)?;
|
||||
// 必须同时写回 auth 和 config: backfill 会恢复 stored provider id,
|
||||
// 并把 live 的 experimental_bearer_token 移到 restored.auth.OPENAI_API_KEY。
|
||||
if let Some(restored_obj) = restored.as_object() {
|
||||
if let Some(auth_value) = restored_obj.get("auth") {
|
||||
obj.insert("auth".to_string(), auth_value.clone());
|
||||
}
|
||||
if let Some(config_value) = restored_obj.get("config") {
|
||||
obj.insert("config".to_string(), config_value.clone());
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user