Register service user endpoint #5

Merged
phoenix merged 12 commits from register_service_user into main 2026-06-11 14:36:10 -04:00
2 changed files with 44 additions and 26 deletions
Showing only changes of commit 3c03e553c4 - Show all commits
+28 -14
View File
@@ -28,7 +28,10 @@ pub mod request {
impl RegisterServiceUserRequest {
pub fn is_empty(&self) -> (bool, Option<String>) {
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<textsender_models::user::ServiceUser>
pub data: Vec<textsender_models::user::ServiceUser>,
}
}
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<bool, std::io::Error> {
pub async fn register_service_user(
axum::Extension(pool): axum::Extension<sqlx::PgPool>,
Json(payload): Json<request::RegisterServiceUserRequest>,
) -> (axum::http::StatusCode, axum::Json<response::RegisterServiceUserResponse>) {
) -> (
axum::http::StatusCode,
axum::Json<response::RegisterServiceUserResponse>,
) {
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),
)
}
}
}
+16 -12
View File
@@ -49,8 +49,10 @@ pub async fn get_passphrase(
}
}
pub async fn get_with_username(pool: &sqlx::PgPool, username: &String) -> Result<textsender_models::user::ServiceUser, sqlx::Error> {
pub async fn get_with_username(
pool: &sqlx::PgPool,
username: &String,
) -> Result<textsender_models::user::ServiceUser, sqlx::Error> {
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<time::OffsetDateTime, sqlx::Error> {
pub async fn insert(
pool: &sqlx::PgPool,
service_user: &textsender_models::user::ServiceUser,
) -> Result<time::OffsetDateTime, sqlx::Error> {
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),
}
}