githubEdit

Claim and Header Validation

After verifying a signature or decrypting a token, you must validate the claims and headers before trusting the token content.

triangle-exclamation

Validate Claims

The ClaimCheckerManager verifies claims contained in the token payload.

<?php

use Jose\Component\Checker\AudienceChecker;
use Jose\Component\Checker\ClaimCheckerManager;
use Jose\Component\Checker\ExpirationTimeChecker;
use Jose\Component\Checker\IssuedAtChecker;
use Jose\Component\Checker\IssuerChecker;
use Jose\Component\Checker\NotBeforeChecker;
use Symfony\Component\Clock\NativeClock;

require_once 'vendor/autoload.php';

// A PSR-20 clock implementation is required for time-based checkers
$clock = new NativeClock();

$claimCheckerManager = new ClaimCheckerManager([
    new IssuedAtChecker($clock),
    new NotBeforeChecker($clock),
    new ExpirationTimeChecker($clock),
    new IssuerChecker(['https://auth.example.com']),
    new AudienceChecker('https://api.example.com'),
]);

// $payload is the decoded payload from a verified JWS or decrypted JWE
$payload = json_decode($jws->getPayload(), true);

// Check claims. The second parameter lists the mandatory claims.
$claimCheckerManager->check($payload, ['iss', 'aud', 'exp']);
// Throws an exception if any check fails
circle-info

The NativeClock class is from the symfony/clock component. You can use any Psr\Clock\ClockInterface implementation.


Allow a Time Drift

Network latency or clock skew between servers may cause valid tokens to be rejected. You can allow a small time drift (in seconds):


Custom Claim Validation with CallableChecker

For application-specific claims, use the CallableChecker:


Validate with IsEqualChecker

For simple equality checks on claims or headers:


Complete Example: Sign, Verify, and Validate

A full example combining token creation, verification, and claim validation:

Last updated

Was this helpful?