diff --git a/src/Repositories/AbstractDatabaseRepository.php b/src/Repositories/AbstractDatabaseRepository.php index 832b5f05..61d8b416 100644 --- a/src/Repositories/AbstractDatabaseRepository.php +++ b/src/Repositories/AbstractDatabaseRepository.php @@ -15,24 +15,21 @@ */ namespace SimpleSAML\Module\oidc\Repositories; -use SimpleSAML\Configuration; use SimpleSAML\Database; use SimpleSAML\Module\oidc\ModuleConfig; +use SimpleSAML\Module\oidc\Utils\ProtocolCache; abstract class AbstractDatabaseRepository { - protected Configuration $config; - - protected Database $database; - /** * ClientRepository constructor. * @throws \Exception */ - public function __construct(protected ModuleConfig $moduleConfig) - { - $this->config = $this->moduleConfig->config(); - $this->database = Database::getInstance(); + public function __construct( + protected readonly ModuleConfig $moduleConfig, + protected readonly Database $database, + protected readonly ?ProtocolCache $protocolCache, + ) { } abstract public function getTableName(): ?string; diff --git a/src/Repositories/AccessTokenRepository.php b/src/Repositories/AccessTokenRepository.php index 357969b9..3e1ac577 100644 --- a/src/Repositories/AccessTokenRepository.php +++ b/src/Repositories/AccessTokenRepository.php @@ -20,6 +20,7 @@ use League\OAuth2\Server\Entities\AccessTokenEntityInterface as OAuth2AccessTokenEntityInterface; use League\OAuth2\Server\Entities\ClientEntityInterface as OAuth2ClientEntityInterface; use RuntimeException; +use SimpleSAML\Database; use SimpleSAML\Error\Error; use SimpleSAML\Module\oidc\Codebooks\DateFormatsEnum; use SimpleSAML\Module\oidc\Entities\AccessTokenEntity; @@ -30,6 +31,7 @@ use SimpleSAML\Module\oidc\Repositories\Interfaces\AccessTokenRepositoryInterface; use SimpleSAML\Module\oidc\Repositories\Traits\RevokeTokenByAuthCodeIdTrait; use SimpleSAML\Module\oidc\Server\Exceptions\OidcServerException; +use SimpleSAML\Module\oidc\Utils\ProtocolCache; class AccessTokenRepository extends AbstractDatabaseRepository implements AccessTokenRepositoryInterface { @@ -39,11 +41,13 @@ class AccessTokenRepository extends AbstractDatabaseRepository implements Access public function __construct( ModuleConfig $moduleConfig, + Database $database, + ?ProtocolCache $protocolCache, protected readonly ClientRepository $clientRepository, protected readonly AccessTokenEntityFactory $accessTokenEntityFactory, protected readonly Helpers $helpers, ) { - parent::__construct($moduleConfig); + parent::__construct($moduleConfig, $database, $protocolCache); } public function getTableName(): string diff --git a/src/Repositories/AuthCodeRepository.php b/src/Repositories/AuthCodeRepository.php index d737d6af..f1b95fba 100644 --- a/src/Repositories/AuthCodeRepository.php +++ b/src/Repositories/AuthCodeRepository.php @@ -18,6 +18,7 @@ use League\OAuth2\Server\Entities\AuthCodeEntityInterface as OAuth2AuthCodeEntityInterface; use RuntimeException; +use SimpleSAML\Database; use SimpleSAML\Error\Error; use SimpleSAML\Module\oidc\Codebooks\DateFormatsEnum; use SimpleSAML\Module\oidc\Entities\AuthCodeEntity; @@ -26,16 +27,19 @@ use SimpleSAML\Module\oidc\Helpers; use SimpleSAML\Module\oidc\ModuleConfig; use SimpleSAML\Module\oidc\Repositories\Interfaces\AuthCodeRepositoryInterface; +use SimpleSAML\Module\oidc\Utils\ProtocolCache; class AuthCodeRepository extends AbstractDatabaseRepository implements AuthCodeRepositoryInterface { public function __construct( ModuleConfig $moduleConfig, + Database $database, + ?ProtocolCache $protocolCache, protected readonly ClientRepository $clientRepository, protected readonly AuthCodeEntityFactory $authCodeEntityFactory, protected readonly Helpers $helpers, ) { - parent::__construct($moduleConfig); + parent::__construct($moduleConfig, $database, $protocolCache); } final public const TABLE_NAME = 'oidc_auth_code'; diff --git a/src/Repositories/ClientRepository.php b/src/Repositories/ClientRepository.php index 6235fdda..6d6cef2f 100644 --- a/src/Repositories/ClientRepository.php +++ b/src/Repositories/ClientRepository.php @@ -17,18 +17,22 @@ use League\OAuth2\Server\Repositories\ClientRepositoryInterface; use PDO; +use SimpleSAML\Database; use SimpleSAML\Module\oidc\Entities\ClientEntity; use SimpleSAML\Module\oidc\Entities\Interfaces\ClientEntityInterface; use SimpleSAML\Module\oidc\Factories\Entities\ClientEntityFactory; use SimpleSAML\Module\oidc\ModuleConfig; +use SimpleSAML\Module\oidc\Utils\ProtocolCache; class ClientRepository extends AbstractDatabaseRepository implements ClientRepositoryInterface { public function __construct( ModuleConfig $moduleConfig, + Database $database, + ?ProtocolCache $protocolCache, protected readonly ClientEntityFactory $clientEntityFactory, ) { - parent::__construct($moduleConfig); + parent::__construct($moduleConfig, $database, $protocolCache); } final public const TABLE_NAME = 'oidc_client'; @@ -389,7 +393,7 @@ private function count(string $query, ?string $owner): int */ private function getItemsPerPage(): int { - return $this->config + return $this->moduleConfig->config() ->getOptionalIntegerRange(ModuleConfig::OPTION_ADMIN_UI_PAGINATION_ITEMS_PER_PAGE, 1, 100, 20); } diff --git a/src/Repositories/RefreshTokenRepository.php b/src/Repositories/RefreshTokenRepository.php index ee025083..e20bb23c 100644 --- a/src/Repositories/RefreshTokenRepository.php +++ b/src/Repositories/RefreshTokenRepository.php @@ -19,6 +19,7 @@ use League\OAuth2\Server\Entities\RefreshTokenEntityInterface as OAuth2RefreshTokenEntityInterface; use League\OAuth2\Server\Exception\OAuthServerException; use RuntimeException; +use SimpleSAML\Database; use SimpleSAML\Module\oidc\Codebooks\DateFormatsEnum; use SimpleSAML\Module\oidc\Entities\Interfaces\RefreshTokenEntityInterface; use SimpleSAML\Module\oidc\Entities\RefreshTokenEntity; @@ -27,6 +28,7 @@ use SimpleSAML\Module\oidc\ModuleConfig; use SimpleSAML\Module\oidc\Repositories\Interfaces\RefreshTokenRepositoryInterface; use SimpleSAML\Module\oidc\Repositories\Traits\RevokeTokenByAuthCodeIdTrait; +use SimpleSAML\Module\oidc\Utils\ProtocolCache; class RefreshTokenRepository extends AbstractDatabaseRepository implements RefreshTokenRepositoryInterface { @@ -36,11 +38,13 @@ class RefreshTokenRepository extends AbstractDatabaseRepository implements Refre public function __construct( ModuleConfig $moduleConfig, + Database $database, + ?ProtocolCache $protocolCache, protected readonly AccessTokenRepository $accessTokenRepository, protected readonly RefreshTokenEntityFactory $refreshTokenEntityFactory, protected readonly Helpers $helpers, ) { - parent::__construct($moduleConfig); + parent::__construct($moduleConfig, $database, $protocolCache); } /** diff --git a/src/Repositories/ScopeRepository.php b/src/Repositories/ScopeRepository.php index ea06d406..e623bc89 100644 --- a/src/Repositories/ScopeRepository.php +++ b/src/Repositories/ScopeRepository.php @@ -26,18 +26,12 @@ use function array_key_exists; use function in_array; -class ScopeRepository extends AbstractDatabaseRepository implements ScopeRepositoryInterface +class ScopeRepository implements ScopeRepositoryInterface { public function __construct( - ModuleConfig $moduleConfig, + protected readonly ModuleConfig $moduleConfig, protected readonly ScopeEntityFactory $scopeEntityFactory, ) { - parent::__construct($moduleConfig); - } - - public function getTableName(): ?string - { - return null; } /** diff --git a/src/Repositories/UserRepository.php b/src/Repositories/UserRepository.php index 4c2772a7..61b7fcba 100644 --- a/src/Repositories/UserRepository.php +++ b/src/Repositories/UserRepository.php @@ -21,11 +21,13 @@ use League\OAuth2\Server\Entities\ClientEntityInterface as OAuth2ClientEntityInterface; use League\OAuth2\Server\Entities\UserEntityInterface; use League\OAuth2\Server\Repositories\UserRepositoryInterface; +use SimpleSAML\Database; use SimpleSAML\Module\oidc\Entities\UserEntity; use SimpleSAML\Module\oidc\Factories\Entities\UserEntityFactory; use SimpleSAML\Module\oidc\Helpers; use SimpleSAML\Module\oidc\ModuleConfig; use SimpleSAML\Module\oidc\Repositories\Interfaces\IdentityProviderInterface; +use SimpleSAML\Module\oidc\Utils\ProtocolCache; class UserRepository extends AbstractDatabaseRepository implements UserRepositoryInterface, IdentityProviderInterface { @@ -33,10 +35,12 @@ class UserRepository extends AbstractDatabaseRepository implements UserRepositor public function __construct( ModuleConfig $moduleConfig, + Database $database, + ?ProtocolCache $protocolCache, protected readonly Helpers $helpers, protected readonly UserEntityFactory $userEntityFactory, ) { - parent::__construct($moduleConfig); + parent::__construct($moduleConfig, $database, $protocolCache); } public function getTableName(): string diff --git a/src/Services/Container.php b/src/Services/Container.php index 95b26640..8d13c20a 100644 --- a/src/Services/Container.php +++ b/src/Services/Container.php @@ -204,7 +204,15 @@ public function __construct() ); $this->services[ClientEntityFactory::class] = $clientEntityFactory; - $clientRepository = new ClientRepository($moduleConfig, $clientEntityFactory); + $database = Database::getInstance(); + $this->services[Database::class] = $database; + + $clientRepository = new ClientRepository( + $moduleConfig, + $database, + $protocolCache, + $clientEntityFactory, + ); $this->services[ClientRepository::class] = $clientRepository; $userEntityFactory = new UserEntityFactory($helpers); @@ -212,6 +220,8 @@ public function __construct() $userRepository = new UserRepository( $moduleConfig, + $database, + $protocolCache, $helpers, $userEntityFactory, ); @@ -228,6 +238,8 @@ public function __construct() $authCodeRepository = new AuthCodeRepository( $moduleConfig, + $database, + $protocolCache, $clientRepository, $authCodeEntityFactory, $helpers, @@ -252,6 +264,8 @@ public function __construct() $accessTokenRepository = new AccessTokenRepository( $moduleConfig, + $database, + $protocolCache, $clientRepository, $accessTokenEntityFactory, $helpers, @@ -263,6 +277,8 @@ public function __construct() $refreshTokenRepository = new RefreshTokenRepository( $moduleConfig, + $database, + $protocolCache, $accessTokenRepository, $refreshTokenEntityFactory, $helpers, @@ -272,12 +288,13 @@ public function __construct() $scopeRepository = new ScopeRepository($moduleConfig, $scopeEntityFactory); $this->services[ScopeRepository::class] = $scopeRepository; - $allowedOriginRepository = new AllowedOriginRepository($moduleConfig); + $allowedOriginRepository = new AllowedOriginRepository( + $moduleConfig, + $database, + $protocolCache, + ); $this->services[AllowedOriginRepository::class] = $allowedOriginRepository; - $database = Database::getInstance(); - $this->services[Database::class] = $database; - $databaseMigration = new DatabaseMigration($database); $this->services[DatabaseMigration::class] = $databaseMigration; diff --git a/tests/integration/src/Repositories/Traits/RevokeTokenByAuthCodeIdTraitTest.php b/tests/integration/src/Repositories/Traits/RevokeTokenByAuthCodeIdTraitTest.php index 12d4a27e..ddb89984 100644 --- a/tests/integration/src/Repositories/Traits/RevokeTokenByAuthCodeIdTraitTest.php +++ b/tests/integration/src/Repositories/Traits/RevokeTokenByAuthCodeIdTraitTest.php @@ -162,10 +162,19 @@ public function getDatabase(): Database $clientEntityFactoryMock = $this->createMock(ClientEntityFactory::class); $clientEntityFactoryMock->method('fromState')->willReturn($clientEntityMock); - $clientRepositoryMock = new ClientRepository($moduleConfig, $clientEntityFactoryMock); + $database = Database::getInstance(); + + $clientRepositoryMock = new ClientRepository( + $moduleConfig, + $database, + null, + $clientEntityFactoryMock + ); $this->accessTokenRepository = new AccessTokenRepository( $moduleConfig, + $database, + null, $clientRepositoryMock, $this->accessTokenEntityFactory, new Helpers(), @@ -180,6 +189,8 @@ public function getDatabase(): Database $user = new UserEntity(self::USER_ID, $createUpdatedAt, $createUpdatedAt, []); $userRepositoryMock = new UserRepository( $moduleConfig, + $database, + null, $helpers, new UserEntityFactory($helpers), ); diff --git a/tests/unit/src/Repositories/AccessTokenRepositoryTest.php b/tests/unit/src/Repositories/AccessTokenRepositoryTest.php index fab5514c..7da2cf77 100644 --- a/tests/unit/src/Repositories/AccessTokenRepositoryTest.php +++ b/tests/unit/src/Repositories/AccessTokenRepositoryTest.php @@ -20,6 +20,7 @@ use PHPUnit\Framework\MockObject\MockObject; use PHPUnit\Framework\TestCase; use SimpleSAML\Configuration; +use SimpleSAML\Database; use SimpleSAML\Module\oidc\Codebooks\DateFormatsEnum; use SimpleSAML\Module\oidc\Entities\AccessTokenEntity; use SimpleSAML\Module\oidc\Entities\Interfaces\ClientEntityInterface; @@ -102,8 +103,12 @@ protected function setUp(): void $this->dateTimeHelperMock = $this->createMock(Helpers\DateTime::class); $this->helpersMock->method('dateTime')->willReturn($this->dateTimeHelperMock); + $database = Database::getInstance(); + $this->repository = new AccessTokenRepository( $this->moduleConfigMock, + $database, + null, $this->clientRepositoryMock, $this->accessTokenEntityFactoryMock, $this->helpersMock, diff --git a/tests/unit/src/Repositories/AllowedOriginRepositoryTest.php b/tests/unit/src/Repositories/AllowedOriginRepositoryTest.php index a5b1d25e..2c6f438c 100644 --- a/tests/unit/src/Repositories/AllowedOriginRepositoryTest.php +++ b/tests/unit/src/Repositories/AllowedOriginRepositoryTest.php @@ -6,6 +6,7 @@ use PHPUnit\Framework\TestCase; use SimpleSAML\Configuration; +use SimpleSAML\Database; use SimpleSAML\Module\oidc\ModuleConfig; use SimpleSAML\Module\oidc\Repositories\AllowedOriginRepository; use SimpleSAML\Module\oidc\Services\DatabaseMigration; @@ -45,7 +46,12 @@ public static function setUpBeforeClass(): void protected function setUp(): void { $moduleConfigMock = $this->createMock(ModuleConfig::class); - $this->repository = new AllowedOriginRepository($moduleConfigMock); + $database = Database::getInstance(); + $this->repository = new AllowedOriginRepository( + $moduleConfigMock, + $database, + null, + ); } public function tearDown(): void diff --git a/tests/unit/src/Repositories/AuthCodeRepositoryTest.php b/tests/unit/src/Repositories/AuthCodeRepositoryTest.php index 735b3f23..99cce538 100644 --- a/tests/unit/src/Repositories/AuthCodeRepositoryTest.php +++ b/tests/unit/src/Repositories/AuthCodeRepositoryTest.php @@ -21,6 +21,7 @@ use PHPUnit\Framework\MockObject\MockObject; use PHPUnit\Framework\TestCase; use SimpleSAML\Configuration; +use SimpleSAML\Database; use SimpleSAML\Module\oidc\Codebooks\DateFormatsEnum; use SimpleSAML\Module\oidc\Entities\AuthCodeEntity; use SimpleSAML\Module\oidc\Entities\ClientEntity; @@ -84,8 +85,12 @@ protected function setUp(): void $this->dateTimeHelperMock = $this->createMock(Helpers\DateTime::class); $this->helpersMock->method('dateTime')->willReturn($this->dateTimeHelperMock); + $database = Database::getInstance(); + $this->repository = new AuthCodeRepository( $this->createMock(ModuleConfig::class), + $database, + null, $this->clientRepositoryMock, $this->authCodeEntityFactoryMock, $this->helpersMock, diff --git a/tests/unit/src/Repositories/ClientRepositoryTest.php b/tests/unit/src/Repositories/ClientRepositoryTest.php index 7a3d56e9..2d18cd8e 100644 --- a/tests/unit/src/Repositories/ClientRepositoryTest.php +++ b/tests/unit/src/Repositories/ClientRepositoryTest.php @@ -18,6 +18,7 @@ use PHPUnit\Framework\MockObject\MockObject; use PHPUnit\Framework\TestCase; use SimpleSAML\Configuration; +use SimpleSAML\Database; use SimpleSAML\Module\oidc\Entities\ClientEntity; use SimpleSAML\Module\oidc\Entities\Interfaces\ClientEntityInterface; use SimpleSAML\Module\oidc\Factories\Entities\ClientEntityFactory; @@ -58,8 +59,12 @@ protected function setUp(): void $this->clientEntityMock = $this->createMock(ClientEntityInterface::class); $this->clientEntityFactoryMock = $this->createMock(ClientEntityFactory::class); + $database = Database::getInstance(); + $this->repository = new ClientRepository( new ModuleConfig(), + $database, + null, $this->clientEntityFactoryMock, ); } diff --git a/tests/unit/src/Repositories/RefreshTokenRepositoryTest.php b/tests/unit/src/Repositories/RefreshTokenRepositoryTest.php index 049b3086..ce04ccbd 100644 --- a/tests/unit/src/Repositories/RefreshTokenRepositoryTest.php +++ b/tests/unit/src/Repositories/RefreshTokenRepositoryTest.php @@ -21,6 +21,7 @@ use PHPUnit\Framework\TestCase; use RuntimeException; use SimpleSAML\Configuration; +use SimpleSAML\Database; use SimpleSAML\Module\oidc\Entities\AccessTokenEntity; use SimpleSAML\Module\oidc\Entities\RefreshTokenEntity; use SimpleSAML\Module\oidc\Factories\Entities\RefreshTokenEntityFactory; @@ -76,8 +77,12 @@ protected function setUp(): void $this->refreshTokenEntityMock = $this->createMock(RefreshTokenEntity::class); + $database = Database::getInstance(); + $this->repository = new RefreshTokenRepository( new ModuleConfig(), + $database, + null, $this->accessTokenRepositoryMock, $this->refreshTokenEntityFactoryMock, new Helpers(), diff --git a/tests/unit/src/Repositories/UserRepositoryTest.php b/tests/unit/src/Repositories/UserRepositoryTest.php index c1016ab1..9f8c47c5 100644 --- a/tests/unit/src/Repositories/UserRepositoryTest.php +++ b/tests/unit/src/Repositories/UserRepositoryTest.php @@ -20,6 +20,7 @@ use PHPUnit\Framework\MockObject\Stub; use PHPUnit\Framework\TestCase; use SimpleSAML\Configuration; +use SimpleSAML\Database; use SimpleSAML\Module\oidc\Entities\UserEntity; use SimpleSAML\Module\oidc\Factories\Entities\UserEntityFactory; use SimpleSAML\Module\oidc\Helpers; @@ -59,8 +60,12 @@ protected function setUp(): void $this->userEntityFactoryMock = $this->createMock(UserEntityFactory::class); $this->userEntityMock = $this->createMock(UserEntity::class); + $database = Database::getInstance(); + self::$repository = new UserRepository( $moduleConfig, + $database, + null, $this->helpersStub, $this->userEntityFactoryMock, );