claude_agent/auth/providers/
explicit.rs

1//! Explicit credential provider.
2
3use async_trait::async_trait;
4
5use crate::Result;
6use crate::auth::{Credential, CredentialProvider};
7
8/// Provider with explicitly set credentials.
9pub struct ExplicitProvider {
10    credential: Credential,
11}
12
13impl ExplicitProvider {
14    /// Create with credential.
15    pub fn new(credential: Credential) -> Self {
16        Self { credential }
17    }
18
19    /// Create with API key.
20    pub fn api_key(key: impl Into<String>) -> Self {
21        Self::new(Credential::api_key(key))
22    }
23
24    /// Create with OAuth token.
25    pub fn oauth(token: impl Into<String>) -> Self {
26        Self::new(Credential::oauth(token))
27    }
28}
29
30#[async_trait]
31impl CredentialProvider for ExplicitProvider {
32    fn name(&self) -> &str {
33        "explicit"
34    }
35
36    async fn resolve(&self) -> Result<Credential> {
37        Ok(self.credential.clone())
38    }
39}
40
41#[cfg(test)]
42mod tests {
43    use super::*;
44
45    #[tokio::test]
46    async fn test_explicit_api_key() {
47        let provider = ExplicitProvider::api_key("test-key");
48        let cred = provider.resolve().await.unwrap();
49        assert!(matches!(cred, Credential::ApiKey(k) if k == "test-key"));
50    }
51
52    #[tokio::test]
53    async fn test_explicit_oauth() {
54        let provider = ExplicitProvider::oauth("test-token");
55        let cred = provider.resolve().await.unwrap();
56        let Credential::OAuth(oauth) = cred else {
57            unreachable!("ExplicitProvider::oauth always creates OAuth credential");
58        };
59        assert_eq!(oauth.access_token, "test-token");
60    }
61}