1use std::collections::HashMap;
35use std::fmt;
36use std::sync::atomic::{AtomicU64, Ordering};
37use std::sync::{Arc, PoisonError, RwLock};
38
39use futures::future::BoxFuture;
40use indexmap::IndexMap;
41use serde::Deserialize;
42
43use crate::credentials::{CachedCredentials, Credential, CredentialSource, EnvCredentials};
44use crate::error::LlmError;
45use crate::registry::ModelRegistry;
46use crate::types::ChatModel;
47
48#[derive(Clone, Copy, Debug, PartialEq, Eq, Deserialize)]
50#[serde(rename_all = "snake_case")]
51#[non_exhaustive]
52pub enum ProviderKind {
53 Openai,
55 Anthropic,
57 Gemini,
59 Bedrock,
61 Typesafe,
63}
64
65impl ProviderKind {
66 pub fn from_known_name(name: &str) -> Option<Self> {
68 Some(match name {
69 "openai" => ProviderKind::Openai,
70 "anthropic" => ProviderKind::Anthropic,
71 "gemini" => ProviderKind::Gemini,
72 "bedrock" => ProviderKind::Bedrock,
73 "typesafe" => ProviderKind::Typesafe,
74 _ => return None,
75 })
76 }
77}
78
79#[derive(Clone, Copy, Debug, PartialEq, Eq, Deserialize)]
81#[serde(rename_all = "snake_case")]
82#[non_exhaustive]
83pub enum SourceKind {
84 External,
85 TokenBroker,
86 Env,
87}
88
89#[derive(Clone, Copy, Debug, PartialEq, Eq, Deserialize, Default)]
91#[serde(rename_all = "snake_case")]
92#[non_exhaustive]
93pub enum AppAuth {
94 #[default]
96 None,
97 External,
100}
101
102#[derive(Clone, Debug, Deserialize)]
104#[serde(deny_unknown_fields)]
105pub struct ProviderCredentialConfig {
106 pub source: SourceKind,
107 #[serde(default)]
108 pub kind: Option<ProviderKind>,
109 #[serde(default)]
110 pub base_url: Option<String>,
111 #[serde(default)]
114 pub headers: IndexMap<String, String>,
115 #[serde(default)]
118 pub auth_header: Option<String>,
119 #[serde(default)]
121 pub region: Option<String>,
122 #[serde(default)]
124 pub var: Option<String>,
125 #[serde(default)]
127 pub url: Option<String>,
128 #[serde(default)]
130 pub claims: serde_json::Map<String, serde_json::Value>,
131 #[serde(default)]
133 pub broker_headers: IndexMap<String, String>,
134 #[serde(default)]
136 pub app_auth: AppAuth,
137 #[serde(default)]
139 pub refresh_skew_ms: Option<u64>,
140 #[serde(default)]
142 pub fetch_timeout_ms: Option<u64>,
143}
144
145impl ProviderCredentialConfig {
146 pub fn kind_for(&self, name: &str) -> Result<ProviderKind, LlmError> {
148 self.kind.or_else(|| ProviderKind::from_known_name(name)).ok_or_else(|| {
149 LlmError::Config(format!(
150 "credentials for `{name}`: set `kind` (openai, anthropic, gemini, bedrock or typesafe); \
151 it is only inferred for providers named exactly like a kind"
152 ))
153 })
154 }
155
156 pub fn validate(&self, name: &str) -> Result<ProviderKind, LlmError> {
158 let kind = self.kind_for(name)?;
159 let bad = |field: &str, why: &str| {
160 Err(LlmError::Config(format!("credentials for `{name}`: `{field}` is not used {why}")))
161 };
162 let broker_only = self.url.is_some()
163 || !self.claims.is_empty()
164 || !self.broker_headers.is_empty()
165 || self.app_auth != AppAuth::None;
166 match self.source {
167 SourceKind::Env => {
168 if broker_only {
169 return bad("url/claims/broker_headers/app_auth", "with `source: env`");
170 }
171 if self.refresh_skew_ms.is_some() || self.fetch_timeout_ms.is_some() {
172 return bad("refresh_skew_ms/fetch_timeout_ms", "with `source: env` (read on every request)");
173 }
174 }
175 SourceKind::External => {
176 if broker_only {
177 return bad("url/claims/broker_headers/app_auth", "with `source: external`");
178 }
179 if self.var.is_some() {
180 return bad("var", "with `source: external`");
181 }
182 }
183 SourceKind::TokenBroker => {
184 if self.var.is_some() {
185 return bad("var", "with `source: token_broker`");
186 }
187 }
188 }
189 if self.auth_header.is_some() && !matches!(kind, ProviderKind::Openai | ProviderKind::Gemini) {
190 return bad("auth_header", "for this provider kind");
191 }
192 if self.region.is_some() && kind != ProviderKind::Bedrock {
193 return bad("region", "for this provider kind");
194 }
195 if kind == ProviderKind::Bedrock && self.region.as_deref().is_none_or(str::is_empty) && self.base_url.is_none()
196 {
197 return Err(LlmError::Config(format!("credentials for `{name}`: bedrock needs `region` (or `base_url`)")));
198 }
199 if let Some(url) = &self.base_url {
200 crate::credentials::require_secure_endpoint(url, &format!("credentials for `{name}`: `base_url`"))?;
201 }
202 Ok(kind)
203 }
204
205 pub fn needs_host(&self) -> bool {
207 self.source == SourceKind::External || self.app_auth == AppAuth::External
208 }
209}
210
211#[derive(Clone, Default)]
218pub struct HostCredentials {
219 sources: Arc<RwLock<HashMap<String, Arc<dyn CredentialSource>>>>,
220 generation: Arc<AtomicU64>,
221}
222
223impl fmt::Debug for HostCredentials {
224 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
225 let sources = self.sources.read().unwrap_or_else(PoisonError::into_inner);
226 let mut names: Vec<&String> = sources.keys().collect();
227 names.sort();
228 f.debug_struct("HostCredentials").field("providers", &names).finish()
229 }
230}
231
232impl HostCredentials {
233 pub fn new() -> Self {
234 Self::default()
235 }
236
237 pub fn set(&self, provider: impl Into<String>, source: Arc<dyn CredentialSource>) {
239 {
240 let mut sources = self.sources.write().unwrap_or_else(PoisonError::into_inner);
241 sources.insert(provider.into(), source);
242 }
243 self.clear_cache();
244 }
245
246 pub fn remove(&self, provider: &str) {
248 {
249 let mut sources = self.sources.write().unwrap_or_else(PoisonError::into_inner);
250 sources.remove(provider);
251 }
252 self.clear_cache();
253 }
254
255 pub fn clear_cache(&self) {
258 self.generation.fetch_add(1, Ordering::SeqCst);
259 }
260
261 fn lookup(&self, provider: &str) -> Option<Arc<dyn CredentialSource>> {
262 let sources = self.sources.read().unwrap_or_else(PoisonError::into_inner);
263 sources.get(provider).or_else(|| sources.get("*")).cloned()
264 }
265
266 fn lookup_exact(&self, provider: &str) -> Option<Arc<dyn CredentialSource>> {
267 let sources = self.sources.read().unwrap_or_else(PoisonError::into_inner);
268 sources.get(provider).cloned()
269 }
270
271 pub fn exact(&self, name: impl Into<String>) -> Arc<dyn CredentialSource> {
275 Arc::new(ExactHostSource { host: self.clone(), name: name.into() })
276 }
277}
278
279impl CredentialSource for HostCredentials {
280 fn credential<'a>(&'a self, provider: &'a str) -> BoxFuture<'a, Result<Credential, LlmError>> {
281 Box::pin(async move {
282 match self.lookup(provider) {
283 Some(source) => source.credential(provider).await,
284 None => Err(LlmError::Auth(format!("no credential provider registered for `{provider}`"))),
285 }
286 })
287 }
288
289 fn invalidate(&self, provider: &str) {
290 if let Some(source) = self.lookup(provider) {
291 source.invalidate(provider);
292 }
293 }
294
295 fn generation(&self) -> u64 {
296 self.generation.load(Ordering::SeqCst)
297 }
298}
299
300#[derive(Debug)]
302struct ExactHostSource {
303 host: HostCredentials,
304 name: String,
305}
306
307impl CredentialSource for ExactHostSource {
308 fn credential<'a>(&'a self, _provider: &'a str) -> BoxFuture<'a, Result<Credential, LlmError>> {
309 Box::pin(async move {
310 match self.host.lookup_exact(&self.name) {
311 Some(source) => source.credential(&self.name).await,
312 None => Err(LlmError::Auth(format!(
313 "no credential provider registered under `{}` (app session for the token broker)",
314 self.name
315 ))),
316 }
317 })
318 }
319
320 fn invalidate(&self, _provider: &str) {
321 if let Some(source) = self.host.lookup_exact(&self.name) {
322 source.invalidate(&self.name);
323 }
324 }
325
326 fn generation(&self) -> u64 {
327 self.host.generation()
328 }
329}
330
331pub fn build_source(
333 name: &str,
334 config: &ProviderCredentialConfig,
335 host: &HostCredentials,
336) -> Result<Arc<dyn CredentialSource>, LlmError> {
337 config.validate(name)?;
338 let bad = |msg: &str| LlmError::Config(format!("credentials for `{name}`: {msg}"));
339 let cached = |source: Arc<dyn CredentialSource>| -> Arc<dyn CredentialSource> {
340 let mut cache = CachedCredentials::new(source);
341 if let Some(skew) = config.refresh_skew_ms {
342 cache = cache.refresh_skew_ms(skew);
343 }
344 if let Some(ms) = config.fetch_timeout_ms {
345 cache = cache.fetch_timeout(std::time::Duration::from_millis(ms));
346 }
347 Arc::new(cache)
348 };
349 match config.source {
350 SourceKind::External => Ok(cached(Arc::new(host.clone()))),
351 SourceKind::Env => {
352 let var = config.var.as_deref().filter(|v| !v.is_empty()).ok_or_else(|| bad("`env` needs `var`"))?;
353 Ok(Arc::new(EnvCredentials::new().var("*", var)))
354 }
355 SourceKind::TokenBroker => build_broker(name, config, host, &bad).map(cached),
356 }
357}
358
359#[cfg(feature = "http")]
360fn build_broker(
361 _name: &str,
362 config: &ProviderCredentialConfig,
363 host: &HostCredentials,
364 bad: &dyn Fn(&str) -> LlmError,
365) -> Result<Arc<dyn CredentialSource>, LlmError> {
366 use crate::credentials::{BrokerAuth, TokenBroker, TokenBrokerConfig};
367 let url = config.url.as_deref().filter(|u| !u.is_empty()).ok_or_else(|| bad("`token_broker` needs `url`"))?;
368 let mut broker = TokenBrokerConfig::new(url);
369 for (k, v) in &config.broker_headers {
370 broker = broker.header(k, v);
371 }
372 for (k, v) in &config.claims {
373 broker = broker.claim(k, v.clone());
374 }
375 if config.app_auth == AppAuth::External {
376 broker = broker.auth(BrokerAuth::Bearer(host.exact("token_broker")));
377 }
378 Ok(Arc::new(TokenBroker::new(broker)?))
379}
380
381#[cfg(not(feature = "http"))]
382fn build_broker(
383 _name: &str,
384 _config: &ProviderCredentialConfig,
385 _host: &HostCredentials,
386 bad: &dyn Fn(&str) -> LlmError,
387) -> Result<Arc<dyn CredentialSource>, LlmError> {
388 Err(bad("`token_broker` requires the `http` feature"))
389}
390
391#[cfg_attr(not(feature = "http"), allow(dead_code))]
392fn static_headers(config: &ProviderCredentialConfig) -> Vec<(String, String)> {
393 config.headers.iter().map(|(k, v)| (k.clone(), v.clone())).collect()
394}
395
396pub fn build_model(
399 name: &str,
400 config: &ProviderCredentialConfig,
401 host: &HostCredentials,
402) -> Result<Arc<dyn ChatModel>, LlmError> {
403 let kind = config.validate(name)?;
404 let source = build_source(name, config, host)?;
405 build_model_with_source(name, kind, config, source)
406}
407
408#[cfg_attr(not(feature = "http"), allow(unused_variables))]
409fn build_model_with_source(
410 name: &str,
411 kind: ProviderKind,
412 config: &ProviderCredentialConfig,
413 source: Arc<dyn CredentialSource>,
414) -> Result<Arc<dyn ChatModel>, LlmError> {
415 match kind {
416 #[cfg(feature = "openai")]
417 ProviderKind::Openai => {
418 use crate::openai::{AuthStyle, OpenAiConfig, OpenAiModel};
419 let mut cfg = OpenAiConfig::default().with_credentials(source).credential_name(name);
420 if let Some(url) = &config.base_url {
421 cfg.base_url.clone_from(url);
422 }
423 if let Some(header) = config.auth_header.as_deref().filter(|h| !h.eq_ignore_ascii_case("authorization")) {
424 cfg.auth = AuthStyle::Header(header.to_owned());
425 }
426 cfg.headers = static_headers(config);
427 cfg.use_max_completion_tokens = cfg.base_url.contains("api.openai.com");
428 Ok(Arc::new(OpenAiModel::new(cfg)?))
429 }
430 #[cfg(feature = "anthropic")]
431 ProviderKind::Anthropic => {
432 use crate::anthropic::{AnthropicConfig, AnthropicModel};
433 let mut cfg = AnthropicConfig::default().with_credentials(source).credential_name(name);
434 if let Some(url) = &config.base_url {
435 cfg.base_url.clone_from(url);
436 }
437 cfg.headers = static_headers(config);
438 Ok(Arc::new(AnthropicModel::new(cfg)?))
439 }
440 #[cfg(feature = "gemini")]
441 ProviderKind::Gemini => {
442 use crate::gemini::{GeminiAuth, GeminiConfig, GeminiModel};
443 let mut cfg =
444 GeminiConfig { provider_name: name.to_owned(), ..GeminiConfig::default() }.with_credentials(source);
445 if let Some(url) = &config.base_url {
446 cfg.base_url.clone_from(url);
447 }
448 match config.auth_header.as_deref() {
449 None => {}
450 Some(h) if h.eq_ignore_ascii_case("authorization") => cfg.auth = GeminiAuth::Bearer,
451 Some(h) if h.eq_ignore_ascii_case("x-goog-api-key") => cfg.auth = GeminiAuth::ApiKey,
452 Some(h) => {
453 return Err(LlmError::Config(format!(
454 "credentials for `{name}`: gemini `auth_header` must be `x-goog-api-key` or \
455 `authorization`, got `{h}`"
456 )));
457 }
458 }
459 cfg.headers = static_headers(config);
460 Ok(Arc::new(GeminiModel::new(cfg)?))
461 }
462 #[cfg(feature = "bedrock")]
463 ProviderKind::Bedrock => {
464 use crate::bedrock::{BedrockConfig, BedrockModel};
465 let region = config.region.clone().unwrap_or_else(|| "us-east-1".to_owned());
466 let mut cfg = BedrockConfig {
467 region,
468 endpoint: config.base_url.clone(),
469 auth: None,
470 credentials: Some(source),
471 provider_name: name.to_owned(),
472 headers: static_headers(config),
473 connect_timeout: std::time::Duration::from_secs(10),
474 };
475 cfg.provider_name = name.to_owned();
476 Ok(Arc::new(BedrockModel::new(cfg)?))
477 }
478 ProviderKind::Typesafe => Err(LlmError::Config(format!(
479 "credentials for `{name}`: a `typesafe` entry configures the System One decision client, \
480 not a chat model"
481 ))),
482 #[allow(unreachable_patterns)]
483 other => {
484 drop(source);
485 Err(LlmError::Config(format!("provider `{name}`: kind {other:?} is not enabled in this build")))
486 }
487 }
488}
489
490#[cfg(feature = "typesafe")]
492pub fn build_system_one(
493 name: &str,
494 config: &ProviderCredentialConfig,
495 host: &HostCredentials,
496) -> Result<crate::typesafe::SystemOneClient, LlmError> {
497 let kind = config.validate(name)?;
498 if kind != ProviderKind::Typesafe {
499 return Err(LlmError::Config(format!("credentials for `{name}`: not a `typesafe` entry")));
500 }
501 let source = build_source(name, config, host)?;
502 system_one_with_source(config, source)
503}
504
505#[cfg(feature = "typesafe")]
506fn system_one_with_source(
507 config: &ProviderCredentialConfig,
508 source: Arc<dyn CredentialSource>,
509) -> Result<crate::typesafe::SystemOneClient, LlmError> {
510 let mut ts = crate::typesafe::SystemOneConfig { credentials: Some(source), ..Default::default() };
511 if let Some(url) = &config.base_url {
512 ts.base_url.clone_from(url);
513 }
514 ts.headers = static_headers(config);
515 crate::typesafe::SystemOneClient::new(ts)
516}
517
518#[derive(Debug, Default)]
520#[non_exhaustive]
521pub struct Applied {
522 #[cfg(feature = "typesafe")]
524 pub system_one: Option<crate::typesafe::SystemOneClient>,
525}
526
527pub fn apply(
533 registry: &mut ModelRegistry,
534 entries: &IndexMap<String, ProviderCredentialConfig>,
535 host: &HostCredentials,
536) -> Result<Applied, LlmError> {
537 #[allow(unused_mut)]
538 let mut applied = Applied::default();
539 for (name, config) in entries {
540 let kind = config.validate(name)?;
541 if kind == ProviderKind::Typesafe {
542 #[cfg(feature = "typesafe")]
543 {
544 if applied.system_one.is_some() {
545 return Err(LlmError::Config("only one `typesafe` credentials entry is supported".into()));
546 }
547 let source = build_source(name, config, host)?;
548 applied.system_one = Some(system_one_with_source(config, source)?);
549 continue;
550 }
551 #[cfg(not(feature = "typesafe"))]
552 return Err(LlmError::Config(format!(
553 "credentials for `{name}`: the `typesafe` kind is not enabled in this build"
554 )));
555 }
556 let source = build_source(name, config, host)?;
557 let model = build_model_with_source(name, kind, config, source)?;
558 registry.remove(name);
559 registry.add(name.clone(), model);
560 }
561 Ok(applied)
562}
563
564#[cfg(test)]
565mod tests {
566 use super::*;
567 use crate::credentials::StaticCredential;
568
569 #[allow(dead_code)] fn entries(v: serde_json::Value) -> IndexMap<String, ProviderCredentialConfig> {
571 serde_json::from_value(v).unwrap()
572 }
573
574 #[tokio::test]
575 async fn host_credentials_dispatch_with_fallback() {
576 let host = HostCredentials::new();
577 assert!(matches!(host.credential("openai").await, Err(LlmError::Auth(_))));
578 host.set("*", Arc::new(StaticCredential::new("sk-fallback")));
579 host.set("anthropic", Arc::new(StaticCredential::new("sk-ant-secret")));
580 assert_eq!(host.credential("openai").await.unwrap().secret, "sk-fallback");
581 assert_eq!(host.credential("anthropic").await.unwrap().secret, "sk-ant-secret");
582 let text = format!("{host:?}");
583 assert!(!text.contains("sk-fallback") && !text.contains("sk-ant-secret"));
584 }
585
586 #[tokio::test]
587 async fn sign_out_takes_effect_through_the_cache() {
588 let host = HostCredentials::new();
589 host.set("openai", Arc::new(StaticCredential::new("user-a")));
590 let cfg: ProviderCredentialConfig = serde_json::from_value(serde_json::json!({"source": "external"})).unwrap();
591 let source = build_source("openai", &cfg, &host).unwrap();
592 assert_eq!(source.credential("openai").await.unwrap().secret, "user-a");
593 host.remove("openai");
595 assert!(source.credential("openai").await.is_err());
596 host.set("openai", Arc::new(StaticCredential::new("user-b")));
598 assert_eq!(source.credential("openai").await.unwrap().secret, "user-b");
599 }
600
601 #[tokio::test]
602 async fn broker_app_token_never_uses_the_wildcard_source() {
603 let host = HostCredentials::new();
604 host.set("*", Arc::new(StaticCredential::new("sk-users-own-key")));
605 let app = host.exact("token_broker");
606 assert!(matches!(app.credential("token_broker").await, Err(LlmError::Auth(_))));
607 host.set("token_broker", Arc::new(StaticCredential::new("session")));
608 assert_eq!(app.credential("token_broker").await.unwrap().secret, "session");
609 }
610
611 #[cfg(all(feature = "openai", feature = "typesafe"))]
612 #[test]
613 fn config_validation() {
614 let mut registry = ModelRegistry::new();
615 let applied = apply(
616 &mut registry,
617 &entries(serde_json::json!({
618 "openai": {"source": "external"},
619 "proxy": {"source": "external", "kind": "openai", "base_url": "https://proxy.example.com/v1"},
620 "server": {"source": "env", "var": "OPENAI_API_KEY", "kind": "openai"},
621 "typesafe": {"source": "env", "var": "TYPESAFE_API_KEY"}
622 })),
623 &HostCredentials::new(),
624 )
625 .unwrap();
626 assert!(registry.get("proxy").is_ok());
627 assert!(registry.get("server").is_ok());
628 assert!(registry.get("typesafe").is_err());
630 assert!(applied.system_one.is_some());
631
632 let bad: Result<IndexMap<String, ProviderCredentialConfig>, _> =
633 serde_json::from_value(serde_json::json!({"x": {"source": "external", "typo": 1}}));
634 assert!(bad.is_err());
635 let host = HostCredentials::new();
636 let fails = |v: serde_json::Value| apply(&mut ModelRegistry::new(), &entries(v), &host).is_err();
637 assert!(fails(serde_json::json!({"x": {"source": "env", "kind": "openai"}})));
640 assert!(fails(serde_json::json!({"mistral": {"source": "external"}})));
641 assert!(fails(serde_json::json!({"openai": {"source": "token_broker", "url": "https://b", "var": "X"}})));
642 assert!(fails(serde_json::json!({"openai": {"source": "env", "var": "X", "url": "https://b"}})));
643 assert!(fails(serde_json::json!({"anthropic": {"source": "external", "auth_header": "x"}})));
644 assert!(fails(serde_json::json!({"openai": {"source": "external", "base_url": "http://proxy.example.com"}})));
645 assert!(fails(serde_json::json!({"openai": {"source": "token_broker", "url": "http://broker.example.com"}})));
646 assert!(fails(serde_json::json!({"bedrock": {"source": "external"}})));
647 }
648
649 #[cfg(all(feature = "gemini", feature = "bedrock"))]
650 #[test]
651 fn gemini_and_bedrock_entries_build_their_own_clients() {
652 let mut registry = ModelRegistry::new();
653 apply(
654 &mut registry,
655 &entries(serde_json::json!({
656 "gemini": {"source": "external"},
657 "bedrock": {"source": "external", "region": "eu-west-1"}
658 })),
659 &HostCredentials::new(),
660 )
661 .unwrap();
662 let gemini = format!("{:?}", registry.get("gemini").unwrap());
663 let bedrock = format!("{:?}", registry.get("bedrock").unwrap());
664 assert!(gemini.contains("Gemini"), "{gemini}");
665 assert!(bedrock.contains("Bedrock"), "{bedrock}");
666 }
667}