cargo fmt
Rust Build / Check (pull_request) Successful in 1m41s
Rust Build / Test Suite (pull_request) Successful in 1m53s
Rust Build / Rustfmt (pull_request) Successful in 31s
Rust Build / Clippy (pull_request) Successful in 1m11s
Rust Build / build (pull_request) Successful in 2m26s
Rust Build / Check (pull_request) Successful in 1m41s
Rust Build / Test Suite (pull_request) Successful in 1m53s
Rust Build / Rustfmt (pull_request) Successful in 31s
Rust Build / Clippy (pull_request) Successful in 1m11s
Rust Build / build (pull_request) Successful in 2m26s
This commit is contained in:
+28
-14
@@ -28,7 +28,10 @@ pub mod request {
|
|||||||
impl RegisterServiceUserRequest {
|
impl RegisterServiceUserRequest {
|
||||||
pub fn is_empty(&self) -> (bool, Option<String>) {
|
pub fn is_empty(&self) -> (bool, Option<String>) {
|
||||||
if self.username.is_empty() && self.passphrase.is_empty() {
|
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() {
|
} else if self.username.is_empty() {
|
||||||
(true, Some(String::from("Username is empty")))
|
(true, Some(String::from("Username is empty")))
|
||||||
} else if self.passphrase.is_empty() {
|
} else if self.passphrase.is_empty() {
|
||||||
@@ -52,12 +55,14 @@ pub mod response {
|
|||||||
#[derive(Default, Deserialize, Serialize, utoipa::ToSchema)]
|
#[derive(Default, Deserialize, Serialize, utoipa::ToSchema)]
|
||||||
pub struct RegisterServiceUserResponse {
|
pub struct RegisterServiceUserResponse {
|
||||||
pub message: String,
|
pub message: String,
|
||||||
pub data: Vec<textsender_models::user::ServiceUser>
|
pub data: Vec<textsender_models::user::ServiceUser>,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
fn generate_the_salt() -> (
|
||||||
fn generate_the_salt() -> (argon2::password_hash::SaltString, textsender_models::user::Salt) {
|
argon2::password_hash::SaltString,
|
||||||
|
textsender_models::user::Salt,
|
||||||
|
) {
|
||||||
let salt_string = hashing::generate_salt().unwrap();
|
let salt_string = hashing::generate_salt().unwrap();
|
||||||
let salt = textsender_models::user::Salt::default();
|
let salt = textsender_models::user::Salt::default();
|
||||||
(salt_string, salt)
|
(salt_string, salt)
|
||||||
@@ -124,10 +129,7 @@ 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 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();
|
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();
|
||||||
@@ -136,7 +138,6 @@ 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)) => {
|
||||||
@@ -200,7 +201,10 @@ async fn is_registration_enabled() -> Result<bool, std::io::Error> {
|
|||||||
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>,
|
||||||
) -> (axum::http::StatusCode, axum::Json<response::RegisterServiceUserResponse>) {
|
) -> (
|
||||||
|
axum::http::StatusCode,
|
||||||
|
axum::Json<response::RegisterServiceUserResponse>,
|
||||||
|
) {
|
||||||
let mut resp = response::RegisterServiceUserResponse {
|
let mut resp = response::RegisterServiceUserResponse {
|
||||||
..Default::default()
|
..Default::default()
|
||||||
};
|
};
|
||||||
@@ -214,13 +218,17 @@ pub async fn register_service_user(
|
|||||||
Ok(exists) => {
|
Ok(exists) => {
|
||||||
if exists {
|
if exists {
|
||||||
resp.message = String::from("Invalid");
|
resp.message = String::from("Invalid");
|
||||||
(axum::http::StatusCode::INTERNAL_SERVER_ERROR, axum::Json(resp))
|
(
|
||||||
|
axum::http::StatusCode::INTERNAL_SERVER_ERROR,
|
||||||
|
axum::Json(resp),
|
||||||
|
)
|
||||||
} else {
|
} else {
|
||||||
let (generate_salt, mut salt) = generate_the_salt();
|
let (generate_salt, mut salt) = generate_the_salt();
|
||||||
salt.id = repo::salt::insert(&pool, &salt).await.unwrap();
|
salt.id = repo::salt::insert(&pool, &salt).await.unwrap();
|
||||||
let mut service_user = textsender_models::user::ServiceUser {
|
let mut service_user = textsender_models::user::ServiceUser {
|
||||||
username: payload.username.clone(),
|
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,
|
salt_id: salt.id,
|
||||||
..Default::default()
|
..Default::default()
|
||||||
};
|
};
|
||||||
@@ -234,14 +242,20 @@ pub async fn register_service_user(
|
|||||||
}
|
}
|
||||||
Err(err) => {
|
Err(err) => {
|
||||||
resp.message = err.to_string();
|
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) => {
|
Err(err) => {
|
||||||
resp.message = err.to_string();
|
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
@@ -49,8 +49,10 @@ pub async fn get_passphrase(
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
pub async fn get_with_username(
|
||||||
pub async fn get_with_username(pool: &sqlx::PgPool, username: &String) -> Result<textsender_models::user::ServiceUser, sqlx::Error> {
|
pool: &sqlx::PgPool,
|
||||||
|
username: &String,
|
||||||
|
) -> Result<textsender_models::user::ServiceUser, sqlx::Error> {
|
||||||
match sqlx::query(
|
match sqlx::query(
|
||||||
r#"SELECT id, username, passphrase, created, last_login FROM "service_user" WHERE username = $1"#
|
r#"SELECT id, username, passphrase, created, last_login FROM "service_user" WHERE username = $1"#
|
||||||
).bind(username)
|
).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(
|
match sqlx::query(
|
||||||
r#"INSERT INTO "service_user" (username, passphrase, salt_id)
|
r#"INSERT INTO "service_user" (username, passphrase, salt_id)
|
||||||
VALUES ($1, $2, $3)
|
VALUES ($1, $2, $3)
|
||||||
RETURNING created
|
RETURNING created
|
||||||
"#
|
"#,
|
||||||
).bind(&service_user.username)
|
)
|
||||||
.bind(&service_user.passphrase)
|
.bind(&service_user.username)
|
||||||
.bind(service_user.salt_id)
|
.bind(&service_user.passphrase)
|
||||||
.fetch_one(pool)
|
.bind(service_user.salt_id)
|
||||||
.await
|
.fetch_one(pool)
|
||||||
|
.await
|
||||||
{
|
{
|
||||||
Ok(row) => {
|
Ok(row) => {
|
||||||
let created: time::OffsetDateTime = row.try_get("created")?;
|
let created: time::OffsetDateTime = row.try_get("created")?;
|
||||||
Ok(created)
|
Ok(created)
|
||||||
}
|
}
|
||||||
Err(err) => {
|
Err(err) => Err(err),
|
||||||
Err(err)
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user