use argon2::{Argon2, PasswordHasher, PasswordVerifier as ArgonPasswordVerifier}; use somethings::auth::HashedPassword; use crate::Error; pub trait IntoHashedPassword { fn hash_password(self) -> HashedPassword; } impl IntoHashedPassword for String { fn hash_password(self) -> HashedPassword { let a2 = Argon2::default(); let a2_hash = a2.hash_password(self.as_bytes()).unwrap(); HashedPassword::new(a2_hash.to_string()) } } pub trait PasswordVerifier: Sized { fn get_password_hash(&self) -> &HashedPassword; fn verify_password(&self, plain: String) -> Result<(), Error> { let a2_phash = argon2::PasswordHash::new(&self.get_password_hash().to_string()) .map_err(|_| Error::AuthenticationFailed)?; Argon2::default() .verify_password(plain.as_bytes(), &a2_phash) .map_err(|_| Error::AuthenticationFailed) } } impl PasswordVerifier for HashedPassword { fn get_password_hash(&self) -> &HashedPassword { self } }