use std::sync::Arc;
use super::{
ascii_sanitize, garnet_acl_authenticator::GarnetAclAuthenticator,
i_garnet_authenticator::IGarnetAuthenticator,
};
use crate::{AccessControlList, AclPassword, UserHandle};
pub struct GarnetAclWithPasswordAuthenticator {
pub base: GarnetAclAuthenticator,
}
impl GarnetAclWithPasswordAuthenticator {
pub fn new(acl: Arc<AccessControlList>) -> Self {
Self {
base: GarnetAclAuthenticator::new(acl),
}
}
fn authenticate_internal(
user_handle: &Arc<UserHandle>,
_username: &[u8],
password: &[u8],
) -> bool {
let password_hash = AclPassword::from_string(&ascii_sanitize(password));
let user = user_handle.read().clone();
user.is_enabled() && user.validate_password(&password_hash)
}
}
impl IGarnetAuthenticator for GarnetAclWithPasswordAuthenticator {
fn is_authenticated(&self) -> bool {
self.base.is_authenticated()
}
fn can_authenticate(&self) -> bool {
true
}
fn has_acl_support(&self) -> bool {
true
}
fn authenticate(&mut self, password: &[u8], username: &[u8]) -> bool {
self
.base
.authenticate(username, password, Self::authenticate_internal)
}
}