diff --git a/unshell-server/src/api/app.rs b/unshell-server/src/api/app.rs index 59f01b2..014b0ba 100644 --- a/unshell-server/src/api/app.rs +++ b/unshell-server/src/api/app.rs @@ -29,7 +29,7 @@ pub async fn start_api(address: &str) { pub async fn protected( Path(path): Path, - Extension(currentUser): Extension, + Extension(_currentUser): Extension, ) -> impl IntoResponse { info!("{}", path); // Json(UserResponse { diff --git a/unshell-server/src/api/auth.rs b/unshell-server/src/api/auth.rs index 3acbdb6..c7dba3b 100644 --- a/unshell-server/src/api/auth.rs +++ b/unshell-server/src/api/auth.rs @@ -8,7 +8,7 @@ use bcrypt::{DEFAULT_COST, hash, verify}; use chrono::Utc; use jsonwebtoken::{Header, TokenData, Validation, decode, encode}; use serde_json::{Value, json}; -use unshell_lib::info; +use unshell_lib::{debug, info}; use crate::api::{ EXPIRE_DURATION, JWT_DECODING_KEY, JWT_ENCODING_KEY, @@ -86,7 +86,13 @@ pub async fn sign_in(Json(user_data): Json) -> Result, S // 1. Retrieve user from the database let user = match retrieve_user_by_email(&user_data.username) { 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 @@ -94,6 +100,7 @@ pub async fn sign_in(Json(user_data): Json) -> Result, S .map_err(|_| StatusCode::INTERNAL_SERVER_ERROR)? // Handle bcrypt errors { + debug!("Denied user {}: Incorrect password hash", user.username); return Err(StatusCode::UNAUTHORIZED); // Wrong password } @@ -104,7 +111,7 @@ pub async fn sign_in(Json(user_data): Json) -> Result, S // 3. Generate JWT 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 Ok(Json(json!({ @@ -115,9 +122,7 @@ pub async fn sign_in(Json(user_data): Json) -> Result, S fn retrieve_user_by_email(_email: &str) -> Option { let current_user: CurrentUser = CurrentUser { - email: "foo".to_string(), - first_name: "Eze".to_string(), - last_name: "Sunday".to_string(), + username: "foo".to_string(), password_hash: hash_password("bar").unwrap(), }; Some(current_user) diff --git a/unshell-server/src/api/mod.rs b/unshell-server/src/api/mod.rs index 07b7168..62d4f01 100644 --- a/unshell-server/src/api/mod.rs +++ b/unshell-server/src/api/mod.rs @@ -6,7 +6,6 @@ extern crate unshell_lib; pub mod app; mod auth; mod structs; -mod userdata; static EXPIRE_DURATION: Duration = Duration::hours(12); diff --git a/unshell-server/src/api/structs.rs b/unshell-server/src/api/structs.rs index 22dc14c..326a1a2 100644 --- a/unshell-server/src/api/structs.rs +++ b/unshell-server/src/api/structs.rs @@ -15,9 +15,7 @@ pub struct SignInData { #[derive(Debug, Clone)] pub struct CurrentUser { - pub email: String, - pub first_name: String, - pub last_name: String, + pub username: String, pub password_hash: String, } diff --git a/unshell-server/src/api/userdata.rs b/unshell-server/src/api/userdata.rs deleted file mode 100644 index 5bd98f9..0000000 --- a/unshell-server/src/api/userdata.rs +++ /dev/null @@ -1,4 +0,0 @@ -pub struct UserData { - username: String, - hash: Vec, -} diff --git a/unshell-server/src/lib.rs b/unshell-server/src/lib.rs index 3f25ad4..b9ddaf5 100644 --- a/unshell-server/src/lib.rs +++ b/unshell-server/src/lib.rs @@ -1,4 +1,6 @@ // #![macro_use] +#[cfg(feature = "run")] mod api; +#[cfg(feature = "run")] pub use api::app::start_api; diff --git a/unshell-server/src/main.rs b/unshell-server/src/main.rs index 5e20193..2c83bf3 100644 --- a/unshell-server/src/main.rs +++ b/unshell-server/src/main.rs @@ -1,7 +1,3 @@ -use axum; -use tokio::net::TcpListener; -use unshell_lib::info; - use unshell_server::start_api; #[tokio::main]