Search code examples
restoauth-2.0microservices

How to provide OAuth through services?


I have 3 services (in the real much more):

  1. Authorization service (uses OAuth 2.0)
  2. Frontend service
  3. Resource service

and client (web-browser).

I store session_id, access_token and refersh_token in cookies of the user's web-browser. The user goes to Auth service, signs in and gets these tokens. After his web-browser is redirected to Frontend.
Frontend and Resource services can't validate tokens because they know a nothing about it, so they must make a request to Auth service.
The current scenarios:
The user (web-browser) sends a request to Frontend service, the Frontend sends a request to Auth service to validate access_token. If it's invalid the Frontend sends a request to refresh token using refresh_token.
If the Frontend needs an access to Resource service to process a request then the Frontend sends its client_id and access_token to Resource service. The Resource service sends a request to the Auth service to validate an access_token too.

Are my thoughts right? Or it has simpler schema?
P.S. All services use RESTful architecture.


Solution

  • OAuth talks about how the tokens be exchanged. What you have mentioned it seem liek you are talking about using implicit grant, which is little less secure and you may think of opting for authorisation flow.

    Other than that, in microservices when you have many services and one user request pass through many downstream services, verifying the token with auth provider at each and every step might become a bottleneck.

    There are ways out there by which you can skip this call to auth server and still validate the sanctity of the token without making an explicit call. One way is to make use of JWT. These tokens are signed by the Auth provider and your services have keys which can help you validate if the token is modified on it way, and token itself has all the information you need to ensure validity of it, like expiry time, intended audience, clients, roles etc.

    On login you get AT and RT. AT could be passed along to downstream for authentication and authorization and RT could be used when AT is expired. You only need to talk to auth provider at the time of login and when you need to refresh the token.

    You can read more about the JWT OAuth2.0 with JWT and OIDC to get more information around it