reqsign_aliyun_oss/provide_credential/
static.rs

1// Licensed to the Apache Software Foundation (ASF) under one
2// or more contributor license agreements.  See the NOTICE file
3// distributed with this work for additional information
4// regarding copyright ownership.  The ASF licenses this file
5// to you under the Apache License, Version 2.0 (the
6// "License"); you may not use this file except in compliance
7// with the License.  You may obtain a copy of the License at
8//
9//   http://www.apache.org/licenses/LICENSE-2.0
10//
11// Unless required by applicable law or agreed to in writing,
12// software distributed under the License is distributed on an
13// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
14// KIND, either express or implied.  See the License for the
15// specific language governing permissions and limitations
16// under the License.
17
18use crate::Credential;
19use async_trait::async_trait;
20use reqsign_core::{Context, ProvideCredential, Result};
21
22/// StaticCredentialProvider provides static Aliyun credentials.
23///
24/// This provider is used when you have the access key ID and access key secret
25/// directly and want to use them without any dynamic loading.
26#[derive(Debug, Clone)]
27pub struct StaticCredentialProvider {
28    access_key_id: String,
29    access_key_secret: String,
30    security_token: Option<String>,
31}
32
33impl StaticCredentialProvider {
34    /// Create a new StaticCredentialProvider with access key ID and access key secret.
35    pub fn new(access_key_id: &str, access_key_secret: &str) -> Self {
36        Self {
37            access_key_id: access_key_id.to_string(),
38            access_key_secret: access_key_secret.to_string(),
39            security_token: None,
40        }
41    }
42
43    /// Set the security token.
44    pub fn with_security_token(mut self, token: &str) -> Self {
45        self.security_token = Some(token.to_string());
46        self
47    }
48}
49
50#[async_trait]
51impl ProvideCredential for StaticCredentialProvider {
52    type Credential = Credential;
53
54    async fn provide_credential(&self, _: &Context) -> Result<Option<Self::Credential>> {
55        Ok(Some(Credential {
56            access_key_id: self.access_key_id.clone(),
57            access_key_secret: self.access_key_secret.clone(),
58            security_token: self.security_token.clone(),
59            expires_in: None,
60        }))
61    }
62}
63
64#[cfg(test)]
65mod tests {
66    use super::*;
67    use reqsign_core::OsEnv;
68    use reqsign_file_read_tokio::TokioFileRead;
69    use reqsign_http_send_reqwest::ReqwestHttpSend;
70
71    #[tokio::test]
72    async fn test_static_credential_provider() -> anyhow::Result<()> {
73        let ctx = Context::new()
74            .with_file_read(TokioFileRead)
75            .with_http_send(ReqwestHttpSend::default())
76            .with_env(OsEnv);
77
78        // Test with basic credentials
79        let provider = StaticCredentialProvider::new("test_access_key", "test_secret_key");
80        let cred = provider.provide_credential(&ctx).await?;
81        assert!(cred.is_some());
82        let cred = cred.unwrap();
83        assert_eq!(cred.access_key_id, "test_access_key");
84        assert_eq!(cred.access_key_secret, "test_secret_key");
85        assert!(cred.security_token.is_none());
86
87        // Test with security token
88        let provider = StaticCredentialProvider::new("test_access_key", "test_secret_key")
89            .with_security_token("test_security_token");
90        let cred = provider.provide_credential(&ctx).await?;
91        assert!(cred.is_some());
92        let cred = cred.unwrap();
93        assert_eq!(cred.access_key_id, "test_access_key");
94        assert_eq!(cred.access_key_secret, "test_secret_key");
95        assert_eq!(cred.security_token, Some("test_security_token".to_string()));
96
97        Ok(())
98    }
99}