use codex_client::Request; use http::HeaderMap; use http::HeaderValue; /// Provides bearer and account identity information for API requests. /// /// Implementations should be cheap and non-blocking; any asynchronous /// refresh or I/O should be handled by higher layers before requests /// reach this interface. pub trait AuthProvider: Send + Sync { fn bearer_token(&self) -> Option; fn authorization_header_value(&self) -> Option { self.bearer_token().map(|token| format!("Bearer {token}")) } fn account_id(&self) -> Option { None } } pub(crate) fn add_auth_headers_to_header_map(auth: &A, headers: &mut HeaderMap) { if let Some(authorization) = auth.authorization_header_value() && let Ok(header) = HeaderValue::from_str(&authorization) { let _ = headers.insert(http::header::AUTHORIZATION, header); } if let Some(account_id) = auth.account_id() && let Ok(header) = HeaderValue::from_str(&account_id) { let _ = headers.insert("ChatGPT-Account-ID", header); } } pub(crate) fn add_auth_headers(auth: &A, mut req: Request) -> Request { add_auth_headers_to_header_map(auth, &mut req.headers); req }