From caaed8a5e15631480959ba0803bf44883f766d78 Mon Sep 17 00:00:00 2001 From: _ <_@_> Date: Fri, 27 Aug 2021 18:34:38 -0500 Subject: [PATCH] remove 30-day limit on scraper keys --- crates/ptth_relay/src/config.rs | 6 +-- crates/ptth_relay/src/key_validity.rs | 73 ++++----------------------- crates/ptth_relay/src/relay_state.rs | 2 +- crates/ptth_relay/src/scraper_api.rs | 1 - 4 files changed, 13 insertions(+), 69 deletions(-) diff --git a/crates/ptth_relay/src/config.rs b/crates/ptth_relay/src/config.rs index 7536170..f28d172 100644 --- a/crates/ptth_relay/src/config.rs +++ b/crates/ptth_relay/src/config.rs @@ -15,7 +15,6 @@ use crate::{ errors::ConfigError, key_validity::{ ScraperKey, - Valid30Days, }, }; @@ -99,7 +98,6 @@ pub mod file { use crate::key_validity::{ BlakeHashWrapper, ScraperKey, - Valid30Days, }; #[derive (Clone, Debug, Deserialize, Serialize)] @@ -142,7 +140,7 @@ pub mod file { pub servers: Option >, // Adding a DB will take a while, so I'm moving these out of dev mode. - pub scraper_keys: Option >>, + pub scraper_keys: Option >, pub news_url: Option , } @@ -156,7 +154,7 @@ pub struct Config { pub address: IpAddr, pub port: Option , pub servers: HashMap , - pub scraper_keys: HashMap >, + pub scraper_keys: HashMap , pub news_url: Option , } diff --git a/crates/ptth_relay/src/key_validity.rs b/crates/ptth_relay/src/key_validity.rs index dbe1c23..d0d5498 100644 --- a/crates/ptth_relay/src/key_validity.rs +++ b/crates/ptth_relay/src/key_validity.rs @@ -78,36 +78,17 @@ impl Serialize for BlakeHashWrapper { } } -pub struct Valid7Days; -pub struct Valid30Days; -//pub struct Valid90Days; - pub trait MaxValidDuration { fn dur () -> Duration; } -impl MaxValidDuration for Valid7Days { - fn dur () -> Duration { - Duration::days (7) - } -} - -impl MaxValidDuration for Valid30Days { - fn dur () -> Duration { - Duration::days (30) - } -} - #[derive (Deserialize)] -pub struct ScraperKey { +pub struct ScraperKey { name: String, not_before: DateTime , not_after: DateTime , pub hash: BlakeHashWrapper, - - #[serde (default)] - _phantom: std::marker::PhantomData , } #[derive (Copy, Clone, Debug, PartialEq)] @@ -121,21 +102,20 @@ pub enum KeyValidity { DurationNegative, } -impl ScraperKey { +impl ScraperKey { pub fn new_30_day > (name: S, input: &[u8]) -> Self { let now = Utc::now (); Self { name: name.into (), not_before: now, - not_after: now + V::dur (), + not_after: now + Duration::days (30), hash: BlakeHashWrapper::from_key (input), - _phantom: Default::default (), } } } -impl ScraperKey { +impl ScraperKey { #[must_use] pub fn is_valid (&self, now: DateTime , input: &[u8]) -> KeyValidity { use KeyValidity::*; @@ -152,13 +132,6 @@ impl ScraperKey { return DurationNegative; } - let max_dur = V::dur (); - let actual_dur = self.not_after - self.not_before; - - if actual_dur > max_dur { - return DurationTooLong (max_dur); - } - if now >= self.not_after { return Expired; } @@ -196,12 +169,11 @@ mod tests { fn duration_negative () { let zero_time = Utc::now (); - let key = ScraperKey:: { + let key = ScraperKey { name: "automated testing".to_string (), not_before: zero_time + Duration::days (1 + 2), not_after: zero_time + Duration::days (1), hash: BlakeHashWrapper::from_key ("bad_password".as_bytes ()), - _phantom: Default::default (), }; let err = DurationNegative; @@ -215,46 +187,22 @@ mod tests { } } - #[test] - fn key_valid_too_long () { - let zero_time = Utc::now (); - - let key = ScraperKey:: { - name: "automated testing".to_string (), - not_before: zero_time + Duration::days (1), - not_after: zero_time + Duration::days (1 + 31), - hash: BlakeHashWrapper::from_key ("bad_password".as_bytes ()), - _phantom: Default::default (), - }; - - let err = DurationTooLong (Duration::days (30)); - - for (input, expected) in &[ - (zero_time + Duration::days (0), err), - (zero_time + Duration::days (2), err), - (zero_time + Duration::days (100), err), - ] { - assert_eq! (key.is_valid (*input, "bad_password".as_bytes ()), *expected); - } - } - #[test] fn normal_key () { let zero_time = Utc::now (); - let key = ScraperKey:: { + let key = ScraperKey { name: "automated testing".to_string (), not_before: zero_time + Duration::days (1), - not_after: zero_time + Duration::days (1 + 30), + not_after: zero_time + Duration::days (1 + 60), hash: BlakeHashWrapper::from_key ("bad_password".as_bytes ()), - _phantom: Default::default (), }; for (input, expected) in &[ (zero_time + Duration::days (0), ClockIsBehind), (zero_time + Duration::days (2), Valid), - (zero_time + Duration::days (29), Valid), - (zero_time + Duration::days (1 + 30), Expired), + (zero_time + Duration::days (60 - 1), Valid), + (zero_time + Duration::days (60 + 1), Expired), (zero_time + Duration::days (100), Expired), ] { assert_eq! (key.is_valid (*input, "bad_password".as_bytes ()), *expected); @@ -265,12 +213,11 @@ mod tests { fn wrong_key () { let zero_time = Utc::now (); - let key = ScraperKey:: { + let key = ScraperKey { name: "automated testing".to_string (), not_before: zero_time + Duration::days (1), not_after: zero_time + Duration::days (1 + 30), hash: BlakeHashWrapper::from_key ("bad_password".as_bytes ()), - _phantom: Default::default (), }; for input in &[ diff --git a/crates/ptth_relay/src/relay_state.rs b/crates/ptth_relay/src/relay_state.rs index c08cfbf..4987446 100644 --- a/crates/ptth_relay/src/relay_state.rs +++ b/crates/ptth_relay/src/relay_state.rs @@ -312,7 +312,7 @@ impl Builder { self } - pub fn scraper_key (mut self, key: crate::key_validity::ScraperKey ) + pub fn scraper_key (mut self, key: crate::key_validity::ScraperKey) -> Self { self.config.scraper_keys.insert (key.hash.encode_base64 (), key); diff --git a/crates/ptth_relay/src/scraper_api.rs b/crates/ptth_relay/src/scraper_api.rs index 67e0688..d85e1e7 100644 --- a/crates/ptth_relay/src/scraper_api.rs +++ b/crates/ptth_relay/src/scraper_api.rs @@ -224,7 +224,6 @@ mod tests { use tokio::runtime::Runtime; use crate::{ - config, key_validity, }; use super::*;