freechat/server/src/auth.rs
shockrah b2a6da2561 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
2020-06-02 17:05:54 -07:00

54 lines
1.4 KiB
Rust

use mysql_async::{Conn, Pool};
use mysql_async::prelude::{params, Queryable};
use std::collections::HashMap;
use crate::routes;
pub enum AuthReason {
Good, //passed regular check
OpenAuth, // route does not require auth
LimitPassed,
NoKey,
}
fn check_key_row(row: Option<(String, i32, i32)>) -> AuthReason {
use self::AuthReason::*;
match row {
Some(data) => {
if data.2 > data.1 {
LimitPassed
}
else {
Good
}
},
None => NoKey
}
}
fn open_route(path: &str) -> bool {
return path == routes::INVITE_JOIN
}
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) {
Ok(AuthReason::OpenAuth)
}
else {
if let Some(key) = params.get("key") {
let conn = pool.get_conn().await?;
// (id, limit, current counter)
let db_tup: (Conn, Option<(String, i32, i32)>) = 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
Ok(check_key_row(db_tup.1))
}
else {
Ok(AuthReason::NoKey)
}
}
}