A JSON Web Token looks opaque, but it's three Base64URL-encoded parts joined by dots. Decoding turns the first two back into JSON. Verifying proves the token was issued by someone holding the right key and hasn't been modified since. Mixing the two up is one of the most common authentication bugs.
What's inside a JWT
eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJzdWIiOiI0MiIsIm5hbWUiOiJBZGEiLCJleHAiOjE3OTk5OTk5OTl9.<signature>
- Header:
{"alg":"HS256","typ":"JWT"}, which says how the token is signed. - Payload: the claims, such as
sub(subject, often a user ID),exp(expiry),iat(issued at),iss(issuer) andaud(audience). - Signature: computed over the first two parts with a secret (HMAC) or a private key (RSA, ECDSA, EdDSA).
The header and payload are encoded, not encrypted. Decoding one takes two lines:
const [header, payload] = token.split(".").slice(0, 2).map((part) =>
JSON.parse(new TextDecoder().decode(Uint8Array.from(atob(part.replace(/-/g, "+").replace(/_/g, "/")), (c) => c.charCodeAt(0)))),
);
That's all a decoder does. It never looks at the signature, so a decoded payload proves nothing about who created it.
What verification checks
Verifying a token means all of the following, in this order:
- The algorithm is one you expect. The server decides which algorithms are acceptable; it must not take the token's word for it.
- The signature matches, using the secret or public key for that issuer.
- The time claims are valid:
expis in the future andnbf(not before), if present, is in the past, with a small tolerance for clock skew. - The issuer and audience match:
issis your identity provider andaudincludes your API. A valid token meant for another service shouldn't work on yours.
Only after all four pass should your code trust sub, roles or any other claim.
Verify in Node.js
With jose, which works in Node.js, browsers, Deno and edge runtimes:
import { jwtVerify, createRemoteJWKSet } from "jose";
const JWKS = createRemoteJWKSet(new URL("https://auth.example.com/.well-known/jwks.json"));
const { payload } = await jwtVerify(token, JWKS, {
issuer: "https://auth.example.com/",
audience: "https://api.example.com",
algorithms: ["RS256"],
});
// payload.sub is now trustworthy
With the older jsonwebtoken package and a shared secret:
import jwt from "jsonwebtoken";
const payload = jwt.verify(token, process.env.JWT_SECRET, {
algorithms: ["HS256"],
issuer: "my-app",
audience: "my-api",
});
Note the trap in jsonwebtoken: jwt.decode(token) exists right next to jwt.verify and returns the payload without any checks.
Both libraries throw when a check fails. Catch that and answer with 401, not 403: an expired or invalid token means the client needs new credentials, which 401 vs 403 explains.
Verify in Python
PyJWT's decode does verify by default, which is the opposite naming of jsonwebtoken:
import jwt
payload = jwt.decode(
token,
key=public_key,
algorithms=["RS256"],
audience="https://api.example.com",
issuer="https://auth.example.com/",
)
Passing options={"verify_signature": False} turns that into a plain decode. Because function names differ between libraries, read the docs of the one you use rather than assuming.
Mistakes that turn decode into a vulnerability
Trusting decoded claims on the server
If an API reads role from jwt.decode() output, anyone can create a token with "role": "admin". Every server-side use of claims must come from a verified token.
Letting the token choose the algorithm
Two classic attacks come from trusting the header's alg:
"alg": "none"declares an unsigned token. Libraries that accept it skip the signature check entirely.- Algorithm confusion: a token signed with HS256 using the server's public RSA key as the HMAC secret. A library that picks the algorithm from the header verifies it successfully.
Always pass an explicit algorithms list. RFC 8725 covers these and other pitfalls.
Ignoring exp, iss and aud
A token with a valid signature can still be expired, or issued for another application by the same identity provider. Configure the issuer and audience checks, not only the key.
Putting secrets in the payload
Anyone holding the token can read its claims. Don't put passwords, API keys or personal data you wouldn't show the user into a JWT.
When decoding without verifying is fine
- Debugging: inspecting what an identity provider put into a token.
- Client-side display: showing the signed-in user's name, or reading
expto refresh a token a minute before it expires. - Routing: reading
kid(key ID) from the header to choose the verification key, which the verify call then uses.
In each case the decoded data is treated as untrusted. exp, iat and nbf are Unix timestamps in seconds; the Unix timestamp guide explains how to read them correctly in JavaScript.
Quick reference
| Task | Decode | Verify |
|---|---|---|
| Read claims while debugging | Yes | Not needed |
| Show the user's name in the UI | Yes | Done by the server |
| Authorize an API request | No | Yes |
| Check if a token expired before sending it | Yes | The server still verifies |
Choose the key by kid |
Yes (header only) | Then verify with that key |