diff --git a/server-api/src/auth.rs b/server-api/src/auth.rs index ba1f371..a4b77b6 100644 --- a/server-api/src/auth.rs +++ b/server-api/src/auth.rs @@ -1,10 +1,14 @@ use bcrypt; -use mysql_async::{Conn, Pool}; -use mysql_async::prelude::{params, Queryable}; +use mysql_async::{Pool}; +use mysql_async::error::Error as SqlError; + use crate::db_types::{BigInt, Integer, UBigInt, VarChar}; use crate::routes; +use db::{member::Member, common::FromDB}; +use db::Response; + // used when we create a new users for the first time pub const BCRYPT_COST: u32 = 14; pub enum AuthReason { @@ -38,7 +42,7 @@ fn valid_perms(user_opt: &Option<(VarChar, VarChar, BigInt, Integer, UBigInt)>, return false; } -pub async fn wall_entry(path: &str, pool: &Pool, params: &serde_json::Value) -> Result { +pub async fn wall_entry(path: &str, pool: &Pool, params: &serde_json::Value) -> Result { // Start by Checking if the api key is in our keystore if routes::is_open(path) { Ok(AuthReason::OpenAuth) @@ -51,18 +55,21 @@ pub async fn wall_entry(path: &str, pool: &Pool, params: &serde_json::Value) -> (Some(id_v), Some(secret_v)) => { /* unwrapping because i couldn't care less about poorly formatted request data */ - let id = id_v.as_u64().unwrap(); - let secret = secret_v.as_str().unwrap(); - let conn = pool.get_conn().await?; - let db_tup: (Conn, Option<(VarChar, VarChar, BigInt, Integer, UBigInt)>) = conn.first_exec( - "SELECT secret, name, joindate, status, permissions FROM members WHERE id = :id", - mysql_async::params!{"id" => id}).await?; - let user_data = &db_tup.1; - if valid_user(secret, user_data) && valid_perms(user_data, path) { - Ok(AuthReason::Good) - } - else { - Ok(AuthReason::BadKey) + let id = id_v.as_u64().unwrap_or(0); // basically nobody is allowed to have 0 as its supposed to be reserved + let secret = secret_v.as_str().unwrap_or(""); + use std::borrow::Cow; + return match Member::get(pool, id).await { + Response::Row(user) => { + if user.secret == secret { + Ok(AuthReason::Good) + } + else { + Ok(AuthReason::BadKey) + } + }, + Response::Empty => Ok(AuthReason::BadKey), + Response::Other(err) => Err(SqlError::Other(Cow::from(err))), + _ => Err(SqlError::Other(Cow::from("Undefined result"))) } }, _ => {