Skip to content

Commit fe49beb

Browse files
committed
Introduce ProtocolCache to repositories
1 parent 91c5756 commit fe49beb

15 files changed

+100
-30
lines changed

src/Repositories/AbstractDatabaseRepository.php

Lines changed: 6 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -15,24 +15,21 @@
1515
*/
1616
namespace SimpleSAML\Module\oidc\Repositories;
1717

18-
use SimpleSAML\Configuration;
1918
use SimpleSAML\Database;
2019
use SimpleSAML\Module\oidc\ModuleConfig;
20+
use SimpleSAML\Module\oidc\Utils\ProtocolCache;
2121

2222
abstract class AbstractDatabaseRepository
2323
{
24-
protected Configuration $config;
25-
26-
protected Database $database;
27-
2824
/**
2925
* ClientRepository constructor.
3026
* @throws \Exception
3127
*/
32-
public function __construct(protected ModuleConfig $moduleConfig)
33-
{
34-
$this->config = $this->moduleConfig->config();
35-
$this->database = Database::getInstance();
28+
public function __construct(
29+
protected readonly ModuleConfig $moduleConfig,
30+
protected readonly Database $database,
31+
protected readonly ?ProtocolCache $protocolCache,
32+
) {
3633
}
3734

3835
abstract public function getTableName(): ?string;

src/Repositories/AccessTokenRepository.php

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
use League\OAuth2\Server\Entities\AccessTokenEntityInterface as OAuth2AccessTokenEntityInterface;
2121
use League\OAuth2\Server\Entities\ClientEntityInterface as OAuth2ClientEntityInterface;
2222
use RuntimeException;
23+
use SimpleSAML\Database;
2324
use SimpleSAML\Error\Error;
2425
use SimpleSAML\Module\oidc\Codebooks\DateFormatsEnum;
2526
use SimpleSAML\Module\oidc\Entities\AccessTokenEntity;
@@ -30,6 +31,7 @@
3031
use SimpleSAML\Module\oidc\Repositories\Interfaces\AccessTokenRepositoryInterface;
3132
use SimpleSAML\Module\oidc\Repositories\Traits\RevokeTokenByAuthCodeIdTrait;
3233
use SimpleSAML\Module\oidc\Server\Exceptions\OidcServerException;
34+
use SimpleSAML\Module\oidc\Utils\ProtocolCache;
3335

3436
class AccessTokenRepository extends AbstractDatabaseRepository implements AccessTokenRepositoryInterface
3537
{
@@ -39,11 +41,13 @@ class AccessTokenRepository extends AbstractDatabaseRepository implements Access
3941

4042
public function __construct(
4143
ModuleConfig $moduleConfig,
44+
Database $database,
45+
?ProtocolCache $protocolCache,
4246
protected readonly ClientRepository $clientRepository,
4347
protected readonly AccessTokenEntityFactory $accessTokenEntityFactory,
4448
protected readonly Helpers $helpers,
4549
) {
46-
parent::__construct($moduleConfig);
50+
parent::__construct($moduleConfig, $database, $protocolCache);
4751
}
4852

4953
public function getTableName(): string

src/Repositories/AuthCodeRepository.php

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818

1919
use League\OAuth2\Server\Entities\AuthCodeEntityInterface as OAuth2AuthCodeEntityInterface;
2020
use RuntimeException;
21+
use SimpleSAML\Database;
2122
use SimpleSAML\Error\Error;
2223
use SimpleSAML\Module\oidc\Codebooks\DateFormatsEnum;
2324
use SimpleSAML\Module\oidc\Entities\AuthCodeEntity;
@@ -26,16 +27,19 @@
2627
use SimpleSAML\Module\oidc\Helpers;
2728
use SimpleSAML\Module\oidc\ModuleConfig;
2829
use SimpleSAML\Module\oidc\Repositories\Interfaces\AuthCodeRepositoryInterface;
30+
use SimpleSAML\Module\oidc\Utils\ProtocolCache;
2931

3032
class AuthCodeRepository extends AbstractDatabaseRepository implements AuthCodeRepositoryInterface
3133
{
3234
public function __construct(
3335
ModuleConfig $moduleConfig,
36+
Database $database,
37+
?ProtocolCache $protocolCache,
3438
protected readonly ClientRepository $clientRepository,
3539
protected readonly AuthCodeEntityFactory $authCodeEntityFactory,
3640
protected readonly Helpers $helpers,
3741
) {
38-
parent::__construct($moduleConfig);
42+
parent::__construct($moduleConfig, $database, $protocolCache);
3943
}
4044

4145
final public const TABLE_NAME = 'oidc_auth_code';

src/Repositories/ClientRepository.php

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,18 +17,22 @@
1717

1818
use League\OAuth2\Server\Repositories\ClientRepositoryInterface;
1919
use PDO;
20+
use SimpleSAML\Database;
2021
use SimpleSAML\Module\oidc\Entities\ClientEntity;
2122
use SimpleSAML\Module\oidc\Entities\Interfaces\ClientEntityInterface;
2223
use SimpleSAML\Module\oidc\Factories\Entities\ClientEntityFactory;
2324
use SimpleSAML\Module\oidc\ModuleConfig;
25+
use SimpleSAML\Module\oidc\Utils\ProtocolCache;
2426

2527
class ClientRepository extends AbstractDatabaseRepository implements ClientRepositoryInterface
2628
{
2729
public function __construct(
2830
ModuleConfig $moduleConfig,
31+
Database $database,
32+
?ProtocolCache $protocolCache,
2933
protected readonly ClientEntityFactory $clientEntityFactory,
3034
) {
31-
parent::__construct($moduleConfig);
35+
parent::__construct($moduleConfig, $database, $protocolCache);
3236
}
3337

3438
final public const TABLE_NAME = 'oidc_client';
@@ -389,7 +393,7 @@ private function count(string $query, ?string $owner): int
389393
*/
390394
private function getItemsPerPage(): int
391395
{
392-
return $this->config
396+
return $this->moduleConfig->config()
393397
->getOptionalIntegerRange(ModuleConfig::OPTION_ADMIN_UI_PAGINATION_ITEMS_PER_PAGE, 1, 100, 20);
394398
}
395399

src/Repositories/RefreshTokenRepository.php

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
use League\OAuth2\Server\Entities\RefreshTokenEntityInterface as OAuth2RefreshTokenEntityInterface;
2020
use League\OAuth2\Server\Exception\OAuthServerException;
2121
use RuntimeException;
22+
use SimpleSAML\Database;
2223
use SimpleSAML\Module\oidc\Codebooks\DateFormatsEnum;
2324
use SimpleSAML\Module\oidc\Entities\Interfaces\RefreshTokenEntityInterface;
2425
use SimpleSAML\Module\oidc\Entities\RefreshTokenEntity;
@@ -27,6 +28,7 @@
2728
use SimpleSAML\Module\oidc\ModuleConfig;
2829
use SimpleSAML\Module\oidc\Repositories\Interfaces\RefreshTokenRepositoryInterface;
2930
use SimpleSAML\Module\oidc\Repositories\Traits\RevokeTokenByAuthCodeIdTrait;
31+
use SimpleSAML\Module\oidc\Utils\ProtocolCache;
3032

3133
class RefreshTokenRepository extends AbstractDatabaseRepository implements RefreshTokenRepositoryInterface
3234
{
@@ -36,11 +38,13 @@ class RefreshTokenRepository extends AbstractDatabaseRepository implements Refre
3638

3739
public function __construct(
3840
ModuleConfig $moduleConfig,
41+
Database $database,
42+
?ProtocolCache $protocolCache,
3943
protected readonly AccessTokenRepository $accessTokenRepository,
4044
protected readonly RefreshTokenEntityFactory $refreshTokenEntityFactory,
4145
protected readonly Helpers $helpers,
4246
) {
43-
parent::__construct($moduleConfig);
47+
parent::__construct($moduleConfig, $database, $protocolCache);
4448
}
4549

4650
/**

src/Repositories/ScopeRepository.php

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -26,18 +26,12 @@
2626
use function array_key_exists;
2727
use function in_array;
2828

29-
class ScopeRepository extends AbstractDatabaseRepository implements ScopeRepositoryInterface
29+
class ScopeRepository implements ScopeRepositoryInterface
3030
{
3131
public function __construct(
32-
ModuleConfig $moduleConfig,
32+
protected readonly ModuleConfig $moduleConfig,
3333
protected readonly ScopeEntityFactory $scopeEntityFactory,
3434
) {
35-
parent::__construct($moduleConfig);
36-
}
37-
38-
public function getTableName(): ?string
39-
{
40-
return null;
4135
}
4236

4337
/**

src/Repositories/UserRepository.php

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,22 +21,26 @@
2121
use League\OAuth2\Server\Entities\ClientEntityInterface as OAuth2ClientEntityInterface;
2222
use League\OAuth2\Server\Entities\UserEntityInterface;
2323
use League\OAuth2\Server\Repositories\UserRepositoryInterface;
24+
use SimpleSAML\Database;
2425
use SimpleSAML\Module\oidc\Entities\UserEntity;
2526
use SimpleSAML\Module\oidc\Factories\Entities\UserEntityFactory;
2627
use SimpleSAML\Module\oidc\Helpers;
2728
use SimpleSAML\Module\oidc\ModuleConfig;
2829
use SimpleSAML\Module\oidc\Repositories\Interfaces\IdentityProviderInterface;
30+
use SimpleSAML\Module\oidc\Utils\ProtocolCache;
2931

3032
class UserRepository extends AbstractDatabaseRepository implements UserRepositoryInterface, IdentityProviderInterface
3133
{
3234
final public const TABLE_NAME = 'oidc_user';
3335

3436
public function __construct(
3537
ModuleConfig $moduleConfig,
38+
Database $database,
39+
?ProtocolCache $protocolCache,
3640
protected readonly Helpers $helpers,
3741
protected readonly UserEntityFactory $userEntityFactory,
3842
) {
39-
parent::__construct($moduleConfig);
43+
parent::__construct($moduleConfig, $database, $protocolCache);
4044
}
4145

4246
public function getTableName(): string

src/Services/Container.php

Lines changed: 22 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -204,14 +204,24 @@ public function __construct()
204204
);
205205
$this->services[ClientEntityFactory::class] = $clientEntityFactory;
206206

207-
$clientRepository = new ClientRepository($moduleConfig, $clientEntityFactory);
207+
$database = Database::getInstance();
208+
$this->services[Database::class] = $database;
209+
210+
$clientRepository = new ClientRepository(
211+
$moduleConfig,
212+
$database,
213+
$protocolCache,
214+
$clientEntityFactory,
215+
);
208216
$this->services[ClientRepository::class] = $clientRepository;
209217

210218
$userEntityFactory = new UserEntityFactory($helpers);
211219
$this->services[UserEntityFactory::class] = $userEntityFactory;
212220

213221
$userRepository = new UserRepository(
214222
$moduleConfig,
223+
$database,
224+
$protocolCache,
215225
$helpers,
216226
$userEntityFactory,
217227
);
@@ -228,6 +238,8 @@ public function __construct()
228238

229239
$authCodeRepository = new AuthCodeRepository(
230240
$moduleConfig,
241+
$database,
242+
$protocolCache,
231243
$clientRepository,
232244
$authCodeEntityFactory,
233245
$helpers,
@@ -252,6 +264,8 @@ public function __construct()
252264

253265
$accessTokenRepository = new AccessTokenRepository(
254266
$moduleConfig,
267+
$database,
268+
$protocolCache,
255269
$clientRepository,
256270
$accessTokenEntityFactory,
257271
$helpers,
@@ -263,6 +277,8 @@ public function __construct()
263277

264278
$refreshTokenRepository = new RefreshTokenRepository(
265279
$moduleConfig,
280+
$database,
281+
$protocolCache,
266282
$accessTokenRepository,
267283
$refreshTokenEntityFactory,
268284
$helpers,
@@ -272,12 +288,13 @@ public function __construct()
272288
$scopeRepository = new ScopeRepository($moduleConfig, $scopeEntityFactory);
273289
$this->services[ScopeRepository::class] = $scopeRepository;
274290

275-
$allowedOriginRepository = new AllowedOriginRepository($moduleConfig);
291+
$allowedOriginRepository = new AllowedOriginRepository(
292+
$moduleConfig,
293+
$database,
294+
$protocolCache,
295+
);
276296
$this->services[AllowedOriginRepository::class] = $allowedOriginRepository;
277297

278-
$database = Database::getInstance();
279-
$this->services[Database::class] = $database;
280-
281298
$databaseMigration = new DatabaseMigration($database);
282299
$this->services[DatabaseMigration::class] = $databaseMigration;
283300

tests/integration/src/Repositories/Traits/RevokeTokenByAuthCodeIdTraitTest.php

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -162,10 +162,19 @@ public function getDatabase(): Database
162162
$clientEntityFactoryMock = $this->createMock(ClientEntityFactory::class);
163163
$clientEntityFactoryMock->method('fromState')->willReturn($clientEntityMock);
164164

165-
$clientRepositoryMock = new ClientRepository($moduleConfig, $clientEntityFactoryMock);
165+
$database = Database::getInstance();
166+
167+
$clientRepositoryMock = new ClientRepository(
168+
$moduleConfig,
169+
$database,
170+
null,
171+
$clientEntityFactoryMock
172+
);
166173

167174
$this->accessTokenRepository = new AccessTokenRepository(
168175
$moduleConfig,
176+
$database,
177+
null,
169178
$clientRepositoryMock,
170179
$this->accessTokenEntityFactory,
171180
new Helpers(),
@@ -180,6 +189,8 @@ public function getDatabase(): Database
180189
$user = new UserEntity(self::USER_ID, $createUpdatedAt, $createUpdatedAt, []);
181190
$userRepositoryMock = new UserRepository(
182191
$moduleConfig,
192+
$database,
193+
null,
183194
$helpers,
184195
new UserEntityFactory($helpers),
185196
);

tests/unit/src/Repositories/AccessTokenRepositoryTest.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
use PHPUnit\Framework\MockObject\MockObject;
2121
use PHPUnit\Framework\TestCase;
2222
use SimpleSAML\Configuration;
23+
use SimpleSAML\Database;
2324
use SimpleSAML\Module\oidc\Codebooks\DateFormatsEnum;
2425
use SimpleSAML\Module\oidc\Entities\AccessTokenEntity;
2526
use SimpleSAML\Module\oidc\Entities\Interfaces\ClientEntityInterface;
@@ -102,8 +103,12 @@ protected function setUp(): void
102103
$this->dateTimeHelperMock = $this->createMock(Helpers\DateTime::class);
103104
$this->helpersMock->method('dateTime')->willReturn($this->dateTimeHelperMock);
104105

106+
$database = Database::getInstance();
107+
105108
$this->repository = new AccessTokenRepository(
106109
$this->moduleConfigMock,
110+
$database,
111+
null,
107112
$this->clientRepositoryMock,
108113
$this->accessTokenEntityFactoryMock,
109114
$this->helpersMock,

0 commit comments

Comments
 (0)