Skip to content

Commit

Permalink
Introduce ProtocolCache to repositories
Browse files Browse the repository at this point in the history
  • Loading branch information
Marko Ivančić committed Oct 31, 2024
1 parent 91c5756 commit fe49beb
Show file tree
Hide file tree
Showing 15 changed files with 100 additions and 30 deletions.
15 changes: 6 additions & 9 deletions src/Repositories/AbstractDatabaseRepository.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
6 changes: 5 additions & 1 deletion src/Repositories/AccessTokenRepository.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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
{
Expand All @@ -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
Expand Down
6 changes: 5 additions & 1 deletion src/Repositories/AuthCodeRepository.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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';
Expand Down
8 changes: 6 additions & 2 deletions src/Repositories/ClientRepository.php
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand Down Expand Up @@ -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);
}

Expand Down
6 changes: 5 additions & 1 deletion src/Repositories/RefreshTokenRepository.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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
{
Expand All @@ -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);
}

/**
Expand Down
10 changes: 2 additions & 8 deletions src/Repositories/ScopeRepository.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

/**
Expand Down
6 changes: 5 additions & 1 deletion src/Repositories/UserRepository.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,22 +21,26 @@
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
{
final public const TABLE_NAME = 'oidc_user';

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
Expand Down
27 changes: 22 additions & 5 deletions src/Services/Container.php
Original file line number Diff line number Diff line change
Expand Up @@ -204,14 +204,24 @@ 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);
$this->services[UserEntityFactory::class] = $userEntityFactory;

$userRepository = new UserRepository(
$moduleConfig,
$database,
$protocolCache,
$helpers,
$userEntityFactory,
);
Expand All @@ -228,6 +238,8 @@ public function __construct()

$authCodeRepository = new AuthCodeRepository(
$moduleConfig,
$database,
$protocolCache,
$clientRepository,
$authCodeEntityFactory,
$helpers,
Expand All @@ -252,6 +264,8 @@ public function __construct()

$accessTokenRepository = new AccessTokenRepository(
$moduleConfig,
$database,
$protocolCache,
$clientRepository,
$accessTokenEntityFactory,
$helpers,
Expand All @@ -263,6 +277,8 @@ public function __construct()

$refreshTokenRepository = new RefreshTokenRepository(
$moduleConfig,
$database,
$protocolCache,
$accessTokenRepository,
$refreshTokenEntityFactory,
$helpers,
Expand All @@ -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;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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(),
Expand All @@ -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),
);
Expand Down
5 changes: 5 additions & 0 deletions tests/unit/src/Repositories/AccessTokenRepositoryTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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,
Expand Down
8 changes: 7 additions & 1 deletion tests/unit/src/Repositories/AllowedOriginRepositoryTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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
Expand Down
5 changes: 5 additions & 0 deletions tests/unit/src/Repositories/AuthCodeRepositoryTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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,
Expand Down
5 changes: 5 additions & 0 deletions tests/unit/src/Repositories/ClientRepositoryTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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,
);
}
Expand Down
Loading

0 comments on commit fe49beb

Please sign in to comment.