Skip to main content

reqsign_aws_core/
credential.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 reqsign_core::SigningCredential;
19use reqsign_core::time::Timestamp;
20use reqsign_core::utils::Redact;
21use std::fmt::{Debug, Formatter};
22use std::time::Duration;
23
24/// Credential that holds the access_key and secret_key.
25#[derive(Default, Clone)]
26pub struct Credential {
27    /// Access key id for aws services.
28    pub access_key_id: String,
29    /// Secret access key for aws services.
30    pub secret_access_key: String,
31    /// Session token for aws services.
32    pub session_token: Option<String>,
33    /// Expiration time for this credential.
34    pub expires_in: Option<Timestamp>,
35}
36
37impl Debug for Credential {
38    fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
39        f.debug_struct("Credential")
40            .field("access_key_id", &Redact::from(&self.access_key_id))
41            .field("secret_access_key", &Redact::from(&self.secret_access_key))
42            .field("session_token", &Redact::from(&self.session_token))
43            .field("expires_in", &self.expires_in)
44            .finish()
45    }
46}
47
48impl SigningCredential for Credential {
49    fn is_valid(&self) -> bool {
50        self.is_valid_at(Timestamp::now() + Duration::from_secs(120))
51    }
52
53    fn is_valid_at(&self, timestamp: Timestamp) -> bool {
54        if self.access_key_id.is_empty() || self.secret_access_key.is_empty() {
55            return false;
56        }
57
58        self.expires_in.is_none_or(|expires| expires > timestamp)
59    }
60}
61
62#[cfg(test)]
63mod tests {
64    use super::*;
65
66    #[test]
67    fn separates_cache_freshness_from_exact_validity() {
68        let now = Timestamp::now();
69        let credential = Credential {
70            access_key_id: "access-key".to_string(),
71            secret_access_key: "secret-key".to_string(),
72            session_token: Some("token".to_string()),
73            expires_in: Some(now + Duration::from_secs(30)),
74        };
75
76        assert!(!credential.is_valid());
77        assert!(credential.is_valid_at(now + Duration::from_secs(10)));
78        assert!(!credential.is_valid_at(now + Duration::from_secs(30)));
79    }
80
81    #[test]
82    fn session_token_does_not_replace_signing_keys() {
83        let credential = Credential {
84            session_token: Some("token".to_string()),
85            ..Default::default()
86        };
87
88        assert!(!credential.is_valid_at(Timestamp::now()));
89    }
90}