A signed pass with your details inside, which a server can check without a database.
A JWT (JSON Web Token, said βjotβ) is a small, signed pass. It carries facts about the user, such as who they are and when the pass expires.
A JWT is a string like xxxxx.yyyyy.zzzzz. Each part is JSON written in base64url, a way to make text safe for URLs and headers.
| Part | What it holds | Example claims |
|---|---|---|
| Header | The token type and the signing algorithm | alg, typ, kid (key id) |
| Payload | Claims: facts about the user and the token | sub (who), iss (who issued), aud (for whom), iat (issued at), exp (expires), role |
| Signature | A seal over header and payload, made with a key | HMAC-SHA256(header.payload, secret) |
| Access token | Refresh token | |
|---|---|---|
| Job | Call the API | Get a new access token |
| Life | Short: minutes (for example 5 to 15) | Long: days or weeks |
| Sent to | Every API call | Only the auth server |
| If stolen | Useful only until it expires | Dangerous. Store safely, rotate it, and be able to revoke it |
| Place | Good | Risk |
|---|---|---|
| HttpOnly + Secure + SameSite cookie | JavaScript cannot read it, so XSS cannot steal it | Needs CSRF care (SameSite helps) |
| localStorage or sessionStorage | Easy to use | Any XSS bug can read it and steal the token |
| In memory only | Hard for others to steal | Lost on page reload (pair with a refresh cookie) |
eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJzdWIiOiJhc2hhIiwicm9sZSI6InVzZXIiLCJpYXQiOjE3MDAwMDAwMDAsImV4cCI6MTcwMDAwMDkwMH0.ZTQzMjk2NGMyZTQzYjM3NTg1YWZlMjBhMjIyMDQ1MzE
{
"alg": "HS256",
"typ": "JWT"
}{
"sub": "asha",
"role": "user",
"iat": 1700000000,
"exp": 1700000900
}| Check | Why |
|---|---|
| Signature is valid | Nobody changed the token |
| alg is the one you expect | Never accept βalg: noneβ or let the token pick its own algorithm |
| exp is in the future | The token has not expired (allow a tiny clock difference) |
| iss (issuer) is yours | It came from a source you trust |
| aud (audience) is this API | It was meant for this service, not another one |
| Term | Meaning in easy words |
|---|---|
| JWT | JSON Web Token: a signed token made of header, payload and signature |
| Claim | One fact in the payload, like sub or role |
| sub | Subject: who the token is about (user id) |
| iss / aud | Issuer (who made it) and audience (who it is for) |
| iat / exp | Issued at / expires at (Unix times in seconds) |
| base64url | A way to write bytes as URL-safe text. It is not encryption |
| HMAC | A keyed hash. Used for HS256 signatures |
| Bearer token | Whoever holds it can use it |
| JWKS | A published list of public keys used to verify tokens |
| Revocation | Cancelling a token before it expires |
| JWE | A different format that encrypts the content (rarely needed) |
| Choice | We gain | We lose |
|---|---|---|
| Very short expiry (5 min) | A stolen token dies fast | More refresh calls |
| Keep a revocation list | Can cancel tokens | You are back to a lookup, which is a bit like sessions |
| Put more claims in the token | Fewer database calls | Bigger token and staler data |
| Asymmetric signing | Verifiers cannot forge tokens | Slower and key management is harder |
alg: none, or trusting the token's own alg. Fix the algorithm on the server. This is a famous attack.header.payload.signature.