first sampling of what the lib migration interactions will look like

This commit is contained in:
shockrah 2020-10-10 19:53:13 -07:00
parent 41c28cc845
commit dfc9f88e66

View File

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