mirror of
https://github.com/openai/codex.git
synced 2026-10-20 17:38:45 +00:00
## What changed Introduce a shared `Auth` abstraction that supplies sensitive authorization headers and the client's redirect policy. Preserve the existing static API key allocation and default redirect behavior, and return an empty `502` response if header acquisition fails. Unify the stdin authentication entry point around platform-specific `read_from_stdin` helpers. ## Testing Add a test verifying that both compact and pretty `Debug` output redact API keys. GitOrigin-RevId: 6a924534f00c1b209b24e8b0af117091cfa3ec07
50 lines
1.3 KiB
Rust
50 lines
1.3 KiB
Rust
//! Supplies sensitive request headers without copying the locked API key.
|
|
|
|
use std::fmt;
|
|
|
|
use anyhow::Result;
|
|
use reqwest::header::HeaderValue;
|
|
|
|
pub(crate) enum Auth {
|
|
ApiKey(&'static str),
|
|
}
|
|
|
|
impl fmt::Debug for Auth {
|
|
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
|
|
match self {
|
|
Self::ApiKey(_) => f.debug_tuple("ApiKey").field(&"[REDACTED]").finish(),
|
|
}
|
|
}
|
|
}
|
|
|
|
#[derive(Clone, Debug)]
|
|
pub(crate) struct AuthHeader {
|
|
pub(crate) value: HeaderValue,
|
|
}
|
|
|
|
impl Auth {
|
|
pub(crate) fn redirect_policy(&self) -> reqwest::redirect::Policy {
|
|
match self {
|
|
Self::ApiKey(_) => reqwest::redirect::Policy::default(),
|
|
}
|
|
}
|
|
|
|
/// Returns a sensitive authorization header. API keys are validated when read,
|
|
/// so this currently always succeeds. The fallible return type allows later
|
|
/// credential providers to report acquisition failures.
|
|
pub(crate) fn header(&self) -> Result<AuthHeader> {
|
|
match self {
|
|
Self::ApiKey(value) => {
|
|
// Keep the existing locked, static API key allocation.
|
|
let mut header = HeaderValue::from_static(value);
|
|
header.set_sensitive(true);
|
|
Ok(AuthHeader { value: header })
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
#[cfg(test)]
|
|
#[path = "auth_tests.rs"]
|
|
mod tests;
|