Skip to content

Commit 7bfbe5b

Browse files
authored
Move to DateTime helper instead of static TimestampGenerator (#262)
* Move to DateTime helper instead of static TimestampGenerator --------- Co-authored-by: Marko Ivančić <[email protected]>
1 parent 897b9a5 commit 7bfbe5b

File tree

12 files changed

+71
-54
lines changed

12 files changed

+71
-54
lines changed

src/Codebooks/DateFormatsEnum.php

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace SimpleSAML\Module\oidc\Codebooks;
6+
7+
enum DateFormatsEnum: string
8+
{
9+
case DB_DATETIME = 'Y-m-d H:i:s';
10+
}

src/Controller/Federation/EntityStatementController.php

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,14 +5,14 @@
55
namespace SimpleSAML\Module\oidc\Controller\Federation;
66

77
use SimpleSAML\Module\oidc\Codebooks\RoutesEnum;
8+
use SimpleSAML\Module\oidc\Helpers;
89
use SimpleSAML\Module\oidc\ModuleConfig;
910
use SimpleSAML\Module\oidc\Repositories\ClientRepository;
1011
use SimpleSAML\Module\oidc\Server\Exceptions\OidcServerException;
1112
use SimpleSAML\Module\oidc\Services\JsonWebKeySetService;
1213
use SimpleSAML\Module\oidc\Services\JsonWebTokenBuilderService;
1314
use SimpleSAML\Module\oidc\Services\OpMetadataService;
1415
use SimpleSAML\Module\oidc\Utils\FederationCache;
15-
use SimpleSAML\Module\oidc\Utils\TimestampGenerator;
1616
use SimpleSAML\OpenID\Codebooks\ClaimsEnum;
1717
use SimpleSAML\OpenID\Codebooks\ClientRegistrationTypesEnum;
1818
use SimpleSAML\OpenID\Codebooks\ContentTypesEnum;
@@ -39,6 +39,7 @@ public function __construct(
3939
private readonly JsonWebKeySetService $jsonWebKeySetService,
4040
private readonly OpMetadataService $opMetadataService,
4141
private readonly ClientRepository $clientRepository,
42+
private readonly Helpers $helpers,
4243
private readonly ?FederationCache $federationCache,
4344
) {
4445
if (!$this->moduleConfig->getFederationEnabled()) {
@@ -69,7 +70,7 @@ public function configuration(): Response
6970
->withHeader(ClaimsEnum::Typ->value, JwtTypesEnum::EntityStatementJwt->value)
7071
->relatedTo($this->moduleConfig->getIssuer()) // This is entity configuration (statement about itself).
7172
->expiresAt(
72-
(TimestampGenerator::utcImmutable())->add($this->moduleConfig->getFederationEntityStatementDuration()),
73+
$this->helpers->dateTime()->getUtc()->add($this->moduleConfig->getFederationEntityStatementDuration()),
7374
)->withClaim(
7475
ClaimsEnum::Jwks->value,
7576
['keys' => array_values($this->jsonWebKeySetService->federationKeys()),],
@@ -198,7 +199,7 @@ public function fetch(Request $request): Response
198199
->withHeader(ClaimsEnum::Typ->value, JwtTypesEnum::EntityStatementJwt->value)
199200
->relatedTo($subject)
200201
->expiresAt(
201-
(TimestampGenerator::utcImmutable())->add($this->moduleConfig->getFederationEntityStatementDuration()),
202+
$this->helpers->dateTime()->getUtc()->add($this->moduleConfig->getFederationEntityStatementDuration()),
202203
)->withClaim(
203204
ClaimsEnum::Jwks->value,
204205
$jwks,

src/Repositories/AccessTokenRepository.php

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,14 +21,15 @@
2121
use League\OAuth2\Server\Entities\ClientEntityInterface as OAuth2ClientEntityInterface;
2222
use RuntimeException;
2323
use SimpleSAML\Error\Error;
24+
use SimpleSAML\Module\oidc\Codebooks\DateFormatsEnum;
2425
use SimpleSAML\Module\oidc\Entities\AccessTokenEntity;
2526
use SimpleSAML\Module\oidc\Entities\Interfaces\AccessTokenEntityInterface;
2627
use SimpleSAML\Module\oidc\Factories\Entities\AccessTokenEntityFactory;
28+
use SimpleSAML\Module\oidc\Helpers;
2729
use SimpleSAML\Module\oidc\ModuleConfig;
2830
use SimpleSAML\Module\oidc\Repositories\Interfaces\AccessTokenRepositoryInterface;
2931
use SimpleSAML\Module\oidc\Repositories\Traits\RevokeTokenByAuthCodeIdTrait;
3032
use SimpleSAML\Module\oidc\Server\Exceptions\OidcServerException;
31-
use SimpleSAML\Module\oidc\Utils\TimestampGenerator;
3233

3334
class AccessTokenRepository extends AbstractDatabaseRepository implements AccessTokenRepositoryInterface
3435
{
@@ -40,6 +41,7 @@ public function __construct(
4041
ModuleConfig $moduleConfig,
4142
protected readonly ClientRepository $clientRepository,
4243
protected readonly AccessTokenEntityFactory $accessTokenEntityFactory,
44+
protected readonly Helpers $helpers,
4345
) {
4446
parent::__construct($moduleConfig);
4547
}
@@ -182,7 +184,7 @@ public function removeExpired(): void
182184
WHERE $accessTokenTableName.id = $refreshTokenTableName.access_token_id AND expires_at > :now
183185
)",
184186
[
185-
'now' => TimestampGenerator::utc()->format('Y-m-d H:i:s'),
187+
'now' => $this->helpers->dateTime()->getUtc()->format(DateFormatsEnum::DB_DATETIME->value),
186188
],
187189
);
188190
}

src/Repositories/AuthCodeRepository.php

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,19 +19,21 @@
1919
use League\OAuth2\Server\Entities\AuthCodeEntityInterface as OAuth2AuthCodeEntityInterface;
2020
use RuntimeException;
2121
use SimpleSAML\Error\Error;
22+
use SimpleSAML\Module\oidc\Codebooks\DateFormatsEnum;
2223
use SimpleSAML\Module\oidc\Entities\AuthCodeEntity;
2324
use SimpleSAML\Module\oidc\Entities\Interfaces\AuthCodeEntityInterface;
2425
use SimpleSAML\Module\oidc\Factories\Entities\AuthCodeEntityFactory;
26+
use SimpleSAML\Module\oidc\Helpers;
2527
use SimpleSAML\Module\oidc\ModuleConfig;
2628
use SimpleSAML\Module\oidc\Repositories\Interfaces\AuthCodeRepositoryInterface;
27-
use SimpleSAML\Module\oidc\Utils\TimestampGenerator;
2829

2930
class AuthCodeRepository extends AbstractDatabaseRepository implements AuthCodeRepositoryInterface
3031
{
3132
public function __construct(
3233
ModuleConfig $moduleConfig,
3334
protected readonly ClientRepository $clientRepository,
3435
protected readonly AuthCodeEntityFactory $authCodeEntityFactory,
36+
protected readonly Helpers $helpers,
3537
) {
3638
parent::__construct($moduleConfig);
3739
}
@@ -139,7 +141,7 @@ public function removeExpired(): void
139141
$this->database->write(
140142
"DELETE FROM {$this->getTableName()} WHERE expires_at < :now",
141143
[
142-
'now' => TimestampGenerator::utc()->format('Y-m-d H:i:s'),
144+
'now' => $this->helpers->dateTime()->getUtc()->format(DateFormatsEnum::DB_DATETIME->value),
143145
],
144146
);
145147
}

src/Repositories/RefreshTokenRepository.php

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,13 +19,14 @@
1919
use League\OAuth2\Server\Entities\RefreshTokenEntityInterface as OAuth2RefreshTokenEntityInterface;
2020
use League\OAuth2\Server\Exception\OAuthServerException;
2121
use RuntimeException;
22+
use SimpleSAML\Module\oidc\Codebooks\DateFormatsEnum;
2223
use SimpleSAML\Module\oidc\Entities\Interfaces\RefreshTokenEntityInterface;
2324
use SimpleSAML\Module\oidc\Entities\RefreshTokenEntity;
2425
use SimpleSAML\Module\oidc\Factories\Entities\RefreshTokenEntityFactory;
26+
use SimpleSAML\Module\oidc\Helpers;
2527
use SimpleSAML\Module\oidc\ModuleConfig;
2628
use SimpleSAML\Module\oidc\Repositories\Interfaces\RefreshTokenRepositoryInterface;
2729
use SimpleSAML\Module\oidc\Repositories\Traits\RevokeTokenByAuthCodeIdTrait;
28-
use SimpleSAML\Module\oidc\Utils\TimestampGenerator;
2930

3031
class RefreshTokenRepository extends AbstractDatabaseRepository implements RefreshTokenRepositoryInterface
3132
{
@@ -37,6 +38,7 @@ public function __construct(
3738
ModuleConfig $moduleConfig,
3839
protected readonly AccessTokenRepository $accessTokenRepository,
3940
protected readonly RefreshTokenEntityFactory $refreshTokenEntityFactory,
41+
protected readonly Helpers $helpers,
4042
) {
4143
parent::__construct($moduleConfig);
4244
}
@@ -144,7 +146,7 @@ public function removeExpired(): void
144146
$this->database->write(
145147
"DELETE FROM {$this->getTableName()} WHERE expires_at < :now",
146148
[
147-
'now' => TimestampGenerator::utc()->format('Y-m-d H:i:s'),
149+
'now' => $this->helpers->dateTime()->getUtc()->format(DateFormatsEnum::DB_DATETIME->value),
148150
],
149151
);
150152
}

src/Services/Container.php

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -230,6 +230,7 @@ public function __construct()
230230
$moduleConfig,
231231
$clientRepository,
232232
$authCodeEntityFactory,
233+
$helpers,
233234
);
234235
$this->services[AuthCodeRepository::class] = $authCodeRepository;
235236

@@ -253,6 +254,7 @@ public function __construct()
253254
$moduleConfig,
254255
$clientRepository,
255256
$accessTokenEntityFactory,
257+
$helpers,
256258
);
257259
$this->services[AccessTokenRepository::class] = $accessTokenRepository;
258260

@@ -263,6 +265,7 @@ public function __construct()
263265
$moduleConfig,
264266
$accessTokenRepository,
265267
$refreshTokenEntityFactory,
268+
$helpers,
266269
);
267270
$this->services[RefreshTokenRepository::class] = $refreshTokenRepository;
268271

src/Stores/Session/LogoutTicketStoreDb.php

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,8 @@
77
use DateInterval;
88
use PDO;
99
use SimpleSAML\Database;
10-
use SimpleSAML\Module\oidc\Utils\TimestampGenerator;
10+
use SimpleSAML\Module\oidc\Codebooks\DateFormatsEnum;
11+
use SimpleSAML\Module\oidc\Helpers;
1112

1213
class LogoutTicketStoreDb implements LogoutTicketStoreInterface
1314
{
@@ -20,8 +21,11 @@ class LogoutTicketStoreDb implements LogoutTicketStoreInterface
2021
*/
2122
protected int $ttl;
2223

23-
public function __construct(?Database $database = null, int $ttl = 60)
24-
{
24+
public function __construct(
25+
?Database $database = null,
26+
int $ttl = 60,
27+
protected readonly Helpers $helpers = new Helpers(),
28+
) {
2529
$this->database = $database ?? Database::getInstance();
2630
$this->ttl = max($ttl, 0);
2731
}
@@ -97,9 +101,9 @@ protected function deleteExpired(): void
97101
$this->database->write(
98102
"DELETE FROM {$this->getTableName()} WHERE created_at <= :expiration",
99103
[
100-
'expiration' => TimestampGenerator::utc()
104+
'expiration' => $this->helpers->dateTime()->getUtc()
101105
->sub(new DateInterval('PT' . $this->ttl . 'S'))
102-
->format('Y-m-d H:i:s'),
106+
->format(DateFormatsEnum::DB_DATETIME->value),
103107
],
104108
);
105109
}

src/Utils/TimestampGenerator.php

Lines changed: 0 additions & 40 deletions
This file was deleted.

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -168,6 +168,7 @@ public function getDatabase(): Database
168168
$moduleConfig,
169169
$clientRepositoryMock,
170170
$this->accessTokenEntityFactory,
171+
new Helpers(),
171172
);
172173

173174
$client = self::clientRepositoryGetClient(self::CLIENT_ID);

tests/unit/src/Repositories/AccessTokenRepositoryTest.php

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,14 +15,17 @@
1515
*/
1616
namespace SimpleSAML\Test\Module\oidc\unit\Repositories;
1717

18+
use DateTimeImmutable;
1819
use Exception;
1920
use PHPUnit\Framework\MockObject\MockObject;
2021
use PHPUnit\Framework\TestCase;
2122
use SimpleSAML\Configuration;
23+
use SimpleSAML\Module\oidc\Codebooks\DateFormatsEnum;
2224
use SimpleSAML\Module\oidc\Entities\AccessTokenEntity;
2325
use SimpleSAML\Module\oidc\Entities\Interfaces\ClientEntityInterface;
2426
use SimpleSAML\Module\oidc\Factories\Entities\AccessTokenEntityFactory;
2527
use SimpleSAML\Module\oidc\Factories\Entities\ClientEntityFactory;
28+
use SimpleSAML\Module\oidc\Helpers;
2629
use SimpleSAML\Module\oidc\ModuleConfig;
2730
use SimpleSAML\Module\oidc\Repositories\AccessTokenRepository;
2831
use SimpleSAML\Module\oidc\Repositories\ClientRepository;
@@ -44,6 +47,8 @@ class AccessTokenRepositoryTest extends TestCase
4447
protected MockObject $clientEntityFactoryMock;
4548
protected MockObject $accessTokenEntityFactoryMock;
4649
protected MockObject $accessTokenEntityMock;
50+
protected MockObject $helpersMock;
51+
protected MockObject $dateTimeHelperMock;
4752

4853
protected static bool $dbSeeded = false;
4954
protected ClientEntityInterface $clientEntity;
@@ -93,10 +98,15 @@ protected function setUp(): void
9398
'auth_code_id' => 'authCode123',
9499
];
95100

101+
$this->helpersMock = $this->createMock(Helpers::class);
102+
$this->dateTimeHelperMock = $this->createMock(Helpers\DateTime::class);
103+
$this->helpersMock->method('dateTime')->willReturn($this->dateTimeHelperMock);
104+
96105
$this->repository = new AccessTokenRepository(
97106
$this->moduleConfigMock,
98107
$this->clientRepositoryMock,
99108
$this->accessTokenEntityFactoryMock,
109+
$this->helpersMock,
100110
);
101111
}
102112

@@ -178,6 +188,12 @@ public function testErrorCheckIsRevokedInvalidToken(): void
178188
*/
179189
public function testRemoveExpired(): void
180190
{
191+
$dateTimeMock = $this->createMock(DateTimeImmutable::class);
192+
$dateTimeMock->expects($this->once())->method('format')
193+
->willReturn(date(DateFormatsEnum::DB_DATETIME->value));
194+
$this->dateTimeHelperMock->expects($this->once())->method('getUtc')
195+
->willReturn($dateTimeMock);
196+
181197
$this->repository->removeExpired();
182198
$notFoundAccessToken = $this->repository->findById(self::ACCESS_TOKEN_ID);
183199

0 commit comments

Comments
 (0)