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
:
The refresh token
:
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 a refresh token
in order to issue a new access token
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.refresh token
passes all tests, the server issues a new access token
to the client.refresh token
fails one test, the server requests the user to re-authenticate.Notes: I am trying to avoid the usage of cookies.
Questions:
access token
, I guess it can also steal the refresh token
. So, how can I make the refresh token
more secure?If the Refresh Token was expired, remove it from database and return message. Continue to use user id field of RefreshToken object as parameter to generate new Access Token using jsonwebtoken library. Return { new accessToken , refreshToken } if everything is done. Or else, send error message.
Refresh token: The refresh token is used to generate a new access token. Typically, if the access token has an expiration date, once it expires, the user would have to authenticate again to obtain an access token.
A refresh token just helps you re-validate a user without them having to re-enter their login credentials multiple times. The access token is re-issued, provided the refresh token is a valid one requesting permission to access confidential resources.
The reason OAuth2 is so confusion to many people is because it uses different authentication flows depending on what kind of client is used.
OAuth2 distinguishes two client type, confidential or public. Next to that, there are 2 grant flows that are redirection based (auth code and implicit) which are meant to be used with a browser or browser control.
The other two flows (resource owner password and client credentials) are meant to be used from non-browser apps (CLI, background services, trusted mobile clients).
I've described the different flows and when to use them in more detail in this answer here.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With