From b2a6da25617c41b5006a8269a77618cb676c9a26 Mon Sep 17 00:00:00 2001 From: shockrah Date: Tue, 2 Jun 2020 17:05:54 -0700 Subject: [PATCH] auth::wall_entry now follows the same error system as invites module Result of function forwards to its caller so we dont deal w/ mysql so much --- server/src/auth.rs | 19 ++++++++----------- 1 file changed, 8 insertions(+), 11 deletions(-) diff --git a/server/src/auth.rs b/server/src/auth.rs index 76055fb..85107c2 100644 --- a/server/src/auth.rs +++ b/server/src/auth.rs @@ -1,4 +1,4 @@ -use mysql_async::Conn; +use mysql_async::{Conn, Pool}; use mysql_async::prelude::{params, Queryable}; use std::collections::HashMap; @@ -9,7 +9,6 @@ pub enum AuthReason { OpenAuth, // route does not require auth LimitPassed, NoKey, - InternalFailure, } fn check_key_row(row: Option<(String, i32, i32)>) -> AuthReason { @@ -31,26 +30,24 @@ fn open_route(path: &str) -> bool { return path == routes::INVITE_JOIN } -pub async fn wall_entry(path: &str, conn: Conn, params: &HashMap<&str, &str>) -> AuthReason { +pub async fn wall_entry(path: &str, pool: &Pool, params: &HashMap<&str, &str>) -> Result { // Start by Checking if the api key is in our keystore if open_route(path) { - AuthReason::OpenAuth + Ok(AuthReason::OpenAuth) } else { if let Some(key) = params.get("key") { + let conn = pool.get_conn().await?; // (id, limit, current counter) - let db_request: Result<(Conn, Option<(String, i32, i32)>), mysql_async::error::Error> = conn + let db_tup: (Conn, Option<(String, i32, i32)>) = conn .first_exec(r"SELECT * FROM keys WHERE id = :id ", mysql_async::params!{ "id" => key}) - .await; + .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 - } + Ok(check_key_row(db_tup.1)) } else { - AuthReason::NoKey + Ok(AuthReason::NoKey) } } }