I know there are already many posts about Oauth, Oauth2, JWT, etc.. I have read many and I more confused than ever so I am looking for some clarification. I will propose my view on the subject and I hope somebody can tell me if my implementation is secure enough or what I am doing wrong and how to improve it.
I am building an API Rest server for serving my resources to my users. Let's suppose it is a bank app where users can deposit, withdraw and transfer money.
I am using nodejs, hapijs, jsonwebtokens, and bcrypt for my server. I want to implement two token authentication flow (Oauth2).
This is the way I am doing it:
User logs in to the auth server by giving some credentials (username and password).
The server verifies the user's credentials, if they are valid, it will grant access to the user and return a refresh token and an access token.
These tokens are saved into the local storage of the browser or mobile device.
The
access token
:- is signed as a jsonwebtoken.
- contains issued date, expiration date (5 min), user data (id, username).
The
refresh token
:- is signed as a jsonwebtoken and encrypted with bcrypt.
- contains a unique identifier
- may contain an expiration date
- is saved in the database.
As long as the
access token
is valid, that means, it has not expired and contains valid user data, the resource server serves the user the requested resources.When the
access token
is no longer valid, the auth server requests the client to provide arefresh token
in order to issue a newaccess token
- The server receives the
refresh token
from the user, decrypts it, compares it to the one in the database, checks if it has been revoked, and checks its unique identifier. - If the
refresh token
passes all tests, the server issues a newaccess token
to the client. - If the
refresh token
fails one test, the server requests the user to re-authenticate.
- The server receives the
Notes: I am trying to avoid the usage of cookies.
Questions:
- If the user is able to steal an
access token
, I guess it can also steal therefresh token
. So, how can I make therefresh token
more secure? - Is my perspective of the Oauth2 flow correct?
- What can I improve?
- Am I missing something?