1#![warn(clippy::all)]
2#![warn(clippy::nursery)]
3#![doc = include_str!(concat!(env!("OUT_DIR"), "/README-rustdocified.md"))]
4
5pub mod client;
6pub mod stapler;
7
8use std::fmt::Display;
9
10pub use client::Client;
11pub use stapler::Stapler;
12
13use anyhow::{anyhow, Error};
14use chrono::{DateTime, FixedOffset, TimeDelta};
15use x509_parser::certificate;
16
17pub(crate) const LEEWAY: TimeDelta = TimeDelta::minutes(5);
19
20#[derive(Clone, Debug)]
22pub struct Validity {
23 pub not_before: DateTime<FixedOffset>,
24 pub not_after: DateTime<FixedOffset>,
25}
26
27impl Display for Validity {
28 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
29 write!(f, "{} -> {}", self.not_before, self.not_after)
30 }
31}
32
33impl TryFrom<&certificate::Validity> for Validity {
34 type Error = Error;
35 fn try_from(v: &certificate::Validity) -> Result<Self, Self::Error> {
36 let not_before = DateTime::from_timestamp(v.not_before.timestamp(), 0)
37 .ok_or_else(|| anyhow!("unable to parse not_before"))?
38 .into();
39
40 let not_after = DateTime::from_timestamp(v.not_after.timestamp(), 0)
41 .ok_or_else(|| anyhow!("unable to parse not_after"))?
42 .into();
43
44 Ok(Self {
45 not_before,
46 not_after,
47 })
48 }
49}
50
51impl Validity {
52 pub fn past_half_validity(&self, now: DateTime<FixedOffset>) -> bool {
54 now >= self.not_before + ((self.not_after - self.not_before) / 2)
55 }
56
57 pub fn valid(&self, now: DateTime<FixedOffset>) -> bool {
59 now >= (self.not_before - LEEWAY) && now <= (self.not_after + LEEWAY)
60 }
61}