The request comes back 401. The token sits in the Authorization header, a long string of letters and dots that looks like the one that worked an hour ago.
So you go looking for a way to read it. That search is where most JWT debugging goes wrong, because reading a token and trusting a token are two very different acts.
Three parts, two dots
A JSON Web Token is a string built from three parts joined by dots. The parts are the header, the payload, and the signature, and RFC 7519 defines the format.
Here is the demo token from jwt.io, the one used in their introduction:
eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJzdWIiOiIxMjM0NTY3ODkwIiwibmFtZSI6IkpvaG4gRG9lIiwiaWF0IjoxNTE2MjM5MDIyfQ.SflKxwRJSMeKKF2QT4fwpMeJf36POk6yJV_adQssw5c
It is public sample data for a fake user named John Doe. Print it, paste it, take it apart. Nobody gets hurt.
Each part is base64url encoded. Base64url is base64 with a URL-safe alphabet, where - and _ stand in for + and /, and the padding is usually left off.
That alphabet keeps the token safe inside HTTP headers and URLs. It does nothing else. Encoding is a transport trick, not a lock.
The header names the algorithm
Decoding the first part gives a small JSON object:
{
"alg": "HS256",
"typ": "JWT"
}
The typ field declares the token type. The alg field names the algorithm that produced the signature.
HS256 means the signature is an HMAC keyed with a shared secret and hashed with SHA-256. RS256 means an RSA signature instead, created with a private key and checkable with the matching public key.
That one field shapes the whole verification story. An RS256 token will never verify against an HMAC secret, and an HS256 token means nothing to a verifier holding only a public key.
The payload is readable by anyone
Decoding the middle part of the demo token gives this:
{
"sub": "1234567890",
"name": "John Doe",
"iat": 1516239022
}
No key was needed to get here. Split on the dots, run a base64url decode, parse the JSON, and the payload is sitting in plain sight.
The demo payload even shows its age. The iat value 1516239022 converts to a date in January 2018.
The signature guards this payload against tampering. It never hides the payload from readers, and jwt.io puts it plainly: signed token contents are readable by anyone.
Keep secrets out of a payload for this reason. User IDs and roles are fine in there. Passwords, reset tokens, and private data are not.
The claims do the talking
Each key in the payload is a claim, a statement the issuer makes about the subject. RFC 7519 registers a handful of standard names, and none of them is mandatory.
Five of them earn their keep during debugging:
- exp is the expiration time. It is a NumericDate, a count of seconds since the epoch, and a token must not be accepted once that time arrives.
- iat is the issued-at time, in the same seconds format. The distance between iat and exp shows the lifetime the issuer intended, often five minutes or one hour.
- sub is the subject, usually the user ID. When an app shows the wrong account, sub tells you which identity the token actually carries.
- iss names the issuer, the service that signed the token. A staging token sent to a production API often fails here, because the issuer string differs between environments.
- aud names the audience, the service the token was minted for. RFC 7519 is blunt about it: a service that cannot find itself in aud must reject the token.
The token that expired an hour ago
Token expiry is the failure that eats the most debugging time. The token decodes cleanly, the claims look right, the user ID is correct, and the API still answers 401.
Convert exp into a real clock time and the mystery ends. The token died an hour ago, and the client kept sending the cached copy with every request.
Nothing below the token was broken. The API did exactly what the spec asks, and the bug was a stale string sitting in client storage.
One more trap hides in the same number. exp counts seconds, while a JavaScript clock returns milliseconds. Compare the two without converting and fresh tokens look expired, or long-dead tokens look alive.
Decoding is not verifying
A decoder does the split and the base64url decode, and then it stops. It never touches the signature in any meaningful way.
That means anyone can edit a decoded payload and encode it again. Change sub to another user ID, and the result still decodes into clean, believable JSON.
Verification is the step that catches the edit:
HMACSHA256(
base64UrlEncode(header) + "." +
base64UrlEncode(payload),
secret)
The verifier reruns the algorithm named in alg over the encoded header and payload. With HS256 it needs the shared secret, and with RS256 it needs the public key that matches the signing private key.
If the recomputed signature differs from the one in the token, the token was changed or signed by someone else. Only after that check passes do the claims deserve trust.
Read decoded claims all day while debugging. Just never let them make an authorization decision. A decoder cannot tell you the signature is valid.
Where you decode matters
A production token is a live credential. Anyone holding it can call the API as that user until the moment it expires.
Now think about where developers paste tokens all day. Random decoder websites take the full token in a form field and send it to a server run by strangers.
That token can land in request logs, analytics events, support tickets, and browser history. You get no notice and no delete button. Call it what it is: a credential privacy problem.
Local decoding removes the trip across the network. The split and the decode run in your own browser tab, and the token never leaves the device.
Full disclosure: I work on ToolPeko, a set of 300+ free online tools that run in the browser with no sign-up. Its JWT decoder decodes locally in the tab, the same way a local script would, so a production token stays on your machine.
A short routine for the next 401
Read the token before you read your code. Most mystery 401s end inside the payload.
- Convert exp from seconds and compare it with the current time.
- Check iss and aud against the service you actually called.
- Remember that every claim is a rumor until your server verifies the signature.
The string was telling you the answer all along. It only needed to be read in a place that keeps it private.
Top comments (0)