From 41ece3063cf201ba4069ae4d9e25d086826086e9 Mon Sep 17 00:00:00 2001 From: Martin Brodbeck Date: Tue, 26 Sep 2023 12:40:52 +0200 Subject: [PATCH] fixed /users/create and /users/auth --- src/main.rs | 19 +++++++------------ 1 file changed, 7 insertions(+), 12 deletions(-) diff --git a/src/main.rs b/src/main.rs index c56d98b..2133644 100644 --- a/src/main.rs +++ b/src/main.rs @@ -36,7 +36,7 @@ async fn root() -> &'static str { "KOreader sync server" } -async fn create_user(Json(payload): Json) -> (StatusCode, String) { +async fn create_user(Json(payload): Json) -> StatusCode { let client = redis::Client::open("redis://127.0.0.1/").unwrap(); let mut con = client.get_connection().unwrap(); @@ -50,26 +50,21 @@ async fn create_user(Json(payload): Json) -> (StatusCode, String) { if does_exist == false { let _: () = con.set(&user_key, password).unwrap(); } else { - return ( - StatusCode::PAYMENT_REQUIRED, - "Username is already registered.".to_owned(), - ); + return StatusCode::PAYMENT_REQUIRED; } - (StatusCode::CREATED, format!("username = {username}")) + StatusCode::CREATED } -async fn auth_user(headers: HeaderMap) -> (StatusCode, String) { +async fn auth_user(headers: HeaderMap) -> StatusCode { let client = redis::Client::open("redis://127.0.0.1/").unwrap(); let mut con = client.get_connection().unwrap(); let username = headers["x-auth-user"].to_str().unwrap_or(""); let password = headers["x-auth-key"].to_str().unwrap_or(""); - println!("AHA"); - if username.is_empty() || password.is_empty() { - return (StatusCode::UNAUTHORIZED, "Unauthorized".to_owned()); + return StatusCode::UNAUTHORIZED; } let user_key = format!("user:{username}:key"); @@ -77,10 +72,10 @@ async fn auth_user(headers: HeaderMap) -> (StatusCode, String) { let redis_pw: String = con.get(&user_key).unwrap(); if password != redis_pw { - return (StatusCode::UNAUTHORIZED, "Unauthorized".to_owned()); + return StatusCode::UNAUTHORIZED; } - (StatusCode::OK, "authorized = 'OK'".to_owned()) + StatusCode::OK } async fn update_progress() {}