Files
codex/codex-rs/model-provider/src/bearer_auth_provider.rs
T
ioanbudea-OAI 68334831d8 Extract model-provider unit tests into separate files (#58936)
## What changed

Move inline tests for authentication, bearer auth, model endpoints, and providers into dedicated `*_tests.rs` files. Register workspace-routing tests under the `workspace_routing` module and keep test-only imports with the tests.

GitOrigin-RevId: 9ba043c82814e27be63432a0e13a4929dc3ea372
2026-11-02 17:35:57 +00:00

56 lines
1.7 KiB
Rust

use codex_api::AuthProvider;
use codex_http_client::CHATGPT_ACCOUNT_ID_HEADER;
use codex_login::auth::is_fedramp_api_key;
use http::HeaderMap;
use http::HeaderValue;
/// Bearer-token auth provider for OpenAI-compatible model-provider requests.
#[derive(Clone, Default)]
pub struct BearerAuthProvider {
pub token: Option<String>,
pub account_id: Option<String>,
pub is_fedramp_account: bool,
}
impl BearerAuthProvider {
pub fn new(token: String) -> Self {
let is_fedramp_account = is_fedramp_api_key(&token);
Self {
token: Some(token),
account_id: None,
is_fedramp_account,
}
}
pub fn for_test(token: Option<&str>, account_id: Option<&str>) -> Self {
Self {
token: token.map(str::to_string),
account_id: account_id.map(str::to_string),
is_fedramp_account: false,
}
}
}
impl AuthProvider for BearerAuthProvider {
fn add_auth_headers(&self, headers: &mut HeaderMap) -> Result<(), codex_api::AuthError> {
if let Some(token) = self.token.as_ref()
&& let Ok(header) = HeaderValue::from_str(&format!("Bearer {token}"))
{
let _ = headers.insert(http::header::AUTHORIZATION, header);
}
if let Some(account_id) = self.account_id.as_ref()
&& let Ok(header) = HeaderValue::from_str(account_id)
{
let _ = headers.insert(CHATGPT_ACCOUNT_ID_HEADER, header);
}
if self.is_fedramp_account {
let _ = headers.insert("X-OpenAI-Fedramp", HeaderValue::from_static("true"));
}
Ok(())
}
}
#[cfg(test)]
#[path = "bearer_auth_provider_tests.rs"]
mod tests;