-
-
Notifications
You must be signed in to change notification settings - Fork 602
Add constraint to verify the existence of a custom claim #1053
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
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
This file contains hidden or 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 hidden or 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,35 @@ | ||
<?php | ||
declare(strict_types=1); | ||
|
||
namespace Lcobucci\JWT\Validation\Constraint; | ||
|
||
use Lcobucci\JWT\Token; | ||
use Lcobucci\JWT\UnencryptedToken; | ||
use Lcobucci\JWT\Validation\Constraint; | ||
use Lcobucci\JWT\Validation\ConstraintViolation; | ||
|
||
use function in_array; | ||
|
||
final class HasClaim implements Constraint | ||
{ | ||
/** @param non-empty-string $claim */ | ||
public function __construct(private readonly string $claim) | ||
{ | ||
if (in_array($claim, Token\RegisteredClaims::ALL, true)) { | ||
throw CannotValidateARegisteredClaim::create($claim); | ||
} | ||
} | ||
|
||
public function assert(Token $token): void | ||
{ | ||
if (! $token instanceof UnencryptedToken) { | ||
throw ConstraintViolation::error('You should pass a plain token', $this); | ||
} | ||
|
||
$claims = $token->claims(); | ||
lcobucci marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
if (! $claims->has($this->claim)) { | ||
throw ConstraintViolation::error('The token does not have the claim "' . $this->claim . '"', $this); | ||
} | ||
} | ||
} |
This file contains hidden or 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,79 @@ | ||
<?php | ||
declare(strict_types=1); | ||
|
||
namespace Lcobucci\JWT\Tests\Validation\Constraint; | ||
|
||
use Lcobucci\JWT\Token; | ||
use Lcobucci\JWT\Validation\Constraint\CannotValidateARegisteredClaim; | ||
use Lcobucci\JWT\Validation\Constraint\HasClaim; | ||
use Lcobucci\JWT\Validation\ConstraintViolation; | ||
|
||
/** | ||
* @covers \Lcobucci\JWT\Validation\ConstraintViolation | ||
* @covers \Lcobucci\JWT\Validation\Constraint\HasClaim | ||
* | ||
* @uses \Lcobucci\JWT\Token\DataSet | ||
* @uses \Lcobucci\JWT\Token\Plain | ||
* @uses \Lcobucci\JWT\Token\Signature | ||
*/ | ||
final class HasClaimTest extends ConstraintTestCase | ||
{ | ||
/** | ||
* @test | ||
* @dataProvider registeredClaims | ||
* | ||
* @covers \Lcobucci\JWT\Validation\Constraint\CannotValidateARegisteredClaim | ||
* | ||
* @param non-empty-string $claim | ||
*/ | ||
public function registeredClaimsCannotBeValidatedUsingThisConstraint(string $claim): void | ||
{ | ||
$this->expectException(CannotValidateARegisteredClaim::class); | ||
$this->expectExceptionMessage( | ||
'The claim "' . $claim . '" is a registered claim, another constraint must be used to validate its value', | ||
); | ||
|
||
new HasClaim($claim); | ||
} | ||
|
||
/** @return iterable<non-empty-string, array{non-empty-string}> */ | ||
public static function registeredClaims(): iterable | ||
{ | ||
foreach (Token\RegisteredClaims::ALL as $claim) { | ||
yield $claim => [$claim]; | ||
} | ||
} | ||
|
||
/** @test */ | ||
public function assertShouldRaiseExceptionWhenClaimIsNotSet(): void | ||
{ | ||
$constraint = new HasClaim('claimId'); | ||
|
||
$this->expectException(ConstraintViolation::class); | ||
$this->expectExceptionMessage('The token does not have the claim "claimId"'); | ||
freebuu marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
$constraint->assert($this->buildToken()); | ||
} | ||
|
||
/** @test */ | ||
public function assertShouldRaiseExceptionWhenTokenIsNotAPlainToken(): void | ||
{ | ||
$token = $this->createMock(Token::class); | ||
$constraint = new HasClaim('claimId'); | ||
|
||
$this->expectException(ConstraintViolation::class); | ||
$this->expectExceptionMessage('You should pass a plain token'); | ||
|
||
$constraint->assert($token); | ||
} | ||
|
||
/** @test */ | ||
public function assertShouldNotRaiseExceptionWhenClaimMatches(): void | ||
{ | ||
$token = $this->buildToken(['claimId' => 'claimValue']); | ||
$constraint = new HasClaim('claimId'); | ||
|
||
$constraint->assert($token); | ||
$this->addToAssertionCount(1); | ||
} | ||
} |
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.
Uh oh!
There was an error while loading. Please reload this page.