How a JWT works
A JWT (JSON Web Token) is a compact string that carries claims, such as who issued a token and when it expires. The idea to keep: the signature protects the token from changes, but it does not hide what is inside.
The short version
A signed JWT has three parts joined by dots: a header, a payload and a signature. The header names the signing algorithm in alg, and the payload holds the claims. Both are base64url encoded. That is an encoding, not encryption, so anyone holding the token can decode and read them (my inference from how the encoding works). The signature is computed over the first two encoded parts, so changing either one breaks it.
Step by step
- The sender builds the header and the payload as JSON.
- It base64url encodes both and joins them with a dot.
- It signs that text with the algorithm named in
alg. - The token is the two parts plus the signature, with dots between them.
- The receiver recomputes the signature, then checks
exp,issandaud.
| Part | Holds | Used for |
|---|---|---|
| Header | alg, typ |
which algorithm signed the token |
| Payload | iss, sub, aud, exp, iat |
issuer, subject, audience, expiry, issue time |
| Signature | the computed signature | catching changes |
Why it matters
Trust no claim until the signature checks out, and take the algorithm from your own settings, not from the token's header. RFC 8725 warns about tokens with alg set to none, which carry no signature. The upside is a compact token that fits in a URL; the downside is that anyone can read it, and a careless check can be fooled.
Sources
- RFC 7519: JSON Web Token (JWT) — the definition, the three parts, registered claims, the validation steps
- RFC 7515: JSON Web Signature (JWS) — the compact form, the signing input, integrity protection
- RFC 8725: JWT Best Current Practices — choosing the algorithm yourself, the
nonealgorithm,issandaudchecks