| 1 | use std::fmt; |
| 2 | |
| 3 | use super::DomainError; |
| 4 | |
| 5 | |
| 6 | |
| 7 | |
| 8 | |
| 9 | |
| 10 | |
| 11 | |
| 12 | |
| 13 | |
| 14 | |
| 15 | |
| 16 | |
| 17 | |
| 18 | |
| 19 | #[derive(Clone, PartialEq, Eq)] |
| 20 | pub struct Password(String); |
| 21 | |
| 22 | impl Password { |
| 23 | |
| 24 | |
| 25 | pub const MIN_LEN: usize = 12; |
| 26 | |
| 27 | |
| 28 | |
| 29 | pub const MAX_BYTES: usize = 512; |
| 30 | |
| 31 | |
| 32 | |
| 33 | |
| 34 | |
| 35 | pub const RULE: &'static str = "Passwords must be at least 12 characters. That is the only rule — length is what makes one hard to guess."; |
| 36 | |
| 37 | |
| 38 | |
| 39 | |
| 40 | |
| 41 | pub fn new(value: impl Into<String>) -> Result<Self, DomainError> { |
| 42 | let value = value.into(); |
| 43 | |
| 44 | if value.chars().count() < Self::MIN_LEN { |
| 45 | return Err(DomainError::validation("password", Self::RULE)); |
| 46 | } |
| 47 | |
| 48 | if value.len() > Self::MAX_BYTES { |
| 49 | return Err(DomainError::validation( |
| 50 | "password", |
| 51 | format!("must be at most {} bytes", Self::MAX_BYTES), |
| 52 | )); |
| 53 | } |
| 54 | |
| 55 | Ok(Self(value)) |
| 56 | } |
| 57 | |
| 58 | |
| 59 | pub fn expose(&self) -> &str { |
| 60 | &self.0 |
| 61 | } |
| 62 | } |
| 63 | |
| 64 | impl fmt::Debug for Password { |
| 65 | fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { |
| 66 | f.write_str("Password(redacted)") |
| 67 | } |
| 68 | } |
| 69 | |
| 70 | |
| 71 | |
| 72 | |
| 73 | |
| 74 | |
| 75 | #[derive(Clone, PartialEq, Eq)] |
| 76 | pub struct PasswordHash(String); |
| 77 | |
| 78 | impl PasswordHash { |
| 79 | |
| 80 | |
| 81 | |
| 82 | pub fn from_trusted(value: impl Into<String>) -> Self { |
| 83 | Self(value.into()) |
| 84 | } |
| 85 | |
| 86 | |
| 87 | pub fn as_str(&self) -> &str { |
| 88 | &self.0 |
| 89 | } |
| 90 | } |
| 91 | |
| 92 | impl fmt::Debug for PasswordHash { |
| 93 | fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { |
| 94 | f.write_str("PasswordHash(redacted)") |
| 95 | } |
| 96 | } |
| 97 | |
| 98 | #[cfg(test)] |
| 99 | mod tests { |
| 100 | use super::*; |
| 101 | |
| 102 | #[test] |
| 103 | fn a_password_shorter_than_twelve_characters_is_refused() { |
| 104 | for short in ["", "hunter2", "elevenchars"] { |
| 105 | let error = Password::new(short).expect_err("should refuse"); |
| 106 | |
| 107 | assert!(matches!( |
| 108 | error, |
| 109 | DomainError::Validation { ref field, .. } if field == "password" |
| 110 | )); |
| 111 | assert_eq!( |
| 112 | error.to_string(), |
| 113 | format!("invalid password: {}", Password::RULE) |
| 114 | ); |
| 115 | } |
| 116 | } |
| 117 | |
| 118 | #[test] |
| 119 | fn twelve_characters_is_enough_and_nothing_else_is_required() { |
| 120 | |
| 121 | let password = Password::new("correcthorse").expect("twelve lowercase letters"); |
| 122 | |
| 123 | assert_eq!(password.expose(), "correcthorse"); |
| 124 | assert!(Password::new("aaaaaaaaaaaa").is_ok()); |
| 125 | } |
| 126 | |
| 127 | #[test] |
| 128 | fn length_is_counted_in_characters_not_bytes() { |
| 129 | |
| 130 | |
| 131 | assert!(Password::new("🔑🔑🔑🔑🔑🔑🔑🔑🔑🔑🔑🔑").is_ok()); |
| 132 | assert!(Password::new("🔑🔑🔑🔑🔑🔑🔑🔑🔑🔑🔑").is_err()); |
| 133 | } |
| 134 | |
| 135 | #[test] |
| 136 | fn spaces_are_part_of_the_password() { |
| 137 | let password = Password::new(" twelve ok ").expect("thirteen characters"); |
| 138 | |
| 139 | assert_eq!(password.expose(), " twelve ok "); |
| 140 | } |
| 141 | |
| 142 | #[test] |
| 143 | fn a_password_past_the_argon2_ceiling_is_refused() { |
| 144 | assert!(Password::new("a".repeat(Password::MAX_BYTES)).is_ok()); |
| 145 | |
| 146 | let error = Password::new("a".repeat(Password::MAX_BYTES + 1)).expect_err("should refuse"); |
| 147 | |
| 148 | assert!(error.to_string().contains("512 bytes")); |
| 149 | } |
| 150 | |
| 151 | #[test] |
| 152 | fn debug_output_redacts_the_plaintext() { |
| 153 | let password = Password::new("correcthorsebattery").expect("valid"); |
| 154 | |
| 155 | assert_eq!(format!("{password:?}"), "Password(redacted)"); |
| 156 | } |
| 157 | |
| 158 | #[test] |
| 159 | fn debug_output_redacts_the_hash() { |
| 160 | let hash = PasswordHash::from_trusted("$argon2id$v=19$m=19456,t=2,p=1$abc$def"); |
| 161 | |
| 162 | let rendered = format!("{hash:?}"); |
| 163 | |
| 164 | assert_eq!(rendered, "PasswordHash(redacted)"); |
| 165 | assert!(!rendered.contains("argon2")); |
| 166 | assert!(!rendered.contains("def")); |
| 167 | } |
| 168 | |
| 169 | #[test] |
| 170 | fn the_hash_is_still_reachable_for_storage() { |
| 171 | let hash = PasswordHash::from_trusted("$argon2id$stored"); |
| 172 | |
| 173 | assert_eq!(hash.as_str(), "$argon2id$stored"); |
| 174 | } |
| 175 | } |