From 967782be5fb8b030f3bef1ea8d07b1fb0f968225 Mon Sep 17 00:00:00 2001 From: shockrah Date: Mon, 28 Dec 2020 21:53:57 -0800 Subject: [PATCH] Movng back to mysql from redis due to tokio reactor errors At some point we should try moving to redis for better cache performance but that change is massive and this works at least as a poc --- server-api/db/src/auth.rs | 43 ++++++++++++++++++--------------------- 1 file changed, 20 insertions(+), 23 deletions(-) diff --git a/server-api/db/src/auth.rs b/server-api/db/src/auth.rs index aeba8ec..e31e1f1 100644 --- a/server-api/db/src/auth.rs +++ b/server-api/db/src/auth.rs @@ -1,28 +1,25 @@ -use crate::UBigInt; +use mysql_async::{params, Pool}; +use mysql_async::prelude::Queryable; +use mysql_async::error::Error; -use redis::{ - Client, AsyncCommands, - RedisResult -}; - -lazy_static! { - static ref REDIS_URL: String = { - std::env::var("REDIS_URL").unwrap() - }; -} - -pub async fn add_jwt(id: UBigInt, token: &str) -> RedisResult<()> { - let client = Client::open(REDIS_URL.as_ref())?; - let mut conn = client.get_tokio_connection().await?; - - let _ = conn.set(id, token).await?; +pub async fn add_jwt(p: &Pool, token: &str) -> Result<(), Error> { + let conn = p.get_conn().await?; + let q = "INSERT INTO jwt (token) VALUES (:tk)"; + let _ = conn.drop_exec(q, params!{"tk" => token}).await?; Ok(()) } -pub async fn active_jwt(id: UBigInt, token: &str) -> RedisResult { - let client = Client::open(REDIS_URL.as_ref())?; - let mut conn = client.get_tokio_connection().await?; - - let val: String = conn.get(id).await?; - Ok(val == token) +pub async fn listed_jwt(p: &Pool, id: crate::UBigInt, token_given: &str) -> Result { + // only checks if the given token is listed somewhere in the db + let conn = p.get_conn().await?; + let q = "SELECT token FROM jwt WHERE id = :id"; + // if id.token == return true + let (_, db_t): (_, Option) = + conn.first_exec(q, params!{"id" => id}).await?; + + + return match db_t { + Some(token_db) => Ok(token_db == token_given), // probably pointless check but its not that expensive so its stays as a sanity check + None => Ok(false) + }; } \ No newline at end of file