Skip to content

Commit

Permalink
add HasClaim constraint
Browse files Browse the repository at this point in the history
  • Loading branch information
freebuu authored and lcobucci committed Apr 11, 2024
1 parent 22cf2f4 commit 57eafdd
Show file tree
Hide file tree
Showing 3 changed files with 80 additions and 0 deletions.
1 change: 1 addition & 0 deletions docs/validating-tokens.md
Original file line number Diff line number Diff line change
Expand Up @@ -90,5 +90,6 @@ This library provides the following constraints:
* `Lcobucci\JWT\Validation\Constraint\StrictValidAt`: verifies presence and validity of the claims `iat`, `nbf`, and `exp` (supports leeway configuration)
* `Lcobucci\JWT\Validation\Constraint\LooseValidAt`: verifies the claims `iat`, `nbf`, and `exp`, when present (supports leeway configuration)
* `Lcobucci\JWT\Validation\Constraint\HasClaimWithValue`: verifies that a **custom claim** has the expected value (not recommended when comparing cryptographic hashes)
* `Lcobucci\JWT\Validation\Constraint\HasClaim`: verifies that a claim is present

You may also create your [own validation constraints](extending-the-library.md#validation-constraints).
30 changes: 30 additions & 0 deletions src/Validation/Constraint/HasClaim.php
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);
}
}
}
49 changes: 49 additions & 0 deletions tests/Validation/Constraint/HasClaimTest.php
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);
}
}

0 comments on commit 57eafdd

Please sign in to comment.