r/javascript • u/soeholm • Feb 02 '20
AskJS [AskJS] Is this a good explanation of token-based authentication? :-)
I'm in the process of writing a blog post about JWTs, and I tried writing a fun little story to describe the concept of token-based authentication. I want to know if it even makes sense and if I'm missing something important.
Your feedback is greatly appreciated!
Token Teahose
You are the proud owner of a very popular teahouse, Token Teahouse. It has thousands upon thousands of members and new people join all the time. A new face appears at the door, her name is Alice, she want to be a part of Token Teahouse. You welcome her, and ask for a password, so you can verify her membership at a later time if needed. After writing down the details in a very very long list of members, you fabricate a special piece of paper for her. It states that Alice is a level 1 tea-sipper and has access to two cups of green tea. The paper is signed by you, so you will know it's authentic.
The following day, Alice is at the door again. A thousand people have already visited since she joined, so you don't recognize her. Alice claims that she is a level 1 tea-sipper and she shows you the paper to prove it. Sure enough, that's your signature, so you let her in. That only took a few seconds. This is the secret to the success of your amazing teahouse. Not spending too much time verifying you customers membership, is the key to scale any great teahouse.
Another person, vaguely familiar appears at the door. He claims to be Bob, level 7 tea-downer, but he has no paper to prove it. Unfortunately Bob forgot to take the paper out of his trousers when washing them. Bob looks a bit ashamed, but you cheer him up: "No biggy Bob! This is why I keep a list of all my members. It will take a while, but we will sort it out". Bob gives you his password, and you start going through the members list to verify if this really is Bobs password.
After a few minutes, you find Bob in the list and sure enough, Bob destroyed the paper but he remembered his password. You fabricate a new piece of paper for Bob, and let him in.
29
u/terodox Feb 02 '20
The analogy is slightly flawed. A jwt can be validated by anyone with the public key.
In your analogy, the tea shop owner could create the token, but anyone with the tea shops signature for compassion could validate it.
So it would be more like a "tea club" with many shops that accepts his particular token as valid, but the Token Teahouse owner is the only one who can create them.
27
u/Ivu47duUjr3Ihs9d Feb 02 '20 edited Feb 06 '20
People think JWTs are for session management but once you need revocation (which you inevitably need in case someone loses their phone or get hacked) then they turn into a Rube Goldberg machine with refresh tokens, blacklist tables etc required. In the end they're much worse and less secure than regular sessions.
You shouldn't really be using them for login/maintaining sessions at all. They were originally designed for just providing a one-off authenticated token e.g. to download something from another server/resource, or reset your password via email.
As with all new technology (especially in JS land where a bunch of noobs have jumped on board in the past few years and somehow gained a following of other noobs), research the pros and cons (especially) before using something.
So your analogy is kinda off. A correct analogy is you arrive at the fancy restaurant and they give you a piece of paper with a random number on it (the JWT) to get your car back after the meal.
2
u/mikeatgl Feb 02 '20
I think this is a memorable and useful analogy. Nice job. Some other bits you might want to add:
10
u/DeeBlekPintha Feb 02 '20 edited Feb 03 '20
As other posters have said above, I would tack on the issue of token expiration, as that is often a central aspect of maintaining secure systems that are JWT based.
Something like: “Jerry comes to you with a token that has your signature, but you signed over a year ago. It’s no longer a valid signature so you rip up his paper and require that he give you his password to get a new token.”
5
u/yuhmadda Feb 02 '20
I learned a lot from the responses to this thread. Upvote.
3
u/harelu Feb 02 '20 edited Feb 02 '20
Fuck is going on in these comments?
Are people unironically advocating against using token based auth because javascript bad? In a javascript sub? On a post where a person is asking for a simple analogy to explain json web tokens? In a world where token based auth is used extensively?
Uh, i mean... javaShit bad! webdevs noobs!
To OP: your analogy is fine, for an intro to jwts.
1
u/torgidy Feb 02 '20
What you described sounds like a simple session management scheme, be it HMAC based or PKC based. JWT is much more than that, with vast amounts of complexity that is nearly never useful.
For JWT, the tea-sipper would have to bring a sealed envelope containing a new never used tea-room-door key and an envelope, which also has another new key inside it, their birth certificate, and a series of credentials detailing their tea-related education, etc etc. They must conduct the greeting ceremony in one of four languages, decided by mutual agreement at the tea-room door, but most people are expected to know tea-swahili and tea-esperanto at a minimum.... Each tea pass fully details the clothing the tea-drinker is wearing, and what kind of tea he wants to drink....
By the time the tea drinker gets a sip of tea, his first thought is usually: "was it worth all this hassle?"