diff --git a/src/confirmation.rs b/src/confirmation.rs index a2a58fc..0c308f3 100644 --- a/src/confirmation.rs +++ b/src/confirmation.rs @@ -1,3 +1,5 @@ +use chrono::Utc; + use crate::errors::Error; use crate::management::{delete_key, Action, Pending}; use crate::pending_path; @@ -21,28 +23,35 @@ pub fn confirm_action(token: &str) -> Result<(), Error> { Ok(key) => key, Err(_) => return Err(Error::DeserializeData), }; - match key.action() { - Action::Add => { - let cert = parse_pem(key.data())?; - let domain = match get_email_from_cert(&cert)?.split('@').last() { - Some(domain) => domain.to_string(), - None => return Err(Error::ParseEmail), - }; - match sequoia_net::wkd::insert( - &SETTINGS.folder_structure.root_folder, - domain, - SETTINGS.variant, - &cert, - ) { - Ok(_) => (), - Err(_) => return Err(Error::AddingKey), - } + if Utc::now().timestamp() - key.timestamp() > SETTINGS.max_age { + match fs::remove_file(pending_path) { + Ok(_) => Err(Error::MissingPending), + Err(_) => Err(Error::Inaccessible), + } + } else { + match key.action() { + Action::Add => { + let cert = parse_pem(key.data())?; + let domain = match get_email_from_cert(&cert)?.split('@').last() { + Some(domain) => domain.to_string(), + None => return Err(Error::ParseEmail), + }; + match sequoia_net::wkd::insert( + &SETTINGS.folder_structure.root_folder, + domain, + SETTINGS.variant, + &cert, + ) { + Ok(_) => (), + Err(_) => return Err(Error::AddingKey), + } + } + Action::Delete => delete_key(key.data())?, + } + match fs::remove_file(&pending_path) { + Ok(_) => Ok(()), + Err(_) => Err(Error::Inaccessible), } - Action::Delete => delete_key(key.data())?, - } - match fs::remove_file(&pending_path) { - Ok(_) => Ok(()), - Err(_) => Err(Error::Inaccessible), } } diff --git a/src/main.rs b/src/main.rs index f018c10..c30b2c1 100644 --- a/src/main.rs +++ b/src/main.rs @@ -36,7 +36,7 @@ struct Email { async fn main() -> std::io::Result<()> { fs::create_dir_all(pending_path!())?; task::spawn(async { - let mut metronome = time::interval(time::Duration::from_secs(60 * 60 * 3)); + let mut metronome = time::interval(time::Duration::from_secs(SETTINGS.cleanup_interval)); loop { metronome.tick().await; clean_stale(SETTINGS.max_age).unwrap(); diff --git a/src/settings.rs b/src/settings.rs index dde4194..6e3510a 100644 --- a/src/settings.rs +++ b/src/settings.rs @@ -9,6 +9,7 @@ pub struct Settings { #[serde(with = "VariantDef")] pub variant: Variant, pub max_age: i64, + pub cleanup_interval: u64, pub port: u16, pub folder_structure: FolderStructure, pub smtp_settings: MailSettings, @@ -38,6 +39,7 @@ pub enum VariantDef { } fn get_settings() -> Settings { + println!("Reaing settings..."); let content = fs::read_to_string("wkd.toml").unwrap(); toml::from_str(&content).unwrap() }