LW IT Solutions logo LW IT Solutions logo LW IT Solutions
« Blog Overview /Web Development / JSON Web Tokens: Structure, What Belongs in...
Read this article in other languages:

JSON Web Tokens: Structure, What Belongs in the Payload and the Difference Between Decoding and Verifying

JSON Web Tokens: Structure, What Belongs in the Payload and the Difference Between Decoding and Verifying

Jump to the interactive example ↓

Contents
  1. What the three parts establish
  2. Decoding, validation and authorization
  3. Claims that fit the actual task
  4. Expiry and server-side rules
  5. Questions and answers
  6. Sources

Reading a JWT reveals recorded claims; accepting those claims requires a separate validation process. This distinction matters because an attacker can create plausible-looking JSON without possessing an issuer’s signing key.

The following explanation covers the common compact JWS representation of a JWT. It has three dot-separated parts: protected header, payload and signature or message authentication code (MAC). The first two are Base64url-encoded JSON, not encrypted. A compact encrypted JWT uses JWE and has five parts. JWT describes a format; not every JWT is an access token.

JWT: decoding is only the first step
A valid signature does not by itself grant access

What the three parts establish

The header identifies the cryptographic algorithm through alg and may contain a key identifier, kid. The payload contains claims about an issuer, subject, audience or authorization context. The third part protects the encoded header and payload against undetected modification and authenticates creation by a holder of the relevant signing or MAC key.

With an asymmetric algorithm, a private key signs and a corresponding public key verifies. With HS256, a shared secret creates and verifies the MAC. Every holder of that secret can also produce valid MACs; verification does not uniquely identify one particular secret holder. A valid result under an arbitrary key is not enough: the application must trust that key for the expected issuer and purpose.

Decoding, validation and authorization

  1. Decoding: parse the expected representation and inspect fields as untrusted input.
  2. Cryptographic validation: accept only configured algorithms and appropriate trusted keys; verify the actual signature or MAC.
  3. Context validation: enforce issuer, audience, token type, required claims and time rules for the applicable profile.
  4. Authorization: decide whether the validated subject and permissions permit this operation on this resource.

A decoder can display a fabricated token. Conversely, a token with a correct signature can still have the wrong audience, be expired or lack permission for the requested account. A library call is useful only when its configuration and the application’s remaining checks implement the intended rules.

The application defines an algorithm allowlist; the token’s alg must match it and the permitted key type. An unsigned alg: none token is unacceptable where a signature is required. RS256-to-HS256 confusion is a known failure of vulnerable or misconfigured verification, not an inevitable behavior of every verifier. The public RSA key must never become an HMAC secret.

kid is an untrusted lookup hint. It may select a key from a trusted issuer’s configured key set, including a safely retrieved JWKS. It must not authorize arbitrary token-supplied keys, URLs or file paths. An ID token must not be accepted as an API access token merely because both use JWT.

Claims that fit the actual task

Passwords, private keys and unnecessary personal information do not belong in a readable payload. Even a short identifier can be sensitive. Real tokens therefore need protection in storage, transport, diagnostics and support workflows; demonstration material should contain synthetic data.

Roles, scopes or tenant identifiers are not categorically forbidden. RFC 9068 explicitly accommodates roles and related authorization claims. Their inclusion depends on the receiving service’s needs, disclosure risk and how quickly changes must take effect. A role embedded in an issued token can become stale. Server lookups, short lifetimes or other controls may be necessary; a lookup does not automatically happen in every JWT design.

sub identifies the subject, iss the issuer and aud the intended recipient or recipients. RFC 7519 does not make all registered claims mandatory in every JWT. A concrete protocol profile can require them: the OAuth access-token profile in RFC 9068 imposes additional requirements. Presence alone is insufficient; issuer and audience must be compared with expected values.

Expiry and server-side rules

exp and nbf use NumericDate values in seconds since the Unix epoch, not JavaScript milliseconds. Without clock-skew leeway, acceptance requires now < exp and, when present, now >= nbf. At exactly the expiry instant the token is expired. The server must also require the claims mandated by its profile; a missing exp must not silently bypass a policy requiring expiry.

No exp does not universally mean unlimited validity. Server policy, revocation, account state and key trust can independently prevent acceptance. Deleting a browser copy does not revoke other copies. Possible designs combine short-lived access tokens with revocable refresh tokens, deny lists or introspection. A jti identifier alone does not implement revocation or replay prevention.

Questions and answers

What changes during signing-key rotation?

Planned rotation makes a new verification key available before new signatures depend on it and retains old keys for a controlled overlap covering still-accepted tokens. Existing tokens must still pass expiry, audience and other checks. Removing an old key from a published JWKS does not instantly remove cached copies from every verifier. Refresh rules, caches and deployment propagation determine when rejection takes effect.

A compromised key requires an incident response that stops new use and removes trust across affected verifiers. Simply deleting one published entry is not proof of immediate global revocation. Refresh-token handling depends on its actual format and validation path; refresh tokens need not be JWTs.

Does encryption make a copied token harmless?

No. JWE can hide claims from parties without the decryption key, but a bearer credential can still be replayed without reading it. Sender-constrained tokens such as DPoP-bound tokens add different possession requirements. Encryption also does not automatically establish the intended issuer’s identity in every key-management mode. The complete signing, encryption and validation profile remains relevant.

How should clock drift be handled?

A slow verifier can accept beyond the intended expiry; a fast one can reject early. A small configured leeway accommodates measured clock differences, but broadens the expiry acceptance boundary and can permit earlier acceptance around nbf. It does not guarantee that every token remains accepted for exactly that extra duration: other checks can reject it first. Time synchronization, monitoring and a deliberately chosen tolerance belong together.

Which negative tests are useful?

A verification suite should reject an altered payload, wrong key, forbidden algorithm, unexpected issuer or audience, expired or not-yet-valid token, missing required claim and inappropriate token type. A valid token must still be rejected for an unauthorized resource. Successful Base64 decoding is not a substitute for those tests.

Learn by trying

One minute: can you spot the mistake?

You decode a JWT and read role: admin. What do you know for certain?

Continue in the matching tool →

Reference ↗

One problem, a clear way through

What is inside this token?

  1. Understand: Understand JWTs
  2. Try it: Inspect a demo token
  3. Inspect a demo token and understand its fields. Readable claims do not prove a valid signature or permission to access anything.
Lukas Wojcik

Lukas Wojcik

Systems architect and technology enthusiast specializing in scalable tracking solutions, GMP Stack (GA4 & GTM), and robust backend architectures. Advocate for clean code and privacy-first design.

Get in touch

A question or an idea for a web project? Tell me a little about it. This form uses reCAPTCHA to protect against spam.

Write a comment

Edge cases the article misses and questions about the code are welcome here.

The email address is not published. Required fields are marked with an asterisk.