Workflow to implement Google OAuth2.0

Viewed 18

worflow

I'm trying to implement Google authentication for my app and the below is the workflow I'm trying to set up.

  1. First, user will authenticate with Google and obtain an access token.
  2. User will make requests with this token to backend services.
  3. Backend services will check with Google to validate these token
  4. Once validated, backend services will send information requested by client back to users

And I have a couple question around it:

  1. Is this the correct way to implement it?
  2. How to avoid check with Google for every single request between Backend and Frontend?
1 Answers

It's sort of the correct way. It depends on the details. If I understand correctly, you are in control of the front and backend (these are both your applications). If this is the case, then you would rather use Google services only to authenticate the user (so use an OpenID Connect flow to get an ID token to verify the user's identity). After that, you would have your backend either issue an access token or establish a session with your frontend. Then you wouldn't have to ask Google for the token's validity every time someone makes a request to your backend.

An access token that you get from Google, Facebook, etc. is meant to be used with their APIs. You could use it to authorize access to your own backend, but you then have to call Google on every request to verify the token. You are also tightly coupled to Google's details of the access token usage — what scopes are available, what data is returned with the token, expiration times, etc.

If the access token is a JWT, then you can verify it on your own in your backend. You don't have to call the issuer every time. But, if I remember correctly, Google issues opaque tokens, so this is not the way to go here.

To sum up. If you're in control of the front and back end, then authenticate with Google, then start a session between your applications. This will be simpler to maintain and also safer, as you wouldn't have to handle tokens in the browser.

Related