Add a timer to revoke all expired tokens( Fix #279

This commit is contained in:
2022-09-29 01:30:03 +00:00
committed by GitHub
parent 677cc576cf
commit fcff540220
8 changed files with 80 additions and 5 deletions

View File

@@ -99,6 +99,7 @@ impl PixivDownloaderSqlite {
Ok(())
}
#[cfg(feature = "server")]
fn _add_token(
tx: &Transaction,
user_id: u64,
@@ -113,6 +114,7 @@ impl PixivDownloaderSqlite {
Ok(())
}
#[cfg(feature = "server")]
fn _add_user(
tx: &Transaction,
name: &str,
@@ -159,7 +161,11 @@ impl PixivDownloaderSqlite {
tx.execute(TOKEN_TABLE, [])?;
}
if db_version < [1, 0, 0, 4] {
tx.execute("DROP TABLE authors, files, tags, token, users;", [])?;
tx.execute("DROP TABLE authors;", [])?;
tx.execute("DROP TABLE files;", [])?;
tx.execute("DROP TABLE tags;", [])?;
tx.execute("DROP TABLE token;", [])?;
tx.execute("DROP TABLE users;", [])?;
tx.execute(AUTHORS_TABLE, [])?;
tx.execute(FILES_TABLE, [])?;
tx.execute(TAGS_TABLE, [])?;
@@ -317,6 +323,12 @@ impl PixivDownloaderSqlite {
}
}
#[cfg(feature = "server")]
fn _revoke_expired_tokens(ts: &Transaction) -> Result<usize, SqliteError> {
let now = Utc::now();
Ok(ts.execute("DELETE FROM token WHERE expired_at < ?;", [now])?)
}
#[cfg(feature = "server")]
async fn _set_user(
&self,
@@ -515,6 +527,15 @@ impl PixivDownloaderDb for PixivDownloaderSqlite {
Ok(())
}
#[cfg(feature = "server")]
async fn revoke_expired_tokens(&self) -> Result<usize, PixivDownloaderDbError> {
let mut db = self.db.lock().await;
let mut tx = db.transaction()?;
let size = Self::_revoke_expired_tokens(&mut tx)?;
tx.commit()?;
Ok(size)
}
#[cfg(feature = "server")]
async fn set_user(
&self,

View File

@@ -72,6 +72,10 @@ pub trait PixivDownloaderDb {
/// Initialize the database (create tables, migrate data, etc.)
async fn init(&self) -> Result<(), PixivDownloaderDbError>;
#[cfg(feature = "server")]
/// Remove all expired tokens
/// Return the number of removed tokens
async fn revoke_expired_tokens(&self) -> Result<usize, PixivDownloaderDbError>;
#[cfg(feature = "server")]
/// Set a user's information by ID
/// * `id`: The user's ID
/// * `name`: The user's name