Simplify code

This commit is contained in:
Pierre HUBERT 2022-04-05 17:23:26 +02:00
parent 31949bf414
commit 30c8470bcb

View File

@ -1,6 +1,6 @@
use actix::Addr; use actix::Addr;
use actix_identity::Identity; use actix_identity::Identity;
use actix_web::{HttpRequest, HttpResponse, Responder, web}; use actix_web::{HttpResponse, Responder, web};
use askama::Template; use askama::Template;
use crate::actors::{bruteforce_actor, users_actor}; use crate::actors::{bruteforce_actor, users_actor};
@ -8,9 +8,8 @@ use crate::actors::bruteforce_actor::BruteForceActor;
use crate::actors::users_actor::{ChangePasswordResult, LoginResult, UsersActor}; use crate::actors::users_actor::{ChangePasswordResult, LoginResult, UsersActor};
use crate::constants::{APP_NAME, MAX_FAILED_LOGIN_ATTEMPTS, MIN_PASS_LEN}; use crate::constants::{APP_NAME, MAX_FAILED_LOGIN_ATTEMPTS, MIN_PASS_LEN};
use crate::controllers::base_controller::{FatalErrorPage, redirect_user}; use crate::controllers::base_controller::{FatalErrorPage, redirect_user};
use crate::data::app_config::AppConfig; use crate::data::remote_ip::RemoteIP;
use crate::data::session_identity::{SessionIdentity, SessionStatus}; use crate::data::session_identity::{SessionIdentity, SessionStatus};
use crate::utils::network_utils::get_remote_ip;
#[derive(Template)] #[derive(Template)]
#[template(path = "base_login_page.html")] #[template(path = "base_login_page.html")]
@ -50,21 +49,19 @@ pub struct LoginRequestQuery {
/// Authenticate user /// Authenticate user
pub async fn login_route( pub async fn login_route(
http_req: HttpRequest, remote_ip: RemoteIP,
users: web::Data<Addr<UsersActor>>, users: web::Data<Addr<UsersActor>>,
bruteforce: web::Data<Addr<BruteForceActor>>, bruteforce: web::Data<Addr<BruteForceActor>>,
query: web::Query<LoginRequestQuery>, query: web::Query<LoginRequestQuery>,
req: Option<web::Form<LoginRequestBody>>, req: Option<web::Form<LoginRequestBody>>,
config: web::Data<AppConfig>,
id: Identity, id: Identity,
) -> impl Responder { ) -> impl Responder {
let mut danger = String::new(); let mut danger = String::new();
let mut success = String::new(); let mut success = String::new();
let mut login = String::new(); let mut login = String::new();
let remote_ip = get_remote_ip(&http_req, config.proxy_ip.as_deref());
let failed_attempts = bruteforce.send(bruteforce_actor::CountFailedAttempt { ip: remote_ip }) let failed_attempts = bruteforce.send(bruteforce_actor::CountFailedAttempt { ip: remote_ip.into() })
.await.unwrap(); .await.unwrap();
if failed_attempts > MAX_FAILED_LOGIN_ATTEMPTS { if failed_attempts > MAX_FAILED_LOGIN_ATTEMPTS {
@ -147,7 +144,7 @@ pub async fn login_route(
log::warn!("Failed login for ip {:?} / username {}: {:?}", remote_ip, login, c); log::warn!("Failed login for ip {:?} / username {}: {:?}", remote_ip, login, c);
danger = "Login failed.".to_string(); danger = "Login failed.".to_string();
bruteforce.send(bruteforce_actor::RecordFailedAttempt { ip: remote_ip }).await.unwrap(); bruteforce.send(bruteforce_actor::RecordFailedAttempt { ip: remote_ip.into() }).await.unwrap();
} }
} }
} }