Skip to main content

reqsign_oracle/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 credentials that are provided at initialization time.
22#[derive(Debug)]
23pub struct StaticCredentialProvider {
24    credential: Credential,
25}
26
27impl StaticCredentialProvider {
28    /// Create a new StaticCredentialProvider with the given credentials.
29    pub fn new(user: &str, tenancy: &str, key_file: &str, fingerprint: &str) -> Self {
30        Self {
31            credential: Credential {
32                user: user.to_string(),
33                tenancy: tenancy.to_string(),
34                key_file: key_file.to_string(),
35                fingerprint: fingerprint.to_string(),
36                expires_in: None,
37            },
38        }
39    }
40}
41impl ProvideCredential for StaticCredentialProvider {
42    type Credential = Credential;
43
44    async fn provide_credential(&self, _ctx: &Context) -> Result<Option<Self::Credential>> {
45        Ok(Some(self.credential.clone()))
46    }
47}
48
49#[cfg(test)]
50mod tests {
51    use super::*;
52    use reqsign_core::OsEnv;
53    use reqsign_file_read_tokio::TokioFileRead;
54    use reqsign_http_send_reqwest::ReqwestHttpSend;
55
56    #[tokio::test]
57    async fn test_static_credential_provider() -> anyhow::Result<()> {
58        let ctx = Context::new()
59            .with_file_read(TokioFileRead)
60            .with_http_send(ReqwestHttpSend::default())
61            .with_env(OsEnv);
62
63        let provider = StaticCredentialProvider::new(
64            "test_user",
65            "test_tenancy",
66            "/path/to/key",
67            "test_fingerprint",
68        );
69        let cred = provider.provide_credential(&ctx).await?;
70        assert!(cred.is_some());
71        let cred = cred.unwrap();
72        assert_eq!(cred.user, "test_user");
73        assert_eq!(cred.tenancy, "test_tenancy");
74        assert_eq!(cred.key_file, "/path/to/key");
75        assert_eq!(cred.fingerprint, "test_fingerprint");
76        assert!(cred.expires_in.is_none());
77
78        Ok(())
79    }
80}