claude_agent/auth/providers/
explicit.rs1use async_trait::async_trait;
4
5use crate::Result;
6use crate::auth::{Credential, CredentialProvider};
7
8pub struct ExplicitProvider {
10 credential: Credential,
11}
12
13impl ExplicitProvider {
14 pub fn new(credential: Credential) -> Self {
16 Self { credential }
17 }
18
19 pub fn api_key(key: impl Into<String>) -> Self {
21 Self::new(Credential::api_key(key))
22 }
23
24 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}