Refactor users management (#4)
	
		
			
	
		
	
	
		
	
		
			All checks were successful
		
		
	
	
		
			
				
	
				continuous-integration/drone/push Build is passing
				
			
		
		
	
	
				
					
				
			
		
			All checks were successful
		
		
	
	continuous-integration/drone/push Build is passing
				
			* Improve deletion of 2FA factors
This commit is contained in:
		@@ -1,7 +1,7 @@
 | 
				
			|||||||
use actix::{Actor, Context, Handler, Message, MessageResult};
 | 
					use actix::{Actor, Context, Handler, Message, MessageResult};
 | 
				
			||||||
use std::net::IpAddr;
 | 
					use std::net::IpAddr;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
use crate::data::user::{TwoFactor, User, UserID};
 | 
					use crate::data::user::{FactorID, TwoFactor, User, UserID};
 | 
				
			||||||
use crate::utils::err::Res;
 | 
					use crate::utils::err::Res;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
/// User storage interface
 | 
					/// User storage interface
 | 
				
			||||||
@@ -12,6 +12,7 @@ pub trait UsersBackend {
 | 
				
			|||||||
    fn change_user_password(&mut self, id: &UserID, password: &str, temporary: bool) -> bool;
 | 
					    fn change_user_password(&mut self, id: &UserID, password: &str, temporary: bool) -> bool;
 | 
				
			||||||
    fn verify_user_password(&self, user: &UserID, password: &str) -> bool;
 | 
					    fn verify_user_password(&self, user: &UserID, password: &str) -> bool;
 | 
				
			||||||
    fn add_2fa_factor(&mut self, user: &UserID, factor: TwoFactor) -> bool;
 | 
					    fn add_2fa_factor(&mut self, user: &UserID, factor: TwoFactor) -> bool;
 | 
				
			||||||
 | 
					    fn remove_2fa_factor(&mut self, user: &UserID, factor: FactorID) -> bool;
 | 
				
			||||||
    fn save_new_successful_2fa_authentication(&mut self, id: &UserID, ip: IpAddr) -> bool;
 | 
					    fn save_new_successful_2fa_authentication(&mut self, id: &UserID, ip: IpAddr) -> bool;
 | 
				
			||||||
    fn clear_2fa_login_history(&mut self, id: &UserID) -> bool;
 | 
					    fn clear_2fa_login_history(&mut self, id: &UserID) -> bool;
 | 
				
			||||||
    fn delete_account(&mut self, id: &UserID) -> bool;
 | 
					    fn delete_account(&mut self, id: &UserID) -> bool;
 | 
				
			||||||
@@ -72,6 +73,10 @@ pub struct ChangePasswordRequest {
 | 
				
			|||||||
#[rtype(result = "bool")]
 | 
					#[rtype(result = "bool")]
 | 
				
			||||||
pub struct Add2FAFactor(pub UserID, pub TwoFactor);
 | 
					pub struct Add2FAFactor(pub UserID, pub TwoFactor);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					#[derive(Message)]
 | 
				
			||||||
 | 
					#[rtype(result = "bool")]
 | 
				
			||||||
 | 
					pub struct Remove2FAFactor(pub UserID, pub FactorID);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
#[derive(Message)]
 | 
					#[derive(Message)]
 | 
				
			||||||
#[rtype(result = "bool")]
 | 
					#[rtype(result = "bool")]
 | 
				
			||||||
pub struct AddSuccessful2FALogin(pub UserID, pub IpAddr);
 | 
					pub struct AddSuccessful2FALogin(pub UserID, pub IpAddr);
 | 
				
			||||||
@@ -145,6 +150,14 @@ impl Handler<Add2FAFactor> for UsersActor {
 | 
				
			|||||||
    }
 | 
					    }
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					impl Handler<Remove2FAFactor> for UsersActor {
 | 
				
			||||||
 | 
					    type Result = <Remove2FAFactor as actix::Message>::Result;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    fn handle(&mut self, msg: Remove2FAFactor, _ctx: &mut Self::Context) -> Self::Result {
 | 
				
			||||||
 | 
					        self.manager.remove_2fa_factor(&msg.0, msg.1)
 | 
				
			||||||
 | 
					    }
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
impl Handler<AddSuccessful2FALogin> for UsersActor {
 | 
					impl Handler<AddSuccessful2FALogin> for UsersActor {
 | 
				
			||||||
    type Result = <AddSuccessful2FALogin as actix::Message>::Result;
 | 
					    type Result = <AddSuccessful2FALogin as actix::Message>::Result;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 
 | 
				
			|||||||
@@ -9,7 +9,7 @@ use crate::constants::MAX_SECOND_FACTOR_NAME_LEN;
 | 
				
			|||||||
use crate::data::action_logger::{Action, ActionLogger};
 | 
					use crate::data::action_logger::{Action, ActionLogger};
 | 
				
			||||||
use crate::data::current_user::CurrentUser;
 | 
					use crate::data::current_user::CurrentUser;
 | 
				
			||||||
use crate::data::totp_key::TotpKey;
 | 
					use crate::data::totp_key::TotpKey;
 | 
				
			||||||
use crate::data::user::{FactorID, TwoFactor, TwoFactorType, User};
 | 
					use crate::data::user::{FactorID, TwoFactor, TwoFactorType};
 | 
				
			||||||
use crate::data::webauthn_manager::WebAuthManagerReq;
 | 
					use crate::data::webauthn_manager::WebAuthManagerReq;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
fn preprocess_factor_name(name: &str) -> String {
 | 
					fn preprocess_factor_name(name: &str) -> String {
 | 
				
			||||||
@@ -125,11 +125,11 @@ pub async fn delete_factor(
 | 
				
			|||||||
    users: web::Data<Addr<UsersActor>>,
 | 
					    users: web::Data<Addr<UsersActor>>,
 | 
				
			||||||
    logger: ActionLogger,
 | 
					    logger: ActionLogger,
 | 
				
			||||||
) -> impl Responder {
 | 
					) -> impl Responder {
 | 
				
			||||||
    let mut user = User::from(user);
 | 
					 | 
				
			||||||
    user.remove_factor(form.0.id.clone());
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
    let res = users
 | 
					    let res = users
 | 
				
			||||||
        .send(users_actor::UpdateUserRequest(user))
 | 
					        .send(users_actor::Remove2FAFactor(
 | 
				
			||||||
 | 
					            user.uid.clone(),
 | 
				
			||||||
 | 
					            form.id.clone(),
 | 
				
			||||||
 | 
					        ))
 | 
				
			||||||
        .await
 | 
					        .await
 | 
				
			||||||
        .unwrap();
 | 
					        .unwrap();
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 
 | 
				
			|||||||
@@ -146,10 +146,6 @@ impl User {
 | 
				
			|||||||
        self.two_factor.push(factor);
 | 
					        self.two_factor.push(factor);
 | 
				
			||||||
    }
 | 
					    }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    pub fn remove_factor(&mut self, factor_id: FactorID) {
 | 
					 | 
				
			||||||
        self.two_factor.retain(|f| f.id != factor_id);
 | 
					 | 
				
			||||||
    }
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
    pub fn find_factor(&self, factor_id: &FactorID) -> Option<&TwoFactor> {
 | 
					    pub fn find_factor(&self, factor_id: &FactorID) -> Option<&TwoFactor> {
 | 
				
			||||||
        self.two_factor.iter().find(|f| f.id.eq(factor_id))
 | 
					        self.two_factor.iter().find(|f| f.id.eq(factor_id))
 | 
				
			||||||
    }
 | 
					    }
 | 
				
			||||||
 
 | 
				
			|||||||
@@ -1,6 +1,6 @@
 | 
				
			|||||||
use crate::actors::users_actor::UsersBackend;
 | 
					use crate::actors::users_actor::UsersBackend;
 | 
				
			||||||
use crate::data::entity_manager::EntityManager;
 | 
					use crate::data::entity_manager::EntityManager;
 | 
				
			||||||
use crate::data::user::{TwoFactor, User, UserID};
 | 
					use crate::data::user::{FactorID, TwoFactor, User, UserID};
 | 
				
			||||||
use crate::utils::err::Res;
 | 
					use crate::utils::err::Res;
 | 
				
			||||||
use crate::utils::time::time;
 | 
					use crate::utils::time::time;
 | 
				
			||||||
use std::net::IpAddr;
 | 
					use std::net::IpAddr;
 | 
				
			||||||
@@ -92,6 +92,13 @@ impl UsersBackend for EntityManager<User> {
 | 
				
			|||||||
        })
 | 
					        })
 | 
				
			||||||
    }
 | 
					    }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    fn remove_2fa_factor(&mut self, id: &UserID, factor_id: FactorID) -> bool {
 | 
				
			||||||
 | 
					        self.update_user(id, |mut user| {
 | 
				
			||||||
 | 
					            user.two_factor.retain(|f| f.id != factor_id);
 | 
				
			||||||
 | 
					            user
 | 
				
			||||||
 | 
					        })
 | 
				
			||||||
 | 
					    }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    fn save_new_successful_2fa_authentication(&mut self, id: &UserID, ip: IpAddr) -> bool {
 | 
					    fn save_new_successful_2fa_authentication(&mut self, id: &UserID, ip: IpAddr) -> bool {
 | 
				
			||||||
        self.update_user(id, |mut user| {
 | 
					        self.update_user(id, |mut user| {
 | 
				
			||||||
            user.last_successful_2fa.insert(ip, time());
 | 
					            user.last_successful_2fa.insert(ip, time());
 | 
				
			||||||
 
 | 
				
			|||||||
		Reference in New Issue
	
	Block a user