Can change password by reset
This commit is contained in:
@ -91,3 +91,50 @@ pub async fn check_reset_password_token(
|
||||
|
||||
Ok(HttpResponse::Ok().json(CheckResetPasswordTokenResponse { name: user.name }))
|
||||
}
|
||||
|
||||
#[derive(serde::Deserialize)]
|
||||
pub struct ResetPasswordBody {
|
||||
token: String,
|
||||
password: String,
|
||||
}
|
||||
|
||||
/// Reset password
|
||||
pub async fn reset_password(remote_ip: RemoteIP, req: web::Json<ResetPasswordBody>) -> HttpResult {
|
||||
// Rate limiting
|
||||
if rate_limiter_service::should_block_action(
|
||||
remote_ip.0,
|
||||
RatedAction::CheckResetPasswordTokenFailed,
|
||||
)
|
||||
.await?
|
||||
{
|
||||
return Ok(HttpResponse::TooManyRequests().finish());
|
||||
}
|
||||
|
||||
let user = match users_service::get_by_pwd_reset_token(&req.token).await {
|
||||
Ok(t) => t,
|
||||
Err(e) => {
|
||||
rate_limiter_service::record_action(
|
||||
remote_ip.0,
|
||||
RatedAction::CheckResetPasswordTokenFailed,
|
||||
)
|
||||
.await?;
|
||||
log::error!("Password reset token could not be used: {}", e);
|
||||
return Ok(HttpResponse::NotFound().finish());
|
||||
}
|
||||
};
|
||||
|
||||
if !StaticConstraints::default()
|
||||
.password_len
|
||||
.validate(&req.password)
|
||||
{
|
||||
return Ok(HttpResponse::BadRequest().json("Taille du mot de passe invalide!"));
|
||||
}
|
||||
|
||||
// Validate account, if required
|
||||
users_service::validate_account(&user).await?;
|
||||
|
||||
// Change user password
|
||||
users_service::change_password(&user, &req.password).await?;
|
||||
|
||||
Ok(HttpResponse::Accepted().finish())
|
||||
}
|
||||
|
@ -31,6 +31,10 @@ async fn main() -> std::io::Result<()> {
|
||||
"/auth/check_reset_password_token",
|
||||
web::post().to(auth_controller::check_reset_password_token),
|
||||
)
|
||||
.route(
|
||||
"/auth/reset_password",
|
||||
web::post().to(auth_controller::reset_password),
|
||||
)
|
||||
})
|
||||
.bind(AppConfig::get().listen_address.as_str())?
|
||||
.run()
|
||||
|
@ -8,7 +8,9 @@ use crate::schema::users;
|
||||
use crate::services::mail_service;
|
||||
use crate::utils::string_utils::rand_str;
|
||||
use crate::utils::time_utils::time;
|
||||
use bcrypt::DEFAULT_COST;
|
||||
use diesel::prelude::*;
|
||||
use std::io::ErrorKind;
|
||||
|
||||
/// Get the information of the user, by its id
|
||||
pub async fn get_by_id(id: UserID) -> anyhow::Result<User> {
|
||||
@ -17,6 +19,13 @@ pub async fn get_by_id(id: UserID) -> anyhow::Result<User> {
|
||||
|
||||
/// Get the information of the user, by its password reset token
|
||||
pub async fn get_by_pwd_reset_token(token: &str) -> anyhow::Result<User> {
|
||||
if token.is_empty() {
|
||||
return Err(anyhow::Error::from(std::io::Error::new(
|
||||
ErrorKind::Other,
|
||||
"Token is empty!",
|
||||
)));
|
||||
}
|
||||
|
||||
db_connection::execute(|conn| {
|
||||
Ok(users::table
|
||||
.filter(
|
||||
@ -109,3 +118,52 @@ pub async fn delete_not_validated_accounts() -> anyhow::Result<()> {
|
||||
Ok(())
|
||||
})
|
||||
}
|
||||
|
||||
/// Mark account as validated
|
||||
pub async fn validate_account(user: &User) -> anyhow::Result<()> {
|
||||
if user.time_activate > 0 {
|
||||
log::debug!(
|
||||
"Did not activate account {} because it is already activated!",
|
||||
user.id
|
||||
);
|
||||
return Ok(());
|
||||
}
|
||||
|
||||
mail_service::send_mail(
|
||||
&user.email,
|
||||
"Activation de votre compte GeneIT",
|
||||
"Bonjour,\n\n\
|
||||
Votre compte GeneIT a été activé avec succès !\n\n\
|
||||
Cordialement,\n\n\
|
||||
L'équipe de GeneIT",
|
||||
)
|
||||
.await?;
|
||||
|
||||
db_connection::execute(|conn| {
|
||||
Ok(
|
||||
diesel::update(users::dsl::users.filter(users::dsl::id.eq(user.id)))
|
||||
.set((users::dsl::time_activate.eq(time() as i64),))
|
||||
.execute(conn)?,
|
||||
)
|
||||
})?;
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
/// Change user paswsord
|
||||
pub async fn change_password(user: &User, new_password: &str) -> anyhow::Result<()> {
|
||||
let hash = bcrypt::hash(new_password, DEFAULT_COST)?;
|
||||
|
||||
db_connection::execute(|conn| {
|
||||
Ok(
|
||||
diesel::update(users::dsl::users.filter(users::dsl::id.eq(user.id)))
|
||||
.set((
|
||||
users::dsl::password.eq(hash),
|
||||
users::dsl::reset_password_token.eq(None::<String>),
|
||||
))
|
||||
.execute(conn)?,
|
||||
)
|
||||
})?;
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
Reference in New Issue
Block a user