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