diff --git a/src/HanyuuContext.php b/src/HanyuuContext.php index 7a2298a..4e653ee 100644 --- a/src/HanyuuContext.php +++ b/src/HanyuuContext.php @@ -68,6 +68,11 @@ class HanyuuContext { return new FsDbMigrationRepo(HAU_DIR_MIGRATIONS); } + public function pruneExpired($logAction = null): void { + $logAction ??= function() {}; + $this->oauth2Ctx->pruneExpired($logAction); + } + public function getAuthInfo(): MisuzuAuthInfo { return $this->authInfo; } diff --git a/src/OAuth2/OAuth2AuthorisationData.php b/src/OAuth2/OAuth2AuthorisationData.php index df30d03..150510b 100644 --- a/src/OAuth2/OAuth2AuthorisationData.php +++ b/src/OAuth2/OAuth2AuthorisationData.php @@ -98,4 +98,8 @@ class OAuth2AuthorisationData { $stmt->addParameter(++$args, $value); $stmt->execute(); } + + public function pruneExpiredAuthorisations(): int { + return (int)$this->dbConn->execute('DELETE FROM hau_oauth2_authorise WHERE auth_expires <= NOW() - INTERVAL 1 HOUR'); + } } diff --git a/src/OAuth2/OAuth2Context.php b/src/OAuth2/OAuth2Context.php index 6f9c91e..908b6e1 100644 --- a/src/OAuth2/OAuth2Context.php +++ b/src/OAuth2/OAuth2Context.php @@ -41,6 +41,26 @@ class OAuth2Context { 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( AppInfo $appInfo, OAuth2AccessInfo $accessInfo diff --git a/src/OAuth2/OAuth2DevicesData.php b/src/OAuth2/OAuth2DevicesData.php index 6b97bb6..10c36f3 100644 --- a/src/OAuth2/OAuth2DevicesData.php +++ b/src/OAuth2/OAuth2DevicesData.php @@ -11,11 +11,11 @@ use Hanyuu\Apps\AppInfo; class OAuth2DevicesData { private const USER_CODE_CHARS = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ234567'; - private IDbConnection $dbConn; private DbStatementCache $cache; - public function __construct(IDbConnection $dbConn) { - $this->dbConn = $dbConn; + public function __construct( + private IDbConnection $dbConn + ) { $this->cache = new DbStatementCache($dbConn); } @@ -138,4 +138,8 @@ class OAuth2DevicesData { $stmt->addParameter(2, $deviceInfo instanceof OAuth2DeviceInfo ? $deviceInfo->getId() : $deviceInfo); $stmt->execute(); } + + public function pruneExpiredDevices(): int { + return (int)$this->dbConn->execute('DELETE FROM hau_oauth2_device WHERE dev_expires <= NOW() - INTERVAL 1 HOUR'); + } } diff --git a/src/OAuth2/OAuth2TokensData.php b/src/OAuth2/OAuth2TokensData.php index d11fa81..9d3e075 100644 --- a/src/OAuth2/OAuth2TokensData.php +++ b/src/OAuth2/OAuth2TokensData.php @@ -9,11 +9,11 @@ use Index\Data\IDbConnection; use Hanyuu\Apps\AppInfo; class OAuth2TokensData { - private IDbConnection $dbConn; private DbStatementCache $cache; - public function __construct(IDbConnection $dbConn) { - $this->dbConn = $dbConn; + public function __construct( + private IDbConnection $dbConn + ) { $this->cache = new DbStatementCache($dbConn); } @@ -94,6 +94,10 @@ class OAuth2TokensData { $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_ACCESS = 'access'; public const REFRESH_BY_TOKEN = 'token'; @@ -185,4 +189,8 @@ class OAuth2TokensData { $stmt->addParameter(++$args, $value); $stmt->execute(); } + + public function pruneExpiredRefresh(): int { + return (int)$this->dbConn->execute('DELETE FROM hau_oauth2_refresh WHERE ref_expires <= NOW()'); + } } diff --git a/tools/prune b/tools/prune new file mode 100755 index 0000000..d33446a --- /dev/null +++ b/tools/prune @@ -0,0 +1,7 @@ +#!/usr/bin/env php +pruneExpired(function(string $format, ...$args) { + echo sprintf($format, ...$args) . PHP_EOL; +});