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
2 changes: 2 additions & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
# httr 1.3.1.9000

* Scopes are de-duplicated, sorted, and stripped of names before being hashed. This eliminates a source of hash mismatch that causes new tokens to be requested, even when existing tokens have the necessary scope. (@jennybc, #495)

* The default value of `failure` argument in `parse_http_date()` is set to `structure(NA_real_, class = "Date")` so that the reponse with a "failure" date can be printed out correctly. (@shrektan, #544)

# httr 1.3.1
Expand Down
9 changes: 8 additions & 1 deletion R/oauth-token.r
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,10 @@ Token <- R6::R6Class("Token", list(
# endpoint = which site
# app = client identification
# params = scope
msg <- serialize(list(self$endpoint, self$app, self$params$scope), NULL)
msg <- serialize(
list(self$endpoint, self$app, normalize_scopes(self$params$scope)),
NULL
)

# for compatibility with digest::digest()
paste(openssl::md5(msg[-(1:14)]), collapse = "")
Expand Down Expand Up @@ -349,3 +352,7 @@ TokenServiceAccount <- R6::R6Class("TokenServiceAccount", inherit = Token2.0, li
cache = function(path) self,
load_from_cache = function() self
))

normalize_scopes <- function(x) {
setNames(sort(unique(x)), NULL)
}
4 changes: 3 additions & 1 deletion tests/testthat/test-oauth-cache.R
Original file line number Diff line number Diff line change
Expand Up @@ -30,13 +30,14 @@ test_that("use_cache() respects options", {
expect_equal(use_cache(), ".httr-oauth")
})

test_that("token saved to and restored from cache", {
test_that("token saved to and restored from cache, scopes normalized in hash", {
owd <- setwd(tmp_dir())
on.exit(setwd(owd))

token_a <- Token2.0$new(
app = oauth_app("x", "y", "z"),
endpoint = oauth_endpoints("google"),
params = list(scope = c(a = "a", "b")),
cache_path = ".tmp-cache",
credentials = list(a = 1)
)
Expand All @@ -45,6 +46,7 @@ test_that("token saved to and restored from cache", {
token_b <- Token2.0$new(
app = oauth_app("x", "y", "z"),
endpoint = oauth_endpoints("google"),
params = list(scope = c(b = "b", "a")),
cache_path = ".tmp-cache"
)

Expand Down