Reputation: 995
Scenario is that we have a REACT SPA and a single API which is both a resource server and an authentication server. We want to implement simple tokens based auths. There is no dedicated SSO server so no need for OAuth2.
I've read lot of articles for the last two days on how to do this correctly and there is one thing that still bothers me. There is a lot of discussion whether the tokens should be stored in the LocalStorage or a cookie? First is vulnerable to XSS attacks because injected code can steal your data from the LocalStorage but protects against CSRF because forged request from malicious website won't be able to steal it. The latter is vulnerable to CSRF because forged request from malicious website makes browser send the cookies along but protects against XSS because malicious website don't have access to LocalStorage of the original website.
Why nobody creates two JWTs signed with two different keys and two different refresh_token secrets? One JWT and refresh_token is then stored in the LocalStorage and the other in a cookie. If I ask google it's always one vs. another. Is having both an anti pattern or a bad idea in any way? Because it is not that much work to implement that...
Here's how I think such flow can work:
/api/auth/login
. In return it gets:access_token
signed by private_key1
and some secret refresh_token
generated for this particular user and stored in some kind of persistence layerhttpOnly
cookie: JWT access_token
signed by private_key2
httpOnly
cookie with path set to /api/auth/refresh
: some secret refresh_token
generated for this particular user and stored in some kind of persistence layer that is different than the one returned in bodyAuthentication
header and the second one is attached automatically by the browser as a cookie./api/auth/refresh
with JWT and refresh_token
from LocalStorage and browser attaches second JWT as a cookie and the other refresh_token
as a cookie as well because the path condition for the cookie is met.refresh_token
s exist in the persistence layer for this user and have not expired. If everything is fine new JWTs and also new refresh_token
s are generated and returned to the react APP same way as in point 2.On logout refresh tokens are removed from the persistence layer so access tokens simply won't be refreshed.
I came to a conclusion that, indeed, adding refresh tokens doesn't increase security for users and have the drawback that there is this small gap after logout/revoke when JWTs are still valid but the refresh tokens are revoked/user logged out but have a lot of other pros that I managed to gather from many many discussions from this portal:
/api/auth/refresh
your team may have a policy that all team members need to accept it plus maybe someone from security teamUpvotes: 3
Views: 533
Reputation: 995
We have implemented this flow and here are two main conclusions:
httpOnly
cookies are blocked by the Safari browser when API and frontend are hosted on a different domain (e.g., frontend.com and api.com)Upvotes: 1