I'm going to implement OAuth 2.0 and REST API with it
to grant different permissions per users and also to scale well.
To scale well, stateless is easier because there is
NO file, database, in-memory based session with it.
Below is how I understand OAuth 2.
- OAuth Server give an access token to a user.
- The user's access token is stored in cookie.
- When user access to REST API, user sends with the access token.
- Server receives request with access token.
- Server find out whether access token is valid and the user has permission to do request.
- Do or reject based on user's privilege.
So I do not have to worry about session storage. Right?
A more recent innovation is JWT - JSON Web Token.
Here is a link to the spec: JWT - JSON Web Token
JWT is a method of using Hashed tokens using a Hashing method such as HMAC which stands for a Hash-based Message Authentication Code. Because the token is hashed using a secret key, the server can determine if the token has been tampered with.
Here is an example method to create a Hashed token for JWT:
Here is an example of decoding a token to ensure it was not tampered with:
Here is an article with a more complete example: Stateless Authentication
What you are describing here, is the OAuth 2 Implicit Grant flow. OAuth 2 also includes three other flows, but as it seems that your ressource owner (the user) is initiating requests using browser side Javascript (you were talking about cookies), this is the flow you should go for.
On client side, OAuth only requires you to store the
access_token
for accessing protected ressources (and arefresh_token
if you're going for an expiringaccess_token
).