5

I am using OAuth Flow in my web app. My web app interacts with Google Calendar API, and users authenticate by Signing In with Google (using their Gmail account).

I just wanted to make sure that my understanding and usage of Authentication Tokens and Access Tokens is correct:

  1. The JWT ID Token is only needed during Sign-in. We just need to validate it once during Sign-in like so: client.verifyIdToken({idToken: token, audience: CLIENT_ID}). The ID Token is not used to Authenticate a user to My Web App. Reference: https://developers.google.com/identity/sign-in/web/backend-auth

  2. This JWT ID Token is completely different from the Authentication Token I pass along to My Web App's secured(authenticated) endpoints. The Authentication Token represents a user's session, and it can be generated using any library (ie.crypto.generateRandomNumber()).

  3. On the other hand, I will have many Access Tokens, which I use to access third-party APIs (ie. Slack API, Google Calendar API). These Access Tokens are different from the JWT ID Token and Authentication Token mentioned above.

Is my understanding/implementation correct ? At one point, I was actually using my Google Calendar API Access Token as my Web App's Authentication Token, but realized this may be wrong. The Access Token would Authorize a user to use Google/Third-Party APIs, but I need a separate Authentication Token to Authenticate users into My Web App.

1 Answer 1

3

The JWT ID Token is only needed during Sign-in

Correct this is called Open Id connect, the id token is used to verify that the user behind the computer is the owner of the account as they know the login and password. Think of it as your birth certificate it proves you are you.

This JWT ID Token is completely different from the Authentication Token

Correct Id token or open id connect is built on top of Oauth2 which is used for authorization.

The Authentication Token represents a user's session, and it can be generated using any library.

Incorrect Oauth2 allows your application to request consent of the user who has been authenticated via Open id connect in some cases to grant the application access to their data. It has nothing to do with a session. With an access token your application has access to data for an amount of time. with a refresh token your app would be able to request a new access token when ever it expires. Consider authencation is more like your drivers license you are authorized to drive a car.

I will have many Access Tokens, which I use to access third-party APIs

Correct each third party api has their own authorization server. Your app will need to be registered by them they give you a client id and secret which you can use to generate access tokens to access your users data via their api.

Client id + client secret + user consent = access token & refresh token to the api that the scope granted

At one point, I was actually using my Google Calendar API Access Token as my Web App's Authentication Token, but realized this may be wrong.

Pre open id connect this probably happened a lot and still does.

The Access Token would Authorize a user to use Google/Third-Party APIs, but I need a separate Authentication Token to Authenticate users into My Web App.

Technically yes you do. But if you have an internal login system where your users create accounts in your system that that is your authentication there, your just requesting additional authorization to the users google account and you can store their refresh token as part of your internal authentication.

You can use multiple authentication providers (Facebook, twitter, google) but its best to have an internal one that maps them all together otherwise the user may end up with three accounts in your system.

4
  • Thanks @DaImTo for the crystal clear explaination. Previously, I've seen web apps which incorporate "Sign In with Google" send their JWT ID Token every time they call a secure, internal endpoint. I've also seen people generate their own Auth Token using some library, and pass that Auth Token to their secure endpoints. Based on your response, it seems the first approach is wrong. Does anybody have resources regarding how to build an internal auth system for node.js projects ? Commented Nov 7, 2021 at 14:48
  • You can not generate your own auth token auth tokens are gerneated by the authorization server. The only thing your application does is send its client id and secret in exchange for an authorization token. Commented Nov 7, 2021 at 19:58
  • @DalmTo, so sorry, I meant Authentication token (session id), rather than Oauth Authorization token in my 1st comment. I'll stick to calling it the session id from now on. Is it correct that I can use the Google Oauth Access Token as well as a Randomly Generated String as my session id ? Would both be valid as a session id for a "Sign in with Google" application ? Commented Nov 13, 2021 at 21:47
  • @universal thinker i am also building a web app with sign in with google and using google calendar api and having similar design problems. the reference you link to developers.google.com/identity/sign-in/web/backend-auth is for google sign-in, using the gapi library, which is being deprecated. is this what you are using or are you using the new sign in with google - developers.google.com/identity/gsi/web/reference/html-reference, which is replacing it. also are you building a traditional web app or an spa? Commented Nov 29, 2021 at 7:11

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.