use super::authentication_settings::{AuthSetup, IAuthenticationSettings};
use crate::{
acl::AclError,
auth::{GarnetPasswordAuthenticator, IGarnetAuthenticator},
};
pub struct PasswordAuthenticationSettings {
pwd: Box<[u8]>,
}
impl PasswordAuthenticationSettings {
pub fn new(pwd: &str) -> Result<Self, AclError> {
if pwd.is_empty() {
return Err(AclError::Acl("Password cannot be null.".into()));
}
Ok(Self {
pwd: pwd.as_bytes().to_vec().into(),
})
}
}
impl IAuthenticationSettings for PasswordAuthenticationSettings {
fn create_authenticator(
&self,
_setup: &AuthSetup,
) -> Result<Box<dyn IGarnetAuthenticator>, AclError> {
Ok(Box::new(GarnetPasswordAuthenticator::new(self.pwd.clone())))
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn empty_password_rejected() {
assert!(PasswordAuthenticationSettings::new("").is_err());
let settings = PasswordAuthenticationSettings::new("pw").unwrap();
assert_eq!(settings.pwd.as_ref(), b"pw");
}
}