What is JWT? — JSON Web Tokens Explained

What is JWT? — JSON Web Tokens Explained

JWT (JSON Web Token, RFC 7519) is a compact string made of three base64url parts separated by dots: a header, a payload of claims, and a signature. A signed JWT lets a server check that the claims were not changed after a trusted party issued them. The payload is encoded, not encrypted, so anyone who holds the token can read it.


What a token looks like

This token was signed with HS256 and a demo secret, with the claims shown below:

eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJzdWIiOiIxMjM0NTY3ODkwIiwibmFtZSI6IkphbmUgRG9lIiwiaWF0IjoxNzY3MjI1NjAwLCJleHAiOjE3NjcyMjkyMDB9.QhPddnGLSkxR_ecxRDwRLReB0DTwv0lAR76Ql_jD9jI

Decode the first two parts with no key at all:

const [header, payload] = TOKEN.split(".");
console.log(Buffer.from(header, "base64url").toString());
console.log(Buffer.from(payload, "base64url").toString());

Output from Node 22:

{"alg":"HS256","typ":"JWT"}
{"sub":"1234567890","name":"Jane Doe","iat":1767225600,"exp":1767229200}

The signature is an HMAC of header.payload using the secret. Change one character of the payload and the signature no longer matches. Paste a token and its secret into the JWT Decoder to check it. The decoder reads alg from the token header, verifies the signature with that algorithm, and reports time claims (exp, nbf, iat) separately.


Claims

Claim Meaning
sub Subject: who the token is about, often a user ID
iss / aud Issuer, and the audience the token is meant for
exp Expiry time, in seconds since the Unix epoch
nbf Not valid before this time
iat Time the token was issued
jti Unique token ID

Common signing algorithms: HS256 uses one shared secret. RS256 and ES256 use a private key to sign and a public key to verify.


Five mistakes

1. Putting secrets in the payload

Anyone can run the decode step above. A payload with a password hash, an internal role code or personal data is readable by anyone who has the token. Keep the payload to an ID and the claims your server needs. Store secrets on the server.

2. Reading claims without verifying the signature

Before: jwt-decode returns the claims of any token it is given, because it does not check the signature.

import { jwtDecode } from "jwt-decode";
const claims = jwtDecode(token);
if (claims.role === "admin") allowAccess();

With a token whose payload was edited to "role":"admin" and whose signature was kept from the original, jwtDecode returns {"sub":"1234567890","role":"admin","exp":1767229200}.

After: verify the signature with your key, and name the algorithm you expect.

import { jwtVerify } from "jose";
const { payload } = await jwtVerify(token, key, { algorithms: ["HS256"] });

With the same edited token, jwtVerify fails with ERR_JWS_SIGNATURE_VERIFICATION_FAILED.

3. Accepting alg: none

A token with {"alg":"none"} in its header has no signature. If your code accepts whatever alg the token names, including none, anyone can forge claims. The LangStop decoder flags it: Unsigned token (alg: none), with the note that its contents can be forged by anyone.

After: pass the algorithm you expect. Given the unsigned token with algorithms: ["HS256"], jwtVerify fails with ERR_JOSE_ALG_NOT_ALLOWED.

4. An exp in milliseconds

exp is in seconds. Before: exp: (Date.now() + 3600 * 1000), which is milliseconds. The decoder then reads the value as seconds, so it shows Expires in 55,982 years (a run with the clock set to 2026-01-01). The token never expires in practice.

After: exp: Math.floor(Date.now() / 1000) + 3600, which is one hour from now in seconds.

5. A token with no exp

A token without exp does not expire. The decoder shows No exp claim: token never expires. Always set exp, keep access tokens short-lived, and plan how you will revoke a token before it expires, because a signed token stays valid until exp no matter what the server does.


Security checklist

  • Verify the signature on every request, with the algorithm pinned on the server.
  • Check exp, iss and aud on the server.
  • Send tokens only over HTTPS.
  • Keep secrets out of the payload.

LangStop JWT Tools