Skip to main content

reqsign_oracle/
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 API private key information.
25#[derive(Default, Clone)]
26pub struct Credential {
27    /// TenantID for Oracle Cloud Infrastructure.
28    pub tenancy: String,
29    /// UserID for Oracle Cloud Infrastructure.
30    pub user: String,
31    /// API Private Key file path for credential.
32    pub key_file: String,
33    /// Fingerprint of the API Key.
34    pub fingerprint: String,
35    /// Deadline after which the credential source should be reloaded.
36    pub expires_in: Option<Timestamp>,
37}
38
39impl Debug for Credential {
40    fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
41        f.debug_struct("Credential")
42            .field("tenancy", &self.tenancy)
43            .field("user", &self.user)
44            .field("key_file", &Redact::from(&self.key_file))
45            .field("fingerprint", &self.fingerprint)
46            .field("expires_in", &self.expires_in)
47            .finish()
48    }
49}
50
51impl Credential {
52    fn has_required_fields(&self) -> bool {
53        !self.tenancy.is_empty()
54            && !self.user.is_empty()
55            && !self.key_file.is_empty()
56            && !self.fingerprint.is_empty()
57    }
58}
59
60impl SigningCredential for Credential {
61    fn is_valid(&self) -> bool {
62        self.has_required_fields()
63            && self
64                .expires_in
65                .is_none_or(|refresh_at| refresh_at > Timestamp::now() + Duration::from_secs(120))
66    }
67
68    fn is_valid_at(&self, _timestamp: Timestamp) -> bool {
69        self.has_required_fields()
70    }
71}
72
73#[cfg(test)]
74mod tests {
75    use super::*;
76
77    #[test]
78    fn refresh_deadline_only_controls_cache_freshness() {
79        let now = Timestamp::now();
80        let credential = Credential {
81            tenancy: "tenancy".to_string(),
82            user: "user".to_string(),
83            key_file: "key.pem".to_string(),
84            fingerprint: "fingerprint".to_string(),
85            expires_in: Some(now + Duration::from_secs(30)),
86        };
87
88        assert!(!credential.is_valid());
89        assert!(credential.is_valid_at(now + Duration::from_secs(3600)));
90    }
91}