Skip to main content

How to Check a JWT's Issuer and Audience in Node.js with jose

· 5 min read
Jagdish Kumawat
Founder @ Dewiride

jose's jwtVerify checks a token's issuer and audience when you pass the issuer and audience options. A token from another issuer, or one made for another API, is rejected with unexpected "iss" claim value or unexpected "aud" claim value.

Syntax​

Syntax
const { payload } = await jwtVerify(<token>, <key>, {
algorithms: ['<alg>'],
issuer: '<expected iss>',
audience: '<expected aud>',
});
PartWhat it does
<token>, <key>The JWT to verify and the key that checks its signature.
algorithmsThe signing algorithms you accept.
issuerThe iss you expect, or an array of several you trust.
audienceThe aud you expect, usually your own API's name, or an array of several.
payloadThe token's claims, returned only when every check passes.

The jose documentation for JWTVerifyOptions describes issuer as "Expected JWT "iss" (Issuer) Claim value(s). Requires the claim to be present." and audience the same way for aud.

The Example​

The example signs one token, issued by https://login.example.org for https://photos.example.org, and verifies it three times: with the right issuer and audience, with the audience of another API, and with another issuer.

Prerequisites​

  • Node.js. This guide uses Node.js 26.13.1.

Step 1: Create the Project​

Terminal
mkdir demo
cd demo
npm init -y
npm pkg set type=module
npm install jose

npm init -y writes a package.json, npm pkg set type=module lets the code use import, and npm install jose adds the library, version 6.2.18 here.

Step 2: Write the Code​

Create main.js:

main.js, which signs a token for the photos API and defines check(issuer, audience) calling jwtVerify with the issuer and audience options, then checks the right pair, the videos audience and another issuer.

main.js
import { SignJWT, jwtVerify, errors } from 'jose';

// 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.
const key = Uint8Array.fromBase64('C4iNOOORzmu+fnrPK29yq/9n92b/zf5mOs5KvOMaY64=');

const token = await new SignJWT()
.setProtectedHeader({ alg: 'HS256' })
.setIssuer('https://login.example.org')
.setSubject('ada')
.setAudience('https://photos.example.org')
.setExpirationTime('2h')
.sign(key);

async function check(issuer, audience) {
try {
const { payload } = await jwtVerify(token, key, { algorithms: ['HS256'], issuer, audience });
console.log(`accepted: sub ${payload.sub}`);
} catch (err) {
if (!(err instanceof errors.JOSEError)) throw err;
console.log(`rejected: ${err.message}`);
}
}

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

{ algorithms: ['HS256'], issuer, audience } uses shorthand property names: issuer means issuer: issuer. check catches only jose's own errors, which all extend errors.JOSEError.

Step 3: Run It​

Terminal
node main.js
Output
accepted: sub ada
rejected: unexpected "aud" claim value
rejected: unexpected "iss" claim value

A zsh terminal where node main.js prints accepted sub ada, then rejected unexpected aud claim value, then rejected unexpected iss claim value.

What Happened​

  • The right issuer and audience passed, and jwtVerify returned the claims.
  • The videos audience failed. The signature was valid and the issuer was right, but the token wasn't made for https://videos.example.org.
  • Another issuer failed in the same way, on iss.

Both rejections are JWTClaimValidationFailed errors. Without the audience option, jose doesn't look at aud at all, so a token made for any API would pass. That's why an API that shares a sign-in service with other APIs must always pass its own audience.

Common Mistakes​

MistakeWhat happensDo this instead
Leaving out audiencejose ignores aud, and a token made for another API passesPass your API's own audience
Writing the issuer with a slash too many or too fewhttps://login.example.org/ and https://login.example.org are different issuersCopy the issuer exactly as the token has it
Catching every error in checkReal bugs look like rejected tokensCatch errors.JOSEError and rethrow the rest

Troubleshooting​

SymptomLikely causeFix
unexpected "aud" claim valueThe token's aud doesn't contain your audienceCheck which API the token was made for
unexpected "iss" claim valueThe token comes from an issuer you didn't listCheck the issuer, or add it to an array if you trust it
missing required "aud" claimThe token has no aud, but you asked for oneHave the sign-in service set an audience

FAQ​

Can I accept several issuers or audiences?​

Yes. Pass an array to issuer or audience. For aud, the token is accepted when any of its values is one you listed.

Does jwtVerify check aud when I don't pass audience?​

No. It checks the signature and the times, and leaves aud alone.

How do I know which claim failed?​

The error message names it, and a JWTClaimValidationFailed error also has a claim property with the claim's name, such as aud.

Conclusion​

Pass issuer and audience to jwtVerify, and jose rejects tokens from issuers you don't trust and tokens made for other APIs, each with an error that names the claim.

Additional Resources​