More changes
Rust Build / Rustfmt (pull_request) Failing after 39s
Rust Build / Check (pull_request) Successful in 1m59s
Rust Build / Clippy (pull_request) Successful in 1m18s
Rust Build / Test Suite (pull_request) Successful in 2m1s
Rust Build / build (pull_request) Successful in 3m34s

This commit is contained in:
2026-06-11 13:29:06 -04:00
parent 5bbc1acd27
commit a5e41116b8
2 changed files with 72 additions and 7 deletions
+43 -7
View File
@@ -56,6 +56,13 @@ pub mod response {
}
}
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)
}
/// Endpoint to register a user
#[utoipa::path(
post,
@@ -117,10 +124,11 @@ 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 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();
user.salt_id = salt.id;
@@ -128,6 +136,7 @@ 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)) => {
@@ -201,12 +210,39 @@ pub async fn register_service_user(
resp.message = msg.unwrap();
(axum::http::StatusCode::BAD_REQUEST, axum::Json(resp))
} else {
match repo::salt::get_with_username(&pool, &payload.username) {
Ok(service_user) => {
match repo::service::exists(&pool, &payload.username).await {
Ok(exists) => {
if exists {
resp.message = String::from("Invalid");
(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(),
salt_id: salt.id,
..Default::default()
};
println!("Creating user");
match repo::service::insert(&pool, &service_user).await {
Ok(created) => {
service_user.created = Some(created);
(axum::http::StatusCode::NOT_IMPLEMENTED, axum::Json(resp))
}
Err(err) => {
resp.message = err.to_string();
(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::NOT_IMPLEMENTED, axum::Json(resp))
}
}
+29
View File
@@ -94,3 +94,32 @@ pub async fn insert(pool: &sqlx::PgPool, service_user: &textsender_models::user:
}
}
}
pub async fn exists(pool: &sqlx::PgPool, service_username: &String) -> Result<bool, sqlx::Error> {
let result = sqlx::query(
r#"
SELECT 1 FROM "service_user" WHERE username = $1
"#,
)
.bind(service_username)
.fetch_optional(pool)
.await;
match result {
Ok(r) => match r {
Some(row) => {
if row.is_empty() {
Ok(false)
} else {
Ok(true)
}
}
None => Ok(false),
},
Err(e) => {
eprintln!("What??");
eprintln!("Error: {e:?}");
Err(e)
}
}
}