1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
use pwhash::bcrypt;
use std::{fmt, str::FromStr};
use thiserror::Error;

#[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Ord)]
pub struct Password(String);

impl Password {
    pub const fn min_len() -> usize {
        6
    }

    pub fn verify(&self, password: &str) -> bool {
        pwhash::bcrypt::verify(password, &self.0)
    }
}

impl From<String> for Password {
    fn from(from: String) -> Self {
        Self(from)
    }
}

impl From<Password> for String {
    fn from(from: Password) -> Self {
        from.0
    }
}

impl AsRef<str> for Password {
    fn as_ref(&self) -> &str {
        &self.0
    }
}

#[derive(Debug, Error)]
pub enum ParseError {
    #[error("Insufficient password length")]
    InsufficientLength,
    #[error("Invalid password")]
    Invalid,
}

impl FromStr for Password {
    type Err = ParseError;

    fn from_str(password: &str) -> Result<Self, Self::Err> {
        if password.len() < Password::min_len() {
            return Err(ParseError::InsufficientLength);
        }
        let res = Self(bcrypt::hash(password).map_err(|e| match e {
            pwhash::error::Error::InsufficientLength => ParseError::InsufficientLength,
            _ => ParseError::Invalid,
        })?);
        debug_assert!(res.verify(password));
        Ok(res)
    }
}

impl fmt::Display for Password {
    fn fmt(&self, f: &mut fmt::Formatter) -> Result<(), std::fmt::Error> {
        write!(f, "{}", self.0)
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn should_hash_and_verify_password() {
        let input = "p^$$w%&7*{}";
        let password = input.parse::<Password>().unwrap();
        assert_ne!(password.as_ref(), input);
        assert!(password.verify(input));
    }

    #[test]
    fn should_fail_to_parse_short_passwords() {
        assert!("a".parse::<Password>().is_err());
        assert!("ab".parse::<Password>().is_err());
        assert!("abc".parse::<Password>().is_err());
        assert!("abcd".parse::<Password>().is_err());
        assert!("abcde".parse::<Password>().is_err());
    }
}