-
-
Notifications
You must be signed in to change notification settings - Fork 597
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
3 changed files
with
80 additions
and
0 deletions.
There are no files selected for viewing
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,30 @@ | ||
<?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; | ||
|
||
final class HasClaim implements Constraint | ||
{ | ||
/** @param non-empty-string $claim */ | ||
public function __construct(private readonly string $claim) | ||
{ | ||
} | ||
|
||
public function assert(Token $token): void | ||
{ | ||
if (! $token instanceof UnencryptedToken) { | ||
throw ConstraintViolation::error('You should pass a plain token', $this); | ||
} | ||
|
||
$claims = $token->claims(); | ||
|
||
if (! $claims->has($this->claim)) { | ||
throw ConstraintViolation::error('The token does not have the claim "' . $this->claim . '"', $this); | ||
} | ||
} | ||
} |
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,49 @@ | ||
<?php | ||
declare(strict_types=1); | ||
|
||
namespace Lcobucci\JWT\Tests\Validation\Constraint; | ||
|
||
use Lcobucci\JWT\Token; | ||
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 */ | ||
public function assertShouldRaiseExceptionWhenClaimIsNotSet(): void | ||
{ | ||
$this->expectException(ConstraintViolation::class); | ||
$this->expectExceptionMessage('The token does not have the claim "claimId"'); | ||
|
||
$constraint = new HasClaim('claimId'); | ||
$constraint->assert($this->buildToken()); | ||
} | ||
|
||
/** @test */ | ||
public function assertShouldRaiseExceptionWhenTokenIsNotAPlainToken(): void | ||
{ | ||
$this->expectException(ConstraintViolation::class); | ||
$this->expectExceptionMessage('You should pass a plain token'); | ||
|
||
$constraint = new HasClaim('claimId'); | ||
$constraint->assert($this->createMock(Token::class)); | ||
} | ||
|
||
/** @test */ | ||
public function assertShouldNotRaiseExceptionWhenClaimMatches(): void | ||
{ | ||
$token = $this->buildToken(['claimId' => 'claimValue']); | ||
$constraint = new HasClaim('claimId'); | ||
|
||
$constraint->assert($token); | ||
$this->addToAssertionCount(1); | ||
} | ||
} |