reqsign_oracle/
credential.rs1use reqsign_core::SigningCredential;
19use reqsign_core::time::Timestamp;
20use reqsign_core::utils::Redact;
21use std::fmt::{Debug, Formatter};
22use std::time::Duration;
23
24#[derive(Default, Clone)]
26pub struct Credential {
27 pub tenancy: String,
29 pub user: String,
31 pub key_file: String,
33 pub fingerprint: String,
35 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}