db-lib now attempts to update tokens from failed update_jwt call

This commit is contained in:
shockrah 2020-12-28 23:03:13 -08:00
parent 7263ae0980
commit b351f63db5

View File

@ -1,15 +1,25 @@
use mysql_async::{params, Pool}; use mysql_async::{params, Pool};
use mysql_async::prelude::Queryable; use mysql_async::prelude::Queryable;
use mysql_async::error::Error; 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 conn = p.get_conn().await?;
let q = "INSERT INTO jwt (token) VALUES (:tk)"; let _ = conn.drop_exec("UPDATE jwt SET token = :tk WHERE id = :id",
let _ = conn.drop_exec(q, params!{"tk" => token}).await?; params!{"tk" => token, "id" => id}).await?;
Ok(()) Ok(())
} }
pub async fn listed_jwt(p: &Pool, id: crate::UBigInt, token_given: &str) -> Result<bool, Error> { 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<bool, Error> {
// only checks if the given token is listed somewhere in the db // only checks if the given token is listed somewhere in the db
let conn = p.get_conn().await?; let conn = p.get_conn().await?;
let q = "SELECT token FROM jwt WHERE id = :id"; let q = "SELECT token FROM jwt WHERE id = :id";