TwiceToldby Develoker
ConceptsSystem designLabsCheat sheetsAbout

Develoker makes free tools for learning to build software. TwiceTold explains each idea twice, as an everyday analogy and the real thing; CloudArch lets you design cloud systems and put load on them.

About ยท Labs ยท Cheat sheets ยท Learning paths

โ† Back to JWT

๐ŸŽŸ๏ธ JWT: cheat sheet

๐ŸŽจ The analogyA festival wristband with a tamper-proof stamp that any gate can check without phoning the ticket office.
๐ŸŽ“ The regular explanationJSON Web Tokens are compact, signed bundles of claims. Any service with the key can verify one on the spot, without a database lookup, which makes them great for APIs and hard to revoke.
Three parts and two dotsStep 3 of 3
Wristband ยท signedheakey alg RS256, typ JWT: header0paykey sub 42, role user, exp 15:00: payload1sigkey sign(header.payload, key): signatureโœฆโœฆโœฆโœฆโœฆ2๐ŸŽชTicket office
  1. The first part of the wristband says what kind of stamp is used.
  2. The middle part is printed in plain ink, who you are, what you're allowed into, and when it stops working.
  3. The last part is a stamp made from the first two parts and a secret only the ticket office knows.
Checking the wristband at the gateStep 3 of 3
๐Ÿง’Youhas JWT๐ŸŽชTicket office๐ŸŽกRide gatesignature โœ“๐Ÿ”Food tentsignature โœ“
  1. You show your ticket at the office once, and they put a stamped wristband on you.
  2. At the ride, the attendant checks the stamp with the office's stamp guide. No phone call to the office needed.
  3. The food tent checks the same way. Any gate with the guide can check the band on the spot.
Tampering and expiryStep 3 of 3
๐Ÿง’Youexpired band๐ŸŽคVIP gatesignature โœ—
  1. Someone scribbles "VIP" over the plain-ink part of their wristband and walks to the VIP gate.
  2. The stamp no longer matches what's printed, so the gate says no.
  3. A real wristband from yesterday has a valid stamp, but the date on it has passed, so it's refused too.

๐Ÿงฉ Key questions

Anyone can read the wristband's printed strip. Why can't they change it?
Because the stamp, made with the office's secret, would no longer match. The strip is readable, so never print secrets on it; the stamp gives tampering away.
Why can every gate check bands without phoning the ticket office?
Because each gate has a guide to the office's stamp. So even huge crowds move quickly.
A real band can't be taken back. What does the office do about that?
Only gives out bands that last a short while. If a band is valid until tonight, it works until tonight.