2023-04-18 23:43:56 +02:00
|
|
|
use crate::log_err;
|
|
|
|
use crate::settings::{ERROR_TEXT, ROOT_FOLDER};
|
2023-04-18 23:53:15 +02:00
|
|
|
use crate::utils::{get_user_file_path, pending_path, read_file};
|
2023-04-13 18:56:32 +02:00
|
|
|
|
2023-04-17 22:33:10 +02:00
|
|
|
use anyhow::Result;
|
2023-04-13 18:56:32 +02:00
|
|
|
use chrono::Utc;
|
2023-04-18 23:43:56 +02:00
|
|
|
use log::{debug, warn};
|
2023-04-13 18:56:32 +02:00
|
|
|
use serde::{Deserialize, Serialize};
|
2023-04-14 12:18:49 +02:00
|
|
|
use std::{fmt::Display, fs, path::Path};
|
2023-04-13 18:56:32 +02:00
|
|
|
|
2023-04-14 18:55:17 +02:00
|
|
|
#[derive(Serialize, Deserialize, Debug, Clone, Copy)]
|
2023-04-13 18:56:32 +02:00
|
|
|
pub enum Action {
|
|
|
|
Add,
|
|
|
|
Delete,
|
|
|
|
}
|
|
|
|
|
2023-04-14 12:18:49 +02:00
|
|
|
impl Display for Action {
|
|
|
|
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
|
|
|
|
write!(f, "{self:?}")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-04-13 18:56:32 +02:00
|
|
|
#[derive(Serialize, Deserialize, Debug)]
|
|
|
|
pub struct Pending {
|
|
|
|
action: Action,
|
|
|
|
data: String,
|
|
|
|
timestamp: i64,
|
|
|
|
}
|
|
|
|
impl Pending {
|
2023-04-13 22:00:33 +02:00
|
|
|
pub fn build_add(pem: String) -> Self {
|
2023-04-13 18:56:32 +02:00
|
|
|
let timestamp = Utc::now().timestamp();
|
2023-04-13 22:00:33 +02:00
|
|
|
Self {
|
2023-04-13 18:56:32 +02:00
|
|
|
action: Action::Add,
|
|
|
|
data: pem,
|
|
|
|
timestamp,
|
|
|
|
}
|
|
|
|
}
|
2023-04-13 22:00:33 +02:00
|
|
|
pub fn build_delete(email: String) -> Self {
|
2023-04-13 18:56:32 +02:00
|
|
|
let timestamp = Utc::now().timestamp();
|
2023-04-13 22:00:33 +02:00
|
|
|
Self {
|
2023-04-13 18:56:32 +02:00
|
|
|
action: Action::Delete,
|
|
|
|
data: email,
|
|
|
|
timestamp,
|
|
|
|
}
|
|
|
|
}
|
2023-04-13 22:00:33 +02:00
|
|
|
pub const fn action(&self) -> &Action {
|
2023-04-13 18:56:32 +02:00
|
|
|
&self.action
|
|
|
|
}
|
|
|
|
pub fn data(&self) -> &str {
|
|
|
|
&self.data
|
|
|
|
}
|
2023-04-13 22:00:33 +02:00
|
|
|
pub const fn timestamp(&self) -> i64 {
|
2023-04-13 18:56:32 +02:00
|
|
|
self.timestamp
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-04-17 22:33:10 +02:00
|
|
|
fn store_pending(pending: &Pending, token: &str) -> Result<()> {
|
2023-04-18 23:43:56 +02:00
|
|
|
let serialized = log_err!(toml::to_string(pending), warn)?;
|
|
|
|
log_err!(fs::write(pending_path().join(token), serialized), warn)?;
|
2023-04-17 22:33:10 +02:00
|
|
|
Ok(())
|
2023-04-13 18:56:32 +02:00
|
|
|
}
|
|
|
|
|
2023-04-17 22:33:10 +02:00
|
|
|
pub fn store_pending_addition(pem: String, _email: &str, token: &str) -> Result<()> {
|
2023-04-13 22:26:41 +02:00
|
|
|
let pending = Pending::build_add(pem);
|
|
|
|
store_pending(&pending, token)?;
|
2023-04-13 22:00:33 +02:00
|
|
|
Ok(())
|
2023-04-13 18:56:32 +02:00
|
|
|
}
|
|
|
|
|
2023-04-17 22:33:10 +02:00
|
|
|
pub fn store_pending_deletion(email: String, token: &str) -> Result<()> {
|
|
|
|
let pending = Pending::build_delete(email);
|
2023-04-13 22:26:41 +02:00
|
|
|
store_pending(&pending, token)?;
|
2023-04-13 22:00:33 +02:00
|
|
|
Ok(())
|
2023-04-13 18:56:32 +02:00
|
|
|
}
|
|
|
|
|
2023-04-14 18:55:17 +02:00
|
|
|
pub fn clean_stale(max_age: i64) {
|
2023-04-18 18:18:16 +02:00
|
|
|
for path in fs::read_dir(pending_path()).unwrap().flatten() {
|
2023-04-13 22:00:33 +02:00
|
|
|
let file_path = path.path();
|
2023-04-17 22:33:10 +02:00
|
|
|
let content = match read_file(&file_path) {
|
|
|
|
Ok(content) => content,
|
2023-04-19 00:25:40 +02:00
|
|
|
Err(error) => {
|
|
|
|
warn!("{} {}", ERROR_TEXT, error);
|
2023-04-17 22:33:10 +02:00
|
|
|
continue;
|
2023-04-13 22:00:33 +02:00
|
|
|
}
|
2023-04-17 22:33:10 +02:00
|
|
|
};
|
|
|
|
let key = match toml::from_str::<Pending>(&content) {
|
|
|
|
Ok(key) => key,
|
2023-04-18 23:43:56 +02:00
|
|
|
Err(error) => {
|
|
|
|
warn!("{} {}", ERROR_TEXT, error);
|
2023-04-17 22:33:10 +02:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
let now = Utc::now().timestamp();
|
|
|
|
if now - key.timestamp() > max_age {
|
2023-04-18 23:43:56 +02:00
|
|
|
match fs::remove_file(&file_path) {
|
|
|
|
Ok(_) => debug!("Deleted {}, since it was stale", file_path.display()),
|
|
|
|
Err(error) => warn!("{} {}", ERROR_TEXT, error),
|
|
|
|
};
|
2023-04-13 18:56:32 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-04-17 22:33:10 +02:00
|
|
|
pub fn delete_key(email: &str) -> Result<()> {
|
2023-04-16 13:58:52 +02:00
|
|
|
let path = Path::new(&ROOT_FOLDER).join(get_user_file_path(email)?);
|
2023-04-18 23:43:56 +02:00
|
|
|
log_err!(fs::remove_file(path), warn)?;
|
2023-04-17 22:33:10 +02:00
|
|
|
Ok(())
|
2023-04-13 18:56:32 +02:00
|
|
|
}
|