Register service user endpoint #5
+43
-7
@@ -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
|
/// Endpoint to register a user
|
||||||
#[utoipa::path(
|
#[utoipa::path(
|
||||||
post,
|
post,
|
||||||
@@ -117,10 +124,11 @@ pub async fn register_user(
|
|||||||
} else {
|
} else {
|
||||||
println!("Good to create");
|
println!("Good to create");
|
||||||
println!("Generate salt string");
|
println!("Generate salt string");
|
||||||
let salt_string = hashing::generate_salt().unwrap();
|
// let salt_string = hashing::generate_salt().unwrap();
|
||||||
let mut salt = textsender_models::user::Salt::default();
|
// let mut salt = textsender_models::user::Salt::default();
|
||||||
let generated_salt = salt_string;
|
// let generated_salt = salt_string;
|
||||||
salt.salt = generated_salt.to_string();
|
// salt.salt = generated_salt.to_string();
|
||||||
|
let (generated_salt, mut salt) = generate_the_salt();
|
||||||
println!("Creating salt");
|
println!("Creating salt");
|
||||||
salt.id = repo::salt::insert(&pool, &salt).await.unwrap();
|
salt.id = repo::salt::insert(&pool, &salt).await.unwrap();
|
||||||
user.salt_id = salt.id;
|
user.salt_id = salt.id;
|
||||||
@@ -128,6 +136,7 @@ pub async fn register_user(
|
|||||||
hashing::hash_password(&user.password, &generated_salt).unwrap();
|
hashing::hash_password(&user.password, &generated_salt).unwrap();
|
||||||
user.password = hashed_password;
|
user.password = hashed_password;
|
||||||
|
|
||||||
|
|
||||||
println!("Creating user");
|
println!("Creating user");
|
||||||
match repo::user::insert(&pool, &user).await {
|
match repo::user::insert(&pool, &user).await {
|
||||||
Ok((id, date_created)) => {
|
Ok((id, date_created)) => {
|
||||||
@@ -201,12 +210,39 @@ pub async fn register_service_user(
|
|||||||
resp.message = msg.unwrap();
|
resp.message = msg.unwrap();
|
||||||
(axum::http::StatusCode::BAD_REQUEST, axum::Json(resp))
|
(axum::http::StatusCode::BAD_REQUEST, axum::Json(resp))
|
||||||
} else {
|
} else {
|
||||||
match repo::salt::get_with_username(&pool, &payload.username) {
|
match repo::service::exists(&pool, &payload.username).await {
|
||||||
Ok(service_user) => {
|
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) => {
|
Err(err) => {
|
||||||
|
resp.message = err.to_string();
|
||||||
|
(axum::http::StatusCode::INTERNAL_SERVER_ERROR, axum::Json(resp))
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
(axum::http::StatusCode::NOT_IMPLEMENTED, axum::Json(resp))
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -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)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user