googleworkspace / apps-script-oauth2

An OAuth2 library for Google Apps Script.
https://developers.google.com/apps-script/
Apache License 2.0
1.56k stars 429 forks source link

Support for Redirect URI direct to a Library Script rather than the Outer Script #326

Closed drewharding-da closed 2 years ago

drewharding-da commented 3 years ago

As of today when using OAuth2 library within a library, the auth callback must always be directed to the outer script, which can then delegate back to the library as needed. This comment on stack overflow confirms this to be the case. However for me to successfully use this library I need the ability for a state token to be created in a library which is running in an outer script, and then having the redirect URI go straight to the library. The reason this feature is needed for my particular case is due to limitations in the application I am trying to integration to (Jira):

  1. I have integrated the Jira API to our Google Sheets for real time data reporting across our projects using a Google App Script library. Until recently Atlassian did not support OAuth2, but have now enabled support for OAuth2 for their APIs. My company has asked that we upgrade our integration to use OAuth2 over basic auth, which is also in line with Atlassian's recommendation.
  2. I can make the interaction work when I use the OAuth2 library directly in the outer script. In this case the interaction works perfectly (OAuth2 is a great library!). However, I have multiple spreadsheets that require this authentication handshake to Jira, so encapsulating it in my own GAS library is much more preferable.
  3. What's more, Atlassian require the Redirect URI to be hardcoded in their OAuth2 App configuration. This means I cannot simply provide a different Redirect URI in my Auth URL for each script even if I wanted to, but instead would need to configure a new app in the Atlassian portal for each spreadsheet that uses my GAS library to integrate to the Jira API. Given the large number of spreadsheets and the way they are created adhoc by my users through copying an existing spreadsheet, this would not be a feasible solution for me to support.
  4. I can override the Redirect URI script id using the OAuth2.getRedirectUri(scriptId) function using the script id of the library and then setting this back in the service, but when this happens the following error is encountered as part of the callback: The state token is invalid or has expired. Please try again. because it's not possible in the current implementation for my custom library to receive the callback request.

I would appreciate any assistance in having the above feature included for OAuth2 integration.

GabrielMontplaisir commented 3 years ago

I am encountering the same issue as you but for a different purpose. I'm trying to implement the Google Picker as a standalone script, to be distributed as a library.

My standalone script works for myself as I can authenticate both the outer script and the library script. However, others do not or will not have access to the library script to authenticate. When a user doesn't authenticate the library and runs the script from the outer script (a Google Sheets), then it returns as a ScriptError: Authentication is required.

This oAuth2 library is great, but requires the outer script to be used as a URI (using the library Script ID returns a 403 mismatch error), which doesn't work when people are creating copies of the document in question.

Would love to see this work, as I'm running out of ideas otherwise.

sqrrrl commented 2 years ago

It's not possible for the callback to go directly to the script as described. However, at least for the original case, the recommended approach is to implement an add-on instead of a library.

Add-ons share the same script ID & storage across all documents it's used in. Users would then only need to authenticate once with Jira, regardless of the number of docs/sheets/etc they're interacting with.