2022-11-19 17:18:46 +00:00
|
|
|
use crate::actors::users_actor::UsersBackend;
|
2022-11-19 16:52:35 +00:00
|
|
|
use crate::data::entity_manager::EntityManager;
|
2022-11-19 17:27:08 +00:00
|
|
|
use crate::data::user::{TwoFactor, User, UserID};
|
2022-11-19 16:52:35 +00:00
|
|
|
use crate::utils::err::Res;
|
|
|
|
use crate::utils::time::time;
|
|
|
|
use std::net::IpAddr;
|
|
|
|
|
2022-11-19 17:18:46 +00:00
|
|
|
impl EntityManager<User> {
|
|
|
|
/// Update user information
|
|
|
|
fn update_user<F>(&mut self, id: &UserID, update: F) -> bool
|
|
|
|
where
|
|
|
|
F: FnOnce(User) -> User,
|
|
|
|
{
|
|
|
|
let user = match self.find_by_user_id(id) {
|
|
|
|
None => return false,
|
|
|
|
Some(user) => user,
|
|
|
|
};
|
|
|
|
|
|
|
|
if let Err(e) = self.replace_entries(|u| u.uid.eq(id), &update(user)) {
|
|
|
|
log::error!("Failed to update user information! {:?}", e);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-11-19 16:52:35 +00:00
|
|
|
fn hash_password<P: AsRef<[u8]>>(pwd: P) -> Res<String> {
|
|
|
|
Ok(bcrypt::hash(pwd, bcrypt::DEFAULT_COST)?)
|
|
|
|
}
|
|
|
|
|
|
|
|
fn verify_password<P: AsRef<[u8]>>(pwd: P, hash: &str) -> bool {
|
|
|
|
match bcrypt::verify(pwd, hash) {
|
|
|
|
Ok(r) => r,
|
|
|
|
Err(e) => {
|
|
|
|
log::warn!("Failed to verify password! {:?}", e);
|
|
|
|
false
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-11-19 17:18:46 +00:00
|
|
|
impl UsersBackend for EntityManager<User> {
|
|
|
|
fn find_by_username_or_email(&self, u: &str) -> Option<User> {
|
2022-11-19 16:52:35 +00:00
|
|
|
for entry in self.iter() {
|
|
|
|
if entry.username.eq(u) || entry.email.eq(u) {
|
|
|
|
return Some(entry.clone());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
None
|
|
|
|
}
|
|
|
|
|
2022-11-19 17:18:46 +00:00
|
|
|
fn find_by_user_id(&self, id: &UserID) -> Option<User> {
|
2022-11-19 16:52:35 +00:00
|
|
|
for entry in self.iter() {
|
|
|
|
if entry.uid.eq(id) {
|
|
|
|
return Some(entry.clone());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
None
|
|
|
|
}
|
|
|
|
|
2022-11-19 17:18:46 +00:00
|
|
|
fn get_entire_users_list(&self) -> Vec<User> {
|
|
|
|
self.cloned()
|
2022-11-19 16:52:35 +00:00
|
|
|
}
|
|
|
|
|
2022-11-19 17:18:46 +00:00
|
|
|
fn change_user_password(&mut self, id: &UserID, password: &str, temporary: bool) -> bool {
|
2022-11-19 16:52:35 +00:00
|
|
|
let new_hash = match hash_password(password) {
|
|
|
|
Ok(h) => h,
|
|
|
|
Err(e) => {
|
|
|
|
log::error!("Failed to hash user password! {}", e);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
self.update_user(id, |mut user| {
|
|
|
|
user.password = new_hash;
|
|
|
|
user.need_reset_password = temporary;
|
|
|
|
user.two_factor_exemption_after_successful_login = Default::default();
|
|
|
|
user
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2022-11-19 17:18:46 +00:00
|
|
|
fn verify_user_password(&self, user: &UserID, password: &str) -> bool {
|
2022-11-19 16:52:35 +00:00
|
|
|
self.find_by_user_id(user)
|
|
|
|
.map(|u| verify_password(password, &u.password))
|
|
|
|
.unwrap_or(false)
|
|
|
|
}
|
|
|
|
|
2022-11-19 17:27:08 +00:00
|
|
|
fn add_2fa_factor(&mut self, id: &UserID, factor: TwoFactor) -> bool {
|
|
|
|
self.update_user(id, |mut user| {
|
|
|
|
user.two_factor.push(factor);
|
|
|
|
user
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2022-11-19 17:18:46 +00:00
|
|
|
fn save_new_successful_2fa_authentication(&mut self, id: &UserID, ip: IpAddr) -> bool {
|
2022-11-19 16:52:35 +00:00
|
|
|
self.update_user(id, |mut user| {
|
|
|
|
user.last_successful_2fa.insert(ip, time());
|
|
|
|
|
|
|
|
// Remove outdated successful attempts
|
|
|
|
user.remove_outdated_successful_2fa_attempts();
|
|
|
|
|
|
|
|
user
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2022-11-19 17:18:46 +00:00
|
|
|
fn clear_2fa_login_history(&mut self, id: &UserID) -> bool {
|
2022-11-19 16:52:35 +00:00
|
|
|
self.update_user(id, |mut user| {
|
|
|
|
user.last_successful_2fa = Default::default();
|
|
|
|
user
|
|
|
|
})
|
|
|
|
}
|
2022-11-19 17:18:46 +00:00
|
|
|
|
|
|
|
fn delete_account(&mut self, id: &UserID) -> bool {
|
|
|
|
let user = match self.find_by_user_id(id) {
|
|
|
|
None => {
|
|
|
|
log::warn!(
|
|
|
|
"Could not delete account {:?} because it was not found!",
|
|
|
|
id
|
|
|
|
);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
Some(s) => s,
|
|
|
|
};
|
|
|
|
|
|
|
|
match self.remove(&user) {
|
|
|
|
Ok(_) => true,
|
|
|
|
Err(e) => {
|
|
|
|
log::error!("Failed to update delete account! {:?}", e);
|
|
|
|
false
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn update_or_insert_user(&mut self, user: User) -> Res {
|
|
|
|
self.update_or_push(user)
|
|
|
|
}
|
2022-11-19 16:52:35 +00:00
|
|
|
}
|