26d9828feat: password hashing behind a port1mo | 1 | |
| 2 | |
| 3 | |
| 4 | |
| 5 | |
| 6 | use crate::domain::PasswordHash; |
| 7 | |
| 8 | |
| 9 | |
| 10 | |
| 11 | |
| 12 | |
| 13 | pub trait PasswordHasher: Send + Sync { |
| 14 | |
| 15 | fn hash(&self, plaintext: &str) -> Result<PasswordHash, PasswordError>; |
| 16 | |
| 17 | |
| 18 | |
| 19 | |
| 20 | |
| 21 | |
| 22 | fn verify(&self, plaintext: &str, hash: &PasswordHash) -> Result<bool, PasswordError>; |
| 23 | } |
| 24 | |
| 25 | |
| 26 | #[derive(Debug, Clone, PartialEq, Eq)] |
| 27 | pub struct PasswordError(String); |
| 28 | |
| 29 | impl PasswordError { |
| 30 | pub fn new(message: impl Into<String>) -> Self { |
| 31 | Self(message.into()) |
| 32 | } |
| 33 | } |
| 34 | |
| 35 | impl std::fmt::Display for PasswordError { |
| 36 | fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { |
| 37 | write!(f, "password hashing failed: {}", self.0) |
| 38 | } |
| 39 | } |
| 40 | |
| 41 | impl std::error::Error for PasswordError {} |