How to Check a JWT's Issuer and Audience in PHP with php-jwt
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
$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');
}
| Part | What it does |
|---|---|
JWT::decode(...) | Verifies the signature and checks exp, nbf and iat, then returns the claims. |
$claims->iss ?? null | The 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. |
UnexpectedValueException | The 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
Step 1: Create the Project
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:

<?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
php main.php
JWT::decode alone accepts it, with aud https://photos.example.org
accepted: sub ada
rejected: Invalid audience
rejected: Invalid issuer

What Happened
JWT::decodealone accepted the token without being told which issuer or audience to expect, because it doesn't take one.checkaccepted 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
| Mistake | What happens | Do this instead |
|---|---|---|
Trusting JWT::decode to check iss and aud | It doesn't, so tokens from other issuers or for other APIs pass | Compare both yourself |
Comparing $claims->aud === $audience | It fails when aud is an array, even one that contains your audience | Use (array) and in_array |
Reading $claims->iss without ?? | A token without iss prints a warning | Use $claims->iss ?? null |
Troubleshooting
| Symptom | Likely cause | Fix |
|---|---|---|
| A token for another API is accepted | aud isn't checked | Add the audience check after JWT::decode |
Warning: Undefined property: stdClass::$aud | The 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
- php-jwt, the library's README.
- RFC 8725 section 3.9: Use and Validate Audience.
