Something went wrong. Try again.
Game sync and live services for independent game developers (targeting itch.io)
Something went wrong. Try again.
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263use anyhow::{anyhow, Result};use chrono::{DateTime, Utc};use sqlx::SqlitePool;
pub struct OAuthStateStore { pool: SqlitePool,}
#[derive(Debug, sqlx::FromRow)]pub struct OAuthStateRow { #[allow(dead_code)] pub state: String, pub expires_at: DateTime<Utc>,}
impl OAuthStateStore { pub fn new(pool: SqlitePool) -> Self { Self { pool } }
pub async fn create(&self, state: &str, expires_at: DateTime<Utc>) -> Result<()> { sqlx::query( r#" INSERT INTO oauth_state (state, expires_at) VALUES (?, ?) "#, ) .bind(state) .bind(expires_at) .execute(&self.pool) .await?;
Ok(()) }
pub async fn validate_and_consume(&self, state: &str) -> Result<()> { let row: Option<OAuthStateRow> = sqlx::query_as( r#" SELECT state, expires_at FROM oauth_state WHERE state = ? "#, ) .bind(state) .fetch_optional(&self.pool) .await?;
let row = row.ok_or_else(|| anyhow!("Invalid or expired state"))?;
if row.expires_at < Utc::now() { sqlx::query("DELETE FROM oauth_state WHERE state = ?") .bind(state) .execute(&self.pool) .await?; return Err(anyhow!("State has expired")); }
sqlx::query("DELETE FROM oauth_state WHERE state = ?") .bind(state) .execute(&self.pool) .await?;
Ok(()) }}