Skip to main content

reqsign_aws_core/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 reqsign_core::{Context, ProvideCredential, Result};
20
21/// StaticCredentialProvider provides static AWS credentials.
22///
23/// This provider is used when you have the access key ID and secret access key
24/// directly and want to use them without any dynamic loading.
25#[derive(Debug, Clone)]
26pub struct StaticCredentialProvider {
27    access_key_id: String,
28    secret_access_key: String,
29    session_token: Option<String>,
30}
31
32impl StaticCredentialProvider {
33    /// Create a new StaticCredentialProvider with access key ID and secret access key.
34    pub fn new(access_key_id: &str, secret_access_key: &str) -> Self {
35        Self {
36            access_key_id: access_key_id.to_string(),
37            secret_access_key: secret_access_key.to_string(),
38            session_token: None,
39        }
40    }
41
42    /// Set the session token.
43    pub fn with_session_token(mut self, token: &str) -> Self {
44        self.session_token = Some(token.to_string());
45        self
46    }
47}
48impl ProvideCredential for StaticCredentialProvider {
49    type Credential = Credential;
50
51    async fn provide_credential(&self, _: &Context) -> Result<Option<Self::Credential>> {
52        Ok(Some(Credential {
53            access_key_id: self.access_key_id.clone(),
54            secret_access_key: self.secret_access_key.clone(),
55            session_token: self.session_token.clone(),
56            expires_in: None,
57        }))
58    }
59}
60
61#[cfg(test)]
62mod tests {
63    use super::*;
64    use reqsign_core::OsEnv;
65    use reqsign_file_read_tokio::TokioFileRead;
66    use reqsign_http_send_reqwest::ReqwestHttpSend;
67
68    #[tokio::test]
69    async fn test_static_credential_provider() -> anyhow::Result<()> {
70        let ctx = Context::new()
71            .with_file_read(TokioFileRead)
72            .with_http_send(ReqwestHttpSend::default())
73            .with_env(OsEnv);
74
75        // Test with basic credentials
76        let provider = StaticCredentialProvider::new("test_access_key", "test_secret_key");
77        let cred = provider.provide_credential(&ctx).await?;
78        assert!(cred.is_some());
79        let cred = cred.unwrap();
80        assert_eq!(cred.access_key_id, "test_access_key");
81        assert_eq!(cred.secret_access_key, "test_secret_key");
82        assert!(cred.session_token.is_none());
83
84        // Test with session token
85        let provider = StaticCredentialProvider::new("test_access_key", "test_secret_key")
86            .with_session_token("test_session_token");
87        let cred = provider.provide_credential(&ctx).await?;
88        assert!(cred.is_some());
89        let cred = cred.unwrap();
90        assert_eq!(cred.access_key_id, "test_access_key");
91        assert_eq!(cred.secret_access_key, "test_secret_key");
92        assert_eq!(cred.session_token, Some("test_session_token".to_string()));
93
94        Ok(())
95    }
96}