use std::sync::Arc;
use super::{
garnet_acl_authenticator::GarnetAclAuthenticator, i_garnet_authenticator::IGarnetAuthenticator,
};
use crate::acl::{AccessControlList, user_handle::UserHandle};
pub struct GarnetAclWithAadAuthenticator {
pub base: GarnetAclAuthenticator,
aad: Box<dyn IGarnetAuthenticator>,
}
impl GarnetAclWithAadAuthenticator {
pub fn new(acl: Arc<AccessControlList>, aad: Box<dyn IGarnetAuthenticator>) -> Self {
Self {
base: GarnetAclAuthenticator::new(acl),
aad,
}
}
fn authenticate_internal(
aad: &mut dyn IGarnetAuthenticator,
user_handle: &Arc<UserHandle>,
username: &[u8],
password: &[u8],
) -> bool {
user_handle.user().is_enabled() && !password.is_empty() && aad.authenticate(password, username)
}
}
impl IGarnetAuthenticator for GarnetAclWithAadAuthenticator {
fn is_authenticated(&self) -> bool {
self.aad.is_authenticated() && 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 {
let Self { base, aad } = self;
base.authenticate(username, password, &mut |handle, username, password| {
Self::authenticate_internal(aad.as_mut(), handle, username, password)
})
}
}