Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add support for using Workbench's generic OAuth2 code capture (#320)
For data scientists using local tools or working in the IDE, authentication with APIs or databases that use the OAuth2 authorisation code flow (e.g. Google, Azure, or GitHub) typically work by running a temporary HTTP server on localhost and using that as the redirect URL. When a user gets redirected to localhost, the temporary HTTP server captures the code automatically and surfaces it to the caller. Unfortunately, this doesn't work at all on Workbench, because you can't just "redirect to localhost" like you can on your desktop. In #248 and follow-ups we added a workaround for this issue that we term the "pseudo out-of-band" flow -- that mechanism was pioneered by the {gargle} package in response to Google deprecating the original "out-of-band" flow. This commit adds support for a *third* mechanism for the auth code flow which uses a feature of the upcoming Workbench release that allows it to serve as a redirect URL for arbitrary OAuth2 applications on a static /oauth_redirect_callback endpoint. httr2 can retrieve any code sent to this URL by calling a simple JSON API at a static /oauth_code endpoint. This mechanism has strong appeal over the pseudo out-of-band flow because it sidesteps the requirement that users copy & paste the code, making it feel much more natural and automatic. It also avoids the need to ask users to host a static "capture your code" page, a la <https://www.tidyverse.org/google-callback/>, because every Workbench instance now has one. (This Workbench feature was explicitly designed for packages like httr2 to make use of so that the auth code flow starts feeling like magic once again.) I've decided to introduce client-specific environment variables here -- specifically, HTTR2_OAUTH_REDIRECT_URL and HTTR2_OAUTH_CODE_SOURCE_URL -- rather than having platform-specific ones prefixed with `WORKBENCH_`. If you're running a daily build of Workbench locally, you can test this as follows: local({ # Automatically determine the Workbench server's URL. We use # RSTUDIO_HTTP_REFERER here, which is only set in RStudio Pro sessions. url_split <- strsplit(Sys.getenv("RSTUDIO_HTTP_REFERER"), "/s/", fixed = TRUE)[[1]] if (nchar(url_split[1]) != 0L && length(url_split) == 2L) { base_url <- url_split[1] Sys.setenv( HTTR2_OAUTH_REDIRECT_URL = sprintf("%s/oauth_redirect_callback", base_url), HTTR2_OAUTH_CODE_SOURCE_URL = sprintf("%s/oauth_code", base_url) ) } }) client <- oauth_client( id = "28acfec0674bb3da9f38", secret = obfuscated(paste0( "J9iiGmyelHltyxqrHXW41ZZPZamyUNxSX1_uKnv", "PeinhhxET_7FfUs2X0LLKotXY2bpgOMoHRCo" )), token_url = "https://github.com/login/oauth/access_token", name = "hadley-oauth-test" ) oauth_flow_auth_code( client, auth_url = "https://github.com/login/oauth/authorize" ) In the future we can set the environment variables in Workbench sessions directly. Unit tests are included. Signed-off-by: Aaron Jacobs <[email protected]>
- Loading branch information