Refactor users management (#3)
All checks were successful
continuous-integration/drone/push Build is passing
All checks were successful
continuous-integration/drone/push Build is passing
* Improve creation of 2FA factors
This commit is contained in:
parent
fcf3ec7036
commit
ec2f271ed4
@ -1,7 +1,7 @@
|
||||
use actix::{Actor, Context, Handler, Message, MessageResult};
|
||||
use std::net::IpAddr;
|
||||
|
||||
use crate::data::user::{User, UserID};
|
||||
use crate::data::user::{TwoFactor, User, UserID};
|
||||
use crate::utils::err::Res;
|
||||
|
||||
/// User storage interface
|
||||
@ -11,6 +11,7 @@ pub trait UsersBackend {
|
||||
fn get_entire_users_list(&self) -> Vec<User>;
|
||||
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 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;
|
||||
@ -67,6 +68,10 @@ pub struct ChangePasswordRequest {
|
||||
pub temporary: bool,
|
||||
}
|
||||
|
||||
#[derive(Message)]
|
||||
#[rtype(result = "bool")]
|
||||
pub struct Add2FAFactor(pub UserID, pub TwoFactor);
|
||||
|
||||
#[derive(Message)]
|
||||
#[rtype(result = "bool")]
|
||||
pub struct AddSuccessful2FALogin(pub UserID, pub IpAddr);
|
||||
@ -132,6 +137,14 @@ impl Handler<ChangePasswordRequest> for UsersActor {
|
||||
}
|
||||
}
|
||||
|
||||
impl Handler<Add2FAFactor> for UsersActor {
|
||||
type Result = <Add2FAFactor as actix::Message>::Result;
|
||||
|
||||
fn handle(&mut self, msg: Add2FAFactor, _ctx: &mut Self::Context) -> Self::Result {
|
||||
self.manager.add_2fa_factor(&msg.0, msg.1)
|
||||
}
|
||||
}
|
||||
|
||||
impl Handler<AddSuccessful2FALogin> for UsersActor {
|
||||
type Result = <AddSuccessful2FALogin as actix::Message>::Result;
|
||||
|
||||
|
@ -56,10 +56,8 @@ pub async fn save_totp_factor(
|
||||
};
|
||||
logger.log(Action::AddNewFactor(&factor));
|
||||
|
||||
let mut user = User::from(user);
|
||||
user.add_factor(factor);
|
||||
let res = users
|
||||
.send(users_actor::UpdateUserRequest(user))
|
||||
.send(users_actor::Add2FAFactor(user.uid.clone(), factor))
|
||||
.await
|
||||
.unwrap();
|
||||
|
||||
@ -104,10 +102,8 @@ pub async fn save_webauthn_factor(
|
||||
};
|
||||
logger.log(Action::AddNewFactor(&factor));
|
||||
|
||||
let mut user = User::from(user);
|
||||
user.add_factor(factor);
|
||||
let res = users
|
||||
.send(users_actor::UpdateUserRequest(user))
|
||||
.send(users_actor::Add2FAFactor(user.uid.clone(), factor))
|
||||
.await
|
||||
.unwrap();
|
||||
|
||||
|
@ -1,6 +1,6 @@
|
||||
use crate::actors::users_actor::UsersBackend;
|
||||
use crate::data::entity_manager::EntityManager;
|
||||
use crate::data::user::{User, UserID};
|
||||
use crate::data::user::{TwoFactor, User, UserID};
|
||||
use crate::utils::err::Res;
|
||||
use crate::utils::time::time;
|
||||
use std::net::IpAddr;
|
||||
@ -85,6 +85,13 @@ impl UsersBackend for EntityManager<User> {
|
||||
.unwrap_or(false)
|
||||
}
|
||||
|
||||
fn add_2fa_factor(&mut self, id: &UserID, factor: TwoFactor) -> bool {
|
||||
self.update_user(id, |mut user| {
|
||||
user.two_factor.push(factor);
|
||||
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());
|
||||
|
Loading…
Reference in New Issue
Block a user