How JWTs Work and What a JWT Decoder Can and Cannot Tell You
The three parts of a JSON Web Token, the standard claims, why decoding is not verifying, and the mistakes that make tokens unsafe.
The structure of a JWT
A JSON Web Token is three pieces of text joined by dots: header.payload.signature. The header and payload are small JSON documents encoded with URL safe Base64. The header says which signing algorithm is used, such as HS256 or RS256. The payload holds the claims, which are statements about the user and the token. The signature is computed from the first two parts and a secret or private key.
Standard claims
A few claim names are defined by the standard and are widely used.
- iss: who issued the token.
- sub: the subject, usually a user identifier.
- aud: who the token is meant for.
- exp: the expiry time, as seconds since 1 January 1970 UTC.
- nbf: the time before which the token must not be accepted.
- iat: the time the token was issued.
Decoding is not verifying
Anyone can decode a JWT, because Base64 is only an encoding. A decoder shows you the header and payload, and can convert exp and iat into readable dates. It cannot tell you whether the token is genuine.
Only a server that holds the right key can verify the signature. A token that decodes cleanly may still be forged or expired. Never make a trust decision on an application from decoded content alone.
Keep secrets out of the payload
Because the payload is readable by anyone who holds the token, it must not contain passwords, API keys or other private data. Treat it as public information that happens to be signed. If you need to hide the contents, use an encrypted token format rather than a signed one.
Mistakes to avoid
These problems appear again and again in real systems.
- Accepting the none algorithm or letting the token choose its own algorithm. The server should fix the allowed algorithms.
- Skipping the checks on exp, aud and iss after the signature passes.
- Using a weak or guessable secret with HS256.
- Keeping long lived tokens in places that scripts can read, which turns any cross site scripting bug into an account takeover.
- Putting the token in a URL, where it ends up in logs and browser history.
Using a decoder safely
A decoder is a useful debugging aid for checking claims and expiry times. Prefer one that runs in your browser, so the token is not sent to a server, and avoid pasting live production tokens into tools you do not trust.
A worked example: decoding a token by hand
Take this token, signed with HS256 using the secret word secret. It has three parts separated by dots.
Part one is eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9. Decoded from Base64, it is {"alg":"HS256","typ":"JWT"}. That tells the server which algorithm was used to sign.
Part two is eyJzdWIiOiIxMjM0IiwibmFtZSI6IkFzaGEiLCJpYXQiOjE3MDAwMDAwMDAsImV4cCI6MTcwMDAwMzYwMH0. Decoded, it is {"sub":"1234","name":"Asha","iat":1700000000,"exp":1700003600}. The user is 1234, the token was issued at 1700000000, and it expires at 1700003600. Subtract the two and you get 3600 seconds, so the token lives for one hour.
Part three is tvtOcgsOH3euEQoNm2kzUDeS3ifVHvPlAnFyJscIN3I. It is an HMAC SHA-256 of the text of parts one and two joined by a dot, computed with the secret and then encoded as URL safe Base64. Nothing in it can be read. The server repeats the same calculation and compares the result with this value.
Change one letter of the payload, for example the sub, and the signature no longer matches. That is the whole protection: the contents stay readable, but they cannot be altered without the key.
Choosing a signing algorithm
| Algorithm | Key type | Who can create tokens | Typical use |
|---|---|---|---|
| HS256 | One shared secret | Anyone who holds the secret | One service that both issues and checks its own tokens |
| RS256 | Private and public key pair | Only the holder of the private key | An identity provider issuing tokens that many other services check |
| ES256 | Private and public key pair, elliptic curve | Only the holder of the private key | Same as RS256, with smaller keys and signatures |
With HS256 every service that can verify a token can also forge one, because it has the same secret. With a key pair, the other services only need the public key, so a leak at one of them does not let an attacker mint tokens.
Troubleshooting a token that is rejected
- Invalid signature: the secret or public key differs from the one used to sign, or the token was changed in transit. Check for stray whitespace or a line break added when the token was copied.
- Token expired: compare exp with the current time in seconds. A server clock that is a few minutes off causes this, so allow a small leeway of a minute or two.
- Token used before nbf or iat: the same clock drift problem in the other direction.
- Wrong audience or issuer: the aud or iss claim does not match what the receiving service expects. This often happens when a token from a test environment reaches production.
- Malformed token: a JWT has exactly two dots. If you see more or fewer, a prefix such as Bearer was left in, or part of the token was cut off.
- exp in milliseconds: some code writes a 13 digit value. The standard says seconds, so a library will read it as a date far in the future and the token never seems to expire.
Frequently asked questions
- Is a JWT encrypted? No. A normal signed JWT is only encoded, so anyone holding it can read the payload. Encryption is a separate format.
- Can I revoke a JWT before it expires? Not by itself, because the server holds no record of it. You need a deny list, a short expiry with refresh tokens, or a per user version number checked on each request.
- How long should an access token last? As short as your application can tolerate, often minutes to an hour. A short life limits the damage if a token leaks.
- Where should I store a token in a browser? A cookie marked HttpOnly, Secure and SameSite keeps scripts from reading it. Storage that scripts can read exposes it to any cross site scripting bug.
- Why does my token decode but the server rejects it? Decoding needs no key. The server also checks the signature, the expiry, the audience and the issuer, and any one of them can fail.