Using JWT for Sessions
Using JWT for Sessions
This is sort of my objection - "Don't use JWT" is widely understood to mean "Don't use tokens," where that is not the case.
Join the DZone community and get the full member experience.Join For Free
The topic has been discussed many times, on hacker news, Reddit, blogs. And the consensus is - DON'T USE JWT (for user sessions).
I won't repeat everything here, so please go and read those articles. You can really shoot yourself in the foot with JWT, it's complex to get to know it well and it has little benefits for most of the use cases. I guess for API calls it makes sense, especially if you reuse the same API in a single-page application and for your RESTful clients, but I'll focus on the user session use case.
Having all this criticism, I've gone against what the articles above recommend, and use JWT, navigating through their arguments and claiming I'm in a sweet spot. I can very well be wrong.
I store the user ID in a JWT token stored as a cookie. Not local storage, as that's problematic. Not the whole state, as I don't need that may lead to problems (pointed out in the linked articles). In fact, I don't have any session state apart from the user data, which I think is a good practice.
What I want to avoid in my setup is sharing sessions across nodes. And this is a very compelling reason to not use the session mechanism of your web server/framework. No, you don't need to have millions of users in order to need your application to run on more than one node. In fact, it should almost always run on (at least) two nodes, because nodes die and you don't want downtime. Sticky sessions at the load balancer are a solution to that problem but you are just outsourcing the centralized session storage to the load balancer (and some load balancers might not support it). Shared session cache (e.g. Memcached, Elasticache, Hazelcast) is also an option, and many web servers (at least in Java) support pluggable session replication mechanisms, but that introduces another component to the architecture, another part of the stack to be supported and that can possibly break. It is not necessarily bad, but if there's a simple way to avoid it, I'd go for it.
In order to avoid shared session storage, you need either the whole session state to be passed in the request/response cycle (as a cookie, request parameter, header), or to receive a userId and load the user from the database or a cache. As we've learned, the former might be a bad choice. Despite that fact that frameworks like ASP.NET and JSF dump the whole state in the HTML of the page, it doesn't intuitively sound good.
As for the latter - you may say "ok, if you are going to load the user from the database on every request this is going to be slow and if you use a cache, then why not use the cache for the sessions themselves?" Well, the cache can be local. Remember we have just a few application nodes. Each node can have a local, in-memory cache for the currently active users. The fact that all nodes will have the same user loaded (after a few requests are routed to them by the load balancer in a round-robin fashion) is not important, as that cache is small. But you won't have to take any care for replicating it across nodes, taking care of new nodes coming and going from the cluster, dealing with network issues between the nodes, etc. Each application node will be an island not caring about any other application node.
So here goes my first objection to the linked articles - just storing the user identifier in a JWT token is not pointless, as it saves you from session replication.
What about the criticism of the JWT standard and the security implications of its cryptography? Entirely correct, it's easy to shoot yourself in the foot. That's why I'm using JWT only with MAC, and only with a particular algorithm that I verify upon receiving the token, thus (allegedly) avoiding all the pitfalls. In all fairness, I'm willing to use the alternative proposed in one of the articles - PASETO - but it doesn't have a Java library and it will take some time implementing one (might do in the future). To summarize - if there was another easy to use way for authenticated encryption of cookies, I'd use it.
So I'm basically using JWT in "PASETO-mode," with only one operation and only one algorithm. And that should be fine as a general approach - the article doesn't criticize the idea of having a user identifier in a token (and a stateless application node), it criticizes the complexity and vulnerabilities of the standard. This is sort of my second objection - "Don't use JWT" is widely understood to mean "Don't use tokens," where that is not the case.
Have I introduced some vulnerability in my strive for architectural simplicity and lack of shared state? I hope not.
Published at DZone with permission of Bozhidar Bozhanov , DZone MVB. See the original article here.
Opinions expressed by DZone contributors are their own.