|
| 1 | +<?php |
| 2 | + |
| 3 | +namespace Lexik\Bundle\JWTAuthenticationBundle\Tests\Functional; |
| 4 | + |
| 5 | +use Lexik\Bundle\JWTAuthenticationBundle\Services\JWTManager; |
| 6 | +use Symfony\Component\HttpFoundation\Response; |
| 7 | + |
| 8 | +class BlocklistTokenTest extends TestCase |
| 9 | +{ |
| 10 | + public function testShouldInvalidateTokenOnLogoutWhenBlockListTokenIsEnabled() |
| 11 | + { |
| 12 | + static::$client = static::createClient(['test_case' => 'BlockListToken']); |
| 13 | + |
| 14 | + $token = static::getAuthenticatedToken(); |
| 15 | + |
| 16 | + static::$client->jsonRequest('GET', '/api/secured', [], ['HTTP_AUTHORIZATION' => "Bearer $token"]); |
| 17 | + static::assertResponseIsSuccessful('Pre condition - a valid token should be able to contact the api'); |
| 18 | + |
| 19 | + static::$client->jsonRequest('GET', '/api/logout', [], ['HTTP_AUTHORIZATION' => "Bearer $token"]); |
| 20 | + static::assertResponseStatusCodeSame(Response::HTTP_FOUND); |
| 21 | + |
| 22 | + static::$client->jsonRequest('GET', '/api/secured', [], ['HTTP_AUTHORIZATION' => "Bearer $token"]); |
| 23 | + static::assertResponseStatusCodeSame(Response::HTTP_UNAUTHORIZED, 'Logout should invalidate token'); |
| 24 | + |
| 25 | + $responseBody = json_decode(static::$client->getResponse()->getContent(), true); |
| 26 | + $this->assertEquals('Invalid JWT Token', $responseBody['message']); |
| 27 | + } |
| 28 | + |
| 29 | + public function testShouldAddJtiWhenBlockListTokenIsEnabled() |
| 30 | + { |
| 31 | + static::$client = static::createClient(['test_case' => 'BlockListToken']); |
| 32 | + |
| 33 | + $token = static::getAuthenticatedToken(); |
| 34 | + /** @var JWTManager $jwtManager */ |
| 35 | + $jwtManager = static::getContainer()->get('lexik_jwt_authentication.jwt_manager'); |
| 36 | + $payload = $jwtManager->parse($token); |
| 37 | + self::assertNotEmpty($payload['jti']); |
| 38 | + } |
| 39 | + |
| 40 | + public function testShouldInvalidateTokenOnLogoutWhenBlockListTokenIsEnabledAndWhenUsingCustomLogout() |
| 41 | + { |
| 42 | + static::$client = static::createClient(['test_case' => 'BlockListToken']); |
| 43 | + |
| 44 | + $token = static::getAuthenticatedToken(); |
| 45 | + |
| 46 | + static::$client->jsonRequest('GET', '/api/secured', [], ['HTTP_AUTHORIZATION' => "Bearer $token"]); |
| 47 | + static::assertResponseIsSuccessful('Pre condition - a valid token should be able to contact the api'); |
| 48 | + |
| 49 | + static::$client->jsonRequest('GET', '/api/logout_custom', [], ['HTTP_AUTHORIZATION' => "Bearer $token"]); |
| 50 | + static::assertResponseStatusCodeSame(Response::HTTP_OK); |
| 51 | + |
| 52 | + static::$client->jsonRequest('GET', '/api/secured', [], ['HTTP_AUTHORIZATION' => "Bearer $token"]); |
| 53 | + static::assertResponseStatusCodeSame(Response::HTTP_UNAUTHORIZED, 'Logout should invalidate token'); |
| 54 | + |
| 55 | + $responseBody = json_decode(static::$client->getResponse()->getContent(), true); |
| 56 | + $this->assertEquals('Invalid JWT Token', $responseBody['message']); |
| 57 | + } |
| 58 | + |
| 59 | + public function testShouldNotInvalidateTokenOnLogoutWhenBlockListTokenIsDisabled() |
| 60 | + { |
| 61 | + static::$client = static::createClient(['test_case' => 'BlockListTokenDisabled']); |
| 62 | + $token = static::getAuthenticatedToken(); |
| 63 | + |
| 64 | + static::$client->jsonRequest('GET', '/api/secured', [], ['HTTP_AUTHORIZATION' => "Bearer $token"]); |
| 65 | + static::assertResponseIsSuccessful('Pre condition - a valid token should be able to contact the api'); |
| 66 | + |
| 67 | + static::$client->jsonRequest('GET', '/api/logout', [], ['HTTP_AUTHORIZATION' => "Bearer $token"]); |
| 68 | + static::assertResponseStatusCodeSame(Response::HTTP_FOUND); |
| 69 | + |
| 70 | + static::$client->jsonRequest('GET', '/api/secured', [], ['HTTP_AUTHORIZATION' => "Bearer $token"]); |
| 71 | + static::assertResponseStatusCodeSame(Response::HTTP_OK, 'Logout should NOT invalidate token when block list config is not enabled'); |
| 72 | + } |
| 73 | + |
| 74 | + public function testShouldNotAddJtiWhenBlockListTokenIsDisabled() |
| 75 | + { |
| 76 | + static::$client = static::createClient(['test_case' => 'BlockListTokenDisabled']); |
| 77 | + |
| 78 | + $token = static::getAuthenticatedToken(); |
| 79 | + /** @var JWTManager $jwtManager */ |
| 80 | + $jwtManager = static::getContainer()->get('lexik_jwt_authentication.jwt_manager'); |
| 81 | + $payload = $jwtManager->parse($token); |
| 82 | + self::assertArrayNotHasKey('jti', $payload); |
| 83 | + } |
| 84 | +} |
0 commit comments