Skip to content

Commit 74131d6

Browse files
authored
gargle_oauth_client_type: new option and accessor (#244)
* gargle_oaut_client_type: new option and accessor * Add NEWS bullet
1 parent 21c11ee commit 74131d6

6 files changed

Lines changed: 55 additions & 2 deletions

File tree

NAMESPACE

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ export(gargle_map_cli)
3333
export(gargle_oauth_cache)
3434
export(gargle_oauth_client)
3535
export(gargle_oauth_client_from_json)
36+
export(gargle_oauth_client_type)
3637
export(gargle_oauth_email)
3738
export(gargle_oauth_sitrep)
3839
export(gargle_oob_default)

NEWS.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,12 @@ Changes of note:
2020

2121
gargle is better able to detect when it's running on Posit Workbench or RStudio Server, e.g., in a subprocess.
2222

23+
`gargle_oauth_client_type()` is a new function that returns either "installed"
24+
or "web".
25+
It returns the value of the new global option by the same name (`"gargle_oauth_client_type"`), if defined.
26+
If the option is not defined, returns "web" on RStudio Server, Posit Workbench, or Posit Cloud, and "installed" otherwise.
27+
In the context of out-of-band (OOB) auth, an "installed" client type leads to the conventional OOB flow (only available for GCP projects in testing mode) and a "web" client leads to the new pseudo-OOB flow.
28+
2329
# gargle 1.3.0
2430

2531
## (Partial) deprecation out-of-band (OOB) auth flow

R/gargle-package.R

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -94,3 +94,24 @@ gargle_oob_default <- function() {
9494
gargle_oauth_cache <- function() {
9595
getOption("gargle_oauth_cache", default = NA)
9696
}
97+
98+
#' @rdname gargle_options
99+
#' @export
100+
#' @section `gargle_oauth_client_type`:
101+
#' `gargle_oauth_client_type()` returns the option named
102+
#' "gargle_oauth_client_type", if defined. If defined, the option must be either
103+
#' "installed" or "web". If the option is not defined, the function returns:
104+
#' * "web" on RStudio Server, Posit Workbench, or Posit Cloud
105+
#' * "installed" otherwise
106+
#' Primarily intended to help infer the most suitable OAuth client when a user
107+
#' is relying on a built-in client, such as the tidyverse client used by
108+
#' packages like bigrquery, googledrive, and googlesheets4.
109+
gargle_oauth_client_type <- function() {
110+
opt <- getOption("gargle_oauth_client_type")
111+
if (is.null(opt)) {
112+
if(is_rstudio_server()) "web" else "installed"
113+
} else {
114+
check_string(opt)
115+
arg_match(opt, values = c("installed", "web"))
116+
}
117+
}

R/gargle_oauth_client.R

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -199,7 +199,7 @@ list_redact <- function(x, names, case_sensitive = TRUE) {
199199
#' }
200200
gargle_client <- function(type = NULL) {
201201
if (is.null(type) || is.na(type)) {
202-
type <- if(is_rstudio_server()) "web" else "installed"
202+
type <- gargle_oauth_client_type()
203203
}
204204
check_string(type)
205205
type <- arg_match(type, values = c("installed", "web"))
@@ -218,7 +218,7 @@ tidyverse_client <- function(type = NULL) {
218218
check_permitted_package(parent.frame())
219219

220220
if (is.null(type) || is.na(type)) {
221-
type <- if(is_rstudio_server()) "web" else "installed"
221+
type <- gargle_oauth_client_type()
222222
}
223223
check_string(type)
224224
type <- arg_match(type, values = c("installed", "web"))

man/gargle_options.Rd

Lines changed: 17 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

tests/testthat/test-assets.R

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,15 +2,18 @@ test_that("default options", {
22
withr::local_options(list(
33
gargle_oauth_cache = NULL,
44
gargle_oob_default = NULL, httr_oob_default = NULL,
5+
gargle_oauth_client_type = NULL,
56
gargle_oauth_email = NULL,
67
gargle_verbosity = NULL,
78
gargle_quiet = NULL
89
))
910
expect_equal(gargle_oauth_cache(), NA)
1011
if (is_rstudio_server()) {
1112
expect_true(gargle_oob_default())
13+
expect_equal(gargle_oauth_client_type(), "web")
1214
} else {
1315
expect_false(gargle_oob_default())
16+
expect_equal(gargle_oauth_client_type(), "installed")
1417
}
1518
expect_null(gargle_oauth_email())
1619
expect_equal(gargle_verbosity(), "info")
@@ -32,6 +35,11 @@ test_that("gargle_oob_default() consults httr's option", {
3235
expect_true(gargle_oob_default())
3336
})
3437

38+
test_that("gargle_oauth_client_type() consults the option", {
39+
withr::local_options(list(gargle_oauth_client_type = "web"))
40+
expect_equal(gargle_oauth_client_type(), "web")
41+
})
42+
3543
test_that("gargle API key", {
3644
key <- gargle_api_key()
3745
expect_true(is_string(key))

0 commit comments

Comments
 (0)