-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #16 from pesca-dev/verification-mail
Feature: Verification mail
- Loading branch information
Showing
12 changed files
with
557 additions
and
11 deletions.
There are no files selected for viewing
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
use std::collections::BTreeMap; | ||
use std::env; | ||
use std::error::Error; | ||
|
||
use hmac::digest::KeyInit; | ||
use hmac::Hmac; | ||
use jwt::{SignWithKey, VerifyWithKey}; | ||
use sha2::Sha256; | ||
|
||
fn key() -> Result<Hmac<Sha256>, Box<dyn Error>> { | ||
let key = env::var("JWT_KEY").expect("JWT key should be given"); | ||
Ok(Hmac::new_from_slice(key.as_bytes())?) | ||
} | ||
|
||
pub fn sign(claims: BTreeMap<String, String>) -> Result<String, Box<dyn Error>> { | ||
let key = key()?; | ||
|
||
Ok(claims.sign_with_key(&key)?) | ||
} | ||
|
||
pub fn extract(token: String) -> Result<BTreeMap<String, String>, Box<dyn Error>> { | ||
let key = key()?; | ||
|
||
Ok(token.verify_with_key(&key)?) | ||
} | ||
|
||
#[cfg(test)] | ||
mod tests { | ||
use super::*; | ||
|
||
use std::{collections::BTreeMap, env}; | ||
|
||
#[test] | ||
fn test_jwt_sign() { | ||
env::set_var("JWT_KEY", "some-key"); | ||
|
||
let mut claims = BTreeMap::new(); | ||
claims.insert("sub".to_string(), "some_user".to_string()); | ||
assert!(sign(claims).is_ok()) | ||
} | ||
|
||
#[test] | ||
fn test_jwt_extract() { | ||
env::set_var("JWT_KEY", "some-key"); | ||
|
||
let mut claims = BTreeMap::new(); | ||
claims.insert("sub".to_string(), "some_user".to_string()); | ||
|
||
let token = sign(claims).unwrap(); | ||
let claims = extract(token).unwrap(); | ||
|
||
assert_eq!(claims["sub"], "some_user".to_string()); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
use std::{env, error::Error}; | ||
|
||
use lettre::{ | ||
message::header::ContentType, transport::smtp::authentication::Credentials, Message, | ||
SmtpTransport, Transport, | ||
}; | ||
|
||
pub struct Mail { | ||
pub subject: Option<String>, | ||
pub recipient: String, | ||
pub content: Option<String>, | ||
} | ||
|
||
impl Mail { | ||
pub fn send(self) -> Result<(), Box<dyn Error>> { | ||
let mail_user = env::var("MAIL_USER").unwrap(); | ||
let mail_pass = env::var("MAIL_PASS").unwrap(); | ||
let mail_server = env::var("MAIL_SERVER").unwrap(); | ||
let mail_sender = env::var("MAIL_SENDER").unwrap(); | ||
|
||
let email = Message::builder() | ||
.from(mail_sender.parse().unwrap()) | ||
.to(format!("<{}>", self.recipient).parse().unwrap()) | ||
.subject(self.subject.unwrap_or("".into())) | ||
.header(ContentType::TEXT_PLAIN) | ||
.body(self.content.unwrap_or("".into())) | ||
.unwrap(); | ||
|
||
let creds = Credentials::new(mail_user, mail_pass); | ||
let mailer = SmtpTransport::relay(&mail_server) | ||
.unwrap() | ||
.credentials(creds) | ||
.build(); | ||
|
||
// Send the email | ||
if let Err(e) = mailer.send(&email) { | ||
tracing::error!("failed to send mail: {e:#?}"); | ||
return Err(Box::new(e)); | ||
} | ||
|
||
Ok(()) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -3,5 +3,7 @@ use cfg_if::cfg_if; | |
cfg_if! { | ||
if #[cfg(feature = "ssr")] { | ||
pub mod database; | ||
pub mod mail; | ||
pub mod jwt; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.