learn

What is a JWT (JSON Web Token)?

A JWT is a signed JSON token in three Base64URL parts. See what each part holds, the standard claims, HS256 vs RS256 and the mistakes that break security.

JWT in one paragraph

A JSON Web Token (JWT, RFC 7519) is a compact, signed piece of JSON that a server hands to a client so the client can prove who it is on later requests. It has three parts separated by dots: a header, a payload and a signature, each encoded in Base64URL. Anyone can read the first two parts; only someone with the key can produce a valid signature. That is the whole idea: a JWT is signed, not encrypted.

The three parts

This token was signed with HS256 and the secret segredo-de-exemplo:

eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJzdWIiOiI0MiIsIm5hbWUiOiJBbmEiLCJpYXQiOjE3MDAwMDAwMDAsImV4cCI6MTcwMDAwMzYwMH0.15e57WLjsIXpOUICble0StaExJ1FRloQ-lsmKQ2qDhs

Decoded, the header is {"alg":"HS256","typ":"JWT"} and the payload is {"sub":"42","name":"Ana","iat":1700000000,"exp":1700003600}. The third part is the HMAC-SHA256 of header.payload with the secret. Paste it in the JWT decoder to see each part, the dates in your time zone and whether the signature matches a key.

Registered claims

ClaimMeaning
ississuer: who created the token
subsubject: whom it is about, usually a user id
audaudience: which service must accept it
expexpiry, in Unix seconds
nbfnot valid before this moment
iatissued at
jtiunique id, used to revoke or detect replays

Times are Unix timestamps in seconds. A frequent bug is writing milliseconds (Date.now() in JavaScript), which pushes the expiry thousands of years ahead.

HS256, RS256 or ES256?

  • HS256 uses one shared secret to sign and verify. Simple, but every service that verifies can also forge tokens. Make the secret long and random, for example from the token generator.
  • RS256 signs with an RSA private key and verifies with the public key, so many services can check tokens without being able to create them. You can make a test key pair with the RSA key generator.
  • ES256 does the same with elliptic curves: shorter keys and signatures. EdDSA (Ed25519) is the modern option where libraries support it.

To build and sign a token for a test, use the JWT generator.

Mistakes that break security

  • Decoding is not verifying. The payload is just Base64URL; a server must check the signature, exp and aud before trusting it.
  • Accepting alg: none or letting the token choose the algorithm. Pin the expected algorithm in the verifier.
  • Putting secrets in the payload. Everyone who holds the token can read it.
  • Long-lived tokens without a way to revoke them. Prefer short exp plus refresh tokens.
  • Storing tokens where scripts can read them (localStorage) on sites exposed to XSS; an HttpOnly, Secure, SameSite cookie is safer for browser sessions.

FAQ

Is a JWT encrypted?

No. A normal JWT (JWS) is only signed: anyone can decode the header and payload. Encrypted tokens exist (JWE, RFC 7516) but are a different format with five parts.

Can I decode a JWT safely online?

Only where the token does not leave your device. The decoder on this site runs in your browser; a production token pasted into a site that uploads it should be treated as leaked.

Why does my token say "expired" right after login?

Usually a clock or unit problem: exp written in milliseconds, servers with skewed clocks, or a time zone applied to a value that is always UTC.

What is the difference between JWT and a session id?

A session id is a random reference that the server looks up; a JWT carries the data itself and is checked with a key, so it needs no lookup but is harder to revoke.