<?php
namespace App\OAuth2\Repository; use App\OAuth2\Entity\RefreshTokenEntity; use League\OAuth2\Server\Entities\RefreshTokenEntityInterface; use League\OAuth2\Server\Exception\UniqueTokenIdentifierConstraintViolationException; use League\OAuth2\Server\Repositories\RefreshTokenRepositoryInterface; class RefreshTokenRepository implements RefreshTokenRepositoryInterface { public function getNewRefreshToken(): ?RefreshTokenEntityInterface { return new RefreshTokenEntity(); } public function persistNewRefreshToken(RefreshTokenEntityInterface $refreshTokenEntity): void { try { $entity = \Entity\OAuth2RefreshToken::insert(); $entity->tokenId = $refreshTokenEntity->getIdentifier(); $entity->accessTokenId = $refreshTokenEntity->getAccessToken()->getIdentifier(); $entity->expiresAt = $refreshTokenEntity->getExpiryDateTime()->format('Y-m-d H:i:s'); $entity->isRevoked = 0; $entity->save(); } catch (\Throwable $e) { if (stripos($e->getMessage(), 'duplicate') !== false) { throw UniqueTokenIdentifierConstraintViolationException::create(); } throw $e; } } public function revokeRefreshToken(string $tokenId): void { global $DBTOauth2RefreshTokens; $db = \DB::getInstance(); $ex = $db->Execute("UPDATE `{$DBTOauth2RefreshTokens}` SET `isRevoked`=1 WHERE `tokenId`=?", [$tokenId]); if (!$ex) { throw new \DBException("OAuth2: could not revoke refresh token."); } } public function isRefreshTokenRevoked(string $tokenId): bool { global $DBTOauth2RefreshTokens; $db = \DB::getInstance(); $ex = $db->Execute("SELECT `isRevoked` FROM `{$DBTOauth2RefreshTokens}` WHERE `tokenId`=?", [$tokenId]); if (!$ex) { throw new \DBException("OAuth2: could not check refresh token revocation."); } $row = $ex->FetchRow(); if (!$row) { return true; } return (bool)$row['isRevoked']; } }