Ver código fonte

Add modules for validating and hashing password

Lee Morgan 1 mês atrás
pai
commit
9f76b1ff42

+ 1 - 0
src/controllers/user/create.rs

@@ -16,5 +16,6 @@ struct Body {
 pub async fn route(
     body: web::Json<Body>
 ) -> Result<HttpResponse, HttpError> {
+
     Ok(HttpResponse::Ok().body("Test"))
 }

+ 18 - 0
src/logic/hash_password.rs

@@ -0,0 +1,18 @@
+use argon2::{
+    Argon2,
+    password_hash::{
+        SaltString,
+        PasswordHasher,
+        rand_core::OsRng
+    }
+};
+use crate::app_error::AppError;
+
+pub fn hash_password(pass: &String) -> Result<String, AppError> {
+    let salt = SaltString::generate(&mut OsRng);
+    let argon2 = Argon2::default();
+    match argon2.hash_password(pass.as_bytes(), &salt) {
+        Ok(h) => Ok(h.to_string()),
+        Err(e) => Err(AppError::InternalError(e.to_string()))
+    }
+}

+ 5 - 0
src/logic/mod.rs

@@ -0,0 +1,5 @@
+pub mod valid_password;
+pub mod hash_password;
+
+pub use valid_password::valid_password;
+pub use hash_password::hash_password;

+ 13 - 0
src/logic/valid_password.rs

@@ -0,0 +1,13 @@
+use crate::app_error::AppError;
+
+pub fn valid_password(pass: &String, confirm_pass: &String) -> Result<(), AppError> {
+    if *pass != *confirm_pass {
+        return Err(AppError::invalid_input("Passwords do not match"));
+    }
+
+    if pass.chars().count() < 12 {
+        return Err(AppError::invalid_input("Password must contain at least 12 characters"));
+    }
+
+    Ok(())
+}

+ 1 - 0
src/main.rs

@@ -8,6 +8,7 @@ mod http_error;
 mod routes;
 mod controllers;
 mod models;
+mod logic;
 
 #[actix_web::main]
 async fn main() -> std::io::Result<()> {