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
This commit is contained in:
shockrah 2020-06-02 17:05:54 -07:00
parent 2400b89b12
commit b2a6da2561

View File

@ -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<AuthReason, mysql_async::error::Error> {
// 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)
}
}
}