What Is a JWT? Structure, Claims and Common Pitfalls
How JSON Web Tokens work: header, payload and signature, Base64URL, standard claims, why decoding is not verifying, alg:none, and where to store tokens.
Three parts, two dots
A JSON Web Token (RFC 7519) is a compact way to pass a set of statements, called claims, between two parties. The common form is a signed token, technically a JSON Web Signature (RFC 7515) in compact serialization. It looks like one long string, but it is three pieces separated by dots:
header.payload.signature — the header says how the token was signed, the payload holds the claims, and the signature lets the receiver check that neither of the first two was changed.
The header and payload are JSON objects. Each is serialized to UTF-8 bytes and then encoded with Base64URL. The signature is computed over the ASCII string header + "." + payload (both still encoded) and is itself Base64URL-encoded. Nothing in a signed JWT is compressed or hidden; it is just made URL- and header-safe.
Why Base64URL rather than ordinary Base64
Standard Base64 (RFC 4648, section 4) uses + and / and pads with =. All three cause trouble in URLs, query strings and some HTTP headers. Base64URL (section 5 of the same RFC) swaps + for - and / for _, and JWTs drop the trailing = padding entirely. The bytes 0xFB 0xFF 0xFE show the difference clearly: standard Base64 gives +//+ while Base64URL gives -__-. A token you can paste into a URL without escaping is the whole point.
A worked decode, by hand
Here is a token built for this guide. It is signed with HMAC-SHA256 (HS256) using the deliberately weak secret correct-horse-battery-staple, and every value below was checked with Node.js.
eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJpc3MiOiJodHRwczovL2F1dGguZXhhbXBsZS5jb20iLCJzdWIiOiJ1c2VyXzQyIiwiYXVkIjoiYXBpLmV4YW1wbGUuY29tIiwiaWF0IjoxNzk1MDAwMDAwLCJuYmYiOjE3OTUwMDAwMDAsImV4cCI6MTc5NTAwMzYwMCwicm9sZSI6ImVkaXRvciJ9.1Pwk1mHZf32a4r8B_03a0UqxZGnymJXfcrmGDnEqrWQ
Step 1, split on the dots. You get three segments of 36, 188 and 43 characters.
Step 2, decode the header. eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9 decodes to {"alg":"HS256","typ":"JWT"}. Almost every JWT starts with eyJ, because that is how the characters {" encode, which makes tokens easy to spot in logs.
Step 3, decode the payload. The second segment decodes to 141 bytes of JSON:
| Claim | Value | Meaning |
|---|---|---|
| iss | https://auth.example.com | Issuer: who created and signed the token |
| sub | user_42 | Subject: who the token is about, usually a user ID |
| aud | api.example.com | Audience: which service should accept it |
| iat | 1795000000 | Issued at: Wed, 18 Nov 2026 11:06:40 UTC |
| nbf | 1795000000 | Not before: the token is invalid before this moment |
| exp | 1795003600 | Expires: Wed, 18 Nov 2026 12:06:40 UTC, one hour later |
| role | editor | A private claim the application defines itself |
The time claims are NumericDate values: seconds since 1 January 1970 UTC, not milliseconds. A common bug is writing Date.now() (milliseconds) into exp, which produces a token that expires tens of thousands of years from now. The Unix timestamp converter will show 1795003600 as 18 November 2026 and immediately expose a value with three extra digits.
Step 4, the signature. The last segment, 1Pwk1mHZf32a4r8B_03a0UqxZGnymJXfcrmGDnEqrWQ, is the Base64URL form of HMAC-SHA256(secret, header + "." + payload). You cannot check it without the secret. Paste the token into the JWT decoder and you will see exactly the header and claims above, with exp and iat as readable dates, but the decoder will tell you it has not verified the signature. That is correct behaviour, not a missing feature.
Registered claims and how servers should check them
RFC 7519 registers seven claim names. None is mandatory in the spec, but a server that accepts tokens should check the ones it relies on:
- exp — reject the token at or after this time. Allow a small clock-skew leeway (a minute or two), not hours.
- nbf — reject it before this time. Useful for tokens issued in advance.
- iat — when it was issued. Lets you reject tokens older than a password change or a forced logout.
- iss — compare against the exact issuer you trust. Without this check, a token from a different tenant of the same identity provider may pass.
- aud — make sure the token was meant for your service. A token issued for one API should not work on another API that trusts the same issuer.
- sub — the identity. Treat it as an opaque string.
- jti — a unique token ID, which makes deny-lists and replay detection possible.
Decoding is not verifying
Because the payload is only Base64URL, anyone can change role from editor to admin, re-encode it and send it back. The only thing that stops this is the signature check on the server. Code that calls a library’s decode function and then trusts the result has no security at all, and this mistake appears in real code reviews more often than any cryptographic weakness.
Proper verification means: parse the header, look up the key for the algorithm you expect (not the algorithm the token asks for), recompute or verify the signature, then validate exp, nbf, iss and aud. Mature libraries do all of this when you pass them an explicit algorithm list and expected issuer and audience.
Signed versus encrypted
A JWS (three parts) proves integrity and origin but hides nothing. A JWE (RFC 7516, five dot-separated parts) encrypts the payload so only the holder of the decryption key can read it. Most access tokens and ID tokens you meet are JWS. If a token has five segments, a decoder cannot show you its claims without the key, and that is by design.
Classic JWT pitfalls
| Pitfall | What goes wrong | Defence |
|---|---|---|
| alg: "none" | The spec defines an unsecured JWT with an empty signature. A library that honours the header accepts a forged token such as eyJhbGciOiJub25lIiwidHlwIjoiSldUIn0.<payload>. with nothing after the last dot. | Pin the allowed algorithms on the server; never accept none for anything security-relevant. |
| RS256 → HS256 confusion | The server verifies RS256 tokens with a public key. An attacker sets alg to HS256 and signs with that public key as the HMAC secret. A naive library uses the same key bytes and the forgery verifies. | Bind each key to one algorithm; reject tokens whose alg does not match. |
| Weak HMAC secret | HS256 tokens can be brute-forced offline: the attacker has the message and the signature and can test candidate secrets as fast as their hardware allows. | Use a random secret of at least 256 bits, as RFC 7518 requires for HS256, not a word or a phrase. |
| Trusting kid or jku | Header fields that point to a key ID or key URL can be abused for path traversal, SQL injection or fetching an attacker’s key. | Look kid up in a fixed allow-list; ignore jku and x5u unless you control the URL. |
| Long-lived tokens | A stolen token works until exp. You cannot recall it without extra server state. | Short expiry (minutes) for access tokens, plus refresh tokens you can revoke. |
| Secrets in the payload | Email addresses, internal IDs or anything sensitive is readable by whoever holds the token, and tokens end up in logs. | Keep payloads minimal; use JWE if the content must be confidential. |
RFC 8725, the JWT Best Current Practices document, collects these and more. It is short and worth reading before you write any token-handling code.
Where to keep a token in the browser
There is no perfect answer, only trade-offs you should choose deliberately.
| Storage | Strength | Weakness |
|---|---|---|
| localStorage / sessionStorage | Simple; not sent automatically, so no CSRF. | Any script on the page can read it. One XSS bug or compromised third-party script leaks the token. |
| HttpOnly, Secure, SameSite cookie | JavaScript cannot read it, which limits the damage from XSS. | Sent automatically, so you need SameSite and usually CSRF protection; harder across domains. |
| In memory only | Gone when the tab closes; not readable from storage. | Lost on reload, so you need a refresh flow, typically via an HttpOnly cookie. |
A common pattern for single-page apps is a short-lived access token held in memory plus a refresh token in an HttpOnly, Secure, SameSite cookie scoped to the refresh endpoint. Whatever you choose, XSS remains the bigger threat: a script running in your page can make authenticated requests even if it cannot read the token.
Debugging tokens safely
- Decode the token locally with the JWT decoder rather than an online service that sends it to a server. A live token is a password for as long as it is valid.
- Check exp and nbf first. Most “invalid token” errors are expired tokens or clocks that disagree.
- Compare iss and aud character by character with your server configuration; trailing slashes matter.
- Confirm alg in the header is the one your server is configured to accept.
- If a segment will not decode, paste it into the Base64 decoder; it accepts the URL-safe alphabet and missing padding.
Sources
Results are estimates for planning and education, not tax, legal, financial or medical advice. Check important decisions with a qualified professional.