I’m building a Chrome extension that:
- Fetches data from a Google API (e.g. Drive)
- From the client, POSTs a different set of data to an API endpoint I control, over HTTPS
In the client, using the chrome.identity API, I’m able to successfully retrieve a short-lived access token (using the implicit flow) that I can use to fetch data from Google APIs. When the token expires, I’m able to silently fetch another in the background. This portion works fine.
Separately, for use case #2 above, I’d like to ensure only the target Google user can successfully POST data. For this purpose, I was considering utilizing the Google Sign-In JavaScript library, retrieving an ID token on successful auth and passing that ID token to my API for server-side validation, as noted here.
The Google Sign-In JavaScript library doesn’t appear to work in Chrome extensions. I was seeing the same error as the author of that issue (“Uncaught gapi.auth2.ExternallyVisibleError: Invalid cookiePolicy”) when trying to implement a Sign-In button within the extension.
I would like to avoid managing any portion of the OAuth / OpenID Connect flow in a custom way. However, using the chrome.identity.launchWebAuthFlow method, I am able to retrieve an ID token, by directing the user to the Google OAuth endpoint using response_type=id_token, adding the correct scopes (“openid” and “email”, for instance) and passing a nonce, like this:
https://accounts.google.com/o/oauth2/v2/auth?
client_id=[client_id]&
response_type=id_token&
scope=scope=openid%20email&
redirect_uri=https://<app-id>.chromiumapp.org/redirect&
state=[state_token]&
nonce=[nonce]
I have three questions:
- Is there a way to retrieve an ID token from Google using a client library that is known to work on Chrome extensions, so I don’t have to retrieve an ID token using a custom implementation?
- In general, is using the implicit flow to retrieve short-lived ID tokens from the client, passing that token to a server, and validating the token from the server using Google OAuth libraries an acceptable and secure way to manage this?
- If this is not ideal, is there a recommended way to validate the user from the API (use case #2) given some of the Chrome extension limitations I’m dealing with? E.g. by using the hybrid flow, or another mechanism?