BasicOIDC/src/controllers/settings_controller.rs

117 lines
3.7 KiB
Rust

use actix::Addr;
use actix_web::{HttpResponse, Responder, web};
use askama::Template;
use crate::actors::{bruteforce_actor, users_actor};
use crate::actors::bruteforce_actor::BruteForceActor;
use crate::actors::users_actor::UsersActor;
use crate::constants::{APP_NAME, MAX_FAILED_LOGIN_ATTEMPTS, MIN_PASS_LEN};
use crate::data::current_user::CurrentUser;
use crate::data::remote_ip::RemoteIP;
use crate::data::user::User;
pub(crate) struct BaseSettingsPage {
pub danger_message: Option<String>,
pub success_message: Option<String>,
pub page_title: &'static str,
pub app_name: &'static str,
pub is_admin: bool,
pub user_name: String,
}
impl BaseSettingsPage {
pub fn get(page_title: &'static str, user: &User,
danger_message: Option<String>, success_message: Option<String>) -> BaseSettingsPage {
Self {
danger_message,
success_message,
page_title,
app_name: APP_NAME,
is_admin: user.admin,
user_name: user.username.to_string(),
}
}
}
#[derive(Template)]
#[template(path = "settings/account_details.html")]
struct AccountDetailsPage {
_p: BaseSettingsPage,
u: User,
}
#[derive(Template)]
#[template(path = "settings/change_password.html")]
struct ChangePasswordPage {
_p: BaseSettingsPage,
min_pwd_len: usize,
}
/// Account details page
pub async fn account_settings_details_route(user: CurrentUser) -> impl Responder {
let user = user.into();
HttpResponse::Ok()
.body(AccountDetailsPage {
_p: BaseSettingsPage::get("Account details", &user, None, None),
u: user,
}.render().unwrap())
}
#[derive(serde::Deserialize)]
pub struct PassChangeRequest {
pub old_pass: String,
pub new_pass: String,
}
/// Change password route
pub async fn change_password_route(user: CurrentUser,
users: web::Data<Addr<UsersActor>>,
req: Option<web::Form<PassChangeRequest>>,
bruteforce: web::Data<Addr<BruteForceActor>>,
remote_ip: RemoteIP) -> impl Responder {
let mut danger = None;
let mut success = None;
let user: User = user.into();
let failed_attempts = bruteforce.send(bruteforce_actor::CountFailedAttempt { ip: remote_ip.into() }).await.unwrap();
if failed_attempts > MAX_FAILED_LOGIN_ATTEMPTS {
danger = Some("Too many invalid password attempts. Please try to change your password later.".to_string());
} else if let Some(req) = req {
// Invalid password
if !user.verify_password(&req.old_pass) {
danger = Some("Old password is invalid!".to_string());
bruteforce.send(bruteforce_actor::RecordFailedAttempt { ip: remote_ip.into() }).await.unwrap();
}
// Password too short
else if req.new_pass.len() < MIN_PASS_LEN {
danger = Some("New password is too short!".to_string());
}
// Change password
else {
let res = users.send(
users_actor::ChangePasswordRequest {
user_id: user.uid.clone(),
new_password: req.new_pass.to_string(),
temporary: false,
}
).await.unwrap().0;
if !res {
danger = Some("An error occurred while trying to change your password!".to_string());
} else {
success = Some("Your password was successfully changed!".to_string());
}
}
}
HttpResponse::Ok()
.body(ChangePasswordPage {
_p: BaseSettingsPage::get("Change password", &user, danger, success),
min_pwd_len: MIN_PASS_LEN,
}.render().unwrap())
}