Base: codex-main originale (prima del fork aKode)
This commit is contained in:
55
codex-rs/model-provider/src/bearer_auth_provider.rs
Normal file
55
codex-rs/model-provider/src/bearer_auth_provider.rs
Normal file
@@ -0,0 +1,55 @@
|
||||
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;
|
||||
Reference in New Issue
Block a user