JWT structure and common security mistakes
How a JSON Web Token is built, what its claims mean, how to verify one correctly and the security mistakes that lead to forged tokens.
By the tools4free.dev team · Last reviewed 4 December 2026
A JSON Web Token (JWT) is a compact, URL-safe string that carries a set of claims, such as "this is user 1234567890 and this token expires at 23:13 UTC". It has three Base64URL-encoded parts separated by dots: a header, a payload and a signature. The payload is readable by anyone who holds the token. The signature only proves it has not been changed.
The three parts
Here is a complete token. It was signed with HMAC-SHA256 and the demonstration secret demo-secret:
eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJzdWIiOiIxMjM0NTY3ODkwIiwibmFtZSI6IkFkYSIsImlhdCI6MTcwMDAwMDAwMCwiZXhwIjoxNzAwMDAzNjAwfQ.fwoQZBa5Qd0EjpQcylRy6sELfnn1MTA3AVX6D2vPrs0
Split on the dots and Base64URL-decode the first two parts:
- Header:
{"alg":"HS256","typ":"JWT"}. It names the signing algorithm. - Payload:
{"sub":"1234567890","name":"Ada","iat":1700000000,"exp":1700003600}. These are the claims. - Signature: the HMAC-SHA256 of the text
header.payload(the first two parts exactly as written, including the dot), Base64URL-encoded without padding.
Base64URL is Base64 with - and _ in place of + and /, and without = padding, so the token is safe in URLs and headers. It is encoding, not encryption. Anyone can decode a standard signed JWT; the claims are not secret.
Registered claims
RFC 7519 defines a small set of standard claim names. All are optional by the specification, but your application should require the ones it depends on.
| Claim | Name | Meaning |
|---|---|---|
iss | Issuer | Who created and signed the token |
sub | Subject | Who the token is about, usually a user ID |
aud | Audience | Which service the token is meant for |
exp | Expiration time | Unix time (seconds) after which the token must be rejected |
nbf | Not before | Unix time before which the token must be rejected |
iat | Issued at | Unix time the token was created |
jti | JWT ID | A unique identifier, useful for revocation lists |
In the example, exp minus iat is 3,600 seconds, so the token lives for one hour. iat 1700000000 is 2023-11-14 22:13:20 UTC and exp is 23:13:20 UTC the same day. All time claims are in seconds, not milliseconds. This is a frequent source of tokens that appear to expire in the year 55000.
Worked example 1: verifying an HS256 token in Node
A verifier must recompute the signature, compare it safely, and then check the claims. This example uses only Node's built-in crypto module:
const crypto = require('crypto');
function verifyHS256(token, secret, expectedAudience) {
const parts = token.split('.');
if (parts.length !== 3) throw new Error('malformed token');
const [h, p, s] = parts;
const header = JSON.parse(Buffer.from(h, 'base64url'));
if (header.alg !== 'HS256') throw new Error('unexpected algorithm');
const expected = crypto.createHmac('sha256', secret)
.update(h + '.' + p).digest();
const given = Buffer.from(s, 'base64url');
if (given.length !== expected.length ||
!crypto.timingSafeEqual(given, expected)) {
throw new Error('bad signature');
}
const claims = JSON.parse(Buffer.from(p, 'base64url'));
const now = Math.floor(Date.now() / 1000);
if (typeof claims.exp !== 'number' || claims.exp <= now) throw new Error('expired');
if (claims.nbf && claims.nbf > now) throw new Error('not yet valid');
if (expectedAudience && claims.aud !== expectedAudience) throw new Error('wrong audience');
return claims;
}
Run against the token above with the secret demo-secret, the signature check passes. The function then throws "expired", because exp is in 2023. That is the correct result. Note three choices: the algorithm is fixed by the server rather than trusted from the header, the comparison is constant-time, and exp is required rather than optional. In real projects use a maintained library, but know what it must do.
Worked example 2: the same token, tampered
Suppose an attacker changes "name":"Ada" to "admin":true in the payload and re-encodes it. The first and third parts stay the same. When the server recomputes the HMAC over the new header.payload, it gets a different value from the third part, so verification fails with "bad signature". Without the secret, the attacker cannot produce a matching signature. The signature protects integrity. It gives no confidentiality at all.
Signing algorithms
| Algorithm | Type | Use when |
|---|---|---|
| HS256 | Shared secret (HMAC) | One service both signs and verifies |
| RS256 | RSA key pair | Many services verify with a public key; widely supported |
| ES256 | ECDSA P-256 key pair | Same as RS256 with smaller keys and signatures |
| EdDSA | Ed25519 key pair | Modern asymmetric option where supported |
With an asymmetric algorithm, the issuer keeps the private key and publishes the public key, often as a JSON Web Key Set at a well-known URL. Verifiers never need a secret.
Sending a token and rotating keys
For API calls, the usual convention is an Authorization: Bearer <token> header over HTTPS. Never put a token in a URL query string, because URLs end up in server logs, browser history and referrer headers. The header may also carry a kid (key ID) value. When the issuer rotates its signing keys, the verifier uses kid to pick the matching public key from the published key set, so old tokens signed with the previous key keep working until they expire. Treat kid as a lookup hint only: use it to select from keys you already trust, never to fetch a key from a location named inside the token.
Common mistakes
- Accepting
alg: none. The specification allows an "unsecured" JWT with no signature. A verifier that obeys the header will accept a forged token. Always specify the allowed algorithms in your verifier configuration. - Algorithm confusion. If a server expects RS256 but also lets the header choose HS256, an attacker can sign with HMAC using the public key as the secret. Pin one algorithm per key.
- Decoding without verifying. Reading claims from the payload is not authentication. Every consumer must verify the signature first.
- Skipping
exp,audorisschecks. A valid signature on a token meant for another service, or one that expired last year, should still be rejected. - Putting secrets in the payload. Passwords, card numbers and personal data do not belong in a token that is merely signed. Use JWE (encrypted JWTs) or keep the data server-side.
- Long-lived tokens with no revocation. A stolen JWT works until
exp. Use short lifetimes, refresh tokens that the server can revoke, and ajtideny-list for emergencies. - Weak HMAC secrets. A short or guessable HS256 secret can be brute-forced offline from one captured token. Use at least 32 random bytes.
- Storing tokens where scripts can read them. Browser storage is readable by any script on the page, so one XSS flaw leaks every token. An
HttpOnly,Securecookie with a suitableSameSitevalue avoids that, at the cost of needing CSRF protection.
Frequently asked questions
Is a JWT encrypted?
Normally no. A signed JWT (JWS) is only encoded. A separate standard, JWE, encrypts the contents, but it is much less common.
Can I revoke a JWT?
Not by itself, because verification needs no server lookup. You can add one: keep a list of revoked jti values, or shorten the lifetime and revoke the refresh token instead.
JWT or session cookie?
For a single web application, a server-side session with an opaque ID is simpler and easy to revoke. JWTs suit cases where separate services must verify identity without calling a central store.
Why does my token fail to decode?
Common causes are a copied space or newline, the word "Bearer " left on the front, or treating Base64URL as standard Base64.
Try it: paste a token into the JWT decoder to read its header and claims. Decoding happens in your browser and does not prove the token is valid.