Added doc gen for endpoint and made some improvements
This commit is contained in:
+74
-39
@@ -198,6 +198,21 @@ async fn is_registration_enabled() -> Result<bool, std::io::Error> {
|
|||||||
}
|
}
|
||||||
|
|
||||||
/// Endpoint to register a service user
|
/// Endpoint to register a service user
|
||||||
|
#[utoipa::path(
|
||||||
|
post,
|
||||||
|
path = super::endpoints::REGISTER_SERVICE_USER,
|
||||||
|
request_body(
|
||||||
|
content = request::RegisterServiceUserRequest,
|
||||||
|
description = "Data required to register service user",
|
||||||
|
content_type = "application/json"
|
||||||
|
),
|
||||||
|
responses(
|
||||||
|
(status = 201, description = "Service user created", body = response::RegisterServiceUserResponse),
|
||||||
|
(status = 400, description = "Issue creating service user", body = response::RegisterServiceUserResponse),
|
||||||
|
(status = 406, description = "Cannot create service user", body = response::RegisterServiceUserResponse),
|
||||||
|
(status = 500, description = "Issue creating service user", body = response::RegisterServiceUserResponse),
|
||||||
|
)
|
||||||
|
)]
|
||||||
pub async fn register_service_user(
|
pub async fn register_service_user(
|
||||||
axum::Extension(pool): axum::Extension<sqlx::PgPool>,
|
axum::Extension(pool): axum::Extension<sqlx::PgPool>,
|
||||||
Json(payload): Json<request::RegisterServiceUserRequest>,
|
Json(payload): Json<request::RegisterServiceUserRequest>,
|
||||||
@@ -209,56 +224,76 @@ pub async fn register_service_user(
|
|||||||
..Default::default()
|
..Default::default()
|
||||||
};
|
};
|
||||||
|
|
||||||
|
let registration_enabled = match is_registration_enabled().await {
|
||||||
|
Ok(value) => value,
|
||||||
|
Err(err) => {
|
||||||
|
eprintln!("Error: {err:?}");
|
||||||
|
resp.message = String::from("Registration check failed");
|
||||||
|
return (
|
||||||
|
axum::http::StatusCode::INTERNAL_SERVER_ERROR,
|
||||||
|
Json(resp)
|
||||||
|
);
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
let (res, msg) = payload.is_empty();
|
let (res, msg) = payload.is_empty();
|
||||||
if res {
|
if res {
|
||||||
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::service::exists(&pool, &payload.username).await {
|
if registration_enabled {
|
||||||
Ok(exists) => {
|
match repo::service::exists(&pool, &payload.username).await {
|
||||||
if exists {
|
Ok(exists) => {
|
||||||
resp.message = String::from("Invalid");
|
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) => {
|
||||||
|
resp.message = String::from(super::messages::SUCCESSFUL_MESSAGE);
|
||||||
|
service_user.created = Some(created);
|
||||||
|
resp.data.push(service_user);
|
||||||
|
(axum::http::StatusCode::CREATED, 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::http::StatusCode::INTERNAL_SERVER_ERROR,
|
||||||
axum::Json(resp),
|
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) => {
|
|
||||||
resp.message = String::from(super::messages::SUCCESSFUL_MESSAGE);
|
|
||||||
service_user.created = Some(created);
|
|
||||||
resp.data.push(service_user);
|
|
||||||
(axum::http::StatusCode::CREATED, axum::Json(resp))
|
|
||||||
}
|
|
||||||
Err(err) => {
|
|
||||||
resp.message = err.to_string();
|
|
||||||
(
|
|
||||||
axum::http::StatusCode::INTERNAL_SERVER_ERROR,
|
|
||||||
axum::Json(resp),
|
|
||||||
)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
Err(err) => {
|
} else {
|
||||||
resp.message = err.to_string();
|
resp.message = String::from("Registration is not enabled");
|
||||||
(
|
(
|
||||||
axum::http::StatusCode::INTERNAL_SERVER_ERROR,
|
axum::http::StatusCode::NOT_ACCEPTABLE,
|
||||||
axum::Json(resp),
|
Json(resp),
|
||||||
)
|
)
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user