Skip to main content

How to Add Registered Claims to a JWT in PHP with php-jwt

· 4 min read
Jagdish Kumawat
Founder @ Dewiride

With firebase/php-jwt, the registered claims are ordinary keys in the payload array you pass to JWT::encode: iss, sub, aud, iat, nbf, exp and jti. The three times are Unix seconds, which time() gives you.

Syntax​

Syntax
$payload = [
'iss' => '<issuer>',
'sub' => '<subject>',
'aud' => '<audience>',
'iat' => <seconds>,
'nbf' => <seconds>,
'exp' => <seconds>,
'jti' => '<id>',
];
$token = JWT::encode($payload, <key>, '<alg>');
PartWhat it does
'iss', 'sub', 'jti'Strings: who made the token, who it's about and its own ID.
'aud'Who the token is for: one string, or an array of strings.
<seconds>A Unix time in seconds, such as time() or time() + 7200.
JWT::encode(...)Signs the payload with the key and returns the token.

The php-jwt README builds its example payload the same way, with 'iss' => 'example.org', 'aud' => 'example.com', 'iat' and 'nbf' as keys of the array.

The Example​

The example signs a token for a user named ada, issued by https://login.example.org for https://photos.example.org. It sets iat and nbf to now, exp two hours later, and jti to 16 random bytes written as hexadecimal. Then it decodes the token with JWT::decode and prints four of its claims.

Prerequisites​

  • PHP and Composer. This guide uses PHP 8.6.0 and Composer 2.11.3.

Step 1: Create the Project​

Terminal
mkdir demo
cd demo
composer require firebase/php-jwt

composer require installs php-jwt, version 7.2.2 here, and writes the autoloader the code loads.

Step 2: Write the Code​

Create main.php:

main.php, which builds a payload with iss, sub, aud, iat and nbf set to time(), exp two hours later and a bin2hex random jti, encodes it with JWT::encode, decodes it with JWT::decode and echoes iss, aud, exp and jti.

main.php
<?php

declare(strict_types=1);

require __DIR__ . '/vendor/autoload.php';

use Firebase\JWT\JWT;
use Firebase\JWT\Key;

// The signing key: 32 random bytes, written as base64.
// It's in the code only for this example; a real app loads it from its configuration.
$key = base64_decode('C4iNOOORzmu+fnrPK29yq/9n92b/zf5mOs5KvOMaY64=', true);

$now = time();
$payload = [
'iss' => 'https://login.example.org',
'sub' => 'ada',
'aud' => 'https://photos.example.org',
'iat' => $now,
'nbf' => $now,
'exp' => $now + 2 * 60 * 60,
'jti' => bin2hex(random_bytes(16)),
];
$token = JWT::encode($payload, $key, 'HS256');
echo $token . "\n";

$claims = JWT::decode($token, new Key($key, 'HS256'));
echo "iss {$claims->iss}, aud {$claims->aud}, exp {$claims->exp}, jti {$claims->jti}\n";

JWT::decode returns the claims as an object, so each claim is a property such as $claims->iss.

Step 3: Run It​

Terminal
php main.php
Output
eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJpc3MiOiJodHRwczovL2xvZ2luLmV4YW1wbGUub3JnIiwic3ViIjoiYWRhIiwiYXVkIjoiaHR0cHM6Ly9waG90b3MuZXhhbXBsZS5vcmciLCJpYXQiOjE3OTYwNTk0NjYsIm5iZiI6MTc5NjA1OTQ2NiwiZXhwIjoxNzk2MDY2NjY2LCJqdGkiOiI2ZGU5Y2JmZjI0YmI5ZGEyNDBmMWQwNjliODNiMjRkYyJ9.I4B6BzQwVJvuD7lGKNFnqews-lGubREOOGSP_tmw_mE
iss https://login.example.org, aud https://photos.example.org, exp 1796066666, jti 6de9cbff24bb9da240f1d069b83b24dc

A zsh terminal where php main.php prints the token, then iss https://login.example.org, aud https://photos.example.org, the exp number and a 32-character hexadecimal jti.

What Happened​

  • php-jwt wrote the array as the token's payload, in the order of its keys.
  • JWT::decode accepted the token: the signature matched, nbf and iat weren't in the future, and exp wasn't past.
  • exp is time() + 2 * 60 * 60, two hours in seconds.

JWT::decode checks exp, nbf and iat, but it doesn't check iss or aud. Code that relies on them has to compare them itself.

Common Mistakes​

MistakeWhat happensDo this instead
Writing times in millisecondsThe token looks valid for tens of thousands of yearsUse time(), which is in seconds
Leaving out expJWT::decode accepts the token foreverAlways set an expiry
Expecting JWT::decode to check audIt doesn't, so a token for another API passesCompare aud yourself after decoding

Troubleshooting​

SymptomLikely causeFix
Firebase\JWT\BeforeValidException: Cannot handle token with nbf prior to ...nbf is in the futureSet nbf to now, or leave it out
UnexpectedValueException: Payload exp must be a numberexp was a string or a datePass a number of seconds

FAQ​

Do I have to set all seven claims?​

No. The JWT standard makes every registered claim optional. Set the ones your API checks, and always an expiry.

Why bin2hex(random_bytes(16)) and not a UUID?​

PHP has no UUID function. Sixteen random bytes written as hexadecimal make an ID that's just as unlikely to repeat, which is all jti needs.

Conclusion​

Registered claims in php-jwt are keys in the payload array, with Unix seconds for the times. JWT::encode signs them, and JWT::decode checks the times but leaves iss and aud to you.

Additional Resources​