Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 17 additions & 0 deletions src/harness/providers/openai/test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2007,6 +2007,23 @@ fn auth_headers_bearer_is_the_default() {
assert_eq!(AuthStyle::default(), AuthStyle::Bearer);
}

#[test]
fn auth_headers_basic_prefixes_the_credential() {
let headers = auth_headers(&AuthStyle::Basic, "base64-credential");
assert_eq!(
headers,
vec![(
"Authorization".to_string(),
"Basic base64-credential".to_string()
)]
);
}

#[test]
fn auth_headers_basic_omits_an_empty_credential() {
assert!(auth_headers(&AuthStyle::Basic, "").is_empty());
}

#[test]
fn auth_headers_x_api_key_sends_bare_key_without_authorization() {
let headers = auth_headers(&AuthStyle::XApiKey, "secret");
Expand Down
12 changes: 8 additions & 4 deletions src/harness/providers/openai/transport.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,17 +16,19 @@ use crate::harness::model::StreamAccumulator;
/// How the provider expects the API credential to be sent on each request.
///
/// OpenAI-compatible endpoints diverge on auth: hosted OpenAI and most gateways
/// use `Bearer`, some providers use a bare `x-api-key`, Anthropic's compat
/// endpoint pairs `x-api-key` with an `anthropic-version` header, and a few need
/// an arbitrary custom header carrying the raw credential. Defaults to
/// [`AuthStyle::Bearer`].
/// use `Bearer`, Inworld uses HTTP Basic credentials, some providers use a bare
/// `x-api-key`, Anthropic's compat endpoint pairs `x-api-key` with an
/// `anthropic-version` header, and a few need an arbitrary custom header
/// carrying the raw credential. Defaults to [`AuthStyle::Bearer`].
#[derive(Debug, Clone, Default, PartialEq, Eq)]
pub enum AuthStyle {
/// No authentication header (e.g. a local Ollama server).
None,
/// `Authorization: Bearer <key>` — hosted OpenAI and most gateways.
#[default]
Bearer,
/// `Authorization: Basic <credential>` — used by Inworld and compatible APIs.
Basic,
/// `x-api-key: <key>` — used by some OpenAI-compatible providers.
XApiKey,
/// `x-api-key: <key>` + `anthropic-version: 2023-06-01` (Anthropic compat).
Expand Down Expand Up @@ -199,6 +201,8 @@ pub(super) fn auth_headers(auth: &AuthStyle, api_key: &str) -> Vec<(String, Stri
match auth {
AuthStyle::None => Vec::new(),
AuthStyle::Bearer => vec![("Authorization".to_string(), format!("Bearer {api_key}"))],
AuthStyle::Basic if api_key.is_empty() => Vec::new(),
AuthStyle::Basic => vec![("Authorization".to_string(), format!("Basic {api_key}"))],
AuthStyle::XApiKey => vec![("x-api-key".to_string(), api_key.to_string())],
AuthStyle::Anthropic => vec![
("x-api-key".to_string(), api_key.to_string()),
Expand Down