Skip to content

Commit

Permalink
OXDEV-8407 Check expiration field during token validation
Browse files Browse the repository at this point in the history
  • Loading branch information
TitaKoleva committed Oct 21, 2024
1 parent 6a4b8f0 commit 0f7a0a1
Show file tree
Hide file tree
Showing 4 changed files with 37 additions and 7 deletions.
20 changes: 20 additions & 0 deletions src/Infrastructure/Token.php
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,26 @@ public function isTokenRegistered(string $tokenId): bool
return $storage->isLoaded();
}

public function isTokenExpired(string $tokenId): bool
{
$queryBuilder = $this->queryBuilderFactory->create()
->select('oxid')
->from('oegraphqltoken')
->where('OXID = :tokenId')
->andWhere('EXPIRES_AT <= NOW()')
->setParameters([
'tokenId' => $tokenId,
]);

$result = $queryBuilder->execute();

if (is_object($result)) {
return $result->fetchOne() > 0;
}

return false;
}

public function removeExpiredTokens(UserInterface $user): void
{
$queryBuilder = $this->queryBuilderFactory->create()
Expand Down
7 changes: 6 additions & 1 deletion src/Service/TokenValidator.php
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ public function __construct(
*/
public function validateToken(UnencryptedToken $token): void
{
if (!$this->areConstraintsValid($token)) {
if (!$this->areConstraintsValid($token) || $this->isTokenExpired($token)) {
throw new InvalidToken();
}

Expand All @@ -62,6 +62,11 @@ private function areConstraintsValid(UnencryptedToken $token): bool
return $validator->validate($token, ...$config->validationConstraints());
}

private function isTokenExpired(UnencryptedToken $token)
{
return $this->tokenInfrastructure->isTokenExpired($token->claims()->get(Token::CLAIM_TOKENID));
}

private function isUserBlocked(?string $userId): bool
{
$groups = $this->legacyInfrastructure->getUserGroupIds($userId);
Expand Down
5 changes: 5 additions & 0 deletions tests/Integration/TestCase.php
Original file line number Diff line number Diff line change
Expand Up @@ -323,6 +323,11 @@ public function isTokenRegistered(string $tokenId): bool
return true;
}

public function isTokenExpired(string $tokenId): bool
{
return false;
}

public function registerToken(UnencryptedToken $token, DateTimeImmutable $time, DateTimeImmutable $expire): void
{
}
Expand Down
12 changes: 6 additions & 6 deletions tests/Unit/Service/TokenValidatorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ public function testTokenShopIdValidation(): void

$tokenInfrastructure = $this->createPartialMock(
TokenInfrastructure::class,
['registerToken', 'isTokenRegistered', 'removeExpiredTokens', 'canIssueToken']
['registerToken', 'isTokenRegistered', 'isTokenExpired', 'removeExpiredTokens', 'canIssueToken']
);
$tokenInfrastructure->method('isTokenRegistered')->willReturn(true);
$tokenInfrastructure->method('canIssueToken')->willReturn(true);
Expand Down Expand Up @@ -54,7 +54,7 @@ public function testTokenShopUrlValidation(): void

$tokenInfrastructure = $this->createPartialMock(
TokenInfrastructure::class,
['registerToken', 'isTokenRegistered', 'removeExpiredTokens', 'canIssueToken']
['registerToken', 'isTokenRegistered', 'isTokenExpired', 'removeExpiredTokens', 'canIssueToken']
);
$tokenInfrastructure->method('isTokenRegistered')->willReturn(true);
$tokenInfrastructure->method('canIssueToken')->willReturn(true);
Expand Down Expand Up @@ -85,7 +85,7 @@ public function testTokenUserInBlockedGroup(): void

$tokenInfrastructure = $this->createPartialMock(
TokenInfrastructure::class,
['registerToken', 'isTokenRegistered', 'removeExpiredTokens', 'canIssueToken']
['registerToken', 'isTokenRegistered', 'isTokenExpired', 'removeExpiredTokens', 'canIssueToken']
);
$tokenInfrastructure->method('isTokenRegistered')->willReturn(true);
$tokenInfrastructure->method('canIssueToken')->willReturn(true);
Expand All @@ -108,7 +108,7 @@ public function testExpiredToken(): void

$tokenInfrastructure = $this->createPartialMock(
TokenInfrastructure::class,
['registerToken', 'isTokenRegistered', 'removeExpiredTokens', 'canIssueToken']
['registerToken', 'isTokenRegistered', 'isTokenExpired', 'removeExpiredTokens', 'canIssueToken']
);
$tokenInfrastructure->method('isTokenRegistered')->willReturn(true);
$tokenInfrastructure->method('canIssueToken')->willReturn(true);
Expand All @@ -134,7 +134,7 @@ public function testDeletedToken(): void

$tokenInfrastructure = $this->createPartialMock(
TokenInfrastructure::class,
['registerToken', 'isTokenRegistered', 'removeExpiredTokens', 'canIssueToken']
['registerToken', 'isTokenRegistered', 'isTokenExpired', 'removeExpiredTokens', 'canIssueToken']
);
$tokenInfrastructure->method('isTokenRegistered')->willReturn(false);
$tokenInfrastructure->method('canIssueToken')->willReturn(true);
Expand All @@ -157,7 +157,7 @@ public function testAnonymousToken(): void

$tokenInfrastructure = $this->createPartialMock(
TokenInfrastructure::class,
['registerToken', 'isTokenRegistered', 'removeExpiredTokens', 'canIssueToken']
['registerToken', 'isTokenRegistered', 'isTokenExpired', 'removeExpiredTokens', 'canIssueToken']
);
$tokenInfrastructure->method('canIssueToken')->willReturn(true);
$validator = $this->getTokenValidator($legacy, $tokenInfrastructure);
Expand Down

0 comments on commit 0f7a0a1

Please sign in to comment.