News for hackers. Stuff that matters.
Re:JSON Web Tokens vs. Sessions by nstart on Saturday June 18, 2016 @01:07PM (#11929465)
That last part where he talks about logging out being the responsibility of the client is rather key. Basically I can't invalidate the key from the server side. So if a user's account is compromised and they recover it on their mobile app for example, I can't sign the user out of everywhere else too. It's what has given me pause about jwt so far and has held me back from using it. I find the cookie is generally good enough to hold most other information I need about the user.
Re:JSON Web Tokens vs. Sessions by tjbiddle on Sunday June 19, 2016 @02:23AM (#11932012)
I store a `login_count` in my User model. When searching for a user I `find_by_user_and_login_count`. The token they have has their current login_count in the claims. If I want to logout server side, I just update this value (Can be a random string if you'd rather it not be in increasing integer) and it will invalidate all tokens. When they logout client side, of course we can just clear the token from their memory. If they want to invalidate all tokens, then we have an API endpoint for that and it will logout from all devices.
Re:JSON Web Tokens vs. Sessions by bigonlogn on Saturday June 18, 2016 @01:25PM (#11929588)
You can implement sign out everywhere by setting a reauth flag on the user in the database. You lose the "completely stateless" aspect that JWT claims to provide, but it's a small trade-off for tighter security.
Re:JSON Web Tokens vs. Sessions by tszming on Saturday June 18, 2016 @02:03PM (#11929822)
Effectively now you are using database as your session storage?
Re:JSON Web Tokens vs. Sessions by dexterdog on Saturday June 18, 2016 @02:07PM (#11929844)
But you're not storing the session there, just the key/token so you can change it. The session payload is still completely in the JSON body maintained by the client and sent with each request.
Re:JSON Web Tokens vs. Sessions by jeremyjh on Saturday June 18, 2016 @02:49PM (#11930023)
What difference does that make? Once you commit to this you have to do a database lookup for every request. Why not keep the session data there?
Re:JSON Web Tokens vs. Sessions by dexterdog on Sunday June 19, 2016 @01:29PM (#11933751)
You only have to validate the token. It doesn't have to be a database-type medium because you're not writing very often in fact all you're really doing is making sure the token is not invalid. The session data could be changing on every request which would be at least one write on every request. With this system you are only writing to the central medium on a session creation or a session invalidation.
Re:JSON Web Tokens vs. Sessions by bigonlogn on Sunday June 19, 2016 @02:28PM (#11933974)
This is not entirely true. Since we talking about implementing stateful sessions, you could receive a valid token (stolen, out otherwise) after the user has logged out.

You are correct that the lookup doesn't have to be via the database. You could implement a caching system where the cache is invalidated when the user logs out and requires reauthentication. This is the notion of the session. By definition they cannot be stateless.

Stateless authentication is inherently (slightly) less secure than sessions. I think of a blind librarian who gives out keys to the library. Whoever has a key has access. You can put limitations on the timeframe someone has access to the library, but that's it. If your key gets stolen, the blind librarian can't help you as there is no way for him to tell if it's really you.

Re:JSON Web Tokens vs. Sessions by nneonneo on Saturday June 18, 2016 @01:55PM (#11929776)
If the user reauthenticates and you unset the reauth flag, wouldn't their previous sessions (e.g. tokens held by an attacker) suddenly become valid again? How would you prevent such an attack?
Re:JSON Web Tokens vs. Sessions by inopinatus on Saturday June 18, 2016 @02:01PM (#11929813)
You wouldn't use a boolean flag. I suggest setting a validity timestamp for the user, and reject any token that was issued-at any earlier time.

(This isn't a perfect scheme since a compromised issuer could have been induced to send post-dated tokens. If your need for global logout was to invalidate tokens issued by a compromised issuer, you'll need to blacklist keys as well)

Re:JSON Web Tokens vs. Sessions by btdiehr on Saturday June 18, 2016 @01:16PM (#11929537)
Why do you think you can't invalidate a JWT? Store a JWT that is associated with some object in a database that has the field `isInvalidated`. This isn't rocket science. Sure - this turns the JWT token into a session but there is no way to invalidate based on something that isn't determined at creation time without storing something in a database.
Re:JSON Web Tokens vs. Sessions by jontro on Saturday June 18, 2016 @03:34PM (#11930223)
What's the point of using a JWT if so? This effectively converts it into a session token
Re:JSON Web Tokens vs. Sessions by baseonmars on Sunday June 19, 2016 @12:07PM (#11933415)
You can create a JWT without writing to a datastore - this is a key difference.
Re:JSON Web Tokens vs. Sessions by kensign on Saturday June 18, 2016 @04:28PM (#11930473)
use a ttl on like a cache store, such as redis
Re:JSON Web Tokens vs. Sessions by jcoffland on Saturday June 18, 2016 @05:11PM (#11930609)
Yes, but this is still a deadend.
Re:JSON Web Tokens vs. Sessions by zbjornson on Sunday June 19, 2016 @12:00PM (#11933390)
How so?

It's effectively a micro-optimization that will have no real effect, but you can do a simple "exists" query when searching the revocation list, and the TTL keeps the collection small.

Not advocating for JWT as it's a silly mess to do everything correctly, but it is possible.

Re:JSON Web Tokens vs. Sessions by jcoffland on Sunday June 19, 2016 @08:13PM (#11935407)
Searching a revocation list misses the point of RESTful authentication.
Re:JSON Web Tokens vs. Sessions by zimbatm on Monday June 20, 2016 @05:42AM (#11937037)
There is no such thing as a RESTful authentication. REST's design hinges on URIs being public.
Re:JSON Web Tokens vs. Sessions by jcoffland on Friday June 24, 2016 @03:52AM (#11967636)
What do public URIs have to do with it? I don't think we are speaking the same language.
Re:JSON Web Tokens vs. Sessions by aleem on Saturday June 18, 2016 @01:16PM (#11929535)
The impact can be mitigated by having low TTLs and using refresh tokens. This will give you a rolling window. If the TTL is 10 minutes and the client doesn't make any requests in 10 minutes, they will be timed out. But if the client continues to have at least one request every 10 minutes the session persist. Session persistence can also be ensured by having your web-client for example make a request to the server every couple of minutes.

Server-side key invalidation is entirely possible but it would require having a blacklist of disabled keys and comparing every requests against the black list. This would obviously concede the benefits of scale from JWT tokens since you are doing the same thing as server side sessions. However, the black list should be considered only as an escape hatch and need not be enabled at all times. In fact, once all the tokens in the black list expire, the black list itself can be disabled and things go back to the way they were.

Re:JSON Web Tokens vs. Sessions by jcoffland on Saturday June 18, 2016 @05:10PM (#11930602)
This is exactly right. Many people confuse this problem with solutions that involve storing state on the server. Doing so completely misses the whole point of RESTful authentication.