|
15 | 15 | #' @param flow An `oauth_flow_` function used to generate the access token. |
16 | 16 | #' @param flow_params Parameters for the flow. This should be a named list |
17 | 17 | #' whose names match the argument names of `flow`. |
| 18 | +#' @param expiry_margin Number of seconds before a token's stated expiry that |
| 19 | +#' it should be treated as expired. Increase this for servers that reject |
| 20 | +#' tokens shortly before they expire. Defaults to 30 seconds. |
18 | 21 | #' @returns An [oauth_token]. |
19 | 22 | #' @keywords internal |
20 | 23 | #' @export |
21 | | -req_oauth <- function(req, flow, flow_params, cache) { |
| 24 | +req_oauth <- function(req, flow, flow_params, cache, expiry_margin = 30) { |
| 25 | + check_number_whole(expiry_margin, min = 0) |
| 26 | + |
22 | 27 | # Want req object to contain meaningful objects, not just a closure |
23 | 28 | req <- req_auth_sign( |
24 | 29 | req, |
25 | 30 | fun = auth_oauth_sign, |
26 | | - params = list(flow = flow, flow_params = flow_params), |
| 31 | + params = list( |
| 32 | + flow = flow, |
| 33 | + flow_params = flow_params, |
| 34 | + expiry_margin = expiry_margin |
| 35 | + ), |
27 | 36 | cache = cache |
28 | 37 | ) |
29 | 38 | req <- req_policies(req, auth_oauth = TRUE) |
30 | 39 | req |
31 | 40 | } |
32 | 41 |
|
33 | | -auth_oauth_sign <- function(req, cache, flow, flow_params) { |
| 42 | +auth_oauth_sign <- function( |
| 43 | + req, |
| 44 | + cache, |
| 45 | + flow, |
| 46 | + flow_params, |
| 47 | + expiry_margin = 30 |
| 48 | +) { |
34 | 49 | token <- auth_oauth_token_get( |
35 | 50 | cache = cache, |
36 | 51 | flow = flow, |
37 | | - flow_params = flow_params |
| 52 | + flow_params = flow_params, |
| 53 | + expiry_margin = expiry_margin |
38 | 54 | ) |
39 | 55 | req_auth_bearer_token(req, token$access_token) |
40 | 56 | } |
41 | 57 |
|
42 | | -auth_oauth_token_get <- function(cache, flow, flow_params = list()) { |
| 58 | +auth_oauth_token_get <- function( |
| 59 | + cache, |
| 60 | + flow, |
| 61 | + flow_params = list(), |
| 62 | + expiry_margin = 30 |
| 63 | +) { |
43 | 64 | token <- cache$get() |
44 | 65 | if (is.null(token)) { |
45 | 66 | token <- exec(flow, !!!flow_params) |
46 | 67 | cache$set(token) |
47 | | - } else if (token_has_expired(token)) { |
| 68 | + } else if (token_has_expired(token, delay = expiry_margin)) { |
48 | 69 | cache$clear() |
49 | 70 | if (is.null(token$refresh_token)) { |
50 | 71 | token <- exec(flow, !!!flow_params) |
|
0 commit comments