Skip to content

Commit 0411423

Browse files
authored
Merge pull request #67 from ScotGovAnalysis/token-timer
Add expiry time to token and discard if expired
2 parents 033df95 + 8315427 commit 0411423

35 files changed

Lines changed: 259 additions & 23 deletions

NEWS.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,10 @@ types) or a list of length 1.
2626
* New `workgroup_mandate_2fa()` provides ability to enable or disable mandatory
2727
two-factor authentication (2FA) in workgroups (#65).
2828

29+
* Expiry time is now stored alongside the authentication token (`token`). If a
30+
token is expired, it will be removed from the Global Environment and
31+
authentication will be attempted with username and password instead (#35).
32+
2933
# objr 0.1.1
3034

3135
* Set minimum versions for `dplyr` and `tidyr` dependencies (#32).

R/assets.R

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,8 @@
2020
#'
2121
#' @return A tibble
2222
#'
23+
#' @family Asset functions
24+
#'
2325
#' @export
2426

2527
assets <- function(workspace_uuid,
@@ -37,7 +39,8 @@ assets <- function(workspace_uuid,
3739
"There is currently a bug in the underlying API preventing",
3840
"users from selecting more than one asset type. See",
3941
"{.href [objr#53](https://github.com/ScotGovAnalysis/objr/issues/53)}",
40-
"for more information."),
42+
"for more information."
43+
),
4144
"i" = paste("To return all assets, use `type = list()` (default)."),
4245
"i" = paste("To return one asset type only, e.g. documents, use",
4346
"`type = list(\"document\")`. See `?assets` for all",
@@ -75,6 +78,8 @@ assets <- function(workspace_uuid,
7578
#'
7679
#' @return A tibble
7780
#'
81+
#' @family Asset functions
82+
#'
7883
#' @export
7984

8085
asset_info <- function(asset_uuid,
@@ -108,6 +113,8 @@ asset_info <- function(asset_uuid,
108113
#'
109114
#' @return API response (invisibly)
110115
#'
116+
#' @family Asset functions
117+
#'
111118
#' @export
112119

113120
delete_asset <- function(asset_uuid,
@@ -144,6 +151,8 @@ delete_asset <- function(asset_uuid,
144151
#'
145152
#' @return API response (invisibly)
146153
#'
154+
#' @family Asset functions
155+
#'
147156
#' @export
148157

149158
rename_asset <- function(asset_uuid,

R/bypass_2fa.R

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,8 @@
1616
#'
1717
#' @return API response (invisibly)
1818
#'
19+
#' @family Two-factor authentication functions
20+
#'
1921
#' @export
2022

2123
workgroup_bypass_2fa <- function(workgroup_uuid,
@@ -63,6 +65,8 @@ workgroup_bypass_2fa <- function(workgroup_uuid,
6365
#'
6466
#' @return API response (invisibly)
6567
#'
68+
#' @family Two-factor authentication functions
69+
#'
6670
#' @export
6771

6872
participant_bypass_2fa <- function(participant_uuid,
@@ -106,6 +110,8 @@ participant_bypass_2fa <- function(participant_uuid,
106110
#'
107111
#' @return API response (invisibly)
108112
#'
113+
#' @family Two-factor authentication functions
114+
#'
109115
#' @export
110116

111117
workgroup_mandate_2fa <- function(workgroup_uuid,

R/comments.R

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,8 @@
2121
#' comments()
2222
#' }
2323
#'
24+
#' @family Comment functions
25+
#'
2426
#' @export
2527

2628
comments <- function(created_after = NULL,
@@ -78,6 +80,8 @@ comments <- function(created_after = NULL,
7880
#'
7981
#' @return API response (invisibly)
8082
#'
83+
#' @family Comment functions
84+
#'
8185
#' @export
8286

8387
new_thread <- function(workspace_uuid,
@@ -121,6 +125,7 @@ new_thread <- function(workspace_uuid,
121125
#'
122126
#' @return API response (invisibly)
123127
#'
128+
#' @family Comment functions
124129
#' @export
125130

126131
new_reply <- function(thread_uuid,

R/create_folder.R

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,8 @@
1616
#'
1717
#' @return API response (invisibly)
1818
#'
19+
#' @family Asset functions
20+
#'
1921
#' @export
2022

2123
create_folder <- function(folder_name,

R/download.R

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ download_helper <- function(document_uuid,
5151
# Read data from file path
5252
x <- read_temp(new_path, ...)
5353

54-
return(x)
54+
x
5555

5656
}
5757

@@ -84,6 +84,8 @@ download_helper <- function(document_uuid,
8484
#'
8585
#' @return Path to downloaded file (invisibly).
8686
#'
87+
#' @family Read/write functions
88+
#'
8789
#' @export
8890

8991
download_file <- function(document_uuid,
@@ -166,6 +168,8 @@ download_file_version <- function(document_uuid,
166168
#'
167169
#' @return A [tibble][tibble::tibble-package].
168170
#'
171+
#' @family Read/write functions
172+
#'
169173
#' @export
170174

171175
read_data <- function(document_uuid,

R/mobile_auth.R

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,8 @@
1515
#' * `mobileAuthLogin`: Has the user enabled login via Mobile Authenticator?
1616
#' * `mobileAuthRegistered`: Has the user registered a Mobile Authenticator?
1717
#'
18+
#' @family Mobile authentication functions
19+
#'
1820
#' @export
1921

2022
mobile_auth_status <- function(use_proxy = FALSE) {
@@ -49,6 +51,8 @@ mobile_auth_status <- function(use_proxy = FALSE) {
4951
#'
5052
#' @return API response (invisibly)
5153
#'
54+
#' @family Mobile authentication functions
55+
#'
5256
#' @export
5357

5458
mobile_auth_login <- function(code = NULL, use_proxy = FALSE) {

R/objr.R

Lines changed: 30 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,26 @@
11
#' Core request function
22
#'
33
#' @details
4+
#' API authentication is handled automatically. See the
5+
# nolint start: line_length_linter
6+
#' [Authentication article](https://scotgovanalysis.github.io/objr/articles/authentication.html)
7+
# nolint end
8+
#' for more information.
9+
#'
410
#' More details on endpoints are available in the
511
# nolint start: line_length_linter
612
#' \href{https://secure.objectiveconnect.co.uk/publicapi/1/swagger-ui/index.html?configUrl=/publicapi/1/v3/api-docs/swagger-config#/}{API documentation}.
713
# nolint end
814
#'
915
#' @param endpoint The endpoint to append to the API server address.
1016
#' @param url_path A list of values to be added to the request URL path.
11-
#' Values will be separated with `/`.
17+
#' Values will be separated with `/`.
1218
#' @param url_query A list of named values to define query parameters.
1319
#' @param method HTTP method to use; e.g. `"GET"`, `"POST"`, `"PUT"`.
14-
#' Defaults to `"GET"`.
20+
#' Defaults to `"GET"`.
1521
#' @param body A list of named values to be passed to the request body.
1622
#' @param path Optional file path to save body of request (mainly used when
17-
#' downloading files).
23+
#' downloading files).
1824
#' @param accept Accept header. Defaults to `"application/json"`.
1925
#' @param content_type Content-Type header. Defaults to `"application/json"`.
2026
#' @param use_proxy Logical to indicate whether to use proxy.
@@ -121,11 +127,24 @@ objr_auth <- function(req,
121127
call = error_call)
122128
}
123129

124-
if (exists("token", where = parent.frame())) {
130+
token_exists <- exists("token", where = globalenv())
131+
132+
if (token_exists) {
133+
if (get("token", pos = globalenv())$expiry < Sys.time()) {
134+
remove("token", pos = globalenv())
135+
token_exists <- FALSE
136+
cli::cli_inform(c(
137+
"i" = "Existing authentication `token` has expired.",
138+
"i" = "Re-trying authentication using username/password..."
139+
))
140+
}
141+
}
142+
143+
if (token_exists) {
125144

126145
httr2::req_headers(
127146
req,
128-
Authorization = get("token", pos = parent.frame())
147+
Authorization = get("token", pos = globalenv())$value
129148
)
130149

131150
} else {
@@ -147,7 +166,7 @@ objr_auth <- function(req,
147166
#' header.
148167
#' @param store_env The environment to bind the token to.
149168
#'
150-
#' @return Returns the token invisibly. This function is primarily used
169+
#' @return API response (invisibly). This function is primarily used
151170
#' for its side effect - an environment variable is created called "token".
152171
#'
153172
#' @noRd
@@ -169,11 +188,12 @@ store_token <- function(response,
169188
call = error_call)
170189
}
171190

172-
token <- httr2::resp_header(response, "Authorization")
191+
token <- list(
192+
value = httr2::resp_header(response, "Authorization"),
193+
expiry = Sys.time() + (20 * 60)
194+
)
173195

174-
if (!exists("token", where = store_env)) {
175-
rlang::env_poke(env = store_env, nm = "token", value = token)
176-
}
196+
rlang::env_poke(env = store_env, nm = "token", value = token)
177197

178198
invisible(response)
179199

R/participants.R

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,8 @@
1111
#'
1212
#' @return A tibble
1313
#'
14+
#' @seealso [add_participants()]
15+
#'
1416
#' @export
1517

1618
participants <- function(workspace_uuid, use_proxy = FALSE) {
@@ -86,6 +88,8 @@ participants <- function(workspace_uuid, use_proxy = FALSE) {
8688
#'
8789
#' @return API response (invisibly)
8890
#'
91+
#' @seealso [participants()]
92+
#'
8993
#' @export
9094

9195
add_participants <- function(workspace_uuid,

R/upload.R

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,8 @@
2626
#'
2727
#' @return API response (invisibly)
2828
#'
29+
#' @family Read/write functions
30+
#'
2931
#' @export
3032

3133
upload_file <- function(file,
@@ -150,6 +152,8 @@ upload_file_version <- function(file,
150152
#'
151153
#' @return API response (invisibly)
152154
#'
155+
#' @family Read/write functions
156+
#'
153157
#' @export
154158

155159
write_data <- function(x,

0 commit comments

Comments
 (0)