Skip to content

Commit

Permalink
separate mail fns
Browse files Browse the repository at this point in the history
update deps
rename some stuff
  • Loading branch information
fairingrey committed Feb 8, 2022
1 parent 423cf59 commit 50b1418
Show file tree
Hide file tree
Showing 9 changed files with 113 additions and 74 deletions.
71 changes: 47 additions & 24 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

31 changes: 13 additions & 18 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -8,16 +8,22 @@ edition = "2018"

[dependencies]
anyhow = "1.0.53"
axum = "0.4.4"
axum = "0.4.5"
bincode = "1.3.3"
chrono = "0.4.19"
dotenv = "0.15.0"
http = "0.2.6"
http-body = "0.4.4"
hyper = { version = "0.14.16", features = ["full"] }
lazy_static = "1.4.0"
lettre = { version = "0.10.0-rc.4", features = [
"tokio1",
"tokio1-native-tls",
"tracing",
"serde",
] }
libreauth = "0.14.1"
oso = { version = "0.25.1", features = ["derive", "uuid-07"] }
oso = { version = "0.26.0", features = ["derive", "uuid-07"] }
oxide-auth = "0.5.1"
oxide-auth-axum = "0.1.0"
rand = "0.8.4"
Expand All @@ -27,7 +33,7 @@ redis = { version = "0.21.5", features = [
"connection-manager",
], default-features = false }
regex = "1.5.4"
serde = { version = "1.0.135", features = ["derive"] }
serde = { version = "1.0.136", features = ["derive"] }
serde_json = "1.0.78"
sqlx = { version = "0.5.10", features = [
"runtime-tokio-native-tls",
Expand All @@ -39,22 +45,11 @@ sqlx = { version = "0.5.10", features = [
] }
tantivy = "0.16.1"
thiserror = "1.0.30"
tokio = { version = "1.15.0", features = ["rt-multi-thread", "macros", "sync"] }
tokio = { version = "1.16.1", features = ["rt-multi-thread", "macros", "sync"] }
tower = "0.4.11"
tower-http = { version = "0.2.1", features = ["add-extension", "trace"] }
tracing = "0.1.29"
tracing-subscriber = "0.3.6"
tower-http = { version = "0.2.2", features = ["add-extension", "trace"] }
tracing = "0.1.30"
tracing-subscriber = "0.3.8"
ulid = { version = "0.5.0", features = ["serde", "uuid"] }
uuid = { version = "0.8.2", features = ["serde", "v4"] }
validator = { version = "0.14.0", features = ["derive"] }

[dependencies.lettre]
version = "0.10.0-rc.4"
features = [
"builder",
"hostname",
"tokio1",
"smtp-transport",
"tokio1-native-tls",
]
default-features = false
2 changes: 1 addition & 1 deletion src/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ use axum::{
use thiserror::Error;

// dost thou know of the pepeloni
const INTERNAL_SERVER_ERROR_MESSAGE: &str = "aah the pepeloni";
const INTERNAL_SERVER_ERROR_MESSAGE: &str = "ahh the pepeloni";

/// Any possible errors
#[derive(Debug, Error)]
Expand Down
19 changes: 2 additions & 17 deletions src/handlers/user.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
use anyhow::format_err;
use axum::{body::Body, extract::Extension};
use http::{Response, StatusCode};
use lettre::{Message, Transport};
use redis::AsyncCommands;
use serde::Deserialize;
use std::sync::Arc;
Expand All @@ -14,7 +13,7 @@ use crate::error::MixiniError;
use crate::handlers::{ValidatedForm, RE_PASSWORD, RE_USERNAME};
use crate::models::User;
use crate::server::State;
use crate::utils::{generate_redis_key, pass::HASHER};
use crate::utils::{generate_redis_key, mail::send_email_verification_request, pass::HASHER};

const VERIFY_KEY_PREFIX: &str = "verify:";
const VERIFY_EXPIRY_SECONDS: usize = 86400;
Expand Down Expand Up @@ -138,21 +137,7 @@ pub(crate) async fn create_verify_entry(
.set_ex(&key, user.id.to_string(), VERIFY_EXPIRY_SECONDS)
.await?;

let mail = Message::builder()
.from(
std::env::var("SMTP_EMAIL")
.unwrap()
.parse()
.expect("SMTP_EMAIL key is invalid"),
)
.to(user.email.parse().unwrap())
.subject("Your Mixini email verification")
.body(format!(
"Your Mixini verification key is {}. Note that it will expire in 24 hours.",
key
))?;

state.mailer.send(&mail)?;
send_email_verification_request(&state.mailsender, user.email, key).await?;

Ok(Response::builder()
.status(StatusCode::OK)
Expand Down
2 changes: 1 addition & 1 deletion src/models/user.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@ pub(crate) struct User {
pub(crate) created_at: DateTime<Utc>,
pub(crate) updated_at: DateTime<Utc>,
pub(crate) name: String,
/// The password in hashed PHC form, as represented in the database
pub(crate) email: String,
/// The password in hashed PHC form, as represented in the database
pub(crate) password: String,
#[polar(attribute)]
pub(crate) verified: bool,
Expand Down
25 changes: 13 additions & 12 deletions src/server.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ use axum::{
};
use lettre::{
transport::smtp::authentication::{Credentials, Mechanism},
SmtpTransport,
AsyncSmtpTransport, Tokio1Executor,
};
use oso::{Oso, PolarClass};
use sqlx::PgPool;
Expand All @@ -21,7 +21,7 @@ pub(crate) struct State {
pub(crate) oso: Arc<Mutex<Oso>>,
pub(crate) db_pool: PgPool,
pub(crate) redis_manager: redis::aio::ConnectionManager,
pub(crate) mailer: SmtpTransport,
pub(crate) mailsender: AsyncSmtpTransport<Tokio1Executor>,
}

impl State {
Expand All @@ -32,21 +32,22 @@ impl State {
let redis_manager = redis::Client::open(std::env::var("REDIS_URL")?)?
.get_tokio_connection_manager()
.await?;
let mailer = SmtpTransport::starttls_relay(&std::env::var("SMTP_SERVER")?)?
// Add credentials for authentication
.credentials(Credentials::new(
std::env::var("SMTP_USERNAME")?,
std::env::var("SMTP_PASSWORD")?,
))
// Configure expected authentication mechanism
.authentication(vec![Mechanism::Plain])
.build();
let mailsender =
AsyncSmtpTransport::<Tokio1Executor>::relay(&std::env::var("SMTP_SERVER")?)?
// Add credentials for authentication
.credentials(Credentials::new(
std::env::var("SMTP_USERNAME")?,
std::env::var("SMTP_PASSWORD")?,
))
// Configure expected authentication mechanism
.authentication(vec![Mechanism::Plain])
.build();

Ok(State {
oso,
db_pool,
redis_manager,
mailer,
mailsender,
})
}
}
Expand Down
Loading

0 comments on commit 50b1418

Please sign in to comment.