Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 8 additions & 7 deletions R/Gargle-class.R
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ gargle2.0_token <- function(email = gargle_oauth_email(),
)

# pseudo-OOB flow
client_type <- if (inherits(app, "gargle_oauth_client")) client$type else NA
client_type <- if (inherits(client, "gargle_oauth_client")) client$type else NA
if (use_oob && identical(client_type, "web")) {
params$oob_value <- select_pseudo_oob_value(client$redirect_uris)
}
Expand Down Expand Up @@ -266,9 +266,9 @@ Gargle2.0 <- R6::R6Class("Gargle2.0", inherit = httr::Token2.0, list(
}

gargle_debug("email: {.email {self$email}}")
gargle_debug("oauth client name: {self$app$name}")
gargle_debug("oauth client name: {self$app$type}")
gargle_debug("oauth client id: {self$app$id}")
gargle_debug("oauth client name: {self$client$name}")
gargle_debug("oauth client name: {self$client$type}")
gargle_debug("oauth client id: {self$client$id}")
gargle_debug("scopes: {commapse(base_scope(self$params$scope))}")

cached <- token_from_cache(self)
Expand All @@ -279,15 +279,16 @@ Gargle2.0 <- R6::R6Class("Gargle2.0", inherit = httr::Token2.0, list(
gargle_debug("matching token found in the cache")
self$endpoint <- cached$endpoint
self$email <- cached$email
self$app <- cached$app
self$client <- cached$client
self$app <- cached$client
self$credentials <- cached$credentials
self$params <- cached$params
TRUE
},
#' @description (Attempt to) refresh a Gargle2.0 token
refresh = function() {
cred <- refresh_oauth2.0(
self$endpoint, self$app, self$credentials,
self$endpoint, self$client, self$credentials,
package = self$package
)
if (is.null(cred)) {
Expand All @@ -311,7 +312,7 @@ Gargle2.0 <- R6::R6Class("Gargle2.0", inherit = httr::Token2.0, list(
}
self$credentials <- init_oauth2.0(
self$endpoint,
self$app,
self$client,
scope = self$params$scope,
use_oob = self$params$use_oob,
oob_value = self$params$oob_value,
Expand Down
14 changes: 7 additions & 7 deletions R/oauth-init.R
Original file line number Diff line number Diff line change
Expand Up @@ -22,11 +22,11 @@
#' @param endpoint An OAuth endpoint, presumably the one returned by
#' `gargle_oauth_endpoint()`. The fact that this is even an argument is
#' because this function is based on `httr::init_oauth2.0()`.
#' @param app An OAuth client, preferably an instance of `gargle_oauth_client`.
#' @param client An OAuth client, preferably an instance of `gargle_oauth_client`.
#' @param scope a character vector of scopes to request.
#' @param use_oob Whether to use out-of-band auth. Results in conventional OOB
#' if the `app` is of type `"installed"` (or if type is unknown) and
#' pseudo-OOB if the `app` is of type `"web"`.
#' if the `client` is of type `"installed"` (or if type is unknown) and
#' pseudo-OOB if the `client` is of type `"web"`.
#' @param oob_value if provided, specifies the value to use for the redirect_uri
#' parameter when retrieving an authorization URL. For conventional OOB, this
#' defaults to "urn:ietf:wg:oauth:2.0:oob". For pseudo-OOB, this should be the
Expand All @@ -36,7 +36,7 @@
#' initial request to the authorization server.
#' @noRd
init_oauth2.0 <- function(endpoint = gargle_oauth_endpoint(),
app = gargle_client(),
client = gargle_client(),
scope = NULL,
use_oob = gargle_oob_default(),
oob_value = NULL,
Expand All @@ -47,7 +47,7 @@ init_oauth2.0 <- function(endpoint = gargle_oauth_endpoint(),

use_oob <- check_oob(use_oob, oob_value)

client_type <- if (inherits(app, "gargle_oauth_client")) app$type else NA
client_type <- if (inherits(client, "gargle_oauth_client")) client$type else NA

if (use_oob) {
redirect_uri <- oob_value %||% "urn:ietf:wg:oauth:2.0:oob"
Expand Down Expand Up @@ -83,7 +83,7 @@ init_oauth2.0 <- function(endpoint = gargle_oauth_endpoint(),

authorize_url <- httr::oauth2.0_authorize_url(
endpoint,
app,
client,
scope = scope,
redirect_uri = redirect_uri,
state = state,
Expand All @@ -99,7 +99,7 @@ init_oauth2.0 <- function(endpoint = gargle_oauth_endpoint(),
# Use authorisation code to get (temporary) access token
httr::oauth2.0_access_token(
endpoint,
app,
client,
code = code,
redirect_uri = redirect_uri
)
Expand Down
18 changes: 9 additions & 9 deletions R/oauth-refresh.R
Original file line number Diff line number Diff line change
@@ -1,29 +1,29 @@
# this file has its origins in oauth-refresh.R and oauth-error.R in httr
# I want to introduce behaviour to error informatively for a deleted OAuth app
# I want to introduce behaviour to error informatively for a deleted OAuth client

# Refresh an OAuth 2.0 credential.
#
# Refreshes the given token, and returns a new credential with a
# valid access_token. Based on:
# https://developers.google.com/identity/protocols/oauth2/native-app#offline
refresh_oauth2.0 <- function(endpoint, app, credentials, package = NULL) {
refresh_oauth2.0 <- function(endpoint, client, credentials, package = NULL) {
if (is.null(credentials$refresh_token)) {
gargle_abort("Refresh token not available.")
}

refresh_url <- endpoint$access
req_params <- list(
refresh_token = credentials$refresh_token,
client_id = app$key,
client_secret = app$secret,
client_id = client$key,
client_secret = client$secret,
grant_type = "refresh_token"
)

response <- httr::POST(refresh_url, body = req_params, encode = "form")

err <- find_oauth2.0_error(response)
if (!is.null(err)) {
gargle_refresh_failure(err, app, package)
gargle_refresh_failure(err, client, package)
return(NULL)
}

Expand Down Expand Up @@ -56,7 +56,7 @@ find_oauth2.0_error <- function(response) {
)
}

gargle_refresh_failure <- function(err, app, package = NULL) {
gargle_refresh_failure <- function(err, client, package = NULL) {
if (!identical(err$error, "deleted_client")) {
# this is basically what httr does, except we don't have an explicit
# whitelist of acceptable values of err$error, because we know Google does
Expand All @@ -70,8 +70,8 @@ gargle_refresh_failure <- function(err, app, package = NULL) {
}

# special handling for 'deleted_client'
app_name <- app$appname %||% ""
is_legacy_app <- grepl(gargle_legacy_app_pattern(), app_name)
client_name <- client$name %||% client$appname %||% ""
is_legacy_app <- grepl(gargle_legacy_app_pattern(), client_name)

# client looks like one of "ours"
if (is_legacy_app) {
Expand All @@ -92,7 +92,7 @@ gargle_refresh_failure <- function(err, app, package = NULL) {
gargle_warn(c(
"Unable to refresh token, because the associated OAuth client \\
has been deleted.",
"*" = if (nzchar(app_name)) "Client name: {.field {app_name}}",
"*" = if (nzchar(client_name)) "Client name: {.field {client_name}}",
if (!is.null(package)) {
c(
"i" = "If you did not configure this OAuth client, it may be built into \\
Expand Down