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
4 changes: 4 additions & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,10 @@ types) or a list of length 1.
* New `workgroup_mandate_2fa()` provides ability to enable or disable mandatory
two-factor authentication (2FA) in workgroups (#65).

* Expiry time is now stored alongside the authentication token (`token`). If a
token is expired, it will be removed from the Global Environment and
authentication will be attempted with username and password instead (#35).

# objr 0.1.1

* Set minimum versions for `dplyr` and `tidyr` dependencies (#32).
Expand Down
11 changes: 10 additions & 1 deletion R/assets.R
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@
#'
#' @return A tibble
#'
#' @family Asset functions
#'
#' @export

assets <- function(workspace_uuid,
Expand All @@ -37,7 +39,8 @@ assets <- function(workspace_uuid,
"There is currently a bug in the underlying API preventing",
"users from selecting more than one asset type. See",
"{.href [objr#53](https://github.com/ScotGovAnalysis/objr/issues/53)}",
"for more information."),
"for more information."
),
"i" = paste("To return all assets, use `type = list()` (default)."),
"i" = paste("To return one asset type only, e.g. documents, use",
"`type = list(\"document\")`. See `?assets` for all",
Expand Down Expand Up @@ -75,6 +78,8 @@ assets <- function(workspace_uuid,
#'
#' @return A tibble
#'
#' @family Asset functions
#'
#' @export

asset_info <- function(asset_uuid,
Expand Down Expand Up @@ -108,6 +113,8 @@ asset_info <- function(asset_uuid,
#'
#' @return API response (invisibly)
#'
#' @family Asset functions
#'
#' @export

delete_asset <- function(asset_uuid,
Expand Down Expand Up @@ -144,6 +151,8 @@ delete_asset <- function(asset_uuid,
#'
#' @return API response (invisibly)
#'
#' @family Asset functions
#'
#' @export

rename_asset <- function(asset_uuid,
Expand Down
6 changes: 6 additions & 0 deletions R/bypass_2fa.R
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@
#'
#' @return API response (invisibly)
#'
#' @family Two-factor authentication functions
#'
#' @export

workgroup_bypass_2fa <- function(workgroup_uuid,
Expand Down Expand Up @@ -63,6 +65,8 @@ workgroup_bypass_2fa <- function(workgroup_uuid,
#'
#' @return API response (invisibly)
#'
#' @family Two-factor authentication functions
#'
#' @export

participant_bypass_2fa <- function(participant_uuid,
Expand Down Expand Up @@ -106,6 +110,8 @@ participant_bypass_2fa <- function(participant_uuid,
#'
#' @return API response (invisibly)
#'
#' @family Two-factor authentication functions
#'
#' @export

workgroup_mandate_2fa <- function(workgroup_uuid,
Expand Down
5 changes: 5 additions & 0 deletions R/comments.R
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@
#' comments()
#' }
#'
#' @family Comment functions
#'
#' @export

comments <- function(created_after = NULL,
Expand Down Expand Up @@ -78,6 +80,8 @@ comments <- function(created_after = NULL,
#'
#' @return API response (invisibly)
#'
#' @family Comment functions
#'
#' @export

new_thread <- function(workspace_uuid,
Expand Down Expand Up @@ -121,6 +125,7 @@ new_thread <- function(workspace_uuid,
#'
#' @return API response (invisibly)
#'
#' @family Comment functions
#' @export

new_reply <- function(thread_uuid,
Expand Down
2 changes: 2 additions & 0 deletions R/create_folder.R
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@
#'
#' @return API response (invisibly)
#'
#' @family Asset functions
#'
#' @export

create_folder <- function(folder_name,
Expand Down
6 changes: 5 additions & 1 deletion R/download.R
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ download_helper <- function(document_uuid,
# Read data from file path
x <- read_temp(new_path, ...)

return(x)
x

}

Expand Down Expand Up @@ -84,6 +84,8 @@ download_helper <- function(document_uuid,
#'
#' @return Path to downloaded file (invisibly).
#'
#' @family Read/write functions
#'
#' @export

download_file <- function(document_uuid,
Expand Down Expand Up @@ -166,6 +168,8 @@ download_file_version <- function(document_uuid,
#'
#' @return A [tibble][tibble::tibble-package].
#'
#' @family Read/write functions
#'
#' @export

read_data <- function(document_uuid,
Expand Down
4 changes: 4 additions & 0 deletions R/mobile_auth.R
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@
#' * `mobileAuthLogin`: Has the user enabled login via Mobile Authenticator?
#' * `mobileAuthRegistered`: Has the user registered a Mobile Authenticator?
#'
#' @family Mobile authentication functions
#'
#' @export

mobile_auth_status <- function(use_proxy = FALSE) {
Expand Down Expand Up @@ -49,6 +51,8 @@ mobile_auth_status <- function(use_proxy = FALSE) {
#'
#' @return API response (invisibly)
#'
#' @family Mobile authentication functions
#'
#' @export

mobile_auth_login <- function(code = NULL, use_proxy = FALSE) {
Expand Down
40 changes: 30 additions & 10 deletions R/objr.R
Original file line number Diff line number Diff line change
@@ -1,20 +1,26 @@
#' Core request function
#'
#' @details
#' API authentication is handled automatically. See the
# nolint start: line_length_linter
#' [Authentication article](https://scotgovanalysis.github.io/objr/articles/authentication.html)
# nolint end
#' for more information.
#'
#' More details on endpoints are available in the
# nolint start: line_length_linter
#' \href{https://secure.objectiveconnect.co.uk/publicapi/1/swagger-ui/index.html?configUrl=/publicapi/1/v3/api-docs/swagger-config#/}{API documentation}.
# nolint end
#'
#' @param endpoint The endpoint to append to the API server address.
#' @param url_path A list of values to be added to the request URL path.
#' Values will be separated with `/`.
#' Values will be separated with `/`.
#' @param url_query A list of named values to define query parameters.
#' @param method HTTP method to use; e.g. `"GET"`, `"POST"`, `"PUT"`.
#' Defaults to `"GET"`.
#' Defaults to `"GET"`.
#' @param body A list of named values to be passed to the request body.
#' @param path Optional file path to save body of request (mainly used when
#' downloading files).
#' downloading files).
#' @param accept Accept header. Defaults to `"application/json"`.
#' @param content_type Content-Type header. Defaults to `"application/json"`.
#' @param use_proxy Logical to indicate whether to use proxy.
Expand Down Expand Up @@ -121,11 +127,24 @@ objr_auth <- function(req,
call = error_call)
}

if (exists("token", where = parent.frame())) {
token_exists <- exists("token", where = globalenv())

if (token_exists) {
if (get("token", pos = globalenv())$expiry < Sys.time()) {
remove("token", pos = globalenv())
token_exists <- FALSE
cli::cli_inform(c(
"i" = "Existing authentication `token` has expired.",
"i" = "Re-trying authentication using username/password..."
))
}
}

if (token_exists) {

httr2::req_headers(
req,
Authorization = get("token", pos = parent.frame())
Authorization = get("token", pos = globalenv())$value
)

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

token <- httr2::resp_header(response, "Authorization")
token <- list(
value = httr2::resp_header(response, "Authorization"),
expiry = Sys.time() + (20 * 60)
)

if (!exists("token", where = store_env)) {
rlang::env_poke(env = store_env, nm = "token", value = token)
}
rlang::env_poke(env = store_env, nm = "token", value = token)

invisible(response)

Expand Down
4 changes: 4 additions & 0 deletions R/participants.R
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@
#'
#' @return A tibble
#'
#' @seealso [add_participants()]
#'
#' @export

participants <- function(workspace_uuid, use_proxy = FALSE) {
Expand Down Expand Up @@ -86,6 +88,8 @@ participants <- function(workspace_uuid, use_proxy = FALSE) {
#'
#' @return API response (invisibly)
#'
#' @seealso [participants()]
#'
#' @export

add_participants <- function(workspace_uuid,
Expand Down
4 changes: 4 additions & 0 deletions R/upload.R
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,8 @@
#'
#' @return API response (invisibly)
#'
#' @family Read/write functions
#'
#' @export

upload_file <- function(file,
Expand Down Expand Up @@ -150,6 +152,8 @@ upload_file_version <- function(file,
#'
#' @return API response (invisibly)
#'
#' @family Read/write functions
#'
#' @export

write_data <- function(x,
Expand Down
4 changes: 4 additions & 0 deletions R/documents.R → R/versions.R
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@
#'
#' @return A tibble
#'
#' @seealso [rollback_to_version()]
#'
#' @export

versions <- function(document_uuid,
Expand Down Expand Up @@ -63,6 +65,8 @@ versions <- function(document_uuid,
#'
#' @return API response (invisibly)
#'
#' @seealso [versions()]
#'
#' @export

rollback_to_version <- function(document_uuid,
Expand Down
3 changes: 3 additions & 0 deletions man/add_participants.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 8 additions & 0 deletions man/asset_info.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 8 additions & 0 deletions man/assets.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 6 additions & 0 deletions man/comments.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 8 additions & 0 deletions man/create_folder.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 8 additions & 0 deletions man/delete_asset.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

7 changes: 7 additions & 0 deletions man/download_file.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 5 additions & 0 deletions man/mobile_auth_login.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 5 additions & 0 deletions man/mobile_auth_status.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading