JWTs explained: what is inside a token and how to debug one
A JSON Web Token looks like a random string, which leads people to treat it as opaque or, worse, as encrypted. It is neither. This guide explains what is in a JWT, how a server decides whether to trust one, and the handful of mistakes behind most JWT problems.
Three parts, separated by dots
A signed JWT has the form header.payload.signature. Each part is encoded with base64url, a variant of Base64 that uses - and _ instead of + and / and normally drops the = padding so the token is safe in URLs and headers.
eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9 ← header
.eyJzdWIiOiI0MiIsImV4cCI6MTc5OTAwMDAwMH0 ← payload
.3q2-7wExampleSignatureBytes... ← signature
Decoding the first two parts gives plain JSON:
{ "alg": "HS256", "typ": "JWT" }
{ "sub": "42", "exp": 1799000000 }
The payload is encoded, not encrypted. Anyone who holds the token can read it. Never put a password, an API secret or personal data you would not show the user into a JWT payload. (An encrypted variant, JWE, exists, but it is a different format with five parts and is far less common.)
The header
The header says how the token was signed. The important field is alg:
HS256: HMAC with SHA-256. One shared secret both signs and verifies. Simple, but every service that can verify a token can also mint one.RS256andES256: RSA and elliptic-curve signatures. A private key signs; a public key verifies. Use these when tokens are issued by one service and checked by others.
A kid (key ID) field often appears too. It tells the verifier which of several published keys was used, which is how keys are rotated without breaking existing tokens.
The standard claims
The payload is a set of claims. Seven names are registered by the specification, and understanding them explains most "token rejected" errors:
| Claim | Meaning |
|---|---|
iss | Issuer: who created the token |
sub | Subject: who the token is about, usually a user ID |
aud | Audience: which service the token is meant for |
exp | Expiry time; the token must be rejected after it |
nbf | Not before; the token must be rejected before it |
iat | Issued at |
jti | A unique ID for this token, useful for revocation lists |
The three time claims are seconds since 1 January 1970 UTC, not milliseconds. A value like 1799000000 has ten digits. If you see thirteen digits, something wrote milliseconds, and a strict verifier will read that as a date tens of thousands of years in the future.
How verification works
The signature is computed over the exact bytes of header.payload as they appear in the token. To verify, a server:
- Splits the token and decodes the header.
- Checks that
algis one it expects. It does not simply use whatever the token says. - Finds the right key (by
kidif there are several) and recomputes or checks the signature. - Only then reads the payload and checks
exp,nbf,issandaud.
Order matters. Claims from a token whose signature has not been checked are just text an attacker typed.
Decoding and verifying are different operations. A decoder, including the JWT Decoder on this site, shows you what a token claims. Only a verifier with the right key can tell you whether those claims are true.
The mistakes that cause real problems
Trusting the algorithm in the token
Two well-known attacks come from letting the token choose its own verification method. In the first, the attacker sets "alg": "none" and removes the signature; a library that honours it accepts anything. In the second, a server that expects RS256 is sent an HS256 token signed with the server's public key as the HMAC secret; a careless library verifies it successfully. The defence for both is the same: configure the accepted algorithm on the server and reject everything else.
Not checking the audience
If two services trust the same issuer, a token issued for service A is validly signed when presented to service B. Without an aud check, B accepts it. Always verify that the token was meant for you.
Clock differences
A token issued by one machine and checked a moment later by another with a slightly slower clock can appear to be "not valid yet". Most libraries accept a small leeway, typically 30 to 60 seconds, on exp and nbf. Use it, and keep servers synchronised with NTP rather than raising the leeway to minutes.
Long-lived tokens with no way to revoke them
A JWT is valid until it expires; there is no built-in "log out". If access tokens live for a week, a stolen one works for a week. The usual design is a short-lived access token (minutes) plus a refresh token that the server stores and can revoke. If you need instant revocation, you need server-side state such as a denylist of jti values, and at that point much of the statelessness is gone. That is a trade-off to make deliberately.
Weak HMAC secrets
An HS256 token can be attacked offline: an attacker who has one token can try secrets on their own hardware until the signature matches. A secret like secret or a short phrase falls in seconds. Use at least 256 bits from a cryptographic random generator.
A debugging routine
When an API answers 401 and you have the token in hand:
- Decode it and look at
exp. Convert it with a timestamp converter; an expired token is the most common cause by far. - Check
audandissagainst what the API expects, character for character. A trailing slash on an issuer URL is enough to fail. - Check
algandkid. After a key rotation, old tokens reference a key the server may no longer publish. - Look for the scopes or roles the endpoint needs. A valid token without the right permission is a 403, but some APIs report it as 401.
- Check how the token was sent:
Authorization: Bearer <token>, one space, no quotes, no line break pasted in the middle.
A note on where you paste tokens
A production token is a credential until it expires. Pasting one into a website that sends it to a server hands that credential to someone else. Prefer a decoder that runs entirely in your browser, and when asking for help, share the decoded claims with the signature removed rather than the whole token.