Skip to main content

graphrt_llm/credentials/
sources.rs

1//! Simple credential sources: async closures and environment variables.
2
3use std::collections::HashMap;
4use std::fmt;
5use std::future::Future;
6use std::sync::Arc;
7
8use futures::future::BoxFuture;
9
10use super::{Credential, CredentialSource};
11use crate::error::LlmError;
12
13type CredentialFn = dyn Fn(String) -> BoxFuture<'static, Result<Credential, LlmError>> + Send + Sync;
14type InvalidateFn = dyn Fn(&str) + Send + Sync;
15
16/// A [`CredentialSource`] backed by an async closure, e.g. a keychain lookup.
17///
18/// ```
19/// use graphrt_llm::credentials::{Credential, FnCredentials};
20/// let source = FnCredentials::new(|provider: String| async move {
21///     // Read the user's own key for `provider` from secure storage here.
22///     Ok(Credential::new(format!("key-for-{provider}")))
23/// });
24/// # let _ = source;
25/// ```
26#[derive(Clone)]
27pub struct FnCredentials {
28    fetch: Arc<CredentialFn>,
29    on_invalidate: Option<Arc<InvalidateFn>>,
30}
31
32impl FnCredentials {
33    pub fn new<F, Fut>(fetch: F) -> Self
34    where
35        F: Fn(String) -> Fut + Send + Sync + 'static,
36        Fut: Future<Output = Result<Credential, LlmError>> + Send + 'static,
37    {
38        Self { fetch: Arc::new(move |provider| Box::pin(fetch(provider))), on_invalidate: None }
39    }
40
41    /// Called when a provider rejects a credential (e.g. to clear a stored key).
42    pub fn on_invalidate<F>(mut self, f: F) -> Self
43    where
44        F: Fn(&str) + Send + Sync + 'static,
45    {
46        self.on_invalidate = Some(Arc::new(f));
47        self
48    }
49}
50
51impl fmt::Debug for FnCredentials {
52    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
53        f.write_str("FnCredentials")
54    }
55}
56
57impl CredentialSource for FnCredentials {
58    fn credential<'a>(&'a self, provider: &'a str) -> BoxFuture<'a, Result<Credential, LlmError>> {
59        (self.fetch)(provider.to_owned())
60    }
61
62    fn invalidate(&self, provider: &str) {
63        if let Some(f) = &self.on_invalidate {
64            f(provider);
65        }
66    }
67}
68
69/// Reads keys from environment variables at request time (servers), so rotating
70/// the variable in a supervisor takes effect without rebuilding the registry.
71#[derive(Clone, Debug, Default)]
72pub struct EnvCredentials {
73    vars: HashMap<String, String>,
74}
75
76impl EnvCredentials {
77    pub fn new() -> Self {
78        Self::default()
79    }
80
81    /// Use environment variable `var` for `provider` (`*` matches any provider).
82    pub fn var(mut self, provider: impl Into<String>, var: impl Into<String>) -> Self {
83        self.vars.insert(provider.into(), var.into());
84        self
85    }
86}
87
88impl CredentialSource for EnvCredentials {
89    fn credential<'a>(&'a self, provider: &'a str) -> BoxFuture<'a, Result<Credential, LlmError>> {
90        let result = match self.vars.get(provider).or_else(|| self.vars.get("*")) {
91            None => Err(LlmError::Config(format!("no environment variable configured for provider `{provider}`"))),
92            Some(var) => match std::env::var(var) {
93                Ok(value) if !value.is_empty() => Ok(Credential::new(value)),
94                _ => Err(LlmError::Auth(format!("environment variable `{var}` is not set"))),
95            },
96        };
97        Box::pin(futures::future::ready(result))
98    }
99}
100
101#[cfg(test)]
102mod tests {
103    use super::*;
104
105    #[tokio::test]
106    async fn fn_credentials_pass_provider_name() {
107        let source = FnCredentials::new(|p: String| async move { Ok(Credential::new(format!("k-{p}"))) });
108        let c = source.credential("openai").await.unwrap();
109        assert_eq!(c.secret, "k-openai");
110    }
111
112    #[tokio::test]
113    async fn env_credentials_report_missing_variable_by_name() {
114        let source = EnvCredentials::new().var("openai", "GRAPHRT_TEST_DEFINITELY_UNSET_VAR");
115        let err = source.credential("openai").await.unwrap_err();
116        assert!(err.to_string().contains("GRAPHRT_TEST_DEFINITELY_UNSET_VAR"));
117        let err = source.credential("other").await.unwrap_err();
118        assert!(matches!(err, LlmError::Config(_)));
119    }
120}