Register service user endpoint (#5)
Rust Build / Rustfmt (push) Successful in 55s
Rust Build / Check (push) Successful in 1m33s
Rust Build / Test Suite (push) Successful in 1m33s
Rust Build / build (push) Successful in 1m38s
Rust Build / Clippy (push) Successful in 2m39s
Rust Build / Rustfmt (pull_request) Successful in 4m0s
Rust Build / Test Suite (pull_request) Successful in 5m18s
Rust Build / Clippy (pull_request) Successful in 1m25s
Rust Build / Check (pull_request) Successful in 5m51s
Rust Build / build (pull_request) Successful in 2m7s
Rust Build / Rustfmt (push) Successful in 55s
Rust Build / Check (push) Successful in 1m33s
Rust Build / Test Suite (push) Successful in 1m33s
Rust Build / build (push) Successful in 1m38s
Rust Build / Clippy (push) Successful in 2m39s
Rust Build / Rustfmt (pull_request) Successful in 4m0s
Rust Build / Test Suite (pull_request) Successful in 5m18s
Rust Build / Clippy (pull_request) Successful in 1m25s
Rust Build / Check (pull_request) Successful in 5m51s
Rust Build / build (pull_request) Successful in 2m7s
Reviewed-on: phoenix/textsender-auth#5
This commit was merged in pull request #5.
This commit is contained in:
@@ -0,0 +1 @@
|
||||
pub const SUCCESSFUL_MESSAGE: &str = "Successful";
|
||||
@@ -1,5 +1,6 @@
|
||||
pub mod common;
|
||||
pub mod login;
|
||||
pub mod messages;
|
||||
pub mod register;
|
||||
|
||||
pub mod endpoints {
|
||||
@@ -8,4 +9,6 @@ pub mod endpoints {
|
||||
/// Endpoint for a user to login
|
||||
pub const LOGIN: &str = "/api/v1/login";
|
||||
pub const DBTEST: &str = "/api/v1/test/db";
|
||||
/// Endpoint constant for service user registration
|
||||
pub const REGISTER_SERVICE_USER: &str = "/api/v1/service/register";
|
||||
}
|
||||
|
||||
+135
-4
@@ -18,6 +18,29 @@ pub mod request {
|
||||
#[serde(skip_serializing_if = "Option::is_none")]
|
||||
pub lastname: Option<String>,
|
||||
}
|
||||
|
||||
#[derive(Default, Deserialize, Serialize, utoipa::ToSchema)]
|
||||
pub struct RegisterServiceUserRequest {
|
||||
pub username: String,
|
||||
pub passphrase: String,
|
||||
}
|
||||
|
||||
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")),
|
||||
)
|
||||
} else if self.username.is_empty() {
|
||||
(true, Some(String::from("Username is empty")))
|
||||
} else if self.passphrase.is_empty() {
|
||||
(true, Some(String::from("Passphrase is empty")))
|
||||
} else {
|
||||
(false, None)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
pub mod response {
|
||||
@@ -28,6 +51,21 @@ pub mod response {
|
||||
pub message: String,
|
||||
pub data: Vec<textsender_models::user::User>,
|
||||
}
|
||||
|
||||
#[derive(Default, Deserialize, Serialize, utoipa::ToSchema)]
|
||||
pub struct RegisterServiceUserResponse {
|
||||
pub message: String,
|
||||
pub data: Vec<textsender_models::user::ServiceUser>,
|
||||
}
|
||||
}
|
||||
|
||||
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
|
||||
@@ -91,10 +129,8 @@ 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();
|
||||
user.salt_id = salt.id;
|
||||
@@ -160,3 +196,98 @@ async fn is_registration_enabled() -> Result<bool, std::io::Error> {
|
||||
))
|
||||
}
|
||||
}
|
||||
|
||||
/// 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(
|
||||
axum::Extension(pool): axum::Extension<sqlx::PgPool>,
|
||||
Json(payload): Json<request::RegisterServiceUserRequest>,
|
||||
) -> (
|
||||
axum::http::StatusCode,
|
||||
axum::Json<response::RegisterServiceUserResponse>,
|
||||
) {
|
||||
let mut resp = response::RegisterServiceUserResponse {
|
||||
..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();
|
||||
if res {
|
||||
resp.message = msg.unwrap();
|
||||
(axum::http::StatusCode::BAD_REQUEST, axum::Json(resp))
|
||||
} else {
|
||||
if registration_enabled {
|
||||
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) => {
|
||||
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::Json(resp),
|
||||
)
|
||||
}
|
||||
}
|
||||
} else {
|
||||
resp.message = String::from("Registration is not enabled");
|
||||
(axum::http::StatusCode::NOT_ACCEPTABLE, Json(resp))
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -97,6 +97,10 @@ pub mod init {
|
||||
callers::endpoints::REGISTER,
|
||||
post(callers::register::register_user),
|
||||
)
|
||||
.route(
|
||||
callers::endpoints::REGISTER_SERVICE_USER,
|
||||
post(callers::register::register_service_user),
|
||||
)
|
||||
.route(callers::endpoints::LOGIN, post(callers::login::user_login))
|
||||
.layer(cors::configure_cors().await)
|
||||
}
|
||||
|
||||
@@ -48,3 +48,82 @@ pub async fn get_passphrase(
|
||||
Err(err) => Err(err),
|
||||
}
|
||||
}
|
||||
|
||||
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)
|
||||
.fetch_one(pool).await {
|
||||
Ok(row) => {
|
||||
let service_user = textsender_models::user::ServiceUser {
|
||||
id: row.try_get("id")?,
|
||||
username: row.try_get("username")?,
|
||||
passphrase: row.try_get("passphrase")?,
|
||||
created: row.try_get("created")?,
|
||||
last_login: row.try_get("last_login")?,
|
||||
salt_id: row.try_get("salt_id")?
|
||||
};
|
||||
|
||||
Ok(service_user)
|
||||
}
|
||||
Err(err) => {
|
||||
Err(err)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
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
|
||||
{
|
||||
Ok(row) => {
|
||||
let created: time::OffsetDateTime = row.try_get("created")?;
|
||||
Ok(created)
|
||||
}
|
||||
Err(err) => Err(err),
|
||||
}
|
||||
}
|
||||
|
||||
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