2023-04-13 22:00:33 +02:00
|
|
|
use crate::errors::Error;
|
2023-04-13 18:56:32 +02:00
|
|
|
use crate::management::{delete_key, Action, Pending};
|
|
|
|
use crate::utils::{get_email_from_cert, parse_pem};
|
|
|
|
use crate::PENDING;
|
|
|
|
use crate::{pending_path, PATH, VARIANT};
|
|
|
|
|
|
|
|
use std::fs;
|
|
|
|
use std::path::Path;
|
|
|
|
|
2023-04-13 22:00:33 +02:00
|
|
|
pub fn confirm_action(token: &str) -> Result<(), Error> {
|
2023-04-13 18:56:32 +02:00
|
|
|
let pending_path = pending_path!().join(token);
|
2023-04-13 22:26:41 +02:00
|
|
|
let content = if pending_path.exists() {
|
2023-04-13 22:00:33 +02:00
|
|
|
match fs::read_to_string(&pending_path) {
|
2023-04-13 22:26:41 +02:00
|
|
|
Ok(content) => content,
|
2023-04-13 22:00:33 +02:00
|
|
|
Err(_) => return Err(Error::Inaccessible),
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
return Err(Error::MissingPath);
|
|
|
|
};
|
2023-04-13 22:26:41 +02:00
|
|
|
let key = match serde_json::from_str::<Pending>(&content) {
|
2023-04-13 22:00:33 +02:00
|
|
|
Ok(key) => key,
|
|
|
|
Err(_) => return Err(Error::ParseStored),
|
|
|
|
};
|
2023-04-13 18:56:32 +02:00
|
|
|
match key.action() {
|
|
|
|
Action::Add => {
|
2023-04-13 22:00:33 +02:00
|
|
|
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::MalformedMail),
|
|
|
|
};
|
|
|
|
match sequoia_net::wkd::insert(PATH, domain, VARIANT, &cert) {
|
|
|
|
Ok(_) => (),
|
|
|
|
Err(_) => return Err(Error::AddingKey),
|
|
|
|
}
|
2023-04-13 18:56:32 +02:00
|
|
|
}
|
2023-04-13 22:00:33 +02:00
|
|
|
Action::Delete => delete_key(key.data())?,
|
|
|
|
}
|
|
|
|
match fs::remove_file(&pending_path) {
|
|
|
|
Ok(_) => Ok(()),
|
|
|
|
Err(_) => Err(Error::Inaccessible),
|
2023-04-13 18:56:32 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-04-13 22:00:33 +02:00
|
|
|
pub fn send_confirmation_email(email: &str, action: &Action, token: &str) {
|
|
|
|
println!("Email sent to {email}");
|
|
|
|
println!("Action: {action:?}");
|
|
|
|
println!("Token: {token}");
|
2023-04-13 18:56:32 +02:00
|
|
|
}
|