Hybrid JWT + session auth #1683
jasongitmail
started this conversation in
Ideas
Replies: 1 comment
-
This introduces the drawback of JWTs, as you mentioned, access tokens cannot be invalidated immediately. There is no way to immediately sign out a compromised account / malicious bot that holds the access token in memory from accessing the system. If this is not a concern, why not just implement a pure JWT strategy instead of a hybrid strategy? Can you explain the advantages of keeping the session table in this case? |
Beta Was this translation helpful? Give feedback.
0 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
-
Firstly, thanks for making Lucia. I just implemented v3 and went smoothly.
I know Pilcrow isn't a fan of JWTs as part of Lucia, but I want to encourage openness toward JWTs.
A hybrid strategy like below, would bring performance & cost benefits: 1.) reduced latency by removing the database request for most authenticated requests, & 2.) reduced DB usage. It could be optional.
Inspiration
Credit to @benawad: https://youtu.be/CcrgG5MjGOk?t=427
On Successful Login
sessions
table (id
,user_id
,expires_at
).access_token
(short expiration; e.g. 15min-4h)refresh_token
(longer expiration; e.g 30 days)Each contains session ID and user ID.
Incoming Requests
access_token
JWT (i.e. signature valid and not expired)refresh_token
JWT (i.e. signature valid and not expired)access_token
with extended expiration.refresh_token
expiration to today+30 days.expires_at
in DB's sessions table.refresh_token
with this expiration.On Successful Logout
sessions
table.access_token
andrefresh_token
cookies.How to expire all of a user's sessions, if needed.
Optional and rarely needed, but shows how to accomplish it if needed.
session_nonce
on the user's table, default to0
.session_nonce
within its data.refresh_token
, check if itssession_nonce
matchessession_nonce
in theuser's table. If not, consider the refresh token invalid and clear the user's
access_token
andrefresh_token
cookies.The user will retain access for the remaining duration of their
access_token
.__
Although security-critical applications, like banking, would want database sessions for immediate session revocation, a huge swath of consumer apps would benefit from performance and cost benefits provided by a hybrid JWT + session strategy.
Beta Was this translation helpful? Give feedback.
All reactions