Skip to content

Commit 1d27b40

Browse files
committed
Export oauth_cache_prune()
1 parent 7188082 commit 1d27b40

6 files changed

Lines changed: 69 additions & 2 deletions

File tree

NAMESPACE

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@ export(local_verbosity)
4242
export(new_response)
4343
export(oauth_cache_clear)
4444
export(oauth_cache_path)
45+
export(oauth_cache_prune)
4546
export(oauth_client)
4647
export(oauth_client_req_auth)
4748
export(oauth_client_req_auth_body)

NEWS.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
* 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.
44
* 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.
55
* `oauth_cache_path()` now defaults to a standard R cache directory (via `tools::R_user_dir()`). Because this release also changes the hash used for cache filenames, existing OAuth tokens will generally not be reused and you may need to authenticate once after upgrading. New tokens are written to the new location, and obsolete tokens in both the old and new locations are removed by the usual pruning rules. httr2 no longer requires the rappdirs package (#800).
6+
* New `oauth_cache_prune()` lets you manually delete cached OAuth tokens older than a given number of days, exposing the pruning that httr2 already performs automatically on load.
67
* `req_oauth_*()` gains an `expiry_margin` argument to control how early cached OAuth tokens are treated as expired; the default margin increases from 5 to 30 seconds (@zacdav-db, #860).
78

89
# httr2 1.2.3

R/oauth.R

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -209,9 +209,27 @@ cache_disk <- function(client, key = NULL) {
209209
)
210210
}
211211

212+
#' Prune the OAuth token cache
213+
#'
214+
#' Deletes cached OAuth tokens (from both the current and legacy cache
215+
#' directories, see [oauth_cache_path()]) that are older than
216+
#' `max_age_days`. This is called automatically when httr2 is loaded, so
217+
#' you should only need to call it yourself if you want to prune the cache
218+
#' immediately.
219+
#'
220+
#' @param max_age_days Delete cached tokens that haven't been modified in
221+
#' this many days.
222+
#' @export
223+
oauth_cache_prune <- function(max_age_days = 30) {
224+
check_number_whole(max_age_days, min = 0)
225+
226+
cache_disk_prune(max_age_days)
227+
invisible()
228+
}
229+
212230
# Update req_oauth_auth_code() docs if change default from 30
213231
cache_disk_prune <- function(
214-
days = 30,
232+
max_age_days = 30,
215233
paths = c(oauth_cache_path(), oauth_cache_path_legacy())
216234
) {
217235
files <- dir(
@@ -222,7 +240,7 @@ cache_disk_prune <- function(
222240
)
223241
mtime <- file.mtime(files)
224242

225-
old <- mtime < (Sys.time() - days * 86400)
243+
old <- mtime < (Sys.time() - max_age_days * 86400)
226244
unlink(files[old])
227245
}
228246

man/oauth_cache_prune.Rd

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

tests/testthat/_snaps/oauth.md

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,3 +13,11 @@
1313
Message
1414
Caching httr2 token in '<oauth-cache-path>/httr2-test/2c0a8a99dc147d5445c3b49d035665b2-token.rds.enc'.
1515

16+
# oauth_cache_prune() validates its input
17+
18+
Code
19+
oauth_cache_prune("x")
20+
Condition
21+
Error in `oauth_cache_prune()`:
22+
! `max_age_days` must be a whole number, not the string "x".
23+

tests/testthat/test-oauth.R

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -235,6 +235,26 @@ test_that("prunes old files from both new and legacy locations", {
235235
expect_equal(dir(legacy_path), "a-token.rds.enc")
236236
})
237237

238+
test_that("oauth_cache_prune() prunes the default cache locations", {
239+
new_path <- withr::local_tempdir()
240+
legacy_path <- withr::local_tempdir()
241+
local_mocked_bindings(
242+
oauth_cache_path = function() new_path,
243+
oauth_cache_path_legacy = function() legacy_path
244+
)
245+
246+
touch(file.path(new_path, "a-token.rds.enc"), Sys.time() - 86400 * 1)
247+
touch(file.path(new_path, "b-token.rds.enc"), Sys.time() - 86400 * 2)
248+
249+
oauth_cache_prune(2)
250+
251+
expect_equal(dir(new_path), "a-token.rds.enc")
252+
})
253+
254+
test_that("oauth_cache_prune() validates its input", {
255+
expect_snapshot(oauth_cache_prune("x"), error = TRUE)
256+
})
257+
238258
# cache_path --------------------------------------------------------------
239259

240260
test_that("can override path with env var", {

0 commit comments

Comments
 (0)