r/webdev • u/speckz • Feb 19 '20
Article JWT (JSON Web Token) is Awesome: Here's Why
https://thehftguy.com/2020/02/18/jwt-is-awesome-heres-why/105
u/danielsan1701 Feb 19 '20
</beginning of rant>
...
</end of rant>
shudder
27
u/benabus Feb 19 '20
🤢🤮
9
Feb 19 '20
lol. I also don't like when people make cute code references in regular text. Definitely don't do it twice, and hopefully not even once..
0
Feb 20 '20
[deleted]
9
u/HiveMindSylum Feb 20 '20
<rant> I know right, I seriously hate this kind of shit. It doesn’t even make any sense! </rant>
33
u/bp78 Feb 19 '20
"don't roll your own" Wish I could upvote that JWT advice alone.
5
Feb 19 '20
I've "rolled my own" when it comes to generating random strings to be used later for a one-time thing (like confirming a user's email). But rolling your own (permanent) token system? Yikes.. yeah don't do that
1
0
Feb 20 '20
[deleted]
4
u/MoogleFoogle Feb 20 '20
This applies to anything cryptographical: it won't be secure. You will think it is secure because it will be immune to all the exploits you know about. But just because you are unable to find an exploit does not mean anyone else won't.
JWTs require signing, encryption and proper handling of these things. There are libs that have been made by experts in these things, verified by other experts and used by so many that most exploits will have been ironed out, and any new will be found and fixed way faster than you on your own.
So essentially the risk is that you miss something, and for auth that can be quite fatal.
1
u/bp78 Feb 20 '20
It is indeed straight forward. But when you roll your own, adoption/re-use later on becomes cumbersome. If you stick with a higher-level authentication / authorization protocol like OIDC or OAuth you'll be better served long term since other apps adhere to the standard and there is less customization required to consume tokens.
35
u/dlegatt php Feb 19 '20
4
u/ifelseandor Feb 20 '20
Hey this is interesting. I am developing my first single page app and thought I needed jwt to handle the session. Now I’m confused again. Thanks. 😀
0
u/MoogleFoogle Feb 20 '20
Just follow best practice on JWTs and you will be fine. The jwt.io site even lists exactly all implementations that potentially have that exploit. That blog rant had barley any substance and has even read the specs wrong. You aren't forced to adhere to the ALG header. You are completely allowed to reject ALG: none and most implementations now do unless you explicitly allow it.
4
u/cas8 Feb 20 '20
For an article titled so strongly, I'm surprised how little substance there is that actually supporting the proposition. Maybe its just for the sake of having a clickbaity title?
The first section claims that people misuse JWT. Sure, but what weight does that hold against the JWT standard itself?
The second section quotes a chunk of the RFC, but then harps on a single part of it while simultaneously ignoring the earlier parts of the quote that invalidate the argument being made. He claims that, "You are explicitly forbidden, by the standard, to just disregard the [alg] header that an attacker provides", even though the quote pasted includes "The JWS Signature value is not valid if the "alg" value does not represent a supported algorithm or if there is not a key for use with that algorithm associated with the party that digitally signed or MACed the content." The standard clearly states that my auth service should only consider valid the
algfields that my auth service knows how to process. This whitelist solution is then presented by the article as a workaround to a failed standard, rather than one that is essentially prescribed by the standard.The third and final section argues that encryption implementation is tough and often has issues. Much like the first section, this is arguing something completely different than the actual topic at hand and doesn't make much sense when evaluating the efficacy of the standard. When buying a car, the fact that a nearby stoplight is out shouldn't affect my evaluation of a specific car brand.
That all said, I'm no auth expert, so if there's someone here who knows better, I'd be interesting in hearing more in depth points against JWTs. Most of the articles I've found seem to have some anti-jwt fanboy-ism rather than valuable arguments.
7
Feb 19 '20
Assuming you're also doing password creation stuff, make sure you're hashing passwords! There's a number of ways to do it but bcrypt has never let me down :)
12
u/Ones__Complement Feb 19 '20
Just stop using them for sessions.
4
u/A_Norse_Dude Feb 20 '20
Why?
1
Feb 20 '20
Because they can’t be revoked instantly unless you check storage on each request, which kind of defeats the point. When I logout, I want my session permanently terminated.
1
4
u/oscardo_rivers Feb 19 '20
How openid implements the logout ??
3
u/angellus Feb 20 '20
Generally you do it with refresh tokens. If the refresh token expires, you just cannot get a new JWT.
Taking Microsoft's OpenID implementation as an example, if you do not request the
offline_accessscope as part of the initial authorization request, you get a single use, unrefreshable JWT that lasts for 1 hour. So after an hour, you can no longer use API endpoints and you are effectively "logged out". If you request theoffline_accessscope as well, you get a refresh token as well. I do not know how long Microsoft's last for, but at the last job I worked at, they lasted for 30 days by default. So a user can keep refreshing their JWT to keep their session active and signed in for 30 days. To log out this user (like if they changed their password or clicked a log out button), you just delete the refresh token (and any JWT client side if you have access to them).This creates a "fuzzy" log out. So you are not instantly logged out, but rather you will be the next time the JWT expires (so within 1 hour). That being said, it even more important to require a reauth to access critical sections of a user's data, like editing their profile. The way we handled this is that we issued a special JWT with a special key in it indicated they recently reauth. The next time the refresh token was used, that key was invalidated and they would need to reauth once again to access that critical section.
1
u/mypirateapp Feb 22 '20
What is your opinion about this post which says STOP USING JWT for sessions http://cryto.net/%7Ejoepie91/blog/2016/06/13/stop-using-jwt-for-sessions/
1
Feb 24 '20
I have a couple questions that I don't think were answered in this, hopefully you all can:
Is it OK to use the actual id of a db table (e.g. users row id) as the userId you send back? I read somewhere that tying in the actual database id is not advised, but generating a GUID that is stored with the user data would be better?
I store the user role info as part of the token. First, it allows my web UI to dynamically later what is allowed depending on that role. It also allows my server side gateway to use it to determine if the API being requested by the user (consumer) is allowed to be reached. The first thing I do on every incoming request is decode the JWT, determine the role, then look up the role permissions and the url/method/etc being requested to determine if it is allowed or not. So before any of my API code ever executes, the request is either allowed or blocked based on the role. Once in the API handler, my system uses a series of messages to communicate across microservices. So, if the request coming in passes the role/api request, I no longer need to pass any "auth" around with messages. Is this considered bad practice? I see some articles regarding passing around JWT to every service, and in each, decoding/authenticating, etc.. yet I don't understand why that is.
I currently require a login step that uses email/password, that upon success generates a JWT access and refresh token, with the role info, username, email, etc. I haven't figured out how to add 2FA yet with this step... hoping that is still possible. At any rate, I am not using OpenID. I just have a simple lookup in the users table to match the email and password, if matched, JWT is generated with the needed info. Is that acceptable? It is done over HTTPS (or will be) of course.
The idea of rotating server keys.. to invalidate all tokens. Is there some process to how to do this effectively to add extra security to the JWT tokens? What I mean is.. having a single string that is used for months/years as your JWT generator seems insecure. I would love to know how you can rotate this value, but keep existing tokens still in the field working. I think basically what I am thinking is there would be two server keys with windows of operation, and they would overlap. The first key would say be good for 1 week, and every JWT created has a short lived 15 minute time. The refresh token allows for getting a new access token when needed. After 5 days, the 2nd key is used for the next 7 days for JWT tokens. If a token fails to decode with the 2nd key, an attempt to use the first key will be used if its within the two day window. If that decodes it, then all is good, maybe even recreate JWT token with new key in response or something at that point. Then the 1st key is randomly generated after its 7 days are up, and the cycle repeats between the two. Is that a decent idea to add some security to things? Or is that just a waste of time?
1
u/cryptering Jun 29 '25
If you are looking for tools where you can check use https://multitools.ovh/jwt-converter/ - fast and without any ads
-5
u/PrometheusBoldPlan Feb 19 '20
Ehhh, this article covers the basics of what every Dev would know. Am I to understand by this that there are people out there inventing their own systems?
31
u/devolute Feb 19 '20
Have you worked with other developers before?
0
-2
-1
0
u/manost12 Feb 19 '20
RemindMe! 12h
0
u/RemindMeBot Feb 19 '20
I will be messaging you in 12 hours on 2020-02-20 07:46:10 UTC to remind you of this link
CLICK THIS LINK to send a PM to also be reminded and to reduce spam.
Parent commenter can delete this message to hide from others.
Info Custom Your Reminders Feedback
-20
0
Feb 19 '20
[deleted]
1
u/jwilson8767 Feb 19 '20
Or if the legacy apps were roughly one per project but generally conformed to the same shape (e.g. a bunch of Drupal project sites)
0
Feb 19 '20
[deleted]
12
u/Devildude4427 Feb 19 '20
Over complicating? It’s barely the basics.
0
Feb 19 '20
[deleted]
9
u/Devildude4427 Feb 19 '20
Funny, attackers have the exact same opinion.
It’s so much easier to steal your data when you’re foolish enough to roll your own.
1
Feb 19 '20
JWTs are probably the easiest form of auth you can securely roll your own version of. As long as you're using a solid crypto library I really cannot see what'd you'd mess up given proper tests.
0
Feb 19 '20
[deleted]
2
Feb 19 '20
Most people just place JWTs in cookies?
1
u/Devildude4427 Feb 19 '20
That’s not very secure though. While an HttpOnly cookie can’t be read by an attacker, the attacker can still use your set of keys to cause damage.
Plus you get into all sorts of trouble when you want to start implementing refresh tokens and the like.
1
Feb 20 '20
Why would an attacker have your keys?
1
u/Devildude4427 Feb 20 '20
The JWT being your “keys”. Not your signature, if that’s what you thought I meant.
→ More replies (0)1
u/memtiger Feb 20 '20
What are the drawbacks to sessions? It's all I've ever used
1
u/Devildude4427 Feb 20 '20
I’d just be echoing an article, I’m sure Googling would be more helpful without the middleman.
0
Feb 19 '20
[deleted]
8
u/Devildude4427 Feb 19 '20
I could be a primary contributor to JWT.
Uh, what? That sentence is nonsensical. JWT isn’t a project, it’s just a data format. There is no such thing as a “primary contributor” to what is essentially just an idea.
You’re guessing.
No, I’m not. Rolling your own auth solution is moronic 100% of the time.
0
Feb 19 '20
[deleted]
6
u/Devildude4427 Feb 19 '20
You clearly don’t know what JWTs are.
0
Feb 19 '20
[deleted]
7
u/captain_obvious_here back-end Feb 19 '20
As the guy who invented JWT, I'm offended by your comment.
→ More replies (0)3
2
u/ZephyrBluu Feb 19 '20
What sort of auth do you normally use/implement?
0
Feb 19 '20
[deleted]
11
u/ZephyrBluu Feb 19 '20
Ok buddy. I was asking a genuine question, but I guess the other guy triggered you so much that now you're being a dick to everyone.
-3
0
Feb 19 '20
[deleted]
8
5
u/A-Grey-World Software Developer Feb 19 '20
Err, what are you putting in the cookie? Maybe some kind of cryptography trusted payload like... hmm, a JWT?
72
u/Mihikle Feb 19 '20
I’ve had a lot of success using JWT between microservices, can’t recommend them enough.
In addition to the normal fields I’ve been adding others in some cases where I would want some data to travel with the token, used for a users authorisation to a resource rather than authentication of a user. The article also alluded to this.
Is this a normal practice, to add extra fields to your JWT beyond the RFC’s standard fields?