-
-
Notifications
You must be signed in to change notification settings - Fork 616
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Invalidate a JWT token - Adding the jti claim by the JWTManager class instead of doing it via a listener #1218
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
If the definition of lexik_jwt_authentication.payload_enrichment.random_jti_enrichment was placed in the Resources/config/blocklist_token.xml file, I feel like this code wouldn't have been necessary
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think this can be cleaned up for 3.0 TBH. Things are kind of in a weird state with this stuff because the payload enrichment services should always be there but the bundle right now only needs the JTI enricher to be active when the blocklist functionality is enabled. Always turning on the JTI enricher in 2.x could be disruptive to downstream users for whatever reason, so it's probably safer to have this block for the next 2.x release, and in 3.0, the bundle defaults to always providing the JTI and a downstream app can replace/remove this service if they want to take control over that.