Refactor users management (#4)
All checks were successful
continuous-integration/drone/push Build is passing

* Improve deletion of 2FA factors
This commit is contained in:
Pierre HUBERT 2022-11-19 18:35:41 +01:00
parent ec2f271ed4
commit e739b10065
4 changed files with 27 additions and 11 deletions

View File

@ -1,7 +1,7 @@
use actix::{Actor, Context, Handler, Message, MessageResult};
use std::net::IpAddr;
use crate::data::user::{TwoFactor, User, UserID};
use crate::data::user::{FactorID, TwoFactor, User, UserID};
use crate::utils::err::Res;
/// User storage interface
@ -12,6 +12,7 @@ pub trait UsersBackend {
fn change_user_password(&mut self, id: &UserID, password: &str, temporary: bool) -> bool;
fn verify_user_password(&self, user: &UserID, password: &str) -> 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 clear_2fa_login_history(&mut self, id: &UserID) -> bool;
fn delete_account(&mut self, id: &UserID) -> bool;
@ -72,6 +73,10 @@ pub struct ChangePasswordRequest {
#[rtype(result = "bool")]
pub struct Add2FAFactor(pub UserID, pub TwoFactor);
#[derive(Message)]
#[rtype(result = "bool")]
pub struct Remove2FAFactor(pub UserID, pub FactorID);
#[derive(Message)]
#[rtype(result = "bool")]
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 {
type Result = <AddSuccessful2FALogin as actix::Message>::Result;

View File

@ -9,7 +9,7 @@ use crate::constants::MAX_SECOND_FACTOR_NAME_LEN;
use crate::data::action_logger::{Action, ActionLogger};
use crate::data::current_user::CurrentUser;
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;
fn preprocess_factor_name(name: &str) -> String {
@ -125,11 +125,11 @@ pub async fn delete_factor(
users: web::Data<Addr<UsersActor>>,
logger: ActionLogger,
) -> impl Responder {
let mut user = User::from(user);
user.remove_factor(form.0.id.clone());
let res = users
.send(users_actor::UpdateUserRequest(user))
.send(users_actor::Remove2FAFactor(
user.uid.clone(),
form.id.clone(),
))
.await
.unwrap();

View File

@ -146,10 +146,6 @@ impl User {
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> {
self.two_factor.iter().find(|f| f.id.eq(factor_id))
}

View File

@ -1,6 +1,6 @@
use crate::actors::users_actor::UsersBackend;
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::time::time;
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 {
self.update_user(id, |mut user| {
user.last_successful_2fa.insert(ip, time());