A common scenario for user authentication follows these steps:
- User registers and logs in using its credentials (username/password)
- The server verifies the user's credentials and, if valid, returns an
access token
and arefresh token
- The
access token
is sent on further requests and, if it is valid, the server responds with the requested resource - When the
access token
is no longer valid, the server requests the client to provide arefresh token
in order to issue a newaccess token
- The server receives the
refresh token
and two things may happen:- if the
refresh token
is valid, a newaccess token
is issued to the client - if not, the server requests the user to authenticate
- if the
For the client to be able to send the access token
in every request, the token should be stored either on browser storage (local/session storage) or cookies, which makes it vulnerable to XSS (Cross-Site Scripting) attacks. This problem may be mitigated if we set a short lifetime to the access token
.
My question, however, is regarding to the refresh token
. This token should have a long lifetime and since we use it to get new access tokens
, it would be a problem if an attacker would intercept it. So storing this token on the client side might not be a good idea, right?
But, what are the alternatives?
Can we store it in a cookie set with the "httpOnly" flag? But wouldn't this make it vulnerable to CSRF (Cross-Site Request Forgery) attacks?
Is it safe to encrypt the token and still save it on browser storage?
Thank you in advance. Best regards!