0
0
Fork 0
mirror of https://git.verdigado.com/NB-Public/simple-wkd.git synced 2024-12-05 03:12:50 +01:00

Refactor emails to be async

This commit is contained in:
Delta1925 2023-04-23 12:08:28 +02:00
parent 18f814dec9
commit 695f5b8559
No known key found for this signature in database
GPG key ID: 1C21ACE44193CB25
5 changed files with 17 additions and 14 deletions

3
backend/Cargo.lock generated
View file

@ -1192,10 +1192,12 @@ version = "0.10.4"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "76bd09637ae3ec7bd605b8e135e757980b3968430ff2b1a4a94fb7769e50166d" checksum = "76bd09637ae3ec7bd605b8e135e757980b3968430ff2b1a4a94fb7769e50166d"
dependencies = [ dependencies = [
"async-trait",
"base64 0.21.0", "base64 0.21.0",
"email-encoding", "email-encoding",
"email_address", "email_address",
"fastrand", "fastrand",
"futures-io",
"futures-util", "futures-util",
"hostname", "hostname",
"httpdate", "httpdate",
@ -1207,6 +1209,7 @@ dependencies = [
"quoted_printable", "quoted_printable",
"socket2", "socket2",
"tokio", "tokio",
"tokio-native-tls",
] ]
[[package]] [[package]]

View file

@ -9,7 +9,7 @@ actix-web = "4.3.1"
anyhow = "1.0.70" anyhow = "1.0.70"
chrono = "0.4.24" chrono = "0.4.24"
flexi_logger = "0.25.3" flexi_logger = "0.25.3"
lettre = "0.10.4" lettre = { version = "0.10.4", features = ["tokio1-native-tls"] }
log = "0.4.17" log = "0.4.17"
once_cell = "1.17.1" once_cell = "1.17.1"
rand = "0.8.5" rand = "0.8.5"

View file

@ -9,7 +9,7 @@ use crate::utils::{extract_domain, get_email_from_cert, parse_pem, read_file};
use crate::{log_err, pending_path}; use crate::{log_err, pending_path};
use anyhow::Result; use anyhow::Result;
use lettre::{Message, Transport}; use lettre::{AsyncTransport, Message};
use std::fs; use std::fs;
use std::path::Path; use std::path::Path;
@ -43,7 +43,7 @@ pub fn confirm_action(token: &str) -> Result<(Action, String)> {
} }
} }
pub fn send_confirmation_email(address: &str, action: &Action, token: &str) -> Result<()> { pub async fn send_confirmation_email(address: &str, action: &Action, token: &str) -> Result<()> {
let template = log_err!( let template = log_err!(
read_file(&Path::new("assets").join("mail-template.html")), read_file(&Path::new("assets").join("mail-template.html")),
error, error,
@ -82,7 +82,7 @@ pub fn send_confirmation_email(address: &str, action: &Action, token: &str) -> R
let email = log_err!(email, warn)?; let email = log_err!(email, warn)?;
match log_err!(MAILER.send(&email), warn) { match log_err!(MAILER.send(email).await, warn) {
Ok(_) => Ok(()), Ok(_) => Ok(()),
Err(_) => Err(SpecialErrors::MailErr)?, Err(_) => Err(SpecialErrors::MailErr)?,
} }

View file

@ -110,7 +110,7 @@ async fn submit(pem: web::Form<Key>) -> Result<HttpResponse, CompatErr> {
"Sending email to {} to add a key... (Request token: {})", "Sending email to {} to add a key... (Request token: {})",
email, token email, token
); );
send_confirmation_email(&email, &Action::Add, &token)?; send_confirmation_email(&email, &Action::Add, &token).await?;
info!("User {} requested to add a key successfully!", email); info!("User {} requested to add a key successfully!", email);
Ok(return_outcome(Ok("You submitted your key successfully!"))?) Ok(return_outcome(Ok("You submitted your key successfully!"))?)
} }
@ -140,7 +140,7 @@ async fn delete(email: web::Query<Email>) -> Result<HttpResponse, CompatErr> {
"Sending email to {} to delete a key... (Request token: {})", "Sending email to {} to delete a key... (Request token: {})",
email.email, token email.email, token
); );
send_confirmation_email(&email.email, &Action::Delete, &token)?; send_confirmation_email(&email.email, &Action::Delete, &token).await?;
info!( info!(
"User {} requested to delete his key successfully!", "User {} requested to delete his key successfully!",
email.email email.email

View file

@ -1,5 +1,5 @@
use lettre::{transport::smtp::authentication::Credentials, SmtpTransport}; use lettre::{transport::smtp::authentication::Credentials, AsyncSmtpTransport, Tokio1Executor};
use log::{debug, error, warn}; use log::{debug, error};
use once_cell::sync::Lazy; use once_cell::sync::Lazy;
use sequoia_net::wkd::Variant; use sequoia_net::wkd::Variant;
use sequoia_openpgp::policy::StandardPolicy; use sequoia_openpgp::policy::StandardPolicy;
@ -65,16 +65,18 @@ fn get_settings() -> Settings {
settings settings
} }
fn get_mailer() -> SmtpTransport { fn get_mailer() -> AsyncSmtpTransport<Tokio1Executor> {
debug!("Setting up smtp..."); debug!("Setting up smtp...");
let creds = Credentials::new( let creds = Credentials::new(
SETTINGS.mail_settings.smtp_username.to_owned(), SETTINGS.mail_settings.smtp_username.to_owned(),
SETTINGS.mail_settings.smtp_password.to_owned(), SETTINGS.mail_settings.smtp_password.to_owned(),
); );
let builder = match &SETTINGS.mail_settings.smtp_tls { let builder = match &SETTINGS.mail_settings.smtp_tls {
SMTPEncryption::Tls => SmtpTransport::relay(&SETTINGS.mail_settings.smtp_host), SMTPEncryption::Tls => {
AsyncSmtpTransport::<Tokio1Executor>::relay(&SETTINGS.mail_settings.smtp_host)
}
SMTPEncryption::Starttls => { SMTPEncryption::Starttls => {
SmtpTransport::starttls_relay(&SETTINGS.mail_settings.smtp_host) AsyncSmtpTransport::<Tokio1Executor>::starttls_relay(&SETTINGS.mail_settings.smtp_host)
} }
}; };
let mailer = match builder { let mailer = match builder {
@ -87,8 +89,6 @@ fn get_mailer() -> SmtpTransport {
.credentials(creds) .credentials(creds)
.port(SETTINGS.mail_settings.smtp_port) .port(SETTINGS.mail_settings.smtp_port)
.build(); .build();
debug!("Testing smtp connection...");
let _ = log_err!(mailer.test_connection(), warn);
mailer mailer
} }
@ -96,4 +96,4 @@ pub const ERROR_TEXT: &str = "An error occoured:";
pub const POLICY: &StandardPolicy = &StandardPolicy::new(); pub const POLICY: &StandardPolicy = &StandardPolicy::new();
pub const ROOT_FOLDER: &str = "data"; pub const ROOT_FOLDER: &str = "data";
pub static SETTINGS: Lazy<Settings> = Lazy::new(get_settings); pub static SETTINGS: Lazy<Settings> = Lazy::new(get_settings);
pub static MAILER: Lazy<SmtpTransport> = Lazy::new(get_mailer); pub static MAILER: Lazy<AsyncSmtpTransport<Tokio1Executor>> = Lazy::new(get_mailer);