From 44d09b68e7fab5f781032dc529096bb70116dbc4 Mon Sep 17 00:00:00 2001 From: Quentin Gliech Date: Mon, 24 Oct 2022 09:16:40 +0200 Subject: [PATCH] Remove usages of Utc::now() in time claims --- crates/jose/src/claims.rs | 33 +++++++-------------------------- 1 file changed, 7 insertions(+), 26 deletions(-) diff --git a/crates/jose/src/claims.rs b/crates/jose/src/claims.rs index 557e73d3..5ec0f220 100644 --- a/crates/jose/src/claims.rs +++ b/crates/jose/src/claims.rs @@ -150,51 +150,32 @@ impl Claim { #[derive(Debug, Clone)] pub struct TimeOptions { - when: Option>, + when: chrono::DateTime, leeway: chrono::Duration, } -impl Default for TimeOptions { - fn default() -> Self { - Self { - when: None, - leeway: chrono::Duration::minutes(5), - } - } -} - impl TimeOptions { #[must_use] pub fn new(when: chrono::DateTime) -> Self { Self { - when: Some(when), - ..Self::default() + when, + leeway: chrono::Duration::minutes(5), } } - #[must_use] - pub fn freeze(mut self) -> Self { - self.when = Some(chrono::Utc::now()); - self - } - #[must_use] pub fn leeway(mut self, leeway: chrono::Duration) -> Self { self.leeway = leeway; self } - - fn when(&self) -> chrono::DateTime { - self.when.unwrap_or_else(chrono::Utc::now) - } } -#[derive(Debug, Clone, Default)] +#[derive(Debug, Clone)] pub struct TimeNotAfter(TimeOptions); impl Validator for TimeNotAfter { fn validate(&self, value: &Timestamp) -> Result<(), anyhow::Error> { - if self.0.when() <= value.0 + self.0.leeway { + if self.0.when <= value.0 + self.0.leeway { Ok(()) } else { Err(anyhow::anyhow!("current time is too far away")) @@ -214,12 +195,12 @@ impl From<&TimeOptions> for TimeNotAfter { } } -#[derive(Debug, Clone, Default)] +#[derive(Debug, Clone)] pub struct TimeNotBefore(TimeOptions); impl Validator for TimeNotBefore { fn validate(&self, value: &Timestamp) -> Result<(), anyhow::Error> { - if self.0.when() >= value.0 - self.0.leeway { + if self.0.when >= value.0 - self.0.leeway { Ok(()) } else { Err(anyhow::anyhow!("current time is too far before"))