diff --git a/src/callers/register.rs b/src/callers/register.rs index c124414..2a63283 100644 --- a/src/callers/register.rs +++ b/src/callers/register.rs @@ -28,7 +28,10 @@ pub mod request { impl RegisterServiceUserRequest { pub fn is_empty(&self) -> (bool, Option) { if self.username.is_empty() && self.passphrase.is_empty() { - (true, Some(String::from("Username and Passphrase are empty"))) + ( + true, + Some(String::from("Username and Passphrase are empty")), + ) } else if self.username.is_empty() { (true, Some(String::from("Username is empty"))) } else if self.passphrase.is_empty() { @@ -52,12 +55,14 @@ pub mod response { #[derive(Default, Deserialize, Serialize, utoipa::ToSchema)] pub struct RegisterServiceUserResponse { pub message: String, - pub data: Vec + pub data: Vec, } } - -fn generate_the_salt() -> (argon2::password_hash::SaltString, textsender_models::user::Salt) { +fn generate_the_salt() -> ( + argon2::password_hash::SaltString, + textsender_models::user::Salt, +) { let salt_string = hashing::generate_salt().unwrap(); let salt = textsender_models::user::Salt::default(); (salt_string, salt) @@ -124,10 +129,7 @@ pub async fn register_user( } else { println!("Good to create"); println!("Generate salt string"); - // let salt_string = hashing::generate_salt().unwrap(); - // let mut salt = textsender_models::user::Salt::default(); - // let generated_salt = salt_string; - // salt.salt = generated_salt.to_string(); + let (generated_salt, mut salt) = generate_the_salt(); println!("Creating salt"); salt.id = repo::salt::insert(&pool, &salt).await.unwrap(); @@ -136,7 +138,6 @@ pub async fn register_user( hashing::hash_password(&user.password, &generated_salt).unwrap(); user.password = hashed_password; - println!("Creating user"); match repo::user::insert(&pool, &user).await { Ok((id, date_created)) => { @@ -200,7 +201,10 @@ async fn is_registration_enabled() -> Result { pub async fn register_service_user( axum::Extension(pool): axum::Extension, Json(payload): Json, - ) -> (axum::http::StatusCode, axum::Json) { +) -> ( + axum::http::StatusCode, + axum::Json, +) { let mut resp = response::RegisterServiceUserResponse { ..Default::default() }; @@ -214,13 +218,17 @@ pub async fn register_service_user( Ok(exists) => { if exists { resp.message = String::from("Invalid"); - (axum::http::StatusCode::INTERNAL_SERVER_ERROR, axum::Json(resp)) + ( + axum::http::StatusCode::INTERNAL_SERVER_ERROR, + axum::Json(resp), + ) } else { let (generate_salt, mut salt) = generate_the_salt(); salt.id = repo::salt::insert(&pool, &salt).await.unwrap(); let mut service_user = textsender_models::user::ServiceUser { username: payload.username.clone(), - passphrase: hashing::hash_password(&payload.username, &generate_salt).unwrap(), + passphrase: hashing::hash_password(&payload.username, &generate_salt) + .unwrap(), salt_id: salt.id, ..Default::default() }; @@ -234,14 +242,20 @@ pub async fn register_service_user( } Err(err) => { resp.message = err.to_string(); - (axum::http::StatusCode::INTERNAL_SERVER_ERROR, axum::Json(resp)) + ( + axum::http::StatusCode::INTERNAL_SERVER_ERROR, + axum::Json(resp), + ) } } } } Err(err) => { resp.message = err.to_string(); - (axum::http::StatusCode::INTERNAL_SERVER_ERROR, axum::Json(resp)) + ( + axum::http::StatusCode::INTERNAL_SERVER_ERROR, + axum::Json(resp), + ) } } } diff --git a/src/repo/service.rs b/src/repo/service.rs index 2d39a19..1f98dcd 100644 --- a/src/repo/service.rs +++ b/src/repo/service.rs @@ -49,8 +49,10 @@ pub async fn get_passphrase( } } - -pub async fn get_with_username(pool: &sqlx::PgPool, username: &String) -> Result { +pub async fn get_with_username( + pool: &sqlx::PgPool, + username: &String, +) -> Result { match sqlx::query( r#"SELECT id, username, passphrase, created, last_login FROM "service_user" WHERE username = $1"# ).bind(username) @@ -73,25 +75,27 @@ pub async fn get_with_username(pool: &sqlx::PgPool, username: &String) -> Result } } -pub async fn insert(pool: &sqlx::PgPool, service_user: &textsender_models::user::ServiceUser) -> Result { +pub async fn insert( + pool: &sqlx::PgPool, + service_user: &textsender_models::user::ServiceUser, +) -> Result { match sqlx::query( r#"INSERT INTO "service_user" (username, passphrase, salt_id) VALUES ($1, $2, $3) RETURNING created - "# - ).bind(&service_user.username) - .bind(&service_user.passphrase) - .bind(service_user.salt_id) - .fetch_one(pool) - .await + "#, + ) + .bind(&service_user.username) + .bind(&service_user.passphrase) + .bind(service_user.salt_id) + .fetch_one(pool) + .await { Ok(row) => { let created: time::OffsetDateTime = row.try_get("created")?; Ok(created) } - Err(err) => { - Err(err) - } + Err(err) => Err(err), } }