<?php
namespace App\OAuth2\Repository; use App\OAuth2\Entity\AuthCodeEntity; use League\OAuth2\Server\Entities\AuthCodeEntityInterface; use League\OAuth2\Server\Exception\UniqueTokenIdentifierConstraintViolationException; use League\OAuth2\Server\Repositories\AuthCodeRepositoryInterface; class AuthCodeRepository implements AuthCodeRepositoryInterface { public function getNewAuthCode(): AuthCodeEntityInterface { return new AuthCodeEntity(); } public function persistNewAuthCode(AuthCodeEntityInterface $authCodeEntity): void { $scopes = implode(' ', array_map( static fn($scope) => $scope->getIdentifier(), $authCodeEntity->getScopes() )); try { $entity = \Entity\OAuth2AuthCode::insert(); $entity->codeId = $authCodeEntity->getIdentifier(); $entity->clientId = $authCodeEntity->getClient()->getIdentifier(); $entity->idUser = $authCodeEntity->getUserIdentifier(); $entity->scopes = $scopes; $entity->redirectUri = (string)$authCodeEntity->getRedirectUri(); $entity->expiresAt = $authCodeEntity->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 revokeAuthCode(string $codeId): void { global $DBTOauth2AuthCodes; $db = \DB::getInstance(); $ex = $db->Execute("UPDATE `{$DBTOauth2AuthCodes}` SET `isRevoked`=1 WHERE `codeId`=?", [$codeId]); if (!$ex) { throw new \DBException("OAuth2: could not revoke auth code."); } } public function isAuthCodeRevoked(string $codeId): bool { global $DBTOauth2AuthCodes; $db = \DB::getInstance(); $ex = $db->Execute("SELECT `isRevoked` FROM `{$DBTOauth2AuthCodes}` WHERE `codeId`=?", [$codeId]); if (!$ex) { throw new \DBException("OAuth2: could not check auth code revocation."); } $row = $ex->FetchRow(); if (!$row) { return true; } return (bool)$row['isRevoked']; } }