Home › Guides › Developer › JWT structure and common security mistakes

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:

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.

ClaimNameMeaning
issIssuerWho created and signed the token
subSubjectWho the token is about, usually a user ID
audAudienceWhich service the token is meant for
expExpiration timeUnix time (seconds) after which the token must be rejected
nbfNot beforeUnix time before which the token must be rejected
iatIssued atUnix time the token was created
jtiJWT IDA 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

AlgorithmTypeUse when
HS256Shared secret (HMAC)One service both signs and verifies
RS256RSA key pairMany services verify with a public key; widely supported
ES256ECDSA P-256 key pairSame as RS256 with smaller keys and signatures
EdDSAEd25519 key pairModern 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

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.

Sources