Skip to content

Commit e702706

Browse files
authored
Token cache fixes (#862)
* Update hashes for rlang 1.3 * Fix bug found by claude; cached tokens were never actually removed
1 parent 74a3acb commit e702706

8 files changed

Lines changed: 16 additions & 12 deletions

File tree

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
11
.Rproj.user
22
docs
33
inst/doc
4+
.posit/assistant/settings.json

DESCRIPTION

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ Imports:
2424
openssl,
2525
R6,
2626
rappdirs,
27-
rlang (>= 1.1.0),
27+
rlang (>= 1.3.0),
2828
vctrs (>= 0.6.3),
2929
withr
3030
Suggests:

NEWS.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
11
# httr2 (development version)
22

3+
* Fixed OAuth token cache pruning so that it actually matches the encrypted `.rds.enc` files written to disk; previously the pruning pattern only matched an unencrypted `.rds` file that was never created, so cached tokens were never automatically deleted regardless of age.
4+
* httr2 now requires rlang >= 1.3.0, which changes the hash used to name files cached by `req_cache()` and on-disk OAuth token caches (e.g. from `req_oauth_auth_code(cache_disk = TRUE)`). Existing cached files won't match the new hash, so they'll be silently ignored (triggering a normal cache miss/re-authentication) and cleaned up over time by the usual pruning rules; you can also delete them manually.
5+
36
# httr2 1.2.3
47

58
* Mocked and cached responses now include the originating request in `resp$request`, just like real responses (#841).

R/oauth.R

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -194,7 +194,7 @@ cache_disk_prune <- function(days = 30, path = oauth_cache_path()) {
194194
path,
195195
recursive = TRUE,
196196
full.names = TRUE,
197-
pattern = "-token\\.rds$"
197+
pattern = "-token\\.rds\\.enc$"
198198
)
199199
mtime <- file.mtime(files)
200200

tests/testthat/_snaps/oauth-client.md

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -41,15 +41,15 @@
4141
oauth_client("x", url)
4242
Output
4343
<httr2_oauth_client>
44-
* name : "bf27508f7925b06bf28a10f3805351ab"
44+
* name : "9758a2659d8a24b1be8a873ab9e4da84"
4545
* id : "x"
4646
* token_url: "http://example.com"
4747
* auth : "oauth_client_req_auth_body"
4848
Code
4949
oauth_client("x", url, secret = "SECRET")
5050
Output
5151
<httr2_oauth_client>
52-
* name : "bf27508f7925b06bf28a10f3805351ab"
52+
* name : "9758a2659d8a24b1be8a873ab9e4da84"
5353
* id : "x"
5454
* secret : <REDACTED>
5555
* token_url: "http://example.com"
@@ -60,7 +60,7 @@
6060
})
6161
Output
6262
<httr2_oauth_client>
63-
* name : "bf27508f7925b06bf28a10f3805351ab"
63+
* name : "9758a2659d8a24b1be8a873ab9e4da84"
6464
* id : "x"
6565
* token_url: "http://example.com"
6666
* auth : <function>

tests/testthat/_snaps/oauth.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,5 +3,5 @@
33
Code
44
cache$set(1)
55
Message
6-
Caching httr2 token in '<oauth-cache-path>/httr2-test/ae743e0fbd718c21f2cca632e77bd180-token.rds.enc'.
6+
Caching httr2 token in '<oauth-cache-path>/httr2-test/2c0a8a99dc147d5445c3b49d035665b2-token.rds.enc'.
77

tests/testthat/_snaps/req-cache.md

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,11 +8,11 @@
88
Code
99
invisible(cache_post_fetch(req, resp))
1010
Message
11-
Saving response to cache "f3805db63ff822b4743f247cfdde10a3"
11+
Saving response to cache "076ab9e79f298c15d4444b2eb6fffa84"
1212
Code
1313
invisible(cache_pre_fetch(req))
1414
Message
15-
Found url in cache "f3805db63ff822b4743f247cfdde10a3"
15+
Found url in cache "076ab9e79f298c15d4444b2eb6fffa84"
1616
Cached value is fresh; using response from cache
1717

1818
---
@@ -22,7 +22,7 @@
2222
invisible(cache_pre_fetch(req))
2323
Message
2424
Pruning cache
25-
Found url in cache "f3805db63ff822b4743f247cfdde10a3"
25+
Found url in cache "076ab9e79f298c15d4444b2eb6fffa84"
2626
Cached value is stale; checking for updates
2727
Code
2828
invisible(cache_post_fetch(req, response(304)))

tests/testthat/test-oauth.R

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -158,10 +158,10 @@ test_that("can explicitly clear cached value", {
158158

159159
test_that("can prune old files", {
160160
path <- withr::local_tempdir()
161-
touch(file.path(path, "a-token.rds"), Sys.time() - 86400 * 1)
162-
touch(file.path(path, "b-token.rds"), Sys.time() - 86400 * 2)
161+
touch(file.path(path, "a-token.rds.enc"), Sys.time() - 86400 * 1)
162+
touch(file.path(path, "b-token.rds.enc"), Sys.time() - 86400 * 2)
163163
cache_disk_prune(2, path)
164-
expect_equal(dir(path), "a-token.rds")
164+
expect_equal(dir(path), "a-token.rds.enc")
165165
})
166166

167167
# cache_path --------------------------------------------------------------

0 commit comments

Comments
 (0)