From b351f63db59e470e929fdab5c036d8fe2abbadac Mon Sep 17 00:00:00 2001 From: shockrah Date: Mon, 28 Dec 2020 23:03:13 -0800 Subject: [PATCH] db-lib now attempts to update tokens from failed update_jwt call --- server-api/db/src/auth.rs | 18 ++++++++++++++---- 1 file changed, 14 insertions(+), 4 deletions(-) diff --git a/server-api/db/src/auth.rs b/server-api/db/src/auth.rs index e31e1f1..5df7208 100644 --- a/server-api/db/src/auth.rs +++ b/server-api/db/src/auth.rs @@ -1,15 +1,25 @@ use mysql_async::{params, Pool}; use mysql_async::prelude::Queryable; use mysql_async::error::Error; +use crate::UBigInt; -pub async fn add_jwt(p: &Pool, token: &str) -> Result<(), Error> { +async fn update_jwt(p: &Pool, id: UBigInt, 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?; + let _ = conn.drop_exec("UPDATE jwt SET token = :tk WHERE id = :id", + params!{"tk" => token, "id" => id}).await?; Ok(()) } -pub async fn listed_jwt(p: &Pool, id: crate::UBigInt, token_given: &str) -> Result { +pub async fn add_jwt(p: &Pool, id: UBigInt, token: &str) -> Result<(), Error> { + let conn = p.get_conn().await?; + let q = "INSERT INTO jwt (id, token) VALUES (:id, :tk)"; + return match conn.prep_exec(q, params!{"tk" => token, "id" => id}).await { + Ok(_) => Ok(()), + Err(_) => update_jwt(p, id, token).await // attempt to refresh token + }; +} + +pub async fn listed_jwt(p: &Pool, id: 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";