Identity and access JSON Web Tokens (JWT) 4 min read
What is a JWT?
You copy a token out of a request to billing-api. You don’t have the key that signed it. How much of it can you read?
-
Three parts, two of them readable
A JWT is three parts joined by dots: a header, a payload and a signature. The header and payload are JSON, encoded with Base64URL so they travel safely in a URL. Encoding isn’t encryption.
So without any key, you can read all of it: the header (
"alg": "HS256") and every claim ("sub": "usr_4821","aud": "billing-api","role": "viewer"). The signature is 32 bytes of HMAC-SHA256, not JSON.Two parts anyone can read, and one part only the key can make. In short A JWT is a signed note, not a locked box.
-
Change one claim and it breaks
The signature is computed over the header and payload with a key only the issuer holds. Whoever receives the token recomputes it over what arrived and compares.
Change
rolefromviewertoadminand keep the old signature, and billing-api gets a different value and rejects it. To make a matching signature you’d need the issuer’s key.Someone changes role from viewer to admin in a token but keeps the old signature, and sends it to billing-api. billing-api recomputes the signature over what arrived, gets a different value, and rejects the token.
Readable by anyone, changeable by no one without the key. In short The signature shows whether the token was changed.
-
Only if the verifier checks properly
A careful verifier uses the algorithm it expects, not whatever the header says. A header claiming
"alg": "none"means “no signature needed”, and a verifier that believes it would accept anything.Then it checks
issis the issuer it trusts (examp1e.comwith a 1 isn’t),audis its own name, andexphasn’t passed.In short Fix the algorithm, then check issuer, audience and expiry.
What signing doesn’t hide
A teammate puts a user’s API key in the token: “It’s signed, so it’s safe.” But signing stops changes, not reading. Anyone who sees the token, in a log, a browser or a proxy, can decode it.
Never put passwords, API keys or sensitive personal data in a signed-only JWT. Store them on the server and put only an ID in the token.
Keep this
A JWT is a signed note, not a locked box.
Anyone holding it can read it. The signature only shows whether it was changed. Never put passwords, API keys or sensitive personal data in a signed-only JWT.- Header
- Says how the token was signed. A careful verifier ignores it and uses the algorithm it expects.
- Payload
- The claims. Base64URL is encoding, not encryption, so anyone can read them.
- Signature
- Shows whether the header or payload changed, when checked with the right algorithm and key.
Questions worth carrying
Does a valid signature mean the user can access this resource?
No. A signature shows the token wasn’t changed. Whether the user may do something is a separate decision your app makes from the checked claims and its own rules.
What if someone steals a valid token?
They can use it until it expires or is revoked. Short lifetimes, tying tokens to a client, and storing them carefully all shrink that window.
Why not just encrypt the token?
You can. Encrypted JWTs exist (JWE). Often the simpler fix is to keep sensitive data on the server and put only an ID in the token.