JWT validation must verify the signature, expected algorithm and required issuer, audience and time claims.
What It Does
JWT validation must verify the signature, expected algorithm and required issuer, audience and time claims. The following examples distinguish a normal use case, a boundary case and a practical application.
Requirements and Compatibility
A validated JWT library and trusted signing public key; example claim checks are never a substitute for verifying signatures.
The JWT header and payload are not trustworthy before a pinned-algorithm cryptographic signature check, and decoded claims still need issuer/audience/time validation.
The code focuses on the core concept. Production implementation may also need routes, validation, persistence, authorization or deployment tooling.
Example 1: Basic Behavior
Install an audited JWT implementation; never implement signature verification with base64 decoding alone.
composer require firebase/php-jwt
This is a command-line example; use it in its appropriate file or shell, not in the PHP source editor.
Example 2: Another Case
Verify cryptographic signature with a server-chosen algorithm and then validate issuer, audience and expiry.
<?php
require __DIR__ . '/vendor/autoload.php';
use Firebase\JWT\JWT;
use Firebase\JWT\Key;
$token = (string) ($_SERVER['HTTP_AUTHORIZATION'] ?? '');
$token = preg_replace('/^Bearer\s+/i', '', $token);
$publicKey = file_get_contents('/srv/secrets/jwt-public.pem');
if ($publicKey === false) throw new RuntimeException('Public key missing');
$claims = JWT::decode($token, new Key($publicKey, 'RS256'));
$now = time();
if ($claims->iss !== 'https://issuer.example.test' ||
!in_array('api', (array) $claims->aud, true) ||
($claims->exp ?? 0) <= $now) throw new RuntimeException('Invalid token claims');
This example depends on an installed extension, third-party library or configured service and is not a standalone local test.
Practical Example: Putting It to Use
Reject a token if any required claim fails; distinguish claim checks from signature verification.
<?php
$claims = ["iss" => "https://issuer.example.test", "aud" => ["api"], "exp" => time() + 60];
$valid = in_array("api", $claims["aud"], true) && $claims["exp"] > time();
var_dump($valid); // Claim checks assume a library already verified the signature
Important Behavior and Edge Cases
Decoding JWT base64 segments or trusting alg from the token header is not authentication.
Comparison with Related Functions or Approaches
| Option | What it means in practice |
|---|---|
| JWT Validation: Algorithms, Issuers, and Audiences | JWT validation must verify the signature, expected algorithm and required issuer, audience and time claims. |
| Related approach | Use an audited JOSE/JWT library with server-pinned verification keys. |
Common Beginner Mistakes
| Mistake | Correct approach |
|---|---|
| Ignoring the important boundary for JWT Validation: Algorithms, Issuers, and Audiences | Decoding JWT base64 segments or trusting alg from the token header is not authentication. |
| Assuming a related mechanism has the same behavior | Use an audited JOSE/JWT library with server-pinned verification keys. |
Key Takeaways
- JWT validation must verify the signature, expected algorithm and required issuer, audience and time claims.
- Decoding JWT base64 segments or trusting alg from the token header is not authentication.
- Use an audited JOSE/JWT library with server-pinned verification keys.
Before adapting these examples, verify the return values, test the failure paths and confirm that your application meets its security and environment requirements.