auth_user works
This commit is contained in:
parent
cdf909159d
commit
968af50352
1 changed files with 27 additions and 25 deletions
52
src/main.rs
52
src/main.rs
|
@ -9,7 +9,7 @@ use axum::{
|
||||||
};
|
};
|
||||||
use serde::{Deserialize, Serialize};
|
use serde::{Deserialize, Serialize};
|
||||||
use serde_json::{json, Value};
|
use serde_json::{json, Value};
|
||||||
use sqlx::{postgres::PgPoolOptions, PgPool};
|
use sqlx::{postgres::PgPoolOptions, PgExecutor, PgPool};
|
||||||
|
|
||||||
use std::env;
|
use std::env;
|
||||||
use std::time::{Duration, SystemTime, UNIX_EPOCH};
|
use std::time::{Duration, SystemTime, UNIX_EPOCH};
|
||||||
|
@ -92,10 +92,9 @@ async fn create_user(
|
||||||
.await
|
.await
|
||||||
.unwrap();
|
.unwrap();
|
||||||
|
|
||||||
let does_exist: bool = if row.0 == 1 { true } else { false };
|
let does_exist: bool = if row.0 >= 1 { true } else { false };
|
||||||
|
|
||||||
if does_exist == false {
|
if does_exist == false {
|
||||||
//let _: () = con.set(&user_key, password).unwrap();
|
|
||||||
sqlx::query("INSERT INTO users (username, password) VALUES ($1, $2)")
|
sqlx::query("INSERT INTO users (username, password) VALUES ($1, $2)")
|
||||||
.bind(&username)
|
.bind(&username)
|
||||||
.bind(&password)
|
.bind(&password)
|
||||||
|
@ -112,48 +111,51 @@ async fn create_user(
|
||||||
(StatusCode::CREATED, Json(json!({ "username" : username })))
|
(StatusCode::CREATED, Json(json!({ "username" : username })))
|
||||||
}
|
}
|
||||||
|
|
||||||
fn authorize(username: &str, password: &str) -> bool {
|
async fn authorize(db: impl PgExecutor<'_>, username: &str, password: &str) -> bool {
|
||||||
/*let client = redis::Client::open("redis://127.0.0.1/").unwrap();
|
|
||||||
let mut con = client.get_connection().unwrap();
|
|
||||||
|
|
||||||
if username.is_empty() || password.is_empty() {
|
if username.is_empty() || password.is_empty() {
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
let user_key = format!("user:{username}:key");
|
let row: Option<(String,)> = sqlx::query_as("SELECT password FROM users WHERE username = $1")
|
||||||
|
.bind(&username)
|
||||||
|
.fetch_optional(db)
|
||||||
|
.await
|
||||||
|
.unwrap();
|
||||||
|
|
||||||
let redis_pw: String = con.get(&user_key).unwrap();
|
if let Some(val) = row {
|
||||||
|
return password == val.0;
|
||||||
|
}
|
||||||
|
|
||||||
if password != redis_pw {
|
false
|
||||||
return false;
|
|
||||||
} */
|
|
||||||
|
|
||||||
true
|
|
||||||
}
|
}
|
||||||
|
|
||||||
async fn auth_user(headers: HeaderMap) -> (StatusCode, Json<Value>) {
|
async fn auth_user(State(db_pool): State<PgPool>, headers: HeaderMap) -> (StatusCode, Json<Value>) {
|
||||||
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("");
|
||||||
|
|
||||||
if authorize(&username, &password) == false {
|
let mut tx = db_pool.begin().await.unwrap();
|
||||||
return (
|
|
||||||
StatusCode::UNAUTHORIZED,
|
if authorize(&mut *tx, username, password).await == true {
|
||||||
Json(json!({"message" : "Unauthorized"})),
|
return (StatusCode::OK, Json(json!({"authorized" : "OK"})));
|
||||||
);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
(StatusCode::OK, Json(json!({"authorized" : "OK"})))
|
tx.commit().await.unwrap();
|
||||||
|
|
||||||
|
(
|
||||||
|
StatusCode::UNAUTHORIZED,
|
||||||
|
Json(json!({"message" : "Unauthorized"})),
|
||||||
|
)
|
||||||
}
|
}
|
||||||
|
|
||||||
async fn update_progress(headers: HeaderMap, Json(payload): Json<UpdateProgress>) -> StatusCode {
|
async fn update_progress(headers: HeaderMap, Json(payload): Json<UpdateProgress>) -> StatusCode {
|
||||||
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("");
|
||||||
|
|
||||||
if authorize(username, password) == false {
|
/*if authorize(username, password) == false {
|
||||||
return StatusCode::UNAUTHORIZED;
|
return StatusCode::UNAUTHORIZED;
|
||||||
}
|
}
|
||||||
|
|
||||||
/*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 timestamp = SystemTime::now()
|
let timestamp = SystemTime::now()
|
||||||
|
@ -186,11 +188,11 @@ async fn get_progress(
|
||||||
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("");
|
||||||
|
|
||||||
if authorize(username, password) == false {
|
/* if authorize(username, password) == false {
|
||||||
return (StatusCode::UNAUTHORIZED, Json(json!("")));
|
return (StatusCode::UNAUTHORIZED, Json(json!("")));
|
||||||
}
|
}
|
||||||
|
|
||||||
/* 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 doc_key = format!("user:{username}:document:{document}");
|
let doc_key = format!("user:{username}:document:{document}");
|
||||||
|
|
Loading…
Reference in a new issue