What's Inside a JWT (and What Decoding Won't Tell You)

By The UtilCraft team ยท 4 min read ยท

JSON Web Tokens are everywhere: in the Authorizationheader of most APIs, in the cookies of single-page apps, in every OAuth and OpenID Connect login. They're also widely misunderstood. The most common misunderstanding, that a JWT is somehow encrypted, has caused real security incidents. Let's take one apart.

Three parts, two dots

A JWT looks like a long random string, but it's three Base64URL-encoded pieces separated by dots:

eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9
.eyJzdWIiOiIxMjM0NTY3ODkwIiwibmFtZSI6IkpvaG4gRG9lIiwiaWF0IjoxNTE2MjM5MDIyfQ
.SflKxwRJSMeKKF2QT4fwpMeJf36POk6yJV_adQssw5c

That's the well-known example token from jwt.io (signed with the secret your-256-bit-secret), split onto three lines for readability. Paste it into our JWT Decoderand you'll see each part.

1. The header

{
  "alg": "HS256",
  "typ": "JWT"
}

It says which algorithm was used to sign the token. HS256 is HMAC with SHA-256, using a shared secret. RS256 and ES256use a private key to sign and a public key to verify, which is what identity providers like Auth0, Okta, Google and Microsoft use. There's often a kid(key ID) too, which tells the verifier which of the issuer's published keys to use.

2. The payload

{
  "sub": "1234567890",
  "name": "John Doe",
  "iat": 1516239022
}

These are the claims: statements about the user and the token. Some are registered in the specification (RFC 7519) and have agreed meanings: sub for the subject, iat for issued-at, exp for expiry, aud for the intended audience, iss for the issuer. Others are custom, like name here, or the roles and scopes your application cares about.

Times are in seconds since 1 January 1970 UTC. 1516239022 is 18 January 2018 at 01:30:22 UTC.

3. The signature

The signature is computed over the first two parts exactly as they appear, encoded and joined by a dot:

HMACSHA256(
  base64url(header) + "." + base64url(payload),
  secret
)

If anyone changes a single character of the header or the payload, say by changing "role": "user" to "admin", the signature no longer matches, and a server that verifies it will reject the token. That's the whole security model: a JWT is tamper-evident, not secret.

Anyone can read it

Base64URL is an encoding, not encryption. There's no key involved. That's why a decoder can show you the contents of any JWT, and it's why every browser extension, proxy, log aggregator and error tracker that sees your tokens can read them too. Practical consequences:

  • Never put secrets in a JWT payload: no passwords, no API keys, no internal credentials.
  • Be careful with personal data. An email address in an ID token is normal; a home address or health data isn't.
  • Don't log full tokens. Log the jti or sub instead.

If you genuinely need confidential claims, JSON Web Encryption (JWE) exists. You can recognise it by its five dot-separated parts instead of three.

What decoding won't tell you

A decoder shows what a token claims. It can't tell you whether those claims are true. Specifically, it doesn't tell you:

  • Whether the signature is valid. That needs the secret or public key. A forged token decodes just as nicely as a real one.
  • Whether the token has been revoked. JWTs are usually validated without a database lookup, so a token stays usable until it expires even if the user logged out. This is why access tokens are short-lived (often 5โ€“60 minutes) and paired with refresh tokens.
  • Whether it's meant for your API. Only checking aud on the server does that.

The classic mistakes

Accepting "alg": "none"

The specification allows unsigned tokens with the algorithm none. Early libraries trusted the header, so an attacker could strip the signature, set alg to none and edit the payload freely. Modern libraries refuse this by default, but only if you tell them which algorithms you expect. Always pass an explicit allow-list.

Algorithm confusion

If a server expects RS256 but its library also accepts HS256, an attacker can sign a token with HMAC using the server's public key as the secret. The public key is public, after all. Again, the fix is to pin the algorithm.

Decoding instead of verifying

Front-end code often decodes the token to show the user's name or check expiry. That's fine for display. The bug is when back-end code does the same, typically jwt.decode() instead of jwt.verify(), and trusts the result.

Storing tokens where scripts can read them

A token in localStorage can be stolen by any cross-site scripting bug on your site. An HttpOnly, Secure, SameSitecookie can't be read by scripts at all, though it brings CSRF considerations of its own. There's no universally right answer, but it should be a deliberate choice.

A checklist for verifying JWTs on the server

  1. Verify the signature with a maintained library, using an explicit list of allowed algorithms.
  2. Fetch keys from the issuer's JWKS endpoint, and select the key by kid.
  3. Check exp and nbf, allowing at most a minute or so of clock skew.
  4. Check that iss is the issuer you trust and aud is your API.
  5. Only then read the custom claims (roles, scopes) and make authorization decisions.

Do that, and JWTs are a solid, well-understood building block. Skip any step, and the token is just a Base64-encoded note anyone could have written.

Tools used in this guide

Keep reading