mirror of
https://github.com/openai/codex.git
synced 2026-11-21 22:50:37 +00:00
Use runtime authentication for TUI WebRTC calls (#62870)
## Why The TUI creates WebRTC calls locally, and that request path did not use runtime agent authentication even when runtime agent instances were enabled. ## What changed - Reuse local credential and runtime managers across calls while the login configuration stays the same. Reload first-party credentials before creating a call and keep network policy under the primary account owner's control. - Use the shared provider request resolver for call authentication and HTTP transport. Apply the WebRTC base URL override before resolving credentials so runtime authentication is selected for the actual destination. ## Testing Add mock-server coverage for concurrent calls sharing a runtime with fresh assertions, credential changes, and both dual-header and assertion-only modes. Cover the feature toggle, custom call destinations, network policy permits, and provider credentials when the local login file is malformed. GitOrigin-RevId: 6503b3375c79d3126911d1d7c1aea898face37b9
This commit is contained in:
@@ -569,6 +569,7 @@ pub(crate) struct App {
|
||||
workspace_command_runner: Option<WorkspaceCommandRunner>,
|
||||
/// Legacy bootstrap and server-setting inputs; local preferences live in `local_settings`.
|
||||
pub(crate) config: Config,
|
||||
realtime_call_client: Arc<crate::realtime_call::Client>,
|
||||
pub(crate) local_settings: crate::local_settings::LocalSettings,
|
||||
backend_preference_context: backend_selection::BackendPreferenceContext,
|
||||
launch_cwd: PathBuf,
|
||||
|
||||
@@ -1035,6 +1035,7 @@ See the Codex keymap documentation for supported actions and examples."
|
||||
backend_preference_context,
|
||||
config,
|
||||
local_settings,
|
||||
realtime_call_client: Arc::default(),
|
||||
launch_cwd,
|
||||
runtime_working_directory_override: None,
|
||||
state_db,
|
||||
|
||||
@@ -65,6 +65,7 @@ pub(crate) async fn make_test_app() -> Box<App> {
|
||||
local_settings: crate::local_settings::LocalSettings::from(&config),
|
||||
config,
|
||||
state_db: None,
|
||||
realtime_call_client: Arc::default(),
|
||||
cli_kv_overrides: Vec::new(),
|
||||
harness_overrides: ConfigOverrides::default(),
|
||||
loader_overrides: LoaderOverrides::without_managed_config_for_tests(),
|
||||
|
||||
@@ -9192,6 +9192,7 @@ async fn make_test_app() -> Box<App> {
|
||||
statusline_picker: None,
|
||||
statusline_metrics: Default::default(),
|
||||
provider_writes: HashMap::new(),
|
||||
realtime_call_client: Arc::default(),
|
||||
feature_write_lock: Arc::default(),
|
||||
config_notification_test_generation: 0,
|
||||
config_notification_test_pending: None,
|
||||
@@ -9339,6 +9340,7 @@ pub(super) async fn make_test_app_with_channels() -> (
|
||||
statusline_picker: None,
|
||||
statusline_metrics: Default::default(),
|
||||
provider_writes: HashMap::new(),
|
||||
realtime_call_client: Arc::default(),
|
||||
feature_write_lock: Arc::default(),
|
||||
config_notification_test_generation: 0,
|
||||
config_notification_test_pending: None,
|
||||
|
||||
@@ -130,13 +130,15 @@ async fn delegated_core_events_keep_private_output_hidden_and_deliver_final_spee
|
||||
let thread = app_server
|
||||
.thread_read(thread_id, /*include_turns*/ false)
|
||||
.await?;
|
||||
let call = crate::realtime_call::create(
|
||||
&app.config,
|
||||
&thread,
|
||||
"v=offer\r\n".to_string(),
|
||||
/*voice*/ None,
|
||||
)
|
||||
.await?;
|
||||
let call = app
|
||||
.realtime_call_client
|
||||
.create(
|
||||
&app.config,
|
||||
&thread,
|
||||
"v=offer\r\n".to_string(),
|
||||
/*voice*/ None,
|
||||
)
|
||||
.await?;
|
||||
assert_eq!(String::from(call.answer_sdp), "v=answer\r\n");
|
||||
app_server
|
||||
.thread_realtime_start(thread_id, call.call_id, call.voice_session_id)
|
||||
|
||||
@@ -921,6 +921,7 @@ impl App {
|
||||
let config = self.config.clone();
|
||||
let cwd = self.chat_widget.config_ref().cwd.clone();
|
||||
let offer = String::from(offer_sdp.clone());
|
||||
let call_client = Arc::clone(&self.realtime_call_client);
|
||||
let tx = self.app_event_tx.clone();
|
||||
tokio::spawn(async move {
|
||||
let create = async {
|
||||
@@ -928,7 +929,7 @@ impl App {
|
||||
let thread = server
|
||||
.thread_read(thread_id, /*include_turns*/ false)
|
||||
.await?;
|
||||
crate::realtime_call::create(&config, &thread, offer, voice).await
|
||||
call_client.create(&config, &thread, offer, voice).await
|
||||
};
|
||||
if let Ok(result) = futures::future::Abortable::new(create, registration).await
|
||||
{
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
//! Negotiate the TUI's media connection using the local user's provider and credentials.
|
||||
//! Negotiate media with the local user's credentials, sharing their runtime across TUI calls.
|
||||
|
||||
use crate::app_command::RealtimeSdp;
|
||||
use crate::legacy_core::config::Config;
|
||||
@@ -6,17 +6,15 @@ use codex_api::RealtimeCallClient;
|
||||
use codex_api::RealtimeEventParser;
|
||||
use codex_api::RealtimeSessionConfig;
|
||||
use codex_api::RealtimeSessionMode;
|
||||
use codex_api::ReqwestTransport;
|
||||
use codex_api::build_realtime_thread_headers;
|
||||
use codex_app_server_protocol::Thread;
|
||||
use codex_config::config_toml::RealtimeWsMode;
|
||||
use codex_http_client::ClientRouteClass;
|
||||
use codex_features::Feature;
|
||||
use codex_login::default_client::ClientRedirectPolicy;
|
||||
use codex_login::default_client::add_originator_header;
|
||||
use codex_login::default_client::create_client_for_route;
|
||||
use codex_login::default_client::originator;
|
||||
use codex_model_provider::create_model_provider_from_local_config;
|
||||
use codex_model_provider::prepare_provider_request;
|
||||
use codex_model_provider::LocalProviderAuth;
|
||||
use codex_model_provider::resolve_request;
|
||||
use codex_prompts::prepare_realtime_backend_prompt;
|
||||
use codex_protocol::protocol::RealtimeOutputModality;
|
||||
use codex_protocol::protocol::RealtimeVoice;
|
||||
@@ -25,6 +23,12 @@ use color_eyre::eyre::ensure;
|
||||
use http::HeaderValue;
|
||||
use uuid::Uuid;
|
||||
|
||||
/// Keep the local credential owner and its runtime across voice calls.
|
||||
#[derive(Default)]
|
||||
pub(crate) struct Client {
|
||||
auth: LocalProviderAuth,
|
||||
}
|
||||
|
||||
#[derive(Debug)]
|
||||
pub(crate) struct CreatedCall {
|
||||
pub(crate) call_id: String,
|
||||
@@ -32,75 +36,83 @@ pub(crate) struct CreatedCall {
|
||||
pub(crate) answer_sdp: RealtimeSdp,
|
||||
}
|
||||
|
||||
pub(crate) async fn create(
|
||||
config: &Config,
|
||||
thread: &Thread,
|
||||
offer: String,
|
||||
voice: Option<RealtimeVoice>,
|
||||
) -> Result<CreatedCall> {
|
||||
ensure!(
|
||||
config.realtime.session_type == RealtimeWsMode::Conversational,
|
||||
"AVAS realtime calls require conversational realtime"
|
||||
);
|
||||
let provider =
|
||||
create_model_provider_from_local_config(config.model_provider.clone(), config).await?;
|
||||
let (mut api_provider, resolved_auth) =
|
||||
prepare_provider_request(provider.as_ref(), provider.info().auth.is_none(), |_| {
|
||||
provider.request_provider_and_auth(/*scope*/ None)
|
||||
})
|
||||
impl Client {
|
||||
pub(crate) async fn create(
|
||||
&self,
|
||||
config: &Config,
|
||||
thread: &Thread,
|
||||
offer: String,
|
||||
voice: Option<RealtimeVoice>,
|
||||
) -> Result<CreatedCall> {
|
||||
ensure!(
|
||||
config.realtime.session_type == RealtimeWsMode::Conversational,
|
||||
"AVAS realtime calls require conversational realtime"
|
||||
);
|
||||
let mut info = config.model_provider.clone();
|
||||
if let Some(url) = &config.experimental_realtime_webrtc_call_base_url {
|
||||
info.base_url = Some(url.clone());
|
||||
}
|
||||
let runtime_auth_mode = config
|
||||
.features
|
||||
.enabled(Feature::RuntimeAgentInstances)
|
||||
.then_some(config.runtime_auth_mode);
|
||||
let provider = self
|
||||
.auth
|
||||
.model_provider(info, config.auth_config(), runtime_auth_mode)
|
||||
.await?;
|
||||
let mut request = resolve_request(
|
||||
provider.as_ref(),
|
||||
/*scope*/ None,
|
||||
/*routing*/ None,
|
||||
config.http_client_factory(),
|
||||
)
|
||||
.await?;
|
||||
if let Some(url) = &config.experimental_realtime_webrtc_call_base_url {
|
||||
api_provider.base_url.clone_from(url);
|
||||
request.redirect_policy = ClientRedirectPolicy::Reject;
|
||||
let transport = request.http_transport("realtime/calls")?;
|
||||
let id = thread.id.clone();
|
||||
let voice_session_id = Uuid::new_v4();
|
||||
let mut headers = build_realtime_thread_headers(
|
||||
thread.session_id.clone(),
|
||||
id.clone(),
|
||||
thread.thread_source.clone().map(Into::into),
|
||||
);
|
||||
headers.insert("openai-alpha", HeaderValue::from_static("quicksilver=v2"));
|
||||
headers.insert("x-session-id", HeaderValue::from_str(&id)?);
|
||||
headers.insert(
|
||||
"voice-session-id",
|
||||
HeaderValue::from_str(&voice_session_id.to_string())?,
|
||||
);
|
||||
add_originator_header(&mut headers, &originator().value);
|
||||
let session = RealtimeSessionConfig {
|
||||
instructions: prepare_realtime_backend_prompt(
|
||||
/*prompt*/ None,
|
||||
config.experimental_realtime_ws_backend_prompt.clone(),
|
||||
),
|
||||
initial_items: Vec::new(),
|
||||
delegation_ack_filler: None,
|
||||
model: Some(
|
||||
config
|
||||
.experimental_realtime_ws_model
|
||||
.clone()
|
||||
.unwrap_or_else(|| "gpt-live-1.1-codex".to_string()),
|
||||
),
|
||||
session_id: Some(id),
|
||||
event_parser: RealtimeEventParser::FramelessBidi,
|
||||
session_mode: RealtimeSessionMode::Conversational,
|
||||
output_modality: RealtimeOutputModality::Audio,
|
||||
voice,
|
||||
};
|
||||
let call = RealtimeCallClient::new(transport, request.api_provider, request.api_auth)
|
||||
.create_with_session_and_headers(offer, session, headers)
|
||||
.await?;
|
||||
Ok(CreatedCall {
|
||||
call_id: call.call_id,
|
||||
voice_session_id,
|
||||
answer_sdp: call.sdp.into(),
|
||||
})
|
||||
}
|
||||
let client = create_client_for_route(
|
||||
&config.http_client_factory(),
|
||||
&api_provider.url_for_path("realtime/calls"),
|
||||
ClientRouteClass::Api,
|
||||
ClientRedirectPolicy::Reject,
|
||||
)?;
|
||||
let id = thread.id.clone();
|
||||
let voice_session_id = Uuid::new_v4();
|
||||
let mut headers = build_realtime_thread_headers(
|
||||
thread.session_id.clone(),
|
||||
id.clone(),
|
||||
thread.thread_source.clone().map(Into::into),
|
||||
);
|
||||
headers.insert("openai-alpha", HeaderValue::from_static("quicksilver=v2"));
|
||||
headers.insert("x-session-id", HeaderValue::from_str(&id)?);
|
||||
headers.insert(
|
||||
"voice-session-id",
|
||||
HeaderValue::from_str(&voice_session_id.to_string())?,
|
||||
);
|
||||
add_originator_header(&mut headers, &originator().value);
|
||||
let session = RealtimeSessionConfig {
|
||||
instructions: prepare_realtime_backend_prompt(
|
||||
/*prompt*/ None,
|
||||
config.experimental_realtime_ws_backend_prompt.clone(),
|
||||
),
|
||||
initial_items: Vec::new(),
|
||||
delegation_ack_filler: None,
|
||||
model: Some(
|
||||
config
|
||||
.experimental_realtime_ws_model
|
||||
.clone()
|
||||
.unwrap_or_else(|| "gpt-live-1.1-codex".to_string()),
|
||||
),
|
||||
session_id: Some(id),
|
||||
event_parser: RealtimeEventParser::FramelessBidi,
|
||||
session_mode: RealtimeSessionMode::Conversational,
|
||||
output_modality: RealtimeOutputModality::Audio,
|
||||
voice,
|
||||
};
|
||||
let call = RealtimeCallClient::new(
|
||||
ReqwestTransport::from_http_client(client),
|
||||
api_provider,
|
||||
resolved_auth.auth,
|
||||
)
|
||||
.create_with_session_and_headers(offer, session, headers)
|
||||
.await?;
|
||||
Ok(CreatedCall {
|
||||
call_id: call.call_id,
|
||||
voice_session_id,
|
||||
answer_sdp: call.sdp.into(),
|
||||
})
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
#[path = "realtime_call_tests.rs"]
|
||||
mod tests;
|
||||
|
||||
@@ -0,0 +1,309 @@
|
||||
#![cfg(test)]
|
||||
|
||||
//! Verify local voice calls use the selected auth mode and retain their credential lifecycle.
|
||||
|
||||
use super::Client;
|
||||
use crate::legacy_core::config::Config;
|
||||
use crate::legacy_core::config::ConfigBuilder;
|
||||
use anyhow::Context;
|
||||
use anyhow::Result;
|
||||
use base64::Engine;
|
||||
use codex_app_server_protocol::Thread;
|
||||
use codex_config::LoaderOverrides;
|
||||
use codex_config::config_toml::RealtimeWsMode;
|
||||
use codex_features::Feature;
|
||||
use codex_features::RuntimeAuthMode;
|
||||
use codex_http_client::DestinationPolicy;
|
||||
use codex_http_client::NetworkPolicyController;
|
||||
use codex_login::AuthCredentialsStoreMode;
|
||||
use codex_login::login_with_api_key;
|
||||
use codex_protocol::config_types::ForcedLoginMethod;
|
||||
use codex_protocol::config_types::ModelProviderAuthInfo;
|
||||
use core_test_support::runtime_auth::PROBE_SERVER;
|
||||
use core_test_support::runtime_auth::run_harness_through_proxy_with_env;
|
||||
use core_test_support::skip_if_no_network;
|
||||
use pretty_assertions::assert_eq;
|
||||
use serde_json::Value;
|
||||
use serde_json::json;
|
||||
use std::collections::BTreeSet;
|
||||
use std::num::NonZeroU64;
|
||||
use wiremock::Mock;
|
||||
use wiremock::MockServer;
|
||||
use wiremock::ResponseTemplate;
|
||||
use wiremock::matchers::body_partial_json;
|
||||
use wiremock::matchers::method;
|
||||
use wiremock::matchers::path;
|
||||
|
||||
const KEY_A: &str = "test-realtime-key-a";
|
||||
const KEY_B: &str = "test-realtime-key-b";
|
||||
|
||||
fn sign_in(config: &Config, key: &str) -> Result<()> {
|
||||
login_with_api_key(
|
||||
config.codex_home.as_path(),
|
||||
key,
|
||||
config.cli_auth_credentials_store_mode,
|
||||
config.auth_keyring_backend_kind(),
|
||||
)?;
|
||||
Ok(())
|
||||
}
|
||||
|
||||
async fn create_call(client: &Client, config: &Config) -> Result<()> {
|
||||
let thread: Thread = serde_json::from_value(json!({
|
||||
"id": "00000000-0000-0000-0000-000000000014",
|
||||
"sessionId": "00000000-0000-0000-0000-000000000014",
|
||||
"preview": "", "ephemeral": true, "modelProvider": "openai",
|
||||
"createdAt": 0, "updatedAt": 0, "status": {"type": "idle"},
|
||||
"cwd": config.cwd, "cliVersion": "test", "source": "cli", "turns": [],
|
||||
}))?;
|
||||
let call = client
|
||||
.create(
|
||||
config,
|
||||
&thread,
|
||||
"offer-sdp".to_string(),
|
||||
/*voice*/ None,
|
||||
)
|
||||
.await
|
||||
.map_err(|error| anyhow::anyhow!("{error:#}"))?;
|
||||
assert_eq!(
|
||||
(call.call_id, String::from(call.answer_sdp)),
|
||||
("rtc_tui".to_string(), "answer-sdp".to_string()),
|
||||
);
|
||||
Ok(())
|
||||
}
|
||||
|
||||
#[tokio::test(flavor = "multi_thread", worker_threads = 2)]
|
||||
async fn local_calls_share_runtime_and_follow_auth_changes() -> Result<()> {
|
||||
skip_if_no_network!(Ok(()));
|
||||
let modes = [RuntimeAuthMode::DualHeader, RuntimeAuthMode::AssertionOnly];
|
||||
if let Ok(server) = std::env::var(PROBE_SERVER) {
|
||||
for mode in modes {
|
||||
let home = tempfile::tempdir()?;
|
||||
let mut config = ConfigBuilder::default()
|
||||
.codex_home(home.path().to_path_buf())
|
||||
.loader_overrides(LoaderOverrides::without_managed_config_for_tests())
|
||||
.build()
|
||||
.await?;
|
||||
config.cli_auth_credentials_store_mode = AuthCredentialsStoreMode::File;
|
||||
config.realtime.session_type = RealtimeWsMode::Conversational;
|
||||
config.runtime_auth_mode = mode;
|
||||
config
|
||||
.features
|
||||
.set_enabled(Feature::RuntimeAgentInstances, /*enabled*/ false)?;
|
||||
let controller = NetworkPolicyController::default();
|
||||
let policy = controller.policy();
|
||||
config.application_network_policy = policy.clone();
|
||||
sign_in(&config, KEY_A)?;
|
||||
assert!(controller.publish(policy.revision(), DestinationPolicy::Unrestricted));
|
||||
let destination = "https://api.openai.com/v1/live".parse()?;
|
||||
let account_a_permit = policy.acquire(&destination)?;
|
||||
let client = Client::default();
|
||||
create_call(&client, &config).await?;
|
||||
|
||||
config.features.enable(Feature::RuntimeAgentInstances)?;
|
||||
tokio::try_join!(create_call(&client, &config), create_call(&client, &config))?;
|
||||
sign_in(&config, KEY_B)?;
|
||||
// The application owner refreshes the managed policy before starting the call.
|
||||
policy.invalidate();
|
||||
assert!(account_a_permit.check().is_err());
|
||||
assert!(controller.publish(policy.revision(), DestinationPolicy::Unrestricted));
|
||||
let account_b_permit = policy.acquire(&destination)?;
|
||||
create_call(&client, &config).await?;
|
||||
account_b_permit.check()?;
|
||||
|
||||
config
|
||||
.features
|
||||
.set_enabled(Feature::RuntimeAgentInstances, /*enabled*/ false)?;
|
||||
create_call(&client, &config).await?;
|
||||
config.features.enable(Feature::RuntimeAgentInstances)?;
|
||||
config.experimental_realtime_webrtc_call_base_url = Some(format!("{server}/v1"));
|
||||
create_call(&client, &config).await?;
|
||||
|
||||
config.forced_login_method = Some(ForcedLoginMethod::Chatgpt);
|
||||
assert!(create_call(&client, &config).await.is_err());
|
||||
}
|
||||
return Ok(());
|
||||
}
|
||||
|
||||
let server = MockServer::start().await;
|
||||
for (key, runtime) in [(KEY_A, "runtime-a"), (KEY_B, "runtime-b")] {
|
||||
Mock::given(method("POST"))
|
||||
.and(path("/agent/register"))
|
||||
.and(body_partial_json(
|
||||
json!({"actor_auth": {"credential": key}}),
|
||||
))
|
||||
.respond_with(ResponseTemplate::new(200).set_body_json(json!({
|
||||
"runtime_agent_instance_id": runtime, "agent_host_id": "host-tui",
|
||||
"actor": {"kind": "platform", "user_id": "user-tui",
|
||||
"organization_id": "org-tui", "project_id": "proj-tui"},
|
||||
})))
|
||||
.expect(2)
|
||||
.mount(&server)
|
||||
.await;
|
||||
}
|
||||
Mock::given(method("POST"))
|
||||
.and(path("/v1/live"))
|
||||
.respond_with(|request: &wiremock::Request| {
|
||||
if !request.headers.contains_key("authorization") {
|
||||
return ResponseTemplate::new(401);
|
||||
}
|
||||
ResponseTemplate::new(201)
|
||||
.insert_header("location", "/v1/live/rtc_tui")
|
||||
.set_body_string("answer-sdp")
|
||||
})
|
||||
.mount(&server)
|
||||
.await;
|
||||
run_harness_through_proxy_with_env(
|
||||
&server,
|
||||
"realtime_call::tests::local_calls_share_runtime_and_follow_auth_changes",
|
||||
/*realtime_target*/ None,
|
||||
&[
|
||||
("CODEX_API_KEY", None),
|
||||
("OPENAI_API_KEY", None),
|
||||
("CODEX_ACCESS_TOKEN", None),
|
||||
],
|
||||
)
|
||||
.await?;
|
||||
server.verify().await;
|
||||
let requests = server
|
||||
.received_requests()
|
||||
.await
|
||||
.context("captured requests")?;
|
||||
let calls = requests
|
||||
.iter()
|
||||
.filter(|request| request.url.path() == "/v1/live")
|
||||
.collect::<Vec<_>>();
|
||||
for request in &calls {
|
||||
if !request.headers.contains_key("authorization") {
|
||||
assert!(!request.headers.contains_key("x-openai-agent-assertion"));
|
||||
}
|
||||
}
|
||||
let calls = calls
|
||||
.into_iter()
|
||||
.filter(|request| request.headers.contains_key("authorization"))
|
||||
.collect::<Vec<_>>();
|
||||
assert_eq!(calls.len(), 12);
|
||||
let mut proofs = BTreeSet::new();
|
||||
for (mode, calls) in modes.into_iter().zip(calls.as_chunks::<6>().0) {
|
||||
for (index, request) in calls.iter().enumerate() {
|
||||
let key = if index < 3 { KEY_A } else { KEY_B };
|
||||
let authorization = request.headers["authorization"].to_str()?;
|
||||
if matches!(index, 0 | 4 | 5) {
|
||||
assert_eq!(authorization, format!("Bearer {key}"));
|
||||
assert!(!request.headers.contains_key("x-openai-agent-assertion"));
|
||||
continue;
|
||||
}
|
||||
let proof = match mode {
|
||||
RuntimeAuthMode::DualHeader => {
|
||||
assert_eq!(authorization, format!("Bearer {key}"));
|
||||
request.headers["x-openai-agent-assertion"].to_str()?
|
||||
}
|
||||
RuntimeAuthMode::AssertionOnly => {
|
||||
assert!(!request.headers.contains_key("x-openai-agent-assertion"));
|
||||
authorization
|
||||
.strip_prefix("AgentAssertion ")
|
||||
.context("runtime auth")?
|
||||
}
|
||||
};
|
||||
assert!(
|
||||
proofs.insert(proof.to_string()),
|
||||
"each call needs a fresh assertion"
|
||||
);
|
||||
let payload = proof
|
||||
.strip_prefix("1.")
|
||||
.context("assertion version")?
|
||||
.split('.')
|
||||
.nth(1)
|
||||
.context("assertion payload")?;
|
||||
let claims: Value = serde_json::from_slice(
|
||||
&base64::engine::general_purpose::URL_SAFE_NO_PAD.decode(payload)?,
|
||||
)?;
|
||||
assert_eq!(
|
||||
claims["runtime_agent_instance"],
|
||||
if index < 3 { "runtime-a" } else { "runtime-b" },
|
||||
);
|
||||
}
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
|
||||
#[tokio::test(flavor = "multi_thread", worker_threads = 2)]
|
||||
async fn local_calls_use_provider_credentials_when_login_storage_fails() -> Result<()> {
|
||||
skip_if_no_network!(Ok(()));
|
||||
if std::env::var(PROBE_SERVER).is_ok() {
|
||||
let home = tempfile::tempdir()?;
|
||||
let mut config = ConfigBuilder::default()
|
||||
.codex_home(home.path().to_path_buf())
|
||||
.loader_overrides(LoaderOverrides::without_managed_config_for_tests())
|
||||
.build()
|
||||
.await?;
|
||||
config.cli_auth_credentials_store_mode = AuthCredentialsStoreMode::File;
|
||||
config.realtime.session_type = RealtimeWsMode::Conversational;
|
||||
config
|
||||
.features
|
||||
.set_enabled(Feature::RuntimeAgentInstances, /*enabled*/ false)?;
|
||||
sign_in(&config, KEY_A)?;
|
||||
let client = Client::default();
|
||||
create_call(&client, &config).await?;
|
||||
std::fs::write(home.path().join("auth.json"), "malformed credentials")?;
|
||||
|
||||
let original_provider = config.model_provider.clone();
|
||||
config.model_provider.experimental_bearer_token = Some(KEY_B.into());
|
||||
create_call(&client, &config).await?;
|
||||
|
||||
config.model_provider = original_provider.clone();
|
||||
config.model_provider.env_key = Some("TUI_TEST_PROVIDER_KEY".to_string());
|
||||
create_call(&client, &config).await?;
|
||||
|
||||
config.model_provider = original_provider.clone();
|
||||
config.model_provider.requires_openai_auth = false;
|
||||
let (command, args) = if cfg!(windows) {
|
||||
("cmd.exe", vec!["/C".to_string(), format!("echo {KEY_B}")])
|
||||
} else {
|
||||
("sh", vec!["-c".to_string(), format!("printf %s {KEY_B}")])
|
||||
};
|
||||
config.model_provider.auth = Some(ModelProviderAuthInfo {
|
||||
command: command.to_string(),
|
||||
args: args.into_iter().map(Into::into).collect(),
|
||||
timeout_ms: NonZeroU64::new(5_000).context("nonzero timeout")?,
|
||||
refresh_interval_ms: 300_000,
|
||||
cwd: config.cwd.clone(),
|
||||
});
|
||||
create_call(&client, &config).await?;
|
||||
|
||||
config.model_provider = original_provider;
|
||||
assert!(create_call(&client, &config).await.is_err());
|
||||
return Ok(());
|
||||
}
|
||||
|
||||
let server = MockServer::start().await;
|
||||
for (key, calls) in [(KEY_A, 1), (KEY_B, 3)] {
|
||||
Mock::given(method("POST"))
|
||||
.and(path("/v1/live"))
|
||||
.and(wiremock::matchers::header(
|
||||
"authorization",
|
||||
format!("Bearer {key}"),
|
||||
))
|
||||
.respond_with(
|
||||
ResponseTemplate::new(201)
|
||||
.insert_header("location", "/v1/live/rtc_tui")
|
||||
.set_body_string("answer-sdp"),
|
||||
)
|
||||
.expect(calls)
|
||||
.mount(&server)
|
||||
.await;
|
||||
}
|
||||
run_harness_through_proxy_with_env(
|
||||
&server,
|
||||
"realtime_call::tests::local_calls_use_provider_credentials_when_login_storage_fails",
|
||||
/*realtime_target*/ None,
|
||||
&[
|
||||
("CODEX_API_KEY", None),
|
||||
("OPENAI_API_KEY", None),
|
||||
("CODEX_ACCESS_TOKEN", None),
|
||||
("TUI_TEST_PROVIDER_KEY", Some(KEY_B)),
|
||||
],
|
||||
)
|
||||
.await?;
|
||||
server.verify().await;
|
||||
Ok(())
|
||||
}
|
||||
Reference in New Issue
Block a user