use crate::errors::Error;
#[derive(Copy, Clone, Eq, PartialEq, Ord, PartialOrd)]
pub enum PasswordStrong {
Low,
Default,
Hard,
}
impl PasswordStrong {
pub fn as_u8(&self) -> u8 {
match self {
PasswordStrong::Low => 2,
PasswordStrong::Default => 3,
PasswordStrong::Hard => 4,
}
}
}
#[derive(Copy, Clone)]
pub struct PasswordStrongChecker {
min_len: usize,
strong: PasswordStrong,
}
impl PasswordStrongChecker {
pub fn new() -> Self {
Self {
min_len: 8,
strong: PasswordStrong::Default,
}
}
pub fn min_len(mut self, min_len: usize) -> Self {
self.min_len = min_len;
self
}
pub fn strong(mut self, strong: PasswordStrong) -> Self {
self.strong = strong;
self
}
pub fn check(&self, raw_password: &str) -> Result<(), Error> {
if raw_password.len() < self.min_len {
return Err(Error::PasswordLength);
}
let estimate = zxcvbn::zxcvbn(raw_password, &[])?;
if estimate.score() < self.strong.as_u8() {
return Err(Error::UnsafePassword);
}
Ok(())
}
}
impl Default for PasswordStrongChecker {
fn default() -> Self {
Self::new()
}
}