Added prune cron script.

This commit is contained in:
flash 2024-09-03 20:16:50 +00:00
parent dcb5cf0175
commit 2ba03885ed
6 changed files with 54 additions and 6 deletions

View file

@ -68,6 +68,11 @@ class HanyuuContext {
return new FsDbMigrationRepo(HAU_DIR_MIGRATIONS); return new FsDbMigrationRepo(HAU_DIR_MIGRATIONS);
} }
public function pruneExpired($logAction = null): void {
$logAction ??= function() {};
$this->oauth2Ctx->pruneExpired($logAction);
}
public function getAuthInfo(): MisuzuAuthInfo { public function getAuthInfo(): MisuzuAuthInfo {
return $this->authInfo; return $this->authInfo;
} }

View file

@ -98,4 +98,8 @@ class OAuth2AuthorisationData {
$stmt->addParameter(++$args, $value); $stmt->addParameter(++$args, $value);
$stmt->execute(); $stmt->execute();
} }
public function pruneExpiredAuthorisations(): int {
return (int)$this->dbConn->execute('DELETE FROM hau_oauth2_authorise WHERE auth_expires <= NOW() - INTERVAL 1 HOUR');
}
} }

View file

@ -41,6 +41,26 @@ class OAuth2Context {
return $this->devices; return $this->devices;
} }
public function pruneExpired($logAction = null): void {
$logAction ??= function() {};
$logAction('Pruning expired refresh tokens...');
$pruned = $this->tokens->pruneExpiredRefresh();
$logAction(' Removed %d!', $pruned);
$logAction('Pruning expired access tokens...');
$pruned = $this->tokens->pruneExpiredAccess();
$logAction(' Removed %d!', $pruned);
$logAction('Pruning expired device authorisation requests...');
$pruned = $this->devices->pruneExpiredDevices();
$logAction(' Removed %d!', $pruned);
$logAction('Pruning expired authorisation codes...');
$pruned = $this->authorisations->pruneExpiredAuthorisations();
$logAction(' Removed %d!', $pruned);
}
public function createRefresh( public function createRefresh(
AppInfo $appInfo, AppInfo $appInfo,
OAuth2AccessInfo $accessInfo OAuth2AccessInfo $accessInfo

View file

@ -11,11 +11,11 @@ use Hanyuu\Apps\AppInfo;
class OAuth2DevicesData { class OAuth2DevicesData {
private const USER_CODE_CHARS = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ234567'; private const USER_CODE_CHARS = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ234567';
private IDbConnection $dbConn;
private DbStatementCache $cache; private DbStatementCache $cache;
public function __construct(IDbConnection $dbConn) { public function __construct(
$this->dbConn = $dbConn; private IDbConnection $dbConn
) {
$this->cache = new DbStatementCache($dbConn); $this->cache = new DbStatementCache($dbConn);
} }
@ -138,4 +138,8 @@ class OAuth2DevicesData {
$stmt->addParameter(2, $deviceInfo instanceof OAuth2DeviceInfo ? $deviceInfo->getId() : $deviceInfo); $stmt->addParameter(2, $deviceInfo instanceof OAuth2DeviceInfo ? $deviceInfo->getId() : $deviceInfo);
$stmt->execute(); $stmt->execute();
} }
public function pruneExpiredDevices(): int {
return (int)$this->dbConn->execute('DELETE FROM hau_oauth2_device WHERE dev_expires <= NOW() - INTERVAL 1 HOUR');
}
} }

View file

@ -9,11 +9,11 @@ use Index\Data\IDbConnection;
use Hanyuu\Apps\AppInfo; use Hanyuu\Apps\AppInfo;
class OAuth2TokensData { class OAuth2TokensData {
private IDbConnection $dbConn;
private DbStatementCache $cache; private DbStatementCache $cache;
public function __construct(IDbConnection $dbConn) { public function __construct(
$this->dbConn = $dbConn; private IDbConnection $dbConn
) {
$this->cache = new DbStatementCache($dbConn); $this->cache = new DbStatementCache($dbConn);
} }
@ -94,6 +94,10 @@ class OAuth2TokensData {
$stmt->execute(); $stmt->execute();
} }
public function pruneExpiredAccess(): int {
return (int)$this->dbConn->execute('DELETE FROM hau_oauth2_access WHERE acc_expires <= NOW() - INTERVAL 1 DAY');
}
public const REFRESH_BY_ID = 'id'; public const REFRESH_BY_ID = 'id';
public const REFRESH_BY_ACCESS = 'access'; public const REFRESH_BY_ACCESS = 'access';
public const REFRESH_BY_TOKEN = 'token'; public const REFRESH_BY_TOKEN = 'token';
@ -185,4 +189,8 @@ class OAuth2TokensData {
$stmt->addParameter(++$args, $value); $stmt->addParameter(++$args, $value);
$stmt->execute(); $stmt->execute();
} }
public function pruneExpiredRefresh(): int {
return (int)$this->dbConn->execute('DELETE FROM hau_oauth2_refresh WHERE ref_expires <= NOW()');
}
} }

7
tools/prune Executable file
View file

@ -0,0 +1,7 @@
#!/usr/bin/env php
<?php
require_once __DIR__ . '/../hanyuu.php';
$hau->pruneExpired(function(string $format, ...$args) {
echo sprintf($format, ...$args) . PHP_EOL;
});