Remove warnings and add some debug logging

This commit is contained in:
Michael Mikovsky
2025-11-30 17:45:32 -07:00
parent f85a0c0295
commit 22650e5668
7 changed files with 15 additions and 19 deletions
+1 -1
View File
@@ -29,7 +29,7 @@ pub async fn start_api(address: &str) {
pub async fn protected( pub async fn protected(
Path(path): Path<String>, Path(path): Path<String>,
Extension(currentUser): Extension<CurrentUser>, Extension(_currentUser): Extension<CurrentUser>,
) -> impl IntoResponse { ) -> impl IntoResponse {
info!("{}", path); info!("{}", path);
// Json(UserResponse { // Json(UserResponse {
+11 -6
View File
@@ -8,7 +8,7 @@ use bcrypt::{DEFAULT_COST, hash, verify};
use chrono::Utc; use chrono::Utc;
use jsonwebtoken::{Header, TokenData, Validation, decode, encode}; use jsonwebtoken::{Header, TokenData, Validation, decode, encode};
use serde_json::{Value, json}; use serde_json::{Value, json};
use unshell_lib::info; use unshell_lib::{debug, info};
use crate::api::{ use crate::api::{
EXPIRE_DURATION, JWT_DECODING_KEY, JWT_ENCODING_KEY, EXPIRE_DURATION, JWT_DECODING_KEY, JWT_ENCODING_KEY,
@@ -86,7 +86,13 @@ pub async fn sign_in(Json(user_data): Json<SignInData>) -> Result<Json<Value>, S
// 1. Retrieve user from the database // 1. Retrieve user from the database
let user = match retrieve_user_by_email(&user_data.username) { let user = match retrieve_user_by_email(&user_data.username) {
Some(user) => user, Some(user) => user,
None => return Err(StatusCode::UNAUTHORIZED), // User not found None => {
debug!(
"Denied user {}: Could not find user data",
user_data.username
);
return Err(StatusCode::UNAUTHORIZED);
} // User not found
}; };
// 2. Compare the password // 2. Compare the password
@@ -94,6 +100,7 @@ pub async fn sign_in(Json(user_data): Json<SignInData>) -> Result<Json<Value>, S
.map_err(|_| StatusCode::INTERNAL_SERVER_ERROR)? .map_err(|_| StatusCode::INTERNAL_SERVER_ERROR)?
// Handle bcrypt errors // Handle bcrypt errors
{ {
debug!("Denied user {}: Incorrect password hash", user.username);
return Err(StatusCode::UNAUTHORIZED); // Wrong password return Err(StatusCode::UNAUTHORIZED); // Wrong password
} }
@@ -104,7 +111,7 @@ pub async fn sign_in(Json(user_data): Json<SignInData>) -> Result<Json<Value>, S
// 3. Generate JWT // 3. Generate JWT
let (token, experation) = let (token, experation) =
encode_jwt(user.email).map_err(|_| StatusCode::INTERNAL_SERVER_ERROR)?; encode_jwt(user.username).map_err(|_| StatusCode::INTERNAL_SERVER_ERROR)?;
// 4. Return the token // 4. Return the token
Ok(Json(json!({ Ok(Json(json!({
@@ -115,9 +122,7 @@ pub async fn sign_in(Json(user_data): Json<SignInData>) -> Result<Json<Value>, S
fn retrieve_user_by_email(_email: &str) -> Option<CurrentUser> { fn retrieve_user_by_email(_email: &str) -> Option<CurrentUser> {
let current_user: CurrentUser = CurrentUser { let current_user: CurrentUser = CurrentUser {
email: "foo".to_string(), username: "foo".to_string(),
first_name: "Eze".to_string(),
last_name: "Sunday".to_string(),
password_hash: hash_password("bar").unwrap(), password_hash: hash_password("bar").unwrap(),
}; };
Some(current_user) Some(current_user)
-1
View File
@@ -6,7 +6,6 @@ extern crate unshell_lib;
pub mod app; pub mod app;
mod auth; mod auth;
mod structs; mod structs;
mod userdata;
static EXPIRE_DURATION: Duration = Duration::hours(12); static EXPIRE_DURATION: Duration = Duration::hours(12);
+1 -3
View File
@@ -15,9 +15,7 @@ pub struct SignInData {
#[derive(Debug, Clone)] #[derive(Debug, Clone)]
pub struct CurrentUser { pub struct CurrentUser {
pub email: String, pub username: String,
pub first_name: String,
pub last_name: String,
pub password_hash: String, pub password_hash: String,
} }
-4
View File
@@ -1,4 +0,0 @@
pub struct UserData {
username: String,
hash: Vec<u8>,
}
+2
View File
@@ -1,4 +1,6 @@
// #![macro_use] // #![macro_use]
#[cfg(feature = "run")]
mod api; mod api;
#[cfg(feature = "run")]
pub use api::app::start_api; pub use api::app::start_api;
-4
View File
@@ -1,7 +1,3 @@
use axum;
use tokio::net::TcpListener;
use unshell_lib::info;
use unshell_server::start_api; use unshell_server::start_api;
#[tokio::main] #[tokio::main]