diff --git a/server/src/auth.rs b/server/src/auth.rs index 05abb9b..76055fb 100644 --- a/server/src/auth.rs +++ b/server/src/auth.rs @@ -1,10 +1,12 @@ use mysql_async::Conn; use mysql_async::prelude::{params, Queryable}; -use hyper::{Response, Body}; use std::collections::HashMap; +use crate::routes; + pub enum AuthReason { - Good, + Good, //passed regular check + OpenAuth, // route does not require auth LimitPassed, NoKey, InternalFailure, @@ -24,25 +26,31 @@ fn check_key_row(row: Option<(String, i32, i32)>) -> AuthReason { None => NoKey } } -pub async fn wall_entry(conn: Conn, params: &HashMap<&str, &str>) -> AuthReason { - // Start by Checking if the api key is in our keystore - if let Some(key) = params.get("key") { - // (id, limit, current counter) - let db_request: Result<(Conn, Option<(String, i32, i32)>), mysql_async::error::Error> = conn - .first_exec(r"SELECT * FROM keys WHERE id = :id ", mysql_async::params!{ "id" => key}) - .await; - - // Error case should probably have some kind of error checking - match db_request { - Ok(db_tup) => check_key_row(db_tup.1), - Err(_) => AuthReason::InternalFailure - } - } - else { - AuthReason::NoKey - } + +fn open_route(path: &str) -> bool { + return path == routes::INVITE_JOIN } -pub fn wall_failure(resp: &Response) { - unimplemented!() -} \ No newline at end of file +pub async fn wall_entry(path: &str, conn: Conn, params: &HashMap<&str, &str>) -> AuthReason { + // Start by Checking if the api key is in our keystore + if open_route(path) { + AuthReason::OpenAuth + } + else { + if let Some(key) = params.get("key") { + // (id, limit, current counter) + let db_request: Result<(Conn, Option<(String, i32, i32)>), mysql_async::error::Error> = conn + .first_exec(r"SELECT * FROM keys WHERE id = :id ", mysql_async::params!{ "id" => key}) + .await; + + // Error case should probably have some kind of error checking + match db_request { + Ok(db_tup) => check_key_row(db_tup.1), + Err(_) => AuthReason::InternalFailure + } + } + else { + AuthReason::NoKey + } + } +}