Skip to content
Closed
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
2 changes: 2 additions & 0 deletions rust/Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

9 changes: 8 additions & 1 deletion rust/crates/api/src/client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,14 @@ impl ProviderClient {
}
_ => OpenAiCompatConfig::openai(),
};
Ok(Self::OpenAi(OpenAiCompatClient::from_env(config)?))
// Try OAuth for OpenAI if env var is not set
if config.provider_name == "OpenAI" {
Ok(Self::OpenAi(OpenAiCompatClient::from_env_or_oauth(
config, "openai",
)?))
} else {
Ok(Self::OpenAi(OpenAiCompatClient::from_env(config)?))
}
}
}
}
Expand Down
4 changes: 2 additions & 2 deletions rust/crates/api/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,9 @@ pub use prompt_cache::{
CacheBreakEvent, PromptCache, PromptCacheConfig, PromptCachePaths, PromptCacheRecord,
PromptCacheStats,
};
pub use providers::anthropic::{AnthropicClient, AnthropicClient as ApiClient, AuthSource};
pub use providers::anthropic::{has_auth_from_env_or_saved as anthropic_has_auth, AnthropicClient, AnthropicClient as ApiClient, AuthSource};
pub use providers::openai_compat::{
build_chat_completion_request, flatten_tool_result_content, is_reasoning_model,
build_chat_completion_request, flatten_tool_result_content, has_api_key, is_reasoning_model,
model_rejects_is_error_field, translate_message, OpenAiCompatClient, OpenAiCompatConfig,
};
pub use providers::{
Expand Down
25 changes: 25 additions & 0 deletions rust/crates/api/src/providers/openai_compat.rs
Original file line number Diff line number Diff line change
Expand Up @@ -139,6 +139,31 @@ impl OpenAiCompatClient {
Ok(Self::new(api_key, config))
}

/// Create a client using an OAuth access token instead of an API key.
/// The token is sent as `Authorization: Bearer {token}`.
#[must_use]
pub fn from_oauth_token(token: impl Into<String>, config: OpenAiCompatConfig) -> Self {
Self::new(token, config)
}

/// Try env var first, then fall back to saved OAuth token for the provider.
/// `provider_id` is the key used in `~/.claw/credentials.json` under `oauth_providers`.
pub fn from_env_or_oauth(
config: OpenAiCompatConfig,
provider_id: &str,
) -> Result<Self, ApiError> {
if let Some(api_key) = read_env_non_empty(config.api_key_env)? {
return Ok(Self::new(api_key, config));
}
if let Ok(Some(token_set)) = runtime::load_provider_oauth(provider_id) {
return Ok(Self::from_oauth_token(token_set.access_token, config));
}
Err(ApiError::missing_credentials(
config.provider_name,
config.credential_env_vars(),
))
}

#[must_use]
pub fn with_base_url(mut self, base_url: impl Into<String>) -> Self {
self.base_url = base_url.into();
Expand Down
1 change: 1 addition & 0 deletions rust/crates/runtime/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ sha2 = "0.10"
glob = "0.3"
plugins = { path = "../plugins" }
regex = "1"
reqwest = { version = "0.12", default-features = false, features = ["json", "rustls-tls"] }
serde = { version = "1", features = ["derive"] }
serde_json.workspace = true
telemetry = { path = "../telemetry" }
Expand Down
12 changes: 7 additions & 5 deletions rust/crates/runtime/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -112,11 +112,13 @@ pub use mcp_stdio::{
UnsupportedMcpServer,
};
pub use oauth::{
clear_oauth_credentials, code_challenge_s256, credentials_path, generate_pkce_pair,
generate_state, load_oauth_credentials, loopback_redirect_uri, parse_oauth_callback_query,
parse_oauth_callback_request_target, save_oauth_credentials, OAuthAuthorizationRequest,
OAuthCallbackParams, OAuthRefreshRequest, OAuthTokenExchangeRequest, OAuthTokenSet,
PkceChallengeMethod, PkceCodePair,
clear_oauth_credentials, clear_provider_oauth, code_challenge_s256, credentials_path,
generate_pkce_pair, generate_state, load_oauth_credentials, load_provider_oauth,
loopback_redirect_uri, open_browser, parse_oauth_callback_query,
parse_oauth_callback_request_target, poll_device_token, run_oauth_callback_server,
save_oauth_credentials, save_provider_oauth, DeviceAuthRequest, DeviceAuthResponse,
OAuthAuthorizationRequest, OAuthCallbackParams, OAuthCallbackResult, OAuthRefreshRequest,
OAuthTokenExchangeRequest, OAuthTokenSet, PkceChallengeMethod, PkceCodePair,
};
pub use permissions::{
PermissionContext, PermissionMode, PermissionOutcome, PermissionOverride, PermissionPolicy,
Expand Down
Loading