Skip to content

Commit 9f1d2a6

Browse files
committed
Introduce BlockedTokenManagerInterface
1 parent 34a456a commit 9f1d2a6

File tree

7 files changed

+53
-32
lines changed

7 files changed

+53
-32
lines changed

EventListener/BlockJWTListener.php

+9-8
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,8 @@
44

55
use Lexik\Bundle\JWTAuthenticationBundle\Exception\JWTDecodeFailureException;
66
use Lexik\Bundle\JWTAuthenticationBundle\Exception\MissingClaimException;
7-
use Lexik\Bundle\JWTAuthenticationBundle\Services\BlockedTokenManager;
7+
use Lexik\Bundle\JWTAuthenticationBundle\Services\BlockedTokenManagerInterface;
8+
use Lexik\Bundle\JWTAuthenticationBundle\Services\CacheItemPoolBlockedTokenManager;
89
use Lexik\Bundle\JWTAuthenticationBundle\Services\JWTTokenManagerInterface;
910
use Lexik\Bundle\JWTAuthenticationBundle\TokenExtractor\TokenExtractorInterface;
1011
use Symfony\Component\HttpFoundation\Request;
@@ -14,16 +15,16 @@
1415

1516
class BlockJWTListener
1617
{
17-
private $jwtManager;
18+
private $blockedTokenManager;
1819
private $tokenExtractor;
19-
private $tokenManager;
20+
private $jwtManager;
2021

2122
public function __construct(
22-
BlockedTokenManager $tokenManager,
23-
TokenExtractorInterface $tokenExtractor,
24-
JWTTokenManagerInterface $jwtManager,
23+
BlockedTokenManagerInterface $blockedTokenManager,
24+
TokenExtractorInterface $tokenExtractor,
25+
JWTTokenManagerInterface $jwtManager
2526
) {
26-
$this->tokenManager = $tokenManager;
27+
$this->blockedTokenManager = $blockedTokenManager;
2728
$this->tokenExtractor = $tokenExtractor;
2829
$this->jwtManager = $jwtManager;
2930
}
@@ -58,7 +59,7 @@ private function blockTokenFromRequest(Request $request): void
5859
}
5960

6061
try {
61-
$this->tokenManager->add($payload);
62+
$this->blockedTokenManager->add($payload);
6263
} catch (MissingClaimException $e) {
6364
// We can't block a token missing the claims our system requires, so silently ignore this one
6465
}

EventListener/RejectBlockedTokenListener.php

+6-3
Original file line numberDiff line numberDiff line change
@@ -5,12 +5,15 @@
55
use Lexik\Bundle\JWTAuthenticationBundle\Event\JWTAuthenticatedEvent;
66
use Lexik\Bundle\JWTAuthenticationBundle\Exception\InvalidTokenException;
77
use Lexik\Bundle\JWTAuthenticationBundle\Exception\MissingClaimException;
8-
use Lexik\Bundle\JWTAuthenticationBundle\Services\BlockedTokenManager;
8+
use Lexik\Bundle\JWTAuthenticationBundle\Services\BlockedTokenManagerInterface;
99

1010
class RejectBlockedTokenListener
1111
{
12-
public function __construct(private BlockedTokenManager $tokenManager)
12+
private $blockedTokenManager;
13+
14+
public function __construct(BlockedTokenManagerInterface $blockedTokenManager)
1315
{
16+
$this->blockedTokenManager = $blockedTokenManager;
1417
}
1518

1619
/**
@@ -19,7 +22,7 @@ public function __construct(private BlockedTokenManager $tokenManager)
1922
public function __invoke(JWTAuthenticatedEvent $event): void
2023
{
2124
try {
22-
if ($this->tokenManager->has($event->getPayload())) {
25+
if ($this->blockedTokenManager->has($event->getPayload())) {
2326
throw new InvalidTokenException('JWT blocked');
2427
}
2528
} catch (MissingClaimException) {

Resources/config/blocklist_token.xml

+3-1
Original file line numberDiff line numberDiff line change
@@ -22,10 +22,12 @@
2222
<tag name="kernel.event_listener" event="lexik_jwt_authentication.on_jwt_authenticated"/>
2323
</service>
2424

25-
<service id="lexik_jwt_authentication.blocked_token_manager" class="Lexik\Bundle\JWTAuthenticationBundle\Services\BlockedTokenManager">
25+
<service id="lexik_jwt_authentication.blocked_token_manager" class="Lexik\Bundle\JWTAuthenticationBundle\Services\BlockedToken\CacheItemPoolBlockedTokenManager">
2626
<argument type="service" id="lexik_jwt_authentication.blocklist_token.cache"/>
2727
</service>
2828

29+
<service id="Lexik\Bundle\JWTAuthenticationBundle\Services\BlockedTokenManagerInterface" alias="lexik_jwt_authentication.blocked_token_manager" />
30+
2931
</services>
3032

3133
</container>

Services/BlockedTokenManager.php renamed to Services/BlockedToken/CacheItemPoolBlockedTokenManager.php

+3-11
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,15 @@
11
<?php
22

3-
namespace Lexik\Bundle\JWTAuthenticationBundle\Services;
3+
namespace Lexik\Bundle\JWTAuthenticationBundle\Services\BlockedToken;
44

55
use DateInterval;
66
use DateTimeImmutable;
77
use DateTimeZone;
88
use Lexik\Bundle\JWTAuthenticationBundle\Exception\MissingClaimException;
9+
use Lexik\Bundle\JWTAuthenticationBundle\Services\BlockedTokenManagerInterface;
910
use Psr\Cache\CacheItemPoolInterface;
1011

11-
class BlockedTokenManager
12+
class CacheItemPoolBlockedTokenManager implements BlockedTokenManagerInterface
1213
{
1314
private $cacheJwt;
1415

@@ -17,9 +18,6 @@ public function __construct(CacheItemPoolInterface $cacheJwt)
1718
$this->cacheJwt = $cacheJwt;
1819
}
1920

20-
/**
21-
* @throws MissingClaimException if required claims do not exist in the payload
22-
*/
2321
public function add(array $payload): bool
2422
{
2523
if (!isset($payload['exp'])) {
@@ -48,9 +46,6 @@ public function add(array $payload): bool
4846
return true;
4947
}
5048

51-
/**
52-
* @throws MissingClaimException if required claims do not exist in the payload
53-
*/
5449
public function has(array $payload): bool
5550
{
5651
if (!isset($payload['jti'])) {
@@ -60,9 +55,6 @@ public function has(array $payload): bool
6055
return $this->cacheJwt->hasItem($payload['jti']);
6156
}
6257

63-
/**
64-
* @throws MissingClaimException if required claims do not exist in the payload
65-
*/
6658
public function remove(array $payload): void
6759
{
6860
if (!isset($payload['jti'])) {
+23
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
<?php
2+
3+
namespace Lexik\Bundle\JWTAuthenticationBundle\Services;
4+
5+
use Lexik\Bundle\JWTAuthenticationBundle\Exception\MissingClaimException;
6+
7+
interface BlockedTokenManagerInterface
8+
{
9+
/**
10+
* @throws MissingClaimException if required claims do not exist in the payload
11+
*/
12+
public function add(array $payload): bool;
13+
14+
/**
15+
* @throws MissingClaimException if required claims do not exist in the payload
16+
*/
17+
public function has(array $payload): bool;
18+
19+
/**
20+
* @throws MissingClaimException if required claims do not exist in the payload
21+
*/
22+
public function remove(array $payload): void;
23+
}

Tests/Functional/BlocklistTokenTest.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
namespace Lexik\Bundle\JWTAuthenticationBundle\Tests\Functional;
44

5-
use Lexik\Bundle\JWTAuthenticationBundle\Services\BlockedTokenManager;
5+
use Lexik\Bundle\JWTAuthenticationBundle\Services\CacheItemPoolBlockedTokenManager;
66
use Lexik\Bundle\JWTAuthenticationBundle\Services\JWTManager;
77
use Lexik\Bundle\JWTAuthenticationBundle\Tests\Stubs\UserProvider;
88
use Psr\Cache\CacheItemPoolInterface;

Tests/Services/BlockedTokenManagerTest.php renamed to Tests/Services/BlockedToken/CacheItemPoolBlockedTokenManagerTest.php

+8-8
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,16 @@
11
<?php
22

3-
namespace Lexik\Bundle\JWTAuthenticationBundle\Tests\Services;
3+
namespace Lexik\Bundle\JWTAuthenticationBundle\Tests\Services\BlockedToken;
44

55
use DateTime;
66
use DateTimeImmutable;
77
use Lexik\Bundle\JWTAuthenticationBundle\Exception\MissingClaimException;
8-
use Lexik\Bundle\JWTAuthenticationBundle\Services\BlockedTokenManager;
8+
use Lexik\Bundle\JWTAuthenticationBundle\Services\BlockedToken\CacheItemPoolBlockedTokenManager;
99
use PHPUnit\Framework\TestCase;
1010
use Symfony\Bridge\PhpUnit\ClockMock;
1111
use Symfony\Component\Cache\Adapter\ArrayAdapter;
1212

13-
class BlockedTokenManagerTest extends TestCase
13+
class CacheItemPoolBlockedTokenManagerTest extends TestCase
1414
{
1515
private const JTI = '3de41d11099ed70e23e634eb32c959da';
1616
private const IAT = 1699455323;
@@ -19,7 +19,7 @@ public function testAddPayloadWithoutExpirationShouldThrowsAnException()
1919
{
2020
$this->expectException(MissingClaimException::class);
2121
$cacheAdapter = new ArrayAdapter();
22-
$blockedTokenManager = new BlockedTokenManager($cacheAdapter);
22+
$blockedTokenManager = new CacheItemPoolBlockedTokenManager($cacheAdapter);
2323
$blockedTokenManager->add(
2424
[
2525
'iat' => self::IAT,
@@ -36,7 +36,7 @@ public function testAddPayloadWithoutJitShouldThrowsAnException()
3636
{
3737
$this->expectException(MissingClaimException::class);
3838
$cacheAdapter = new ArrayAdapter();
39-
$blockedTokenManager = new BlockedTokenManager($cacheAdapter);
39+
$blockedTokenManager = new CacheItemPoolBlockedTokenManager($cacheAdapter);
4040
$blockedTokenManager->add(
4141
[
4242
'iat' => self::IAT,
@@ -52,7 +52,7 @@ public function testAddPayloadWithoutJitShouldThrowsAnException()
5252
public function testShouldNotAddPayloadIfItHasExpired()
5353
{
5454
$cacheAdapter = new ArrayAdapter();
55-
$blockedTokenManager = new BlockedTokenManager($cacheAdapter);
55+
$blockedTokenManager = new CacheItemPoolBlockedTokenManager($cacheAdapter);
5656
self::assertFalse(
5757
$blockedTokenManager->add(
5858
[
@@ -74,7 +74,7 @@ public function testShouldBlockTokenIfPaylaodHasNotExpired()
7474
ClockMock::register(ArrayAdapter::class);
7575

7676
$cacheAdapter = new ArrayAdapter();
77-
$blockedTokenManager = new BlockedTokenManager($cacheAdapter);
77+
$blockedTokenManager = new CacheItemPoolBlockedTokenManager($cacheAdapter);
7878

7979
$expirationDateTime = new DateTimeImmutable('2050-01-01 00:00:00');
8080
self::assertTrue(
@@ -103,7 +103,7 @@ public function testShouldBlockTokenIfPaylaodHasNotExpired()
103103
public function testHasToken()
104104
{
105105
$cacheAdapter = new ArrayAdapter();
106-
$blockedTokenManager = new BlockedTokenManager($cacheAdapter);
106+
$blockedTokenManager = new CacheItemPoolBlockedTokenManager($cacheAdapter);
107107

108108
$expirationDateTime = new DateTimeImmutable('2050-01-01 00:00:00');
109109
$payload = [

0 commit comments

Comments
 (0)