diff --git a/backend/src/handlers/auth.rs b/backend/src/handlers/auth.rs index c0f4ef7..3419df7 100644 --- a/backend/src/handlers/auth.rs +++ b/backend/src/handlers/auth.rs @@ -10,6 +10,7 @@ use wither::bson::oid::ObjectId; use crate::{ auth::jwt::{Claims, JwtService}, + auth::password::verify_password, config::AppState, models::user::{User, UserRepository}, }; @@ -221,7 +222,7 @@ pub async fn login( }; // Verify password - match user.verify_password(&req.password) { + match verify_password(&req.password, &user.password_hash) { Ok(true) => {} Ok(false) => { return Err(( @@ -493,7 +494,7 @@ pub async fn setup_recovery( }; // Verify current password - match user.verify_password(&req.current_password) { + match verify_password(&req.current_password, &user.password_hash) { Ok(true) => {} Ok(false) => { return Err(( diff --git a/backend/src/models/user.rs b/backend/src/models/user.rs index 8327d58..e2687a5 100644 --- a/backend/src/models/user.rs +++ b/backend/src/models/user.rs @@ -6,7 +6,7 @@ use wither::{ IndexModel, IndexOptions, Model, }; -use crate::auth::password::PasswordService; +use crate::auth::password::{PasswordService, verify_password}; #[derive(Debug, Clone, Serialize, Deserialize, Model)] #[model(collection_name="users")] @@ -57,14 +57,12 @@ impl User { password: String, recovery_phrase: Option, ) -> Result { - let password_service = PasswordService::new(); - // Hash the password - let password_hash = password_service.hash_password(&password)?; + let password_hash = PasswordService::hash_password(&password)?; // Hash the recovery phrase if provided let recovery_phrase_hash = if let Some(phrase) = recovery_phrase { - Some(password_service.hash_password(&phrase)?) + Some(PasswordService::hash_password(&phrase)?) } else { None }; @@ -89,8 +87,7 @@ impl User { /// Verify a password against the stored hash pub fn verify_password(&self, password: &str) -> Result { - let password_service = PasswordService::new(); - password_service.verify_password(password, &self.password_hash) + verify_password(password, &self.password_hash) } /// Verify a recovery phrase against the stored hash @@ -99,23 +96,20 @@ impl User { return Ok(false); } - let password_service = PasswordService::new(); let hash = self.recovery_phrase_hash.as_ref().unwrap(); - password_service.verify_password(phrase, hash) + verify_password(phrase, hash) } /// Update the password hash (increments token_version to invalidate all tokens) pub fn update_password(&mut self, new_password: String) -> Result<(), anyhow::Error> { - let password_service = PasswordService::new(); - self.password_hash = password_service.hash_password(&new_password)?; + self.password_hash = PasswordService::hash_password(&new_password)?; self.token_version += 1; Ok(()) } /// Set or update the recovery phrase pub fn set_recovery_phrase(&mut self, phrase: String) -> Result<(), anyhow::Error> { - let password_service = PasswordService::new(); - self.recovery_phrase_hash = Some(password_service.hash_password(&phrase)?); + self.recovery_phrase_hash = Some(PasswordService::hash_password(&phrase)?); self.recovery_enabled = true; Ok(()) }