-
-
Notifications
You must be signed in to change notification settings - Fork 611
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feature #1218 Invalidate a JWT token - Adding the jti claim by the JW…
…TManager class instead of doing it via a listener (ldaspt) This PR was squashed before being merged into the 2.x branch. Discussion ---------- Invalidate a JWT token - Adding the jti claim by the JWTManager class instead of doing it via a listener Hello `@chalasr` and `@mbabker`, This PR aims to address the enhancements suggested by `@mbabker` in the discussion of the PR #1170. `@see` #1170 (comment) Changes included: * Remove AddClaimsToJWTListener * Addition of the concept of Payload Enrichment which aims to enrich the payload just before generating the token in the JwtManager class * Added a random jti when the JWT token invalidation functionality is enabled * Added a null payload enrichment and a chain enrichment (which may be superfluous) The payload enrichment cannot be overridden via the bundle configuration, if the developer wants to overload it, he will have to decorate the service `lexik_jwt_authentication.payload_enrichment`. If necessary, I can do as for the `lexik_jwt_authentication.encoder` service so that this is configurable via the bundle configuration I hope the PR answers the request correctly Please review the changes at your convenience, and I welcome any feedback or further suggestions Commits ------- 069b7bb Invalidate a JWT token - Adding the jti claim by the JWTManager class instead of doing it via a listener
- Loading branch information
Showing
16 changed files
with
226 additions
and
29 deletions.
There are no files selected for viewing
22 changes: 22 additions & 0 deletions
22
DependencyInjection/Compiler/CollectPayloadEnrichmentsPass.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
<?php | ||
|
||
namespace Lexik\Bundle\JWTAuthenticationBundle\DependencyInjection\Compiler; | ||
|
||
use Symfony\Component\DependencyInjection\Compiler\CompilerPassInterface; | ||
use Symfony\Component\DependencyInjection\Compiler\PriorityTaggedServiceTrait; | ||
use Symfony\Component\DependencyInjection\ContainerBuilder; | ||
|
||
class CollectPayloadEnrichmentsPass implements CompilerPassInterface | ||
{ | ||
use PriorityTaggedServiceTrait; | ||
|
||
public function process(ContainerBuilder $container): void | ||
{ | ||
if (!$container->hasDefinition('lexik_jwt_authentication.payload_enrichment')) { | ||
return; | ||
} | ||
|
||
$container->getDefinition('lexik_jwt_authentication.payload_enrichment') | ||
->replaceArgument(0, $this->findAndSortTaggedServices('lexik_jwt_authentication.payload_enrichment', $container)); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
<?php | ||
|
||
namespace Lexik\Bundle\JWTAuthenticationBundle\Services\PayloadEnrichment; | ||
|
||
use Lexik\Bundle\JWTAuthenticationBundle\Services\PayloadEnrichmentInterface; | ||
use Symfony\Component\Security\Core\User\UserInterface; | ||
|
||
class ChainEnrichment implements PayloadEnrichmentInterface | ||
{ | ||
private $enrichments; | ||
|
||
/** | ||
* @param PayloadEnrichmentInterface[] $enrichments | ||
*/ | ||
public function __construct(array $enrichments) | ||
{ | ||
$this->enrichments = $enrichments; | ||
} | ||
|
||
public function enrich(UserInterface $user, array &$payload): void | ||
{ | ||
foreach ($this->enrichments as $enrichment) { | ||
$enrichment->enrich($user, $payload); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
<?php | ||
|
||
namespace Lexik\Bundle\JWTAuthenticationBundle\Services\PayloadEnrichment; | ||
|
||
use Lexik\Bundle\JWTAuthenticationBundle\Services\PayloadEnrichmentInterface; | ||
use Symfony\Component\Security\Core\User\UserInterface; | ||
|
||
class NullEnrichment implements PayloadEnrichmentInterface | ||
{ | ||
public function enrich(UserInterface $user, array &$payload): void | ||
{ | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
<?php | ||
|
||
namespace Lexik\Bundle\JWTAuthenticationBundle\Services\PayloadEnrichment; | ||
|
||
use Lexik\Bundle\JWTAuthenticationBundle\Services\PayloadEnrichmentInterface; | ||
use Symfony\Component\Security\Core\User\UserInterface; | ||
|
||
class RandomJtiEnrichment implements PayloadEnrichmentInterface | ||
{ | ||
public function enrich(UserInterface $user, array &$payload): void | ||
{ | ||
$payload['jti'] = bin2hex(random_bytes(16)); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
<?php | ||
|
||
namespace Lexik\Bundle\JWTAuthenticationBundle\Services; | ||
|
||
use Symfony\Component\Security\Core\User\UserInterface; | ||
|
||
interface PayloadEnrichmentInterface | ||
{ | ||
public function enrich(UserInterface $user, array &$payload): void; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
<?php | ||
|
||
namespace Lexik\Bundle\JWTAuthenticationBundle\Services\PayloadEnrichment; | ||
|
||
use Lexik\Bundle\JWTAuthenticationBundle\Services\PayloadEnrichmentInterface; | ||
use PHPUnit\Framework\TestCase; | ||
use Symfony\Component\Security\Core\User\UserInterface; | ||
|
||
class ChainEnrichmentTest extends TestCase | ||
{ | ||
public function testEnrich(): void | ||
{ | ||
$payload = ['foo' => 'bar']; | ||
|
||
$enrichmentFoo = new class() implements PayloadEnrichmentInterface { | ||
public function enrich(UserInterface $user, array &$payload): void | ||
{ | ||
$payload['foo'] = 'baz'; | ||
} | ||
}; | ||
|
||
$enrichmentBar = new class() implements PayloadEnrichmentInterface { | ||
public function enrich(UserInterface $user, array &$payload): void | ||
{ | ||
$payload['bar'] = 'qux'; | ||
} | ||
}; | ||
|
||
$chainEnrichment = new ChainEnrichment([$enrichmentFoo, $enrichmentBar]); | ||
$chainEnrichment->enrich($this->createMock(UserInterface::class), $payload); | ||
|
||
$this->assertEquals(['foo' => 'baz', 'bar' => 'qux'], $payload); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
<?php | ||
|
||
namespace Lexik\Bundle\JWTAuthenticationBundle\Services\PayloadEnrichment; | ||
|
||
use PHPUnit\Framework\TestCase; | ||
use Symfony\Component\Security\Core\User\UserInterface; | ||
|
||
class NullEnrichmentTest extends TestCase | ||
{ | ||
public function testEnrich(): void | ||
{ | ||
$payload = ['foo' => 'bar']; | ||
$enrichment = new NullEnrichment(); | ||
$enrichment->enrich($this->createMock(UserInterface::class), $payload); | ||
|
||
$this->assertEquals(['foo' => 'bar'], $payload); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
<?php | ||
|
||
namespace Lexik\Bundle\JWTAuthenticationBundle\Services\PayloadEnrichment; | ||
|
||
use PHPUnit\Framework\TestCase; | ||
use Symfony\Component\Security\Core\User\UserInterface; | ||
|
||
class RandomJtiEnrichmentTest extends TestCase | ||
{ | ||
public function testEnrich(): void | ||
{ | ||
$payload = ['foo' => 'bar']; | ||
$enrichment = new RandomJtiEnrichment(); | ||
$enrichment->enrich($this->createMock(UserInterface::class), $payload); | ||
|
||
$this->assertArrayHasKey('jti', $payload); | ||
$this->assertIsString($payload['jti']); | ||
$this->assertArrayHasKey('foo', $payload); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters