mirror of
https://git.verdigado.com/NB-Public/simple-wkd.git
synced 2024-12-06 14:52:41 +01:00
Improve logger
This commit is contained in:
parent
b717852376
commit
8484febb9d
5 changed files with 154 additions and 43 deletions
|
@ -1,42 +1,67 @@
|
|||
use chrono::Utc;
|
||||
use log::{debug, error, trace, warn};
|
||||
|
||||
use crate::errors::Error;
|
||||
use crate::management::{delete_key, Action, Pending};
|
||||
use crate::pending_path;
|
||||
use crate::settings::{MAILER, SETTINGS};
|
||||
use crate::utils::{get_email_from_cert, parse_pem};
|
||||
use crate::utils::{get_email_from_cert, get_filename, parse_pem};
|
||||
use crate::PENDING_FOLDER;
|
||||
|
||||
use lettre::{Message, Transport};
|
||||
use std::fs;
|
||||
use std::path::Path;
|
||||
|
||||
pub fn confirm_action(token: &str) -> Result<(), Error> {
|
||||
pub fn confirm_action(token: &str) -> Result<(Action, String), Error> {
|
||||
trace!("Handling token {}", token);
|
||||
let pending_path = pending_path!().join(token);
|
||||
let content = if pending_path.is_file() {
|
||||
match fs::read_to_string(&pending_path) {
|
||||
Ok(content) => content,
|
||||
Err(_) => return Err(Error::Inaccessible),
|
||||
Err(_) => {
|
||||
warn!(
|
||||
"Token {} was requested, but can't be read to string!",
|
||||
token
|
||||
);
|
||||
return Err(Error::Inaccessible);
|
||||
}
|
||||
}
|
||||
} else {
|
||||
trace!("Requested token {} isn't a file", token);
|
||||
return Err(Error::MissingPending);
|
||||
};
|
||||
let key = match serde_json::from_str::<Pending>(&content) {
|
||||
Ok(key) => key,
|
||||
Err(_) => return Err(Error::DeserializeData),
|
||||
Err(_) => {
|
||||
warn!("Error while deserializing token {}!", token);
|
||||
return Err(Error::DeserializeData);
|
||||
}
|
||||
};
|
||||
if Utc::now().timestamp() - key.timestamp() > SETTINGS.max_age {
|
||||
match fs::remove_file(pending_path) {
|
||||
Ok(_) => Err(Error::MissingPending),
|
||||
Err(_) => Err(Error::Inaccessible),
|
||||
match fs::remove_file(&pending_path) {
|
||||
Ok(_) => {
|
||||
debug!(
|
||||
"Deleted stale token {}",
|
||||
get_filename(&pending_path).unwrap()
|
||||
);
|
||||
Err(Error::MissingPending)
|
||||
}
|
||||
Err(_) => {
|
||||
warn!("Stale token {} can't be deleted!", token);
|
||||
Err(Error::Inaccessible)
|
||||
}
|
||||
}
|
||||
} else {
|
||||
match key.action() {
|
||||
let address = match key.action() {
|
||||
Action::Add => {
|
||||
let cert = parse_pem(key.data())?;
|
||||
let domain = match get_email_from_cert(&cert)?.split('@').last() {
|
||||
let email = get_email_from_cert(&cert)?;
|
||||
let domain = match email.split('@').last() {
|
||||
Some(domain) => domain.to_string(),
|
||||
None => return Err(Error::ParseEmail),
|
||||
None => {
|
||||
warn!("Error while parsing email's domain in token {}", token);
|
||||
return Err(Error::ParseEmail);
|
||||
}
|
||||
};
|
||||
match sequoia_net::wkd::insert(
|
||||
&SETTINGS.root_folder,
|
||||
|
@ -44,28 +69,54 @@ pub fn confirm_action(token: &str) -> Result<(), Error> {
|
|||
SETTINGS.variant,
|
||||
&cert,
|
||||
) {
|
||||
Ok(_) => (),
|
||||
Err(_) => return Err(Error::AddingKey),
|
||||
Ok(_) => email,
|
||||
Err(_) => {
|
||||
warn!("Unable to create a wkd entry for token {}", token);
|
||||
return Err(Error::AddingKey);
|
||||
}
|
||||
}
|
||||
}
|
||||
Action::Delete => delete_key(key.data())?,
|
||||
}
|
||||
Action::Delete => match delete_key(key.data()) {
|
||||
Ok(_) => key.data().to_owned(),
|
||||
Err(error) => {
|
||||
warn!("Unable to delete key for user {}", key.data());
|
||||
return Err(error);
|
||||
}
|
||||
},
|
||||
};
|
||||
debug!("Token {} was confirmed", token);
|
||||
match fs::remove_file(&pending_path) {
|
||||
Ok(_) => Ok(()),
|
||||
Err(_) => Err(Error::Inaccessible),
|
||||
Ok(_) => {
|
||||
trace!(
|
||||
"Deleted confirmed token {}",
|
||||
pending_path.file_name().unwrap().to_str().unwrap()
|
||||
);
|
||||
Ok((*key.action(), address))
|
||||
}
|
||||
Err(_) => {
|
||||
warn!("Unable to delete confirmed token {}", token);
|
||||
Err(Error::Inaccessible)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
pub fn send_confirmation_email(email: &str, action: &Action, token: &str) -> Result<(), Error> {
|
||||
pub fn send_confirmation_email(address: &str, action: &Action, token: &str) -> Result<(), Error> {
|
||||
debug!("Sending email to {}", address);
|
||||
let email = Message::builder()
|
||||
.from(match SETTINGS.mail_settings.mail_from.parse() {
|
||||
Ok(mailbox) => mailbox,
|
||||
Err(_) => panic!("Unable to parse the email in the settings!"),
|
||||
Err(_) => {
|
||||
error!("Unable to parse the email in the settings!");
|
||||
panic!("Unable to parse the email in the settings!")
|
||||
}
|
||||
})
|
||||
.to(match email.parse() {
|
||||
.to(match address.parse() {
|
||||
Ok(mailbox) => mailbox,
|
||||
Err(_) => return Err(Error::ParseEmail),
|
||||
Err(_) => {
|
||||
warn!("Error while parsing destination email for token {}", token);
|
||||
return Err(Error::ParseEmail);
|
||||
}
|
||||
})
|
||||
.subject(
|
||||
SETTINGS
|
||||
|
@ -87,11 +138,20 @@ pub fn send_confirmation_email(email: &str, action: &Action, token: &str) -> Res
|
|||
|
||||
let message = match email {
|
||||
Ok(message) => message,
|
||||
Err(_) => return Err(Error::MailGeneration),
|
||||
Err(_) => {
|
||||
warn!("Unable to build email for token {}", token);
|
||||
return Err(Error::MailGeneration);
|
||||
}
|
||||
};
|
||||
|
||||
match MAILER.send(&message) {
|
||||
Ok(_) => Ok(()),
|
||||
Err(_) => Err(Error::SendMail),
|
||||
Ok(_) => {
|
||||
debug!("successfully sent email to {}", address);
|
||||
Ok(())
|
||||
}
|
||||
Err(_) => {
|
||||
warn!("Unable to send email to {}", address);
|
||||
Err(Error::SendMail)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue