# JWT

> A signed, self-contained pass that tells a server who you are and what you may do.

- Level: Advanced
- Topics: Security, Software
- URL: https://tidbit.legios.com.ar/jwt/
- In Spanish: [JWT](https://tidbit.legios.com.ar/es/jwt/)

A **JWT** (JSON Web Token, often pronounced "jot") is a compact string that carries a few facts about a user, called claims, plus a cryptographic signature. It has three parts separated by dots: a header that names the signing algorithm, a payload with the claims (for example `sub` for the user id and `exp` for the expiry time) and the signature. Each part is base64url-encoded JSON, so it travels easily in an HTTP header like `Authorization: Bearer eyJ...`. The format is defined in RFC 7519.

The point is that a server can check a JWT without looking anything up. The login service signs the token with a secret key (HMAC, as in `HS256`) or a private key (as in `RS256` or `ES256`), and any API that holds the matching key can verify that nobody changed a single character. That makes JWTs popular in microservices and in OpenID Connect, where the ID token you get after "Sign in with Google" is a JWT.

Two traps catch beginners. First, a signed JWT is not encrypted: anyone who grabs it can decode the payload and read it, so it must never hold secrets, and it should only travel over HTTPS. Second, because verification needs no database, a stolen JWT keeps working until it expires. The usual answer is short lifetimes (minutes) plus a separate refresh token that the server can revoke. Libraries must also reject tokens whose header says `"alg": "none"` or switches the algorithm, a classic source of vulnerabilities.

## An analogy

A festival wristband with a hologram. The guard at each stage does not call the ticket office: they look at the hologram and the date printed on it. Anyone can read the band, nobody can forge it, and a stolen one works until the festival ends.

## Keep in mind

- Three dot-separated parts: header, payload and signature.
- Signed, not encrypted: the payload is readable by anyone.
- Servers verify it with a key, without a database lookup.
- Hard to revoke, so keep lifetimes short and use refresh tokens.

## Go deeper

- [Introduction to JSON Web Tokens (jwt.io)](https://jwt.io/introduction)
- [RFC 7519: JSON Web Token (IETF)](https://datatracker.ietf.org/doc/html/rfc7519)
- [JSON Web Token on Wikipedia](https://en.wikipedia.org/wiki/JSON_Web_Token)

## Related terms

- [Algorithm](https://tidbit.legios.com.ar/algorithm/): A precise, step-by-step method for solving a problem or doing a task.
- [JSON](https://tidbit.legios.com.ar/json/): The plain-text format programs use to hand each other structured data.
- [HTTP](https://tidbit.legios.com.ar/http/): The request-and-response language browsers and websites speak.
- [Server](https://tidbit.legios.com.ar/server/): A computer whose job is to answer requests from other computers, all day, every day.
- [Token](https://tidbit.legios.com.ar/token/): The small chunk of text a language model actually reads.
- [Public-key cryptography](https://tidbit.legios.com.ar/public-key-cryptography/): Two linked keys: one you share with everyone to lock, one you keep to unlock.
- [API](https://tidbit.legios.com.ar/api/): The menu of things one program lets another program ask for.
- [Microservices](https://tidbit.legios.com.ar/microservices/): Building one application as many small services that each do one job.
