Skip to main content

How to Check a JWT's Issuer and Audience in PHP with php-jwt

· 5 min read
Jagdish Kumawat
Founder @ Dewiride

firebase/php-jwt's JWT::decode checks a token's signature and its times, but it never looks at iss or aud: a token made for another API passes. Check both yourself, right after JWT::decode, and throw when they don't match.

Syntax​

Syntax
$claims = JWT::decode(<token>, new Key(<key>, '<alg>'));
if (($claims->iss ?? null) !== <expected iss>) {
throw new UnexpectedValueException('Invalid issuer');
}
if (!in_array(<expected aud>, (array) ($claims->aud ?? []), true)) {
throw new UnexpectedValueException('Invalid audience');
}
PartWhat it does
JWT::decode(...)Verifies the signature and checks exp, nbf and iat, then returns the claims.
$claims->iss ?? nullThe token's issuer, or null when it has none.
(array) ($claims->aud ?? [])The token's audiences as an array, whether aud was one string, an array or missing.
in_array(..., true)Checks that your audience is among them, comparing exactly.
UnexpectedValueExceptionThe class php-jwt's own exceptions extend, so one catch handles every rejection.

The php-jwt source documents what JWT::decode throws: a SignatureInvalidException, a BeforeValidException for nbf and iat, and an ExpiredException for exp. Nothing in it mentions iss or aud.

The Example​

The example signs a token issued by https://login.example.org for https://photos.example.org. It first decodes it with JWT::decode alone, then checks it three times with a function that adds the two comparisons: with the right issuer and audience, with the audience of another API, and with another issuer.

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 signs a token for the photos API, defines check() that calls JWT::decode and then compares iss with !== and aud with in_array, prints what JWT::decode alone accepts, and checks the token three ways.

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);

$token = JWT::encode([
'iss' => 'https://login.example.org',
'sub' => 'ada',
'aud' => 'https://photos.example.org',
'exp' => time() + 2 * 60 * 60,
], $key, 'HS256');

// JWT::decode checks the signature and the times, but not iss or aud.
function check(string $key, string $token, string $issuer, string $audience): void
{
try {
$claims = JWT::decode($token, new Key($key, 'HS256'));
if (($claims->iss ?? null) !== $issuer) {
throw new UnexpectedValueException('Invalid issuer');
}
if (!in_array($audience, (array) ($claims->aud ?? []), true)) {
throw new UnexpectedValueException('Invalid audience');
}
echo "accepted: sub {$claims->sub}\n";
} catch (UnexpectedValueException $e) {
echo "rejected: {$e->getMessage()}\n";
}
}

$decoded = JWT::decode($token, new Key($key, 'HS256'));
echo "JWT::decode alone accepts it, with aud {$decoded->aud}\n";

check($key, $token, 'https://login.example.org', 'https://photos.example.org');
check($key, $token, 'https://login.example.org', 'https://videos.example.org');
check($key, $token, 'https://other.example.org', 'https://photos.example.org');

Step 3: Run It​

Terminal
php main.php
Output
JWT::decode alone accepts it, with aud https://photos.example.org
accepted: sub ada
rejected: Invalid audience
rejected: Invalid issuer

A zsh terminal where php main.php prints that JWT::decode alone accepts the token with aud https://photos.example.org, then accepted sub ada, rejected Invalid audience and rejected Invalid issuer.

What Happened​

  • JWT::decode alone accepted the token without being told which issuer or audience to expect, because it doesn't take one.
  • check accepted the right issuer and audience, and rejected the videos audience and the other issuer with its own messages.

Because aud can be a single string or an array, (array) turns it into an array first, and in_array with true looks for your audience in it. In a separate test, a token whose aud was ["https://orders.example.com", "https://billing.example.com"] passed the same check for the orders API, and a token with no aud failed it.

Common Mistakes​

MistakeWhat happensDo this instead
Trusting JWT::decode to check iss and audIt doesn't, so tokens from other issuers or for other APIs passCompare both yourself
Comparing $claims->aud === $audienceIt fails when aud is an array, even one that contains your audienceUse (array) and in_array
Reading $claims->iss without ??A token without iss prints a warningUse $claims->iss ?? null

Troubleshooting​

SymptomLikely causeFix
A token for another API is acceptedaud isn't checkedAdd the audience check after JWT::decode
Warning: Undefined property: stdClass::$audThe token has no aud, and the code read it without ??Use $claims->aud ?? []

FAQ​

Why throw UnexpectedValueException?​

php-jwt's own exceptions, such as SignatureInvalidException and ExpiredException, extend UnexpectedValueException, so one catch (UnexpectedValueException $e) handles a bad signature, an expired token and your own issuer and audience checks alike.

Can I accept several issuers?​

Yes. Keep a list of the issuers you trust and check $claims->iss with in_array($claims->iss ?? null, $trusted, true).

Conclusion​

php-jwt verifies signatures and times, and leaves iss and aud to you. Two comparisons after JWT::decode close that gap.

Additional Resources​