A JSON Web Token (JWT) is three Base64URL-encoded sections joined by dots: a header, a payload of claims, and a signature. Anyone holding the token can decode the first two sections and read them as plain JSON. Only the signature, checked with the right key, proves the contents were not altered.

The three parts of a JWT

A compact JWT looks like xxxxx.yyyyy.zzzzz. Each segment is Base64URL, a variant of Base64 that swaps + and / for - and _ and usually drops the = padding so the token is safe in URLs and headers. If you want a refresher on the encoding itself, the Base64 encoder and decoder handles it, though a dedicated decoder is easier for tokens.

  • Header: metadata about the token, mainly the signing algorithm (alg) and token type (typ). It sometimes carries a key identifier, kid.
  • Payload: the claims, which are statements about the user or session and about the token itself.
  • Signature: a cryptographic value computed over the encoded header and payload, using a secret or private key.

A worked example

The token below was built for this article with a throwaway secret. It is for demonstration only and grants access to nothing.

eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJzdWIiOiIxMjM0NTY3ODkwIiwiaWF0IjoxNzY3MjI1NjAwLCJleHAiOjE3NjcyMjkyMDB9.SyerGeJ1EdU7q5vgIw3LEnc0y3Ldkppe5kYgyXyYU4s

Decoding the first segment gives the header:

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

Decoding the second gives the payload:

{"sub":"1234567890","iat":1767225600,"exp":1767229200}

The iat and exp values are Unix timestamps in seconds, not milliseconds. Converted to UTC, iat 1767225600 is 2026-01-01 00:00:00 and exp 1767229200 is 2026-01-01 01:00:00. The difference is 3600 seconds, so this token lives for one hour. A common slip is feeding these numbers to a tool that expects milliseconds, which produces a date in January 1970. The timestamp converter shows both interpretations.

To decode it yourself in a shell, take the middle segment, restore padding and decode:

python3 -c "import base64;s='eyJzdWIiOiIxMjM0NTY3ODkwIiwiaWF0IjoxNzY3MjI1NjAwLCJleHAiOjE3NjcyMjkyMDB9';print(base64.urlsafe_b64decode(s+'='*(-len(s)%4)).decode())"

For quick inspection, the free JWT decoder splits a token, pretty-prints both JSON sections and converts the time claims for you.

Registered claims you will meet

Claims are simply the key-value pairs in the payload. Some describe the subject, while others describe the token's own lifetime and intended recipient.

RFC 7519 defines a short list of registered claim names. None is mandatory, but they are widely used, so libraries know how to handle them.

ClaimMeaning
issIssuer: who created and signed the token
subSubject: who the token is about, often a user ID
audAudience: which service the token is meant for
expExpiry: the time after which the token must be rejected
nbfNot before: the time before which it must be rejected
iatIssued at: when the token was created
jtiJWT ID: a unique identifier, useful for revocation lists and replay checks

Applications add their own claims too, such as a role or tenant ID. Keep those small, because the token travels with every request.

Decoding is not verifying

This distinction causes most JWT bugs. Decoding is just reversing Base64URL, which requires no secret, so anyone can do it and so can an attacker who edits the payload. Verifying means recomputing the signature and comparing it, then checking claims such as exp, iss and aud. A decoder website tells you what a token says, not whether to believe it.

With HS256 (HMAC with SHA-256), one shared secret both signs and verifies, so every service that verifies can also forge tokens. With RS256 (RSA with SHA-256), the issuer signs with a private key and others verify with a public key, so verifying services cannot mint tokens. That is why RS256 or similar asymmetric algorithms suit setups with several consuming services or third-party identity providers.

Security mistakes to avoid

  • Trusting the payload before verifying the signature. Reading role: admin from a decoded token and acting on it lets anyone grant themselves access. Always verify first.
  • Accepting alg: none or letting the token pick the algorithm. The header is attacker-controlled. Configure your verifier with an explicit allowed algorithm list and reject unsigned tokens. A related classic attack feeds an RS256 public key to a verifier that then treats it as an HS256 secret, which an algorithm allow-list prevents.
  • Putting sensitive data in the payload. A standard signed JWT is encoded, not encrypted. Passwords, personal data and internal details do not belong there.
  • Long-lived tokens. A stolen token works until exp. Use short access-token lifetimes, plus refresh tokens that you can revoke.
  • Skipping aud and iss checks. A valid token from the same issuer but meant for another service should be rejected.
  • Pasting production tokens into untrusted sites. A live token is a credential. Use a tool that decodes locally in your browser, or decode in your own terminal, and prefer expired or test tokens.

Common mistakes

  • Treating a successful decode as proof that the token is valid.
  • Comparing exp to the current time in milliseconds when it is in seconds.
  • Ignoring clock skew between servers. Many libraries allow a small leeway, such as a minute.
  • Storing tokens somewhere scripts can read them, which makes cross-site scripting far more damaging.
  • Debugging a 401 by decoding only, without checking the algorithm, key and audience the server expects.

JWTs usually travel in the Authorization: Bearer header, so the HTTP headers guide is useful background. If a browser app cannot send the token cross-origin, the CORS explainer covers the likely cause.

Frequently Asked Questions

Can anyone read the contents of my JWT?

Yes. The header and payload are only Base64URL-encoded, so anyone with the token can read them. Do not store secrets in claims unless you use encrypted tokens (JWE).

Does decoding a JWT check whether it is valid?

No. Decoding shows the claims. Validity requires signature verification with the correct key, plus checks on expiry, issuer and audience.

Why does my JWT have only two dots and no padding?

Three segments always mean two dots. The missing = padding is normal for Base64URL; add it back if your decoder demands it.

Should I use HS256 or RS256?

HS256 is simple when one service both issues and verifies. RS256 is better when other parties need to verify tokens without being able to create them.