Cleanup
This commit is contained in:
@@ -43,7 +43,7 @@ pub fn encode_token(header: &Header, id: String, key: &EncodingKey) -> String {
|
||||
expires: expires as usize,
|
||||
};
|
||||
let token = encode(header, &claims, key);
|
||||
return token.expect("token return failed");
|
||||
token.expect("token return failed")
|
||||
}
|
||||
|
||||
/// Decodes and validates a JSON Web Token (JWT).
|
||||
@@ -77,5 +77,5 @@ pub fn decode_token(token: String, key: &DecodingKey) -> Result<Claims, (StatusC
|
||||
(StatusCode::UNAUTHORIZED, Json(error))
|
||||
})?
|
||||
.claims;
|
||||
return Ok(claims);
|
||||
Ok(claims)
|
||||
}
|
||||
|
||||
@@ -48,13 +48,7 @@ pub async fn validate_token(
|
||||
.headers()
|
||||
.get(header::AUTHORIZATION)
|
||||
.and_then(|header| header.to_str().ok())
|
||||
.and_then(|value| {
|
||||
if value.starts_with("Bearer ") {
|
||||
Some(value[7..].to_owned())
|
||||
} else {
|
||||
None
|
||||
}
|
||||
})
|
||||
.and_then(|value| value.strip_prefix("Bearer ").map(|s| s.to_owned()))
|
||||
});
|
||||
|
||||
let token = token.ok_or_else(|| {
|
||||
@@ -77,7 +71,7 @@ pub async fn validate_token(
|
||||
(status, Json(error))
|
||||
})?;
|
||||
|
||||
let uuid = (&claims.sub).parse::<i16>().map_err(|_| {
|
||||
let uuid = claims.sub.parse::<i16>().map_err(|_| {
|
||||
let error = json!({
|
||||
"status": "error",
|
||||
"message": "Invalid user id"
|
||||
@@ -143,13 +137,7 @@ pub async fn validate_admin(
|
||||
.headers()
|
||||
.get(header::AUTHORIZATION)
|
||||
.and_then(|header| header.to_str().ok())
|
||||
.and_then(|value| {
|
||||
if value.starts_with("Bearer ") {
|
||||
Some(value[7..].to_owned())
|
||||
} else {
|
||||
None
|
||||
}
|
||||
})
|
||||
.and_then(|value| value.strip_prefix("Bearer ").map(|s| s.to_owned()))
|
||||
});
|
||||
|
||||
let token = token.ok_or_else(|| {
|
||||
@@ -172,7 +160,7 @@ pub async fn validate_admin(
|
||||
(status, Json(error))
|
||||
})?;
|
||||
|
||||
let uuid = (&claims.sub).parse::<i16>().map_err(|_| {
|
||||
let uuid = claims.sub.parse::<i16>().map_err(|_| {
|
||||
let error = json!({
|
||||
"status": "error",
|
||||
"message": "Invalid user id"
|
||||
|
||||
@@ -60,7 +60,7 @@ pub async fn create_user(
|
||||
)
|
||||
})?;
|
||||
|
||||
if let Some(_) = exist_check {
|
||||
if exist_check.is_some() {
|
||||
return Err((
|
||||
StatusCode::BAD_REQUEST,
|
||||
Json(json!({"status": "error", "message": "user already exists"})),
|
||||
@@ -90,10 +90,10 @@ pub async fn create_user(
|
||||
})?;
|
||||
|
||||
if user.rows_affected() < 1 {
|
||||
return Err((
|
||||
Err((
|
||||
StatusCode::INTERNAL_SERVER_ERROR,
|
||||
Json(json!({"status": "error", "message": "Error creating user"})),
|
||||
));
|
||||
))
|
||||
} else {
|
||||
Ok(Json(json!({"status": "success", "result": "User created"})))
|
||||
}
|
||||
@@ -151,7 +151,7 @@ pub async fn login(
|
||||
|
||||
let pwd_hash = PasswordHash::new(&user.pwd);
|
||||
let valid_pwd = Argon2::default()
|
||||
.verify_password(&request.pwd.as_bytes(), &pwd_hash.unwrap())
|
||||
.verify_password(request.pwd.as_bytes(), &pwd_hash.unwrap())
|
||||
.is_ok();
|
||||
|
||||
if !valid_pwd {
|
||||
@@ -340,7 +340,7 @@ pub async fn get_users(
|
||||
|
||||
let response = users
|
||||
.iter()
|
||||
.map(|user| filter_user(&user))
|
||||
.map(filter_user)
|
||||
.collect::<Vec<FilteredUser>>();
|
||||
let json_respnse = json!(response);
|
||||
Ok(Json(json_respnse))
|
||||
@@ -372,22 +372,22 @@ pub async fn get_user_by_id(
|
||||
match query {
|
||||
Ok(user) => {
|
||||
let response = serde_json::json!(filter_user(&user));
|
||||
return Ok(Json(response));
|
||||
Ok(Json(response))
|
||||
}
|
||||
Err(sqlx::Error::RowNotFound) => {
|
||||
let error_response = serde_json::json!({
|
||||
"status": "fail",
|
||||
"message": format!("User with ID {} not found", id)
|
||||
});
|
||||
return Err((StatusCode::NOT_FOUND, Json(error_response)));
|
||||
Err((StatusCode::NOT_FOUND, Json(error_response)))
|
||||
}
|
||||
Err(e) => {
|
||||
return Err((
|
||||
Err((
|
||||
StatusCode::INTERNAL_SERVER_ERROR,
|
||||
Json(json!({"status": "error", "message": format!("{:?}", e)})),
|
||||
));
|
||||
))
|
||||
}
|
||||
};
|
||||
}
|
||||
}
|
||||
|
||||
/// Updates an existing user's information.
|
||||
@@ -409,7 +409,7 @@ pub async fn get_user_by_id(
|
||||
/// # Security Note
|
||||
/// - Passwords are hashed using Argon2 before storage.
|
||||
/// - This endpoint requires admin privileges (enforced by middleware via
|
||||
/// [`validate_admin`](crate::cookie::validation::validate_admin)).
|
||||
/// [`validate_admin`](crate::cookie::validation::validate_admin)).
|
||||
pub async fn update_user(
|
||||
Path(id): Path<i32>,
|
||||
State(data): State<Arc<AppState>>,
|
||||
@@ -572,10 +572,10 @@ pub async fn setup_initial_admin(
|
||||
})?;
|
||||
|
||||
if user.rows_affected() < 1 {
|
||||
return Err((
|
||||
Err((
|
||||
StatusCode::INTERNAL_SERVER_ERROR,
|
||||
Json(json!({"status": "error", "message": "Error creating admin user"})),
|
||||
));
|
||||
))
|
||||
} else {
|
||||
Ok(Json(
|
||||
json!({"status": "success", "result": "Admin user created"}),
|
||||
@@ -616,6 +616,6 @@ pub fn filter_user(user: &User) -> FilteredUser {
|
||||
first_name: user.first_name.clone(),
|
||||
last_name: user.last_name.clone(),
|
||||
username: user.username.clone(),
|
||||
is_admin: user.is_admin.clone(),
|
||||
is_admin: user.is_admin,
|
||||
}
|
||||
}
|
||||
|
||||
@@ -230,22 +230,22 @@ pub async fn get_ticket_by_id(
|
||||
user_last_name: row.get("last_name"),
|
||||
};
|
||||
let response = serde_json::json!(ticket_response);
|
||||
return Ok(Json(response));
|
||||
Ok(Json(response))
|
||||
}
|
||||
Err(sqlx::Error::RowNotFound) => {
|
||||
let error_response = serde_json::json!({
|
||||
"status": "fail",
|
||||
"message": format!("Ticket with ID {} not found", id)
|
||||
});
|
||||
return Err((StatusCode::NOT_FOUND, Json(error_response)));
|
||||
Err((StatusCode::NOT_FOUND, Json(error_response)))
|
||||
}
|
||||
Err(e) => {
|
||||
return Err((
|
||||
Err((
|
||||
StatusCode::INTERNAL_SERVER_ERROR,
|
||||
Json(json!({"status": "error", "message": format!("{:?}", e)})),
|
||||
));
|
||||
))
|
||||
}
|
||||
};
|
||||
}
|
||||
}
|
||||
|
||||
/// Updates a ticket's status.
|
||||
|
||||
@@ -64,7 +64,7 @@ async fn main() {
|
||||
let database_url = &env.db_url;
|
||||
|
||||
// Establish connection pool to PostgreSQL
|
||||
let pool = match PgPoolOptions::new().connect(&database_url).await {
|
||||
let pool = match PgPoolOptions::new().connect(database_url).await {
|
||||
Ok(pool) => {
|
||||
println!("Database connection successful");
|
||||
pool
|
||||
|
||||
Reference in New Issue
Block a user