mirror of
https://git.verdigado.com/NB-Public/simple-wkd.git
synced 2024-10-30 03:05:51 +01:00
Revamp error handling, disable logging for now
This commit is contained in:
parent
9f4c5fbc05
commit
0f5b9ae1d6
8 changed files with 167 additions and 303 deletions
1
backend/Cargo.lock
generated
1
backend/Cargo.lock
generated
|
@ -1966,6 +1966,7 @@ version = "0.1.0"
|
|||
dependencies = [
|
||||
"actix-files",
|
||||
"actix-web",
|
||||
"anyhow",
|
||||
"chrono",
|
||||
"flexi_logger",
|
||||
"lettre",
|
||||
|
|
|
@ -6,6 +6,7 @@ edition = "2021"
|
|||
[dependencies]
|
||||
actix-files = "0.6.2"
|
||||
actix-web = "4.3.1"
|
||||
anyhow = "1.0.70"
|
||||
chrono = "0.4.24"
|
||||
flexi_logger = "0.25.3"
|
||||
lettre = "0.10.4"
|
||||
|
|
|
@ -1,56 +1,24 @@
|
|||
use chrono::Utc;
|
||||
use lettre::message::header::ContentType;
|
||||
use log::{debug, error, trace, warn};
|
||||
|
||||
use crate::errors::Error;
|
||||
use crate::errors::SpecialErrors;
|
||||
use crate::management::{delete_key, Action, Pending};
|
||||
use crate::pending_path;
|
||||
use crate::settings::{MAILER, ROOT_FOLDER, SETTINGS};
|
||||
use crate::utils::{get_email_from_cert, get_filename, parse_pem};
|
||||
use crate::utils::{get_email_from_cert, parse_pem, read_file};
|
||||
use anyhow::{anyhow, bail, Result};
|
||||
|
||||
use lettre::{Message, Transport};
|
||||
use std::fs;
|
||||
use std::path::Path;
|
||||
|
||||
pub fn confirm_action(token: &str) -> Result<(Action, String), Error> {
|
||||
trace!("Handling token {}", token);
|
||||
pub fn confirm_action(token: &str) -> Result<(Action, String)> {
|
||||
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(_) => {
|
||||
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 toml::from_str::<Pending>(&content) {
|
||||
Ok(key) => key,
|
||||
Err(_) => {
|
||||
warn!("Error while deserializing token {}!", token);
|
||||
return Err(Error::DeserializeData);
|
||||
}
|
||||
};
|
||||
let content = read_file(&pending_path)?;
|
||||
let key = toml::from_str::<Pending>(&content)?;
|
||||
if Utc::now().timestamp() - key.timestamp() > SETTINGS.max_age {
|
||||
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)
|
||||
}
|
||||
}
|
||||
fs::remove_file(&pending_path)?;
|
||||
Err(SpecialErrors::ExpiredRequest)?
|
||||
} else {
|
||||
let address = match key.action() {
|
||||
Action::Add => {
|
||||
|
@ -59,46 +27,24 @@ pub fn confirm_action(token: &str) -> Result<(Action, String), Error> {
|
|||
let domain = match email.split('@').last() {
|
||||
Some(domain) => domain.to_string(),
|
||||
None => {
|
||||
warn!("Error while parsing email's domain in token {}", token);
|
||||
return Err(Error::ParseEmail);
|
||||
Err(SpecialErrors::MalformedEmail)?
|
||||
}
|
||||
};
|
||||
match sequoia_net::wkd::insert(ROOT_FOLDER, domain, SETTINGS.variant, &cert) {
|
||||
Ok(_) => email,
|
||||
Err(_) => {
|
||||
warn!("Unable to create a wkd entry for token {}", token);
|
||||
return Err(Error::AddingKey);
|
||||
}
|
||||
}
|
||||
sequoia_net::wkd::insert(ROOT_FOLDER, domain, SETTINGS.variant, &cert)?;
|
||||
email
|
||||
}
|
||||
Action::Delete => {
|
||||
delete_key(key.data())?;
|
||||
key.data().to_owned()
|
||||
}
|
||||
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(_) => {
|
||||
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)
|
||||
}
|
||||
}
|
||||
fs::remove_file(&pending_path)?;
|
||||
Ok((*key.action(), address))
|
||||
}
|
||||
}
|
||||
|
||||
pub fn send_confirmation_email(address: &str, action: &Action, token: &str) -> Result<(), Error> {
|
||||
debug!("Sending email to {}", address);
|
||||
let template = fs::read_to_string(Path::new("assets").join("mail-template.html")).unwrap();
|
||||
pub fn send_confirmation_email(address: &str, action: &Action, token: &str) -> Result<()> {
|
||||
let template = read_file(&Path::new("assets").join("mail-template.html"))?;
|
||||
let mut url = SETTINGS
|
||||
.external_url
|
||||
.join("api/")
|
||||
|
@ -110,17 +56,10 @@ pub fn send_confirmation_email(address: &str, action: &Action, token: &str) -> R
|
|||
.from(match SETTINGS.mail_settings.mail_from.parse() {
|
||||
Ok(mailbox) => mailbox,
|
||||
Err(_) => {
|
||||
error!("Unable to parse the email in the settings!");
|
||||
panic!("Unable to parse the email in the settings!")
|
||||
}
|
||||
})
|
||||
.to(match address.parse() {
|
||||
Ok(mailbox) => mailbox,
|
||||
Err(_) => {
|
||||
warn!("Error while parsing destination email for token {}", token);
|
||||
return Err(Error::ParseEmail);
|
||||
}
|
||||
})
|
||||
.to(address.parse()?)
|
||||
.subject(
|
||||
SETTINGS
|
||||
.mail_settings
|
||||
|
@ -132,24 +71,8 @@ pub fn send_confirmation_email(address: &str, action: &Action, token: &str) -> R
|
|||
template
|
||||
.replace("{{%u}}", url.as_ref())
|
||||
.replace("{{%a}}", &action.to_string().to_lowercase()),
|
||||
);
|
||||
)?;
|
||||
|
||||
let message = match email {
|
||||
Ok(message) => message,
|
||||
Err(_) => {
|
||||
warn!("Unable to build email for token {}", token);
|
||||
return Err(Error::MailGeneration);
|
||||
}
|
||||
};
|
||||
|
||||
match MAILER.send(&message) {
|
||||
Ok(_) => {
|
||||
debug!("successfully sent email to {}", address);
|
||||
Ok(())
|
||||
}
|
||||
Err(_) => {
|
||||
warn!("Unable to send email to {}", address);
|
||||
Err(Error::SendMail)
|
||||
}
|
||||
}
|
||||
MAILER.send(&email)?;
|
||||
Ok(())
|
||||
}
|
||||
|
|
|
@ -1,50 +1,69 @@
|
|||
use actix_web::{http::StatusCode, HttpResponseBuilder, ResponseError};
|
||||
use thiserror::Error;
|
||||
use anyhow::Error;
|
||||
use std::fmt::Display;
|
||||
use thiserror::Error as DeriveError;
|
||||
|
||||
use crate::utils::return_outcome;
|
||||
|
||||
#[derive(Error, Debug, Clone, Copy)]
|
||||
pub enum Error {
|
||||
#[error("(0x01) Cert is invalid")]
|
||||
InvalidCert,
|
||||
#[error("(0x02) Error while parsing cert")]
|
||||
ParseCert,
|
||||
#[error("(0x03) Error while parsing an E-Mail address")]
|
||||
ParseEmail,
|
||||
#[error("(0x04) There is no pending request associated to this token")]
|
||||
MissingPending,
|
||||
#[error("(0x05) Requested key does not exist")]
|
||||
MissingKey,
|
||||
#[error("(0x06) No E-Mail found in the certificate")]
|
||||
MissingMail,
|
||||
#[error("(0x07) Error while sending the E-Mail")]
|
||||
SendMail,
|
||||
#[error("(0x08) rror while serializing data")]
|
||||
SerializeData,
|
||||
#[error("(0x09) Error while deserializing data")]
|
||||
DeserializeData,
|
||||
#[error("(0x0A) The file is inaccessible")]
|
||||
Inaccessible,
|
||||
#[error("(0x0B) Error while adding a key to the wkd")]
|
||||
AddingKey,
|
||||
#[error("(0x0C) Error while generating the wkd path")]
|
||||
PathGeneration,
|
||||
#[error("(0x0D) Error while generating the email")]
|
||||
MailGeneration,
|
||||
#[error("(0x0E) Wrong email domain")]
|
||||
WrongDomain,
|
||||
#[error("(0x0F) The requested file does not exist")]
|
||||
#[derive(Debug, DeriveError)]
|
||||
pub enum SpecialErrors {
|
||||
#[error("The request had expired!")]
|
||||
ExpiredRequest,
|
||||
#[error("The key for the requested user does not exist!")]
|
||||
InexistingUser,
|
||||
#[error("Could not parse user email: malformed email")]
|
||||
MalformedEmail,
|
||||
#[error("Could not find any primay user email in the keyblock!")]
|
||||
MalformedCert,
|
||||
#[error("The requested file does not exist!")]
|
||||
MissingFile,
|
||||
#[error("User email rejected: domain not allowed")]
|
||||
UnallowedDomain,
|
||||
}
|
||||
|
||||
impl ResponseError for Error {
|
||||
#[derive(Debug)]
|
||||
pub enum CompatErr {
|
||||
AnyhowErr(Error),
|
||||
SpecialErr(SpecialErrors),
|
||||
}
|
||||
|
||||
impl Display for CompatErr {
|
||||
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
|
||||
match self {
|
||||
Self::AnyhowErr(error) => write!(f, "{}", error),
|
||||
Self::SpecialErr(error) => write!(f, "{}", error),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl From<SpecialErrors> for CompatErr {
|
||||
fn from(value: SpecialErrors) -> Self {
|
||||
CompatErr::SpecialErr(value)
|
||||
}
|
||||
}
|
||||
|
||||
impl From<Error> for CompatErr {
|
||||
fn from(value: Error) -> Self {
|
||||
if value.is::<SpecialErrors>() {
|
||||
CompatErr::from(value.downcast::<SpecialErrors>().unwrap())
|
||||
} else {
|
||||
CompatErr::AnyhowErr(value)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl ResponseError for CompatErr {
|
||||
fn status_code(&self) -> actix_web::http::StatusCode {
|
||||
match self {
|
||||
Self::MissingPending => StatusCode::from_u16(404).unwrap(),
|
||||
Self::MissingKey => StatusCode::from_u16(404).unwrap(),
|
||||
Self::MissingFile => StatusCode::from_u16(404).unwrap(),
|
||||
Self::WrongDomain => StatusCode::from_u16(401).unwrap(),
|
||||
_ => StatusCode::from_u16(500).unwrap(),
|
||||
Self::AnyhowErr(_) => StatusCode::INTERNAL_SERVER_ERROR,
|
||||
Self::SpecialErr(error) => match error {
|
||||
SpecialErrors::ExpiredRequest => StatusCode::BAD_REQUEST,
|
||||
SpecialErrors::InexistingUser => StatusCode::NOT_FOUND,
|
||||
SpecialErrors::MalformedCert => StatusCode::BAD_REQUEST,
|
||||
SpecialErrors::MalformedEmail => StatusCode::BAD_REQUEST,
|
||||
SpecialErrors::MissingFile => StatusCode::NOT_FOUND,
|
||||
SpecialErrors::UnallowedDomain => StatusCode::UNAUTHORIZED,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
@ -5,11 +5,11 @@ mod settings;
|
|||
mod utils;
|
||||
|
||||
use crate::confirmation::{confirm_action, send_confirmation_email};
|
||||
use crate::errors::Error;
|
||||
use crate::errors::CompatErr;
|
||||
use crate::management::{clean_stale, store_pending_addition, store_pending_deletion, Action};
|
||||
use crate::settings::{ROOT_FOLDER, SETTINGS};
|
||||
use crate::utils::{
|
||||
gen_random_token, get_email_from_cert, is_email_allowed, parse_pem, return_outcome,
|
||||
gen_random_token, get_email_from_cert, is_email_allowed, parse_pem, return_outcome, read_file,
|
||||
};
|
||||
|
||||
use actix_files::Files;
|
||||
|
@ -18,7 +18,8 @@ use actix_web::http::StatusCode;
|
|||
use actix_web::{
|
||||
get, post, web, App, HttpRequest, HttpResponse, HttpResponseBuilder, HttpServer, Result,
|
||||
};
|
||||
use log::{debug, error, info};
|
||||
use anyhow::anyhow;
|
||||
use errors::SpecialErrors;
|
||||
use serde::Deserialize;
|
||||
use std::env;
|
||||
use std::fs;
|
||||
|
@ -47,7 +48,6 @@ async fn main() -> std::io::Result<()> {
|
|||
env::set_var("RUST_LOG", format!("simple_wkd={}", value));
|
||||
}
|
||||
if init_logger().is_err() {
|
||||
error!("Could not set up logger!");
|
||||
panic!("Could not set up logger!")
|
||||
};
|
||||
fs::create_dir_all(pending_path!())?;
|
||||
|
@ -55,15 +55,9 @@ async fn main() -> std::io::Result<()> {
|
|||
let mut metronome = time::interval(time::Duration::from_secs(SETTINGS.cleanup_interval));
|
||||
loop {
|
||||
metronome.tick().await;
|
||||
info!("Running cleanup...");
|
||||
clean_stale(SETTINGS.max_age);
|
||||
info!("Cleanup completed!");
|
||||
}
|
||||
});
|
||||
info!(
|
||||
"Running server on http://{}:{} (External URL: {})",
|
||||
SETTINGS.bind_host, SETTINGS.port, SETTINGS.external_url
|
||||
);
|
||||
HttpServer::new(|| {
|
||||
App::new()
|
||||
.service(submit)
|
||||
|
@ -80,7 +74,7 @@ async fn main() -> std::io::Result<()> {
|
|||
.await
|
||||
}
|
||||
|
||||
async fn index(req: HttpRequest) -> Result<HttpResponse, Error> {
|
||||
async fn index(req: HttpRequest) -> Result<HttpResponse, CompatErr> {
|
||||
let path = webpage_path!().join(req.match_info().query("filename"));
|
||||
for file in &["", "index.html"] {
|
||||
let path = if file.is_empty() {
|
||||
|
@ -89,55 +83,44 @@ async fn index(req: HttpRequest) -> Result<HttpResponse, Error> {
|
|||
path.join(file)
|
||||
};
|
||||
if path.is_file() {
|
||||
let template = match fs::read_to_string(&path) {
|
||||
Ok(template) => template,
|
||||
Err(_) => {
|
||||
debug!("File {} is inaccessible", path.display());
|
||||
return Err(Error::Inaccessible);
|
||||
}
|
||||
};
|
||||
let template = read_file(&path)?;
|
||||
let page = template.replace("((%u))", SETTINGS.external_url.as_ref());
|
||||
return Ok(HttpResponseBuilder::new(StatusCode::OK)
|
||||
.insert_header(ContentType::html())
|
||||
.body(page));
|
||||
}
|
||||
}
|
||||
debug!("File {} does not exist", path.display());
|
||||
Err(Error::MissingFile)
|
||||
Err(SpecialErrors::MissingFile)?
|
||||
}
|
||||
|
||||
#[post("/api/submit")]
|
||||
async fn submit(pem: web::Form<Key>) -> Result<HttpResponse, Error> {
|
||||
async fn submit(pem: web::Form<Key>) -> Result<HttpResponse, CompatErr> {
|
||||
let cert = parse_pem(&pem.key)?;
|
||||
let email = get_email_from_cert(&cert)?;
|
||||
is_email_allowed(&email)?;
|
||||
let token = gen_random_token();
|
||||
store_pending_addition(pem.key.clone(), &email, &token)?;
|
||||
send_confirmation_email(&email, &Action::Add, &token)?;
|
||||
info!("User {} submitted a key!", &email);
|
||||
return_outcome(Ok("You submitted your key successfully!"))
|
||||
Ok(return_outcome(Ok("You submitted your key successfully!"))?)
|
||||
}
|
||||
|
||||
#[get("/api/confirm")]
|
||||
async fn confirm(token: web::Query<Token>) -> Result<HttpResponse, Error> {
|
||||
let (action, email) = confirm_action(&token.token)?;
|
||||
async fn confirm(token: web::Query<Token>) -> Result<HttpResponse, CompatErr> {
|
||||
let (action, _email) = confirm_action(&token.token)?;
|
||||
match action {
|
||||
Action::Add => {
|
||||
info!("Key for user {} was added successfully!", email);
|
||||
return_outcome(Ok("Your key was added successfully!"))
|
||||
Ok(return_outcome(Ok("Your key was added successfully!"))?)
|
||||
}
|
||||
Action::Delete => {
|
||||
info!("Key for user {} was deleted successfully!", email);
|
||||
return_outcome(Ok("Your key was deleted successfully!"))
|
||||
Ok(return_outcome(Ok("Your key was deleted successfully!"))?)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[get("/api/delete")]
|
||||
async fn delete(email: web::Query<Email>) -> Result<HttpResponse, Error> {
|
||||
async fn delete(email: web::Query<Email>) -> Result<HttpResponse, CompatErr> {
|
||||
let token = gen_random_token();
|
||||
store_pending_deletion(email.email.clone(), &token)?;
|
||||
send_confirmation_email(&email.email, &Action::Delete, &token)?;
|
||||
info!("User {} requested the deletion of his key!", email.email);
|
||||
return_outcome(Ok("You requested the deletion of your key successfully!"))
|
||||
Ok(return_outcome(Ok("You requested the deletion of your key successfully!"))?)
|
||||
}
|
||||
|
|
|
@ -1,10 +1,9 @@
|
|||
use crate::pending_path;
|
||||
use crate::settings::ROOT_FOLDER;
|
||||
use crate::utils::{get_user_file_path, key_exists};
|
||||
use crate::{errors::Error, utils::get_filename};
|
||||
use crate::utils::{get_user_file_path, key_exists, read_file};
|
||||
|
||||
use anyhow::Result;
|
||||
use chrono::Utc;
|
||||
use log::{debug, error, warn};
|
||||
use serde::{Deserialize, Serialize};
|
||||
use std::{fmt::Display, fs, path::Path};
|
||||
|
||||
|
@ -54,87 +53,49 @@ impl Pending {
|
|||
}
|
||||
}
|
||||
|
||||
fn store_pending(pending: &Pending, token: &str) -> Result<(), Error> {
|
||||
let serialized = match toml::to_string(pending) {
|
||||
Ok(serialized) => serialized,
|
||||
Err(_) => return Err(Error::SerializeData),
|
||||
};
|
||||
match fs::write(pending_path!().join(token), serialized) {
|
||||
Ok(_) => Ok(()),
|
||||
Err(_) => Err(Error::Inaccessible),
|
||||
}
|
||||
}
|
||||
|
||||
pub fn store_pending_addition(pem: String, email: &str, token: &str) -> Result<(), Error> {
|
||||
let pending = Pending::build_add(pem);
|
||||
store_pending(&pending, token)?;
|
||||
debug!("Stored submission from {} with token {}", email, token);
|
||||
fn store_pending(pending: &Pending, token: &str) -> Result<()> {
|
||||
let serialized = toml::to_string(pending)?;
|
||||
fs::write(pending_path!().join(token), serialized)?;
|
||||
Ok(())
|
||||
}
|
||||
|
||||
pub fn store_pending_deletion(email: String, token: &str) -> Result<(), Error> {
|
||||
if let Err(error) = key_exists(&email) {
|
||||
match error {
|
||||
Error::PathGeneration => debug!("Error while generating path for user {}", email),
|
||||
Error::MissingKey => debug!("There is no key for user {}", email),
|
||||
_ => error!("An unexpected error occoured!"),
|
||||
}
|
||||
return Err(error);
|
||||
}
|
||||
let pending = Pending::build_delete(email.clone());
|
||||
pub fn store_pending_addition(pem: String, _email: &str, token: &str) -> Result<()> {
|
||||
let pending = Pending::build_add(pem);
|
||||
store_pending(&pending, token)?;
|
||||
Ok(())
|
||||
}
|
||||
|
||||
pub fn store_pending_deletion(email: String, token: &str) -> Result<()> {
|
||||
key_exists(&email)?;
|
||||
let pending = Pending::build_delete(email);
|
||||
store_pending(&pending, token)?;
|
||||
debug!(
|
||||
"Stored deletion request from {} with token {}",
|
||||
email, token
|
||||
);
|
||||
Ok(())
|
||||
}
|
||||
|
||||
pub fn clean_stale(max_age: i64) {
|
||||
for path in fs::read_dir(pending_path!()).unwrap().flatten() {
|
||||
let file_path = path.path();
|
||||
if file_path.is_file() {
|
||||
let content = match fs::read_to_string(&file_path) {
|
||||
Ok(content) => content,
|
||||
Err(_) => {
|
||||
warn!(
|
||||
"Could not read contents of token {} to string",
|
||||
get_filename(&file_path).unwrap()
|
||||
);
|
||||
continue;
|
||||
}
|
||||
};
|
||||
let key = match toml::from_str::<Pending>(&content) {
|
||||
Ok(key) => key,
|
||||
Err(_) => {
|
||||
warn!(
|
||||
"Could not deserialize token {}",
|
||||
get_filename(&file_path).unwrap()
|
||||
);
|
||||
continue;
|
||||
}
|
||||
};
|
||||
let now = Utc::now().timestamp();
|
||||
if now - key.timestamp() > max_age {
|
||||
if fs::remove_file(&file_path).is_err() {
|
||||
{
|
||||
warn!(
|
||||
"Could not delete stale token {}",
|
||||
get_filename(&file_path).unwrap()
|
||||
);
|
||||
continue;
|
||||
};
|
||||
}
|
||||
debug!("Deleted stale token {}", get_filename(&file_path).unwrap())
|
||||
let content = match read_file(&file_path) {
|
||||
Ok(content) => content,
|
||||
Err(_) => {
|
||||
continue;
|
||||
}
|
||||
};
|
||||
let key = match toml::from_str::<Pending>(&content) {
|
||||
Ok(key) => key,
|
||||
Err(_) => {
|
||||
continue;
|
||||
}
|
||||
};
|
||||
let now = Utc::now().timestamp();
|
||||
if now - key.timestamp() > max_age {
|
||||
let _ = fs::remove_file(&file_path);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
pub fn delete_key(email: &str) -> Result<(), Error> {
|
||||
pub fn delete_key(email: &str) -> Result<()> {
|
||||
let path = Path::new(&ROOT_FOLDER).join(get_user_file_path(email)?);
|
||||
match fs::remove_file(path) {
|
||||
Ok(_) => Ok(()),
|
||||
Err(_) => Err(Error::Inaccessible),
|
||||
}
|
||||
fs::remove_file(path)?;
|
||||
Ok(())
|
||||
}
|
||||
|
|
|
@ -1,11 +1,12 @@
|
|||
use lettre::{transport::smtp::authentication::Credentials, SmtpTransport};
|
||||
use log::{debug, error, warn};
|
||||
use once_cell::sync::Lazy;
|
||||
use sequoia_net::wkd::Variant;
|
||||
use serde::{Deserialize, Serialize};
|
||||
use std::fs;
|
||||
use std::{path::{PathBuf}};
|
||||
use url::Url;
|
||||
|
||||
use crate::utils::read_file;
|
||||
|
||||
#[derive(Serialize, Deserialize, Debug)]
|
||||
pub struct Settings {
|
||||
#[serde(with = "VariantDef")]
|
||||
|
@ -44,27 +45,22 @@ pub enum SMTPEncryption {
|
|||
}
|
||||
|
||||
fn get_settings() -> Settings {
|
||||
debug!("Reading settings...");
|
||||
let content = match fs::read_to_string("config.toml") {
|
||||
let content = match read_file(&PathBuf::from("config.toml")) {
|
||||
Ok(content) => content,
|
||||
Err(_) => {
|
||||
error!("Unable to access settings file!");
|
||||
panic!("Unable to access settings file!")
|
||||
}
|
||||
};
|
||||
let settings = match toml::from_str(&content) {
|
||||
Ok(settings) => settings,
|
||||
Err(_) => {
|
||||
error!("Unable to parse settings from file!");
|
||||
panic!("Unable to parse settings from file!")
|
||||
}
|
||||
};
|
||||
debug!("Successfully read setting!");
|
||||
settings
|
||||
}
|
||||
|
||||
fn get_mailer() -> SmtpTransport {
|
||||
debug!("Setting up SMTP...");
|
||||
let creds = Credentials::new(
|
||||
SETTINGS.mail_settings.smtp_username.to_owned(),
|
||||
SETTINGS.mail_settings.smtp_password.to_owned(),
|
||||
|
@ -78,18 +74,13 @@ fn get_mailer() -> SmtpTransport {
|
|||
let mailer = match builder {
|
||||
Ok(builder) => builder,
|
||||
Err(_) => {
|
||||
error!("Unable to set up smtp");
|
||||
panic!("Unable to set up smtp")
|
||||
}
|
||||
}
|
||||
.credentials(creds)
|
||||
.port(SETTINGS.mail_settings.smtp_port)
|
||||
.build();
|
||||
debug!("Checking connection...");
|
||||
if mailer.test_connection().is_err() {
|
||||
warn!("Connection test to smtp host failed!");
|
||||
}
|
||||
debug!("SMTP setup successful!");
|
||||
let _ = mailer.test_connection();
|
||||
mailer
|
||||
}
|
||||
|
||||
|
|
|
@ -1,12 +1,15 @@
|
|||
use crate::errors::SpecialErrors;
|
||||
use crate::settings::ROOT_FOLDER;
|
||||
use crate::settings::SETTINGS;
|
||||
use crate::{errors::Error, settings::ROOT_FOLDER};
|
||||
|
||||
use actix_web::{
|
||||
http::{header::ContentType, StatusCode},
|
||||
HttpResponse, HttpResponseBuilder,
|
||||
};
|
||||
use flexi_logger::{style, DeferredNow, FileSpec, FlexiLoggerError, Logger, LoggerHandle, Record};
|
||||
use log::debug;
|
||||
use anyhow::{anyhow, bail, Result};
|
||||
use flexi_logger::{
|
||||
detailed_format, style, DeferredNow, FileSpec, FlexiLoggerError, Logger, LoggerHandle, Record,
|
||||
};
|
||||
use rand::{distributions::Alphanumeric, thread_rng, Rng};
|
||||
use sequoia_net::wkd::Url;
|
||||
use sequoia_openpgp::{parse::Parse, policy::StandardPolicy, Cert};
|
||||
|
@ -29,26 +32,29 @@ macro_rules! webpage_path {
|
|||
};
|
||||
}
|
||||
|
||||
pub fn is_email_allowed(email: &str) -> Result<(), Error> {
|
||||
pub fn read_file(path: &PathBuf) -> Result<String> {
|
||||
if path.is_file() {
|
||||
Ok(fs::read_to_string(path)?)
|
||||
} else {
|
||||
Err(SpecialErrors::MissingFile)?
|
||||
}
|
||||
}
|
||||
|
||||
pub fn is_email_allowed(email: &str) -> Result<()> {
|
||||
let allowed = match email.split('@').last() {
|
||||
Some(domain) => SETTINGS.allowed_domains.contains(&domain.to_string()),
|
||||
None => return Err(Error::ParseEmail),
|
||||
None => Err(SpecialErrors::MalformedEmail)?,
|
||||
};
|
||||
if !allowed {
|
||||
return Err(Error::WrongDomain);
|
||||
Err(SpecialErrors::UnallowedDomain)?;
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
|
||||
pub fn parse_pem(pemfile: &str) -> Result<Cert, Error> {
|
||||
let cert = match sequoia_openpgp::Cert::from_bytes(pemfile.as_bytes()) {
|
||||
Ok(cert) => cert,
|
||||
Err(_) => return Err(Error::ParseCert),
|
||||
};
|
||||
pub fn parse_pem(pemfile: &str) -> Result<Cert> {
|
||||
let cert = sequoia_openpgp::Cert::from_bytes(pemfile.as_bytes())?;
|
||||
let policy = StandardPolicy::new();
|
||||
if cert.with_policy(&policy, None).is_err() {
|
||||
return Err(Error::InvalidCert);
|
||||
};
|
||||
cert.with_policy(&policy, None)?;
|
||||
Ok(cert)
|
||||
}
|
||||
|
||||
|
@ -57,46 +63,31 @@ pub fn gen_random_token() -> String {
|
|||
(0..10).map(|_| rng.sample(Alphanumeric) as char).collect()
|
||||
}
|
||||
|
||||
pub fn get_email_from_cert(cert: &Cert) -> Result<String, Error> {
|
||||
pub fn get_email_from_cert(cert: &Cert) -> Result<String> {
|
||||
let policy = StandardPolicy::new();
|
||||
let validcert = match cert.with_policy(&policy, None) {
|
||||
Ok(validcert) => validcert,
|
||||
Err(_) => return Err(Error::InvalidCert),
|
||||
};
|
||||
let userid_opt = match validcert.primary_userid() {
|
||||
Ok(userid_opt) => userid_opt,
|
||||
Err(_) => return Err(Error::ParseCert),
|
||||
};
|
||||
let email_opt = match userid_opt.email() {
|
||||
Ok(email_opt) => email_opt,
|
||||
Err(_) => return Err(Error::ParseCert),
|
||||
};
|
||||
let validcert = cert.with_policy(&policy, None)?;
|
||||
let userid_opt = validcert.primary_userid()?;
|
||||
let email_opt = userid_opt.email()?;
|
||||
match email_opt {
|
||||
Some(email) => Ok(email),
|
||||
None => Err(Error::MissingMail),
|
||||
None => Err(SpecialErrors::MalformedCert)?,
|
||||
}
|
||||
}
|
||||
|
||||
pub fn get_user_file_path(email: &str) -> Result<PathBuf, Error> {
|
||||
let wkd_url = match Url::from(email) {
|
||||
Ok(wkd_url) => wkd_url,
|
||||
Err(_) => return Err(Error::PathGeneration),
|
||||
};
|
||||
match wkd_url.to_file_path(SETTINGS.variant) {
|
||||
Ok(path) => Ok(path),
|
||||
Err(_) => Err(Error::PathGeneration),
|
||||
}
|
||||
pub fn get_user_file_path(email: &str) -> Result<PathBuf> {
|
||||
let wkd_url = Url::from(email)?;
|
||||
wkd_url.to_file_path(SETTINGS.variant)
|
||||
}
|
||||
|
||||
pub fn key_exists(email: &str) -> Result<bool, Error> {
|
||||
pub fn key_exists(email: &str) -> Result<bool> {
|
||||
let path = get_user_file_path(email)?;
|
||||
if !Path::new(&ROOT_FOLDER).join(path).is_file() {
|
||||
return Err(Error::MissingKey);
|
||||
Err(SpecialErrors::InexistingUser)?
|
||||
}
|
||||
Ok(true)
|
||||
}
|
||||
|
||||
pub fn get_filename(path: &Path) -> Option<&str> {
|
||||
pub fn _get_filename(path: &Path) -> Option<&str> {
|
||||
path.file_name()?.to_str()
|
||||
}
|
||||
|
||||
|
@ -135,7 +126,7 @@ pub fn init_logger() -> Result<LoggerHandle, FlexiLoggerError> {
|
|||
Logger::try_with_env_or_str("simple_wkd=debug")?
|
||||
.log_to_file(FileSpec::default().directory("logs"))
|
||||
.duplicate_to_stdout(flexi_logger::Duplicate::All)
|
||||
.format_for_files(custom_monochrome_format)
|
||||
.format_for_files(detailed_format)
|
||||
.adaptive_format_for_stdout(flexi_logger::AdaptiveFormat::Custom(
|
||||
custom_monochrome_format,
|
||||
custom_color_format,
|
||||
|
@ -144,15 +135,9 @@ pub fn init_logger() -> Result<LoggerHandle, FlexiLoggerError> {
|
|||
.start()
|
||||
}
|
||||
|
||||
pub fn return_outcome(data: Result<&str, &str>) -> Result<HttpResponse, Error> {
|
||||
pub fn return_outcome(data: Result<&str, &str>) -> Result<HttpResponse> {
|
||||
let path = webpage_path!().join("status").join("index.html");
|
||||
let template = match fs::read_to_string(&path) {
|
||||
Ok(template) => template,
|
||||
Err(_) => {
|
||||
debug!("file {} is inaccessible", path.display());
|
||||
return Err(Error::Inaccessible);
|
||||
}
|
||||
};
|
||||
let template = read_file(&path)?;
|
||||
let (page, message) = match data {
|
||||
Ok(message) => (template.replace("((%s))", "Success!"), message),
|
||||
Err(message) => (template.replace("((%s))", "Failure!"), message),
|
||||
|
|
Loading…
Reference in a new issue