fixed /users/create and /users/auth
This commit is contained in:
parent
0e96cb1bda
commit
41ece3063c
1 changed files with 7 additions and 12 deletions
19
src/main.rs
19
src/main.rs
|
@ -36,7 +36,7 @@ async fn root() -> &'static str {
|
||||||
"KOreader sync server"
|
"KOreader sync server"
|
||||||
}
|
}
|
||||||
|
|
||||||
async fn create_user(Json(payload): Json<User>) -> (StatusCode, String) {
|
async fn create_user(Json(payload): Json<User>) -> StatusCode {
|
||||||
let client = redis::Client::open("redis://127.0.0.1/").unwrap();
|
let client = redis::Client::open("redis://127.0.0.1/").unwrap();
|
||||||
let mut con = client.get_connection().unwrap();
|
let mut con = client.get_connection().unwrap();
|
||||||
|
|
||||||
|
@ -50,26 +50,21 @@ async fn create_user(Json(payload): Json<User>) -> (StatusCode, String) {
|
||||||
if does_exist == false {
|
if does_exist == false {
|
||||||
let _: () = con.set(&user_key, password).unwrap();
|
let _: () = con.set(&user_key, password).unwrap();
|
||||||
} else {
|
} else {
|
||||||
return (
|
return StatusCode::PAYMENT_REQUIRED;
|
||||||
StatusCode::PAYMENT_REQUIRED,
|
|
||||||
"Username is already registered.".to_owned(),
|
|
||||||
);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
(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 client = redis::Client::open("redis://127.0.0.1/").unwrap();
|
||||||
let mut con = client.get_connection().unwrap();
|
let mut con = client.get_connection().unwrap();
|
||||||
|
|
||||||
let username = headers["x-auth-user"].to_str().unwrap_or("");
|
let username = headers["x-auth-user"].to_str().unwrap_or("");
|
||||||
let password = headers["x-auth-key"].to_str().unwrap_or("");
|
let password = headers["x-auth-key"].to_str().unwrap_or("");
|
||||||
|
|
||||||
println!("AHA");
|
|
||||||
|
|
||||||
if username.is_empty() || password.is_empty() {
|
if username.is_empty() || password.is_empty() {
|
||||||
return (StatusCode::UNAUTHORIZED, "Unauthorized".to_owned());
|
return StatusCode::UNAUTHORIZED;
|
||||||
}
|
}
|
||||||
|
|
||||||
let user_key = format!("user:{username}:key");
|
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();
|
let redis_pw: String = con.get(&user_key).unwrap();
|
||||||
|
|
||||||
if password != redis_pw {
|
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() {}
|
async fn update_progress() {}
|
||||||
|
|
Loading…
Reference in a new issue