diff --git a/src/actions/users.rs b/src/actions/users.rs index afbab43..bbe464e 100644 --- a/src/actions/users.rs +++ b/src/actions/users.rs @@ -43,6 +43,7 @@ pub fn get_all( pub fn insert_new_user( nu: models::NewUser, conn: &impl diesel::Connection, + hash_cost: Option, ) -> Result { // It is common when using Diesel with Actix web to import schema-related // modules inside a function's scope (rather than the normal module's scope) @@ -50,7 +51,7 @@ pub fn insert_new_user( use crate::schema::users::dsl::*; let nu = { let mut nu2 = nu; - nu2.password = hash(&nu2.password, DEFAULT_COST)?; + nu2.password = hash(&nu2.password, hash_cost.unwrap_or(DEFAULT_COST))?; nu2 }; diff --git a/src/lib.rs b/src/lib.rs index c465908..4fc4e41 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -39,13 +39,13 @@ pub struct EnvConfig { pub database_url: String, pub http_host: String, #[serde(default = "default_hash_cost")] - pub hash_cost: u8, + pub hash_cost: u32, pub logger_format: LoggerFormat, } #[derive(Deserialize, Debug, Clone)] pub struct AppConfig { - pub hash_cost: u8, + pub hash_cost: u32, } #[derive(Clone)] @@ -54,7 +54,7 @@ pub struct AppData { pub pool: DbPool, } -pub fn default_hash_cost() -> u8 { +pub fn default_hash_cost() -> u32 { 8 } @@ -72,7 +72,8 @@ pub fn configure_app(app_data: AppData) -> Box { .route( "/{user_id}", web::get().to(routes::users::get_user), - ), + ) + .route("", web::post().to(routes::users::add_user)), ) .route( "/build-info", @@ -84,7 +85,7 @@ pub fn configure_app(app_data: AppData) -> Box { .service(routes::auth::login) .service(routes::auth::logout) .service(routes::auth::index) - .service(routes::users::add_user) + // .service(routes::users::add_user) .service(fs::Files::new("/", "./static")); }) } diff --git a/src/routes/users.rs b/src/routes/users.rs index 58ceb13..100d29b 100644 --- a/src/routes/users.rs +++ b/src/routes/users.rs @@ -58,6 +58,8 @@ pub async fn get_user2( } } +///List all users +#[tracing::instrument(level = "debug", skip(app_data))] pub async fn get_all_users( app_data: web::Data, ) -> Result { @@ -70,7 +72,7 @@ pub async fn get_all_users( .await .map_err(|err| DomainError::new_thread_pool_error(err.to_string()))?; - tracing::debug!("{:?}", users); + tracing::trace!("{:?}", users); if !users.is_empty() { Ok(HttpResponse::Ok().json(users)) @@ -82,7 +84,7 @@ pub async fn get_all_users( } //TODO: Add refinement here /// Inserts new user with name defined in form. -#[post("/do_registration")] +#[tracing::instrument(level = "debug", skip(app_data))] pub async fn add_user( app_data: web::Data, form: web::Json, @@ -92,7 +94,11 @@ pub async fn add_user( Ok(_) => web::block(move || { let pool = &app_data.pool; let conn = pool.get()?; - actions::insert_new_user(form.0, &conn) + actions::insert_new_user( + form.0, + &conn, + Some(app_data.config.hash_cost), + ) }) .await .map(|user| { diff --git a/src/services/user_service.rs b/src/services/user_service.rs index 6088392..b5d1355 100644 --- a/src/services/user_service.rs +++ b/src/services/user_service.rs @@ -58,7 +58,7 @@ impl UserService for UserServiceImpl { nu: models::NewUser, ) -> Result { let conn = self.pool.get()?; - actions::insert_new_user(nu, &conn) + actions::insert_new_user(nu, &conn, Some(8)) } fn verify_password(