-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
11 changed files
with
121 additions
and
1 deletion.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
use actix_session::Session; | ||
use actix_web::{web, Responder}; | ||
use crate::api::app_write::AppWrite; | ||
use crate::api::dto::email_dto::{EmailCaptcha, EmailCaptchaCheck}; | ||
use crate::metadata::service::MetaService; | ||
pub const ALLOW_NEXT_KEY: &str = "allow_next"; | ||
pub const CAPTCHA: &str = "captcha"; | ||
|
||
#[utoipa::path( | ||
post, | ||
tag = "email", | ||
path = "/api/v1/email/captcha", | ||
request_body = EmailCaptcha, | ||
responses( | ||
( status = 200, description = "Success" ), | ||
( status = 400, description = "Bad Request" ) | ||
) | ||
)] | ||
pub async fn api_email_rand_captcha( | ||
session: Session, | ||
service: web::Data<MetaService>, | ||
dto: web::Json<EmailCaptcha> | ||
) | ||
-> impl Responder | ||
{ | ||
match service.email_service().generate_and_send_captcha(dto.email.clone()).await{ | ||
Ok(result) => { | ||
session.insert(CAPTCHA, result).ok(); | ||
AppWrite::<String>::ok_msg("[Ok]".to_string()) | ||
} | ||
Err(e) => { | ||
AppWrite::error(e.to_string()) | ||
} | ||
} | ||
} | ||
|
||
#[utoipa::path( | ||
put, | ||
tag = "email", | ||
path = "/api/v1/email/captcha", | ||
request_body = EmailCaptchaCheck, | ||
responses( | ||
( status = 200, description = "Success" ), | ||
( status = 400, description = "Bad Request" ) | ||
) | ||
)] | ||
pub async fn api_email_captcha_check( | ||
session: Session, | ||
dto: web::Json<EmailCaptchaCheck> | ||
) | ||
-> impl Responder | ||
{ | ||
let captcha = session.get::<String>(CAPTCHA).unwrap(); | ||
if captcha.is_none(){ | ||
return AppWrite::<String>::error("[Error] Captcha Expired".to_string()) | ||
} | ||
if captcha.unwrap() == dto.code { | ||
session.insert(ALLOW_NEXT_KEY, true).ok(); | ||
AppWrite::ok_msg("[Ok]".to_string()) | ||
} else { | ||
AppWrite::error("[Error] Captcha Error".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 @@ | ||
pub mod captcha; |
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,15 @@ | ||
use rand::distributions::Alphanumeric; | ||
use rand::Rng; | ||
use crate::metadata::service::email_service::EmailService; | ||
|
||
impl EmailService { | ||
pub async fn generate_and_send_captcha(&self, email: String) -> anyhow::Result<String>{ | ||
let rng = rand::thread_rng(); | ||
let code: String = rng.sample_iter(&Alphanumeric).take(6).map(char::from).collect(); | ||
self.email.send_captcha(email.parse().map_err(|e|{ | ||
log::error!("[Error] {}", e); | ||
anyhow::anyhow!("[Email Err] {}", e) | ||
})?, code.clone()).await; | ||
Ok(code) | ||
} | ||
} |
Empty file.
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,23 @@ | ||
use sea_orm::DatabaseConnection; | ||
use crate::metadata::service::MetaService; | ||
use crate::server::email::EmailServer; | ||
|
||
pub mod captcha; | ||
pub mod forget; | ||
|
||
#[allow(unused)] | ||
pub struct EmailService{ | ||
db: DatabaseConnection, | ||
redis: deadpool_redis::Pool, | ||
email: EmailServer, | ||
} | ||
|
||
impl From<&MetaService> for EmailService { | ||
fn from(value: &MetaService) -> Self { | ||
Self{ | ||
db: value.pg(), | ||
redis: value.redis(), | ||
email: value.email(), | ||
} | ||
} | ||
} |
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