Skip to content

Commit

Permalink
Pull out scope helper
Browse files Browse the repository at this point in the history
And use in oauth_service_token(). Fixes r-lib#389
  • Loading branch information
hadley authored and jiwalker-usgs committed Jul 24, 2017
1 parent c1a9e9d commit 186e647
Show file tree
Hide file tree
Showing 4 changed files with 30 additions and 3 deletions.
3 changes: 3 additions & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
# httr 1.2.1.9000

* `oauth_service_token()` now accepts a character vector containing multiple
scopes as described in the documentation (#389).

* Don't export `length()` method for internal `path` class (#395)

* New oauth cache files are always added to `.gitignore` and, if it exists, `.Rbuildignore`. Specifically, this now happens when option `httr_oauth_cache = TRUE` or user specifies cache file name explicitly. (@jennybc #436)
Expand Down
13 changes: 12 additions & 1 deletion R/oauth-init.R
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ init_oauth2.0 <- function(endpoint, app, scope = NULL, user_params = NULL,
state <- nonce()
}

scope_arg <- paste(scope, collapse = ' ')
scope_arg <- check_scope(scope)

authorize_url <- modify_url(endpoint$authorize, query = compact(list(
client_id = app$key,
Expand Down Expand Up @@ -121,3 +121,14 @@ init_oauth2.0 <- function(endpoint, app, scope = NULL, user_params = NULL,
stop_for_status(req, task = "get an access token")
content(req, type = type)
}

check_scope <- function(x) {
if (is.null(x)) {
return(NULL)
}

if (!is.character(x)) {
stop("`scope` must be a character vector", call. = FALSE)
}
paste(x, collapse = ' ')
}
4 changes: 2 additions & 2 deletions R/oauth-token.r
Original file line number Diff line number Diff line change
Expand Up @@ -273,8 +273,8 @@ oauth_service_token <- function(endpoint, secrets, scope = NULL) {
stop("`endpoint` must be an OAuth endpoint", call. = FALSE)
if (!is.list(secrets))
stop("`secrets` must be a list.", call. = FALSE)
if (!is.null(scope) && !(is.character(scope) && length(scope) == 1))
stop("`scope` must be a length 1 character vector.", call. = FALSE)

scope <- check_scope(scope)

TokenServiceAccount$new(
endpoint = endpoint,
Expand Down
13 changes: 13 additions & 0 deletions tests/testthat/test-oauth.R
Original file line number Diff line number Diff line change
Expand Up @@ -52,3 +52,16 @@ test_that("oauth_encode1 works", {

expect_equal(orig_string, restored_string)
})


# Parameter checking ------------------------------------------------------

test_that("scope must be character or NULL", {
expect_equal(check_scope("a"), "a")
expect_equal(check_scope(NULL), NULL)
expect_error(check_scope(1), "character vector")
})

test_that("multiple scopes collapsed with space", {
expect_equal(check_scope(c("a", "b")), "a b")
})

0 comments on commit 186e647

Please sign in to comment.