Part 4 of 8 · 3 min read
Tokens that carry their own proof
Signed tokens that skip the lookup, and the price paid in revocation.
Think of a train ticket with a hologram. The guard does not phone the station to check it. The hologram proves that the ticket is real, and the printed fields say the journey and the date.
A token can work in the same way. Instead of a random id that points to a record, the server hands out a message that contains the facts and a proof that the server made it.
Signed token
A signed token is a short message with facts in it (such as user 7, role customer, expires at a time) and a signature, a value computed from the message with a secret key. Anyone who holds the key can check that the message has not been changed.
Example: the token says user=7; exp=12:15 and carries a signature. If someone edits user=7 to user=1, the signature no longer matches, and the server rejects the token.
A widely used form is the JSON Web Token (JWT), a signed token with a standard layout. The idea matters more than the name.
How it works:
- At login, the server builds the token and signs it.
- The client keeps the token and sends it with each request, usually in a header.
- Any server with the key checks the signature and the expiry. It then trusts the facts inside. There is no store lookup.
This helps stateless design, since no shared store is needed to authenticate. It helps across data centres and between services. A service in another region can check the token on its own.
The price is revocation. A session store can delete an entry, and the user is logged out at once. A token already issued stays valid until it expires, because the servers check only the signature.
Ways to live with that:
- Short life. The access token lasts 15 minutes. A stolen one is useful for a short time.
- Refresh token. A second token, kept more carefully, lasts 30 days. The client trades it for a new 15-minute token. The refresh step goes to the server's store, where it can be refused.
- Blocklist. The server keeps a small list of tokens or users that are cancelled, and checks it. This brings a lookup back, but the list is small.
A worked example. A user's phone is stolen at 10:00. The access token was issued at 09:55 and lasts 15 minutes.
- The stolen token works until 10:10.
- The owner reports it and the server blocks the refresh token at 10:03.
- After 10:10, the thief cannot get a new access token. The exposure is at most 10 minutes of access, not 30 days.
| Server-side session | Signed token | |
|---|---|---|
| Needs a store lookup for each request | Yes | No |
| Instant logout | Yes | Only with a blocklist or short life |
| Size sent with each request | A short id | The whole token, larger |
| Works across services and regions | Needs access to the store | Only needs the key |
| Where a leak hurts | Store breach | Key leak: all tokens forgeable |
Larger tokens add bytes to every request. If a token holds 40 fields, it may be 1 kilobyte, and 30,000 requests a second would send 30 MB of headers each second. Keep tokens small.
For the curious For the curious
The book recommends centralising login checks at the API gateway when many services exist, so that each service does not repeat the work. The gateway checks the token or session, then passes the user's identity inward. Page 6 and Level 7 return to this.
Common mistake
Putting private data in a token because "it is signed". A signature stops edits, not reading. Anyone can read a standard token. Never put secrets in it.
Check yourself:
- Why can a signed token not be cancelled instantly without extra work?
- What two numbers set the longest time a stolen access token is useful?