From 8c012bfb40857cebacf90000623a51c2078b53ec Mon Sep 17 00:00:00 2001 From: Sander Devisscher Date: Thu, 28 Mar 2024 16:40:23 +0100 Subject: [PATCH 01/34] my attempt at storing in Sys env #20 --- R/get_access_token.R | 11 ++++++++++- 1 file changed, 10 insertions(+), 1 deletion(-) diff --git a/R/get_access_token.R b/R/get_access_token.R index 54b2907..b074c92 100644 --- a/R/get_access_token.R +++ b/R/get_access_token.R @@ -15,8 +15,17 @@ get_access_token <- # build a request and perform it login_request <- httr2::request(base_url = "https://api.iasset.nl/login/") - hash <- askpass::askpass() %>% + + pwd <- Sys.getenv("iasset_password") %>% openssl::md5() + + if(pwd == openssl::md5("")){ + hash <- askpass::askpass() %>% + openssl::md5() + }else{ + hash <- pwd + } + login_response <- login_request %>% httr2::req_body_form( username = username, From dbb04c91c32bc1f28f43aed9f182c330731458f4 Mon Sep 17 00:00:00 2001 From: Sander Devisscher Date: Fri, 29 Mar 2024 14:08:51 +0100 Subject: [PATCH 02/34] use keyring? #20 --- R/get_access_token.R | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/R/get_access_token.R b/R/get_access_token.R index b074c92..78e8d9a 100644 --- a/R/get_access_token.R +++ b/R/get_access_token.R @@ -6,6 +6,13 @@ #' @return Invisibily, an access token upon succes #' @export #' +#' @details +#' This function uses keyring to retrieve the password. If no password has been +#' set using keyring you'll be prompted to enter your password using askpass. +#' Setting the password using keyring should best be done interactivly +#' (in the console) using `keyring::key_set("iasset_password")`. +#' Keyring uses secret environment variables on GitHub Actions. +#' #' @examples \dontrun{get_access_token("my_username")} get_access_token <- function(username, quiet = FALSE) { @@ -16,6 +23,8 @@ get_access_token <- login_request <- httr2::request(base_url = "https://api.iasset.nl/login/") + Sys.setenv("iasset_password" = keyring::key_get("iasset_password")) + pwd <- Sys.getenv("iasset_password") %>% openssl::md5() From 4c1e6e7ddf72284452bb2c4fc55b117f69db6540 Mon Sep 17 00:00:00 2001 From: Sander Devisscher Date: Fri, 29 Mar 2024 14:15:06 +0100 Subject: [PATCH 03/34] Update get_access_token.Rd #20 --- man/get_access_token.Rd | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/man/get_access_token.Rd b/man/get_access_token.Rd index a502bbb..a2338d6 100644 --- a/man/get_access_token.Rd +++ b/man/get_access_token.Rd @@ -17,6 +17,13 @@ Invisibily, an access token upon succes \description{ Login to iAsset and retreive access token } +\details{ +This function uses keyring to retrieve the password. If no password has been +set using keyring you'll be prompted to enter your password using askpass. +Setting the password using keyring should best be done interactivly +(in the console) using \code{keyring::key_set("iasset_password")}. +Keyring uses secret environment variables on GitHub Actions. +} \examples{ \dontrun{get_access_token("my_username")} } From 9d27097ebb3594dbc2f8f8cc928efafa0469c2a4 Mon Sep 17 00:00:00 2001 From: Sander Devisscher Date: Fri, 29 Mar 2024 14:19:41 +0100 Subject: [PATCH 04/34] import keyring #20 --- NAMESPACE | 1 + R/get_access_token.R | 1 + 2 files changed, 2 insertions(+) diff --git a/NAMESPACE b/NAMESPACE index cca531a..9a86345 100644 --- a/NAMESPACE +++ b/NAMESPACE @@ -4,5 +4,6 @@ export("%>%") export(get_access_token) export(get_fields) export(get_records) +import(keyring) importFrom(magrittr,"%>%") importFrom(rlang,.data) diff --git a/R/get_access_token.R b/R/get_access_token.R index 78e8d9a..09447c3 100644 --- a/R/get_access_token.R +++ b/R/get_access_token.R @@ -13,6 +13,7 @@ #' (in the console) using `keyring::key_set("iasset_password")`. #' Keyring uses secret environment variables on GitHub Actions. #' +#' @import keyring #' @examples \dontrun{get_access_token("my_username")} get_access_token <- function(username, quiet = FALSE) { From 1fb46695b3516263343d5249a8832c777c326184 Mon Sep 17 00:00:00 2001 From: Sander Devisscher Date: Fri, 29 Mar 2024 14:25:10 +0100 Subject: [PATCH 05/34] Update DESCRIPTION #20 --- DESCRIPTION | 1 + 1 file changed, 1 insertion(+) diff --git a/DESCRIPTION b/DESCRIPTION index 6c9f122..fa68489 100644 --- a/DESCRIPTION +++ b/DESCRIPTION @@ -21,6 +21,7 @@ Imports: glue, httr2, janitor, + keyring, magrittr, openssl, purrr, From 5312aef73540b3212961fc2590e3972aef13d925 Mon Sep 17 00:00:00 2001 From: Pieter Huybrechts <48065851+PietrH@users.noreply.github.com> Date: Fri, 29 Mar 2024 16:26:38 +0100 Subject: [PATCH 06/34] devtools::document() --- DESCRIPTION | 4 ++-- man/iassetR-package.Rd | 1 + 2 files changed, 3 insertions(+), 2 deletions(-) diff --git a/DESCRIPTION b/DESCRIPTION index e2e5c2a..39ee775 100644 --- a/DESCRIPTION +++ b/DESCRIPTION @@ -25,5 +25,5 @@ Imports: stringr Encoding: UTF-8 Roxygen: list(markdown = TRUE) -RoxygenNote: 7.2.3 -BugReports: https://github.com/inbo/iassetR/issues \ No newline at end of file +RoxygenNote: 7.3.1 +BugReports: https://github.com/inbo/iassetR/issues diff --git a/man/iassetR-package.Rd b/man/iassetR-package.Rd index ef34f8c..643c6ce 100644 --- a/man/iassetR-package.Rd +++ b/man/iassetR-package.Rd @@ -12,6 +12,7 @@ This package is an interface to the REST api of iAsset. Useful links: \itemize{ \item \url{https://inbo.github.io/iassetR/} + \item Report bugs at \url{https://github.com/inbo/iassetR/issues} } } From 12b344cee233e385eb1a5fbce7f2de159cdaa01b Mon Sep 17 00:00:00 2001 From: Pieter Huybrechts <48065851+PietrH@users.noreply.github.com> Date: Fri, 29 Mar 2024 16:32:55 +0100 Subject: [PATCH 07/34] Increment version number to 0.0.1 --- DESCRIPTION | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/DESCRIPTION b/DESCRIPTION index f26a594..f8b83fe 100644 --- a/DESCRIPTION +++ b/DESCRIPTION @@ -1,6 +1,6 @@ Package: iassetR Title: Use the iAsset API in R -Version: 0.0.0.9000 +Version: 0.0.1 Authors@R: c( person("Pieter", "Huybrechts", , "pieter.huybrechts@inbo.be", role = "aut", comment = c(ORCID = "0000-0002-6658-6062")), From 2e938d3b536fbe6666ac626b301969ba0c04e603 Mon Sep 17 00:00:00 2001 From: Pieter Huybrechts <48065851+PietrH@users.noreply.github.com> Date: Fri, 29 Mar 2024 16:33:14 +0100 Subject: [PATCH 08/34] Increment version number to 0.0.1.9000 --- DESCRIPTION | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/DESCRIPTION b/DESCRIPTION index f8b83fe..8ee6f2d 100644 --- a/DESCRIPTION +++ b/DESCRIPTION @@ -1,6 +1,6 @@ Package: iassetR Title: Use the iAsset API in R -Version: 0.0.1 +Version: 0.0.1.9000 Authors@R: c( person("Pieter", "Huybrechts", , "pieter.huybrechts@inbo.be", role = "aut", comment = c(ORCID = "0000-0002-6658-6062")), From 5aaf33c08dbd29eae430333fa154a9fdb4cb90e7 Mon Sep 17 00:00:00 2001 From: Pieter Huybrechts <48065851+PietrH@users.noreply.github.com> Date: Fri, 29 Mar 2024 16:46:14 +0100 Subject: [PATCH 09/34] fix spelling mistake --- R/get_access_token.R | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/R/get_access_token.R b/R/get_access_token.R index 09447c3..a229c02 100644 --- a/R/get_access_token.R +++ b/R/get_access_token.R @@ -9,7 +9,7 @@ #' @details #' This function uses keyring to retrieve the password. If no password has been #' set using keyring you'll be prompted to enter your password using askpass. -#' Setting the password using keyring should best be done interactivly +#' Setting the password using keyring should best be done interactively #' (in the console) using `keyring::key_set("iasset_password")`. #' Keyring uses secret environment variables on GitHub Actions. #' From 6bf2e9e4f4dd69733b26a127aaa64c06348cdea9 Mon Sep 17 00:00:00 2001 From: Pieter Huybrechts <48065851+PietrH@users.noreply.github.com> Date: Fri, 29 Mar 2024 16:56:08 +0100 Subject: [PATCH 10/34] directly fetch and hash key when building `httr` request we don't want to store the key in sys.env, to avoid leaking it that way. --- R/get_access_token.R | 14 +------------- 1 file changed, 1 insertion(+), 13 deletions(-) diff --git a/R/get_access_token.R b/R/get_access_token.R index a229c02..3c7d8ed 100644 --- a/R/get_access_token.R +++ b/R/get_access_token.R @@ -24,22 +24,10 @@ get_access_token <- login_request <- httr2::request(base_url = "https://api.iasset.nl/login/") - Sys.setenv("iasset_password" = keyring::key_get("iasset_password")) - - pwd <- Sys.getenv("iasset_password") %>% - openssl::md5() - - if(pwd == openssl::md5("")){ - hash <- askpass::askpass() %>% - openssl::md5() - }else{ - hash <- pwd - } - login_response <- login_request %>% httr2::req_body_form( username = username, - password = hash, + password = openssl::md5(keyring::key_get("iasset_password")), domain = "riparias", version = "9.7" ) %>% From 45d5e1e52662577a3df0be7758395bf1ea514f15 Mon Sep 17 00:00:00 2001 From: Pieter Huybrechts <48065851+PietrH@users.noreply.github.com> Date: Fri, 29 Mar 2024 17:06:11 +0100 Subject: [PATCH 11/34] assert keyring support --- R/get_access_token.R | 2 ++ 1 file changed, 2 insertions(+) diff --git a/R/get_access_token.R b/R/get_access_token.R index 3c7d8ed..f49c7b0 100644 --- a/R/get_access_token.R +++ b/R/get_access_token.R @@ -20,6 +20,8 @@ get_access_token <- # check input params assertthat::assert_that(assertthat::is.string(username)) assertthat::assert_that(assertthat::is.flag(quiet)) + # check for keyring support + assertthat::assert_that(keyring::has_keyring_support()) # build a request and perform it login_request <- httr2::request(base_url = "https://api.iasset.nl/login/") From bea951996f0472d15e94fa317097012233c26a96 Mon Sep 17 00:00:00 2001 From: Pieter Huybrechts <48065851+PietrH@users.noreply.github.com> Date: Fri, 29 Mar 2024 17:06:31 +0100 Subject: [PATCH 12/34] document to set username within the keyring instead --- R/get_access_token.R | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/R/get_access_token.R b/R/get_access_token.R index f49c7b0..4cbf425 100644 --- a/R/get_access_token.R +++ b/R/get_access_token.R @@ -10,7 +10,8 @@ #' This function uses keyring to retrieve the password. If no password has been #' set using keyring you'll be prompted to enter your password using askpass. #' Setting the password using keyring should best be done interactively -#' (in the console) using `keyring::key_set("iasset_password")`. +#' (in the console) using +#' `keyring::key_set("iasset_password", username = "my_username")`. #' Keyring uses secret environment variables on GitHub Actions. #' #' @import keyring From cdc799599ae67de53bc17a3952883ed41a1d01d8 Mon Sep 17 00:00:00 2001 From: Pieter Huybrechts <48065851+PietrH@users.noreply.github.com> Date: Fri, 29 Mar 2024 17:10:13 +0100 Subject: [PATCH 13/34] Check for multiple stored passwords --- R/get_access_token.R | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/R/get_access_token.R b/R/get_access_token.R index 4cbf425..0a85091 100644 --- a/R/get_access_token.R +++ b/R/get_access_token.R @@ -23,6 +23,16 @@ get_access_token <- assertthat::assert_that(assertthat::is.flag(quiet)) # check for keyring support assertthat::assert_that(keyring::has_keyring_support()) + # check that only one keyring is set + number_of_keyrings <- nrow(keyring::key_list(service = "iasset_password")) + assertthat::assert_that(number_of_keyrings <= 1, + msg = paste( + "iassetR currently only supports storing one iAsset account at a time.", + "Delete any other accounts using", + 'keyring::key_delete(service = "iasset_password",', + 'username = "username_to_delete")' + ) + ) # build a request and perform it login_request <- httr2::request(base_url = "https://api.iasset.nl/login/") From 63c8c782a0d9de784c2e26f10fe436677289d1d9 Mon Sep 17 00:00:00 2001 From: Pieter Huybrechts <48065851+PietrH@users.noreply.github.com> Date: Fri, 29 Mar 2024 17:10:24 +0100 Subject: [PATCH 14/34] Stylr --- R/get_access_token.R | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/R/get_access_token.R b/R/get_access_token.R index 0a85091..a91d98c 100644 --- a/R/get_access_token.R +++ b/R/get_access_token.R @@ -15,7 +15,9 @@ #' Keyring uses secret environment variables on GitHub Actions. #' #' @import keyring -#' @examples \dontrun{get_access_token("my_username")} +#' @examples \dontrun{ +#' get_access_token("my_username") +#' } get_access_token <- function(username, quiet = FALSE) { # check input params From d1ecd1d1d4db4672b3458a42da432b7914be2019 Mon Sep 17 00:00:00 2001 From: Pieter Huybrechts <48065851+PietrH@users.noreply.github.com> Date: Tue, 2 Apr 2024 10:36:53 +0200 Subject: [PATCH 15/34] check if iasset keyring exists --- R/get_access_token.R | 3 +++ 1 file changed, 3 insertions(+) diff --git a/R/get_access_token.R b/R/get_access_token.R index a91d98c..d961147 100644 --- a/R/get_access_token.R +++ b/R/get_access_token.R @@ -25,6 +25,9 @@ get_access_token <- assertthat::assert_that(assertthat::is.flag(quiet)) # check for keyring support assertthat::assert_that(keyring::has_keyring_support()) + # check if a keyring exists + iasset_keyring_exists <- + "iasset_password" %in% dplyr::pull(keyring::key_list(), "service") # check that only one keyring is set number_of_keyrings <- nrow(keyring::key_list(service = "iasset_password")) assertthat::assert_that(number_of_keyrings <= 1, From 81c8510cd360f2a2148faee51c6eda666187a9e1 Mon Sep 17 00:00:00 2001 From: Pieter Huybrechts <48065851+PietrH@users.noreply.github.com> Date: Tue, 2 Apr 2024 10:51:56 +0200 Subject: [PATCH 16/34] check if a username is set --- R/get_access_token.R | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/R/get_access_token.R b/R/get_access_token.R index d961147..3e45b5f 100644 --- a/R/get_access_token.R +++ b/R/get_access_token.R @@ -28,6 +28,15 @@ get_access_token <- # check if a keyring exists iasset_keyring_exists <- "iasset_password" %in% dplyr::pull(keyring::key_list(), "service") + ## check if a username is set + iasset_username_missing <- + dplyr::filter( + keyring::key_list(), + service == "iasset_password" + ) %>% + dplyr::pull("username") %>% + is.na() + # check that only one keyring is set number_of_keyrings <- nrow(keyring::key_list(service = "iasset_password")) assertthat::assert_that(number_of_keyrings <= 1, From 9183212d82ea7e8a4558c1e6afe58261d909a348 Mon Sep 17 00:00:00 2001 From: Pieter Huybrechts <48065851+PietrH@users.noreply.github.com> Date: Tue, 2 Apr 2024 11:02:00 +0200 Subject: [PATCH 17/34] prompt if credentials are missing --- R/get_access_token.R | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) diff --git a/R/get_access_token.R b/R/get_access_token.R index 3e45b5f..05c1c33 100644 --- a/R/get_access_token.R +++ b/R/get_access_token.R @@ -37,6 +37,21 @@ get_access_token <- dplyr::pull("username") %>% is.na() + # prompt user for credentials if password or username is missing + if(!iasset_keyring_exists | iasset_username_missing){ + message( + paste( + "iasset credentials are missing, please enter your credentials or", + "contact your domain admin to aquire some." + ) + ) + keyring::key_set( + service = "iasset_password", + username = + askpass::askpass(prompt = "Please enter your iasset username: ") + ) + } + # check that only one keyring is set number_of_keyrings <- nrow(keyring::key_list(service = "iasset_password")) assertthat::assert_that(number_of_keyrings <= 1, @@ -47,6 +62,7 @@ get_access_token <- 'username = "username_to_delete")' ) ) + # build a request and perform it login_request <- httr2::request(base_url = "https://api.iasset.nl/login/") From 8488e96917040bad94ab391e7a2c86fa34e3fe0b Mon Sep 17 00:00:00 2001 From: Pieter Huybrechts <48065851+PietrH@users.noreply.github.com> Date: Tue, 2 Apr 2024 11:02:59 +0200 Subject: [PATCH 18/34] add custom message for credentials prompt --- R/get_access_token.R | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/R/get_access_token.R b/R/get_access_token.R index 05c1c33..ff3d200 100644 --- a/R/get_access_token.R +++ b/R/get_access_token.R @@ -48,7 +48,8 @@ get_access_token <- keyring::key_set( service = "iasset_password", username = - askpass::askpass(prompt = "Please enter your iasset username: ") + askpass::askpass(prompt = "Please enter your iasset username: "), + prompt = "Please enter your iasset password: " ) } From fd3a4a5957ad600ea5c700132b030b7438723768 Mon Sep 17 00:00:00 2001 From: Pieter Huybrechts <48065851+PietrH@users.noreply.github.com> Date: Tue, 2 Apr 2024 13:49:33 +0200 Subject: [PATCH 19/34] add helper to fetch username associated with credentials --- R/get_username.R | 22 ++++++++++++++++++++++ 1 file changed, 22 insertions(+) create mode 100644 R/get_username.R diff --git a/R/get_username.R b/R/get_username.R new file mode 100644 index 0000000..38fd1c1 --- /dev/null +++ b/R/get_username.R @@ -0,0 +1,22 @@ +#' Get the username associated with the iAsset credentials in the system credentials store. +#' +#' @param service Character. The keyring service to fetch the stored username for. +#' +#' @return A character vector of stored usernames for the service. NA if none. +#' +#' @examples get_username("iasset_password") +get_username <- function(service = "iasset_password") { + # check input params + assertthat::assert_that(assertthat::is.string(service)) + + # check that the system supports keyring + assertthat::assert_that(keyring::has_keyring_support()) + + # check that the queried service is in the keyring + assertthat::assert_that(service %in% keyring::key_list()$service, + msg = glue::glue("{service} not found in keyring") + ) + + # fetch the associated usernames + keyring::key_list(service = service)$username +} From d2f56e83c2588d70237ff8b44b75f0b13e543c6f Mon Sep 17 00:00:00 2001 From: Pieter Huybrechts <48065851+PietrH@users.noreply.github.com> Date: Tue, 2 Apr 2024 13:50:31 +0200 Subject: [PATCH 20/34] export `get_username()` --- R/get_username.R | 2 ++ 1 file changed, 2 insertions(+) diff --git a/R/get_username.R b/R/get_username.R index 38fd1c1..e1c2195 100644 --- a/R/get_username.R +++ b/R/get_username.R @@ -4,6 +4,8 @@ #' #' @return A character vector of stored usernames for the service. NA if none. #' +#' @export +#' #' @examples get_username("iasset_password") get_username <- function(service = "iasset_password") { # check input params From 29d7fe0e34bf333ea7092a912152926443e004d1 Mon Sep 17 00:00:00 2001 From: Pieter Huybrechts <48065851+PietrH@users.noreply.github.com> Date: Tue, 2 Apr 2024 13:50:52 +0200 Subject: [PATCH 21/34] set default option for username --- R/get_access_token.R | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/R/get_access_token.R b/R/get_access_token.R index ff3d200..b3986e1 100644 --- a/R/get_access_token.R +++ b/R/get_access_token.R @@ -19,7 +19,7 @@ #' get_access_token("my_username") #' } get_access_token <- - function(username, quiet = FALSE) { + function(username = get_username(), quiet = FALSE) { # check input params assertthat::assert_that(assertthat::is.string(username)) assertthat::assert_that(assertthat::is.flag(quiet)) From 36e1b6092d2f7c5a873808cc849dab3eb0f59e92 Mon Sep 17 00:00:00 2001 From: Pieter Huybrechts <48065851+PietrH@users.noreply.github.com> Date: Tue, 2 Apr 2024 13:51:19 +0200 Subject: [PATCH 22/34] devtools::document() --- DESCRIPTION | 2 +- NAMESPACE | 1 + man/get_access_token.Rd | 11 +++++++---- man/get_username.Rd | 20 ++++++++++++++++++++ 4 files changed, 29 insertions(+), 5 deletions(-) create mode 100644 man/get_username.Rd diff --git a/DESCRIPTION b/DESCRIPTION index 8ee6f2d..ebcee03 100644 --- a/DESCRIPTION +++ b/DESCRIPTION @@ -27,5 +27,5 @@ Imports: stringr Encoding: UTF-8 Roxygen: list(markdown = TRUE) -RoxygenNote: 7.2.3 +RoxygenNote: 7.3.1 BugReports: https://github.com/inbo/iassetR/issues diff --git a/NAMESPACE b/NAMESPACE index 9a86345..723ef42 100644 --- a/NAMESPACE +++ b/NAMESPACE @@ -4,6 +4,7 @@ export("%>%") export(get_access_token) export(get_fields) export(get_records) +export(get_username) import(keyring) importFrom(magrittr,"%>%") importFrom(rlang,.data) diff --git a/man/get_access_token.Rd b/man/get_access_token.Rd index a2338d6..c64197c 100644 --- a/man/get_access_token.Rd +++ b/man/get_access_token.Rd @@ -4,7 +4,7 @@ \alias{get_access_token} \title{Login to iAsset and retreive access token} \usage{ -get_access_token(username, quiet = FALSE) +get_access_token(username = get_username(), quiet = FALSE) } \arguments{ \item{username}{the iAsset username} @@ -20,10 +20,13 @@ Login to iAsset and retreive access token \details{ This function uses keyring to retrieve the password. If no password has been set using keyring you'll be prompted to enter your password using askpass. -Setting the password using keyring should best be done interactivly -(in the console) using \code{keyring::key_set("iasset_password")}. +Setting the password using keyring should best be done interactively +(in the console) using +\code{keyring::key_set("iasset_password", username = "my_username")}. Keyring uses secret environment variables on GitHub Actions. } \examples{ -\dontrun{get_access_token("my_username")} +\dontrun{ +get_access_token("my_username") +} } diff --git a/man/get_username.Rd b/man/get_username.Rd new file mode 100644 index 0000000..13b12c3 --- /dev/null +++ b/man/get_username.Rd @@ -0,0 +1,20 @@ +% Generated by roxygen2: do not edit by hand +% Please edit documentation in R/get_username.R +\name{get_username} +\alias{get_username} +\title{Get the username associated with the iAsset credentials in the system credentials store.} +\usage{ +get_username(service = "iasset_password") +} +\arguments{ +\item{service}{Character. The keyring service to fetch the stored username for.} +} +\value{ +A character vector of stored usernames for the service. NA if none. +} +\description{ +Get the username associated with the iAsset credentials in the system credentials store. +} +\examples{ +get_username("iasset_password") +} From 02119ef36ec5be7c9bea0d72357699df977cd60f Mon Sep 17 00:00:00 2001 From: Pieter Huybrechts <48065851+PietrH@users.noreply.github.com> Date: Tue, 2 Apr 2024 13:56:53 +0200 Subject: [PATCH 23/34] use helper instead --- R/get_access_token.R | 8 +------- 1 file changed, 1 insertion(+), 7 deletions(-) diff --git a/R/get_access_token.R b/R/get_access_token.R index b3986e1..9d0f0e5 100644 --- a/R/get_access_token.R +++ b/R/get_access_token.R @@ -29,13 +29,7 @@ get_access_token <- iasset_keyring_exists <- "iasset_password" %in% dplyr::pull(keyring::key_list(), "service") ## check if a username is set - iasset_username_missing <- - dplyr::filter( - keyring::key_list(), - service == "iasset_password" - ) %>% - dplyr::pull("username") %>% - is.na() + iasset_username_missing <- is.na(get_username()) # prompt user for credentials if password or username is missing if(!iasset_keyring_exists | iasset_username_missing){ From 892a4de5f7d1e3f1294764ec0af13201df1b3fce Mon Sep 17 00:00:00 2001 From: Pieter Huybrechts <48065851+PietrH@users.noreply.github.com> Date: Tue, 2 Apr 2024 13:57:41 +0200 Subject: [PATCH 24/34] simply message --- R/get_access_token.R | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/R/get_access_token.R b/R/get_access_token.R index 9d0f0e5..36cd909 100644 --- a/R/get_access_token.R +++ b/R/get_access_token.R @@ -36,7 +36,7 @@ get_access_token <- message( paste( "iasset credentials are missing, please enter your credentials or", - "contact your domain admin to aquire some." + "if you don't have any contact your domain admin." ) ) keyring::key_set( From a32ca646112afdc8597128172f646337832e51e1 Mon Sep 17 00:00:00 2001 From: Pieter Huybrechts <48065851+PietrH@users.noreply.github.com> Date: Tue, 2 Apr 2024 14:08:53 +0200 Subject: [PATCH 25/34] don't attempt the example --- R/get_username.R | 3 ++- man/get_username.Rd | 2 +- 2 files changed, 3 insertions(+), 2 deletions(-) diff --git a/R/get_username.R b/R/get_username.R index e1c2195..72fcb4d 100644 --- a/R/get_username.R +++ b/R/get_username.R @@ -6,7 +6,8 @@ #' #' @export #' -#' @examples get_username("iasset_password") +#' @examples +#' \dontrun{get_username("iasset_password")} get_username <- function(service = "iasset_password") { # check input params assertthat::assert_that(assertthat::is.string(service)) diff --git a/man/get_username.Rd b/man/get_username.Rd index 13b12c3..0df5b08 100644 --- a/man/get_username.Rd +++ b/man/get_username.Rd @@ -16,5 +16,5 @@ A character vector of stored usernames for the service. NA if none. Get the username associated with the iAsset credentials in the system credentials store. } \examples{ -get_username("iasset_password") +\dontrun{get_username("iasset_password")} } From 6e2bfea333f40fa50ce039f9fee5524477eba9dd Mon Sep 17 00:00:00 2001 From: Pieter Huybrechts <48065851+PietrH@users.noreply.github.com> Date: Tue, 2 Apr 2024 14:09:32 +0200 Subject: [PATCH 26/34] warn, don't stop on missing service --- R/get_username.R | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/R/get_username.R b/R/get_username.R index 72fcb4d..03266b5 100644 --- a/R/get_username.R +++ b/R/get_username.R @@ -16,7 +16,7 @@ get_username <- function(service = "iasset_password") { assertthat::assert_that(keyring::has_keyring_support()) # check that the queried service is in the keyring - assertthat::assert_that(service %in% keyring::key_list()$service, + assertthat::validate_that(service %in% keyring::key_list()$service, msg = glue::glue("{service} not found in keyring") ) From 92e0bf219fe9d56e096d5664ec1f7203535951ec Mon Sep 17 00:00:00 2001 From: Pieter Huybrechts <48065851+PietrH@users.noreply.github.com> Date: Tue, 2 Apr 2024 14:11:16 +0200 Subject: [PATCH 27/34] move string assertion to behind where we construct a username sometimes --- R/get_access_token.R | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/R/get_access_token.R b/R/get_access_token.R index 36cd909..eda4182 100644 --- a/R/get_access_token.R +++ b/R/get_access_token.R @@ -21,7 +21,7 @@ get_access_token <- function(username = get_username(), quiet = FALSE) { # check input params - assertthat::assert_that(assertthat::is.string(username)) + assertthat::assert_that(assertthat::is.flag(quiet)) # check for keyring support assertthat::assert_that(keyring::has_keyring_support()) @@ -57,6 +57,10 @@ get_access_token <- 'username = "username_to_delete")' ) ) + # fetch the username, we'll fetch the password in line to avoid storing it + username <- get_username() + # check that the fetched username is a string + assertthat::assert_that(assertthat::is.string(username)) # build a request and perform it login_request <- From 41248a82fd2579b232250ea1cd0c2fe501c8a211 Mon Sep 17 00:00:00 2001 From: Pieter Huybrechts <48065851+PietrH@users.noreply.github.com> Date: Tue, 2 Apr 2024 14:11:46 +0200 Subject: [PATCH 28/34] check for single user earlier on, fail earlier --- R/get_access_token.R | 20 ++++++++++---------- 1 file changed, 10 insertions(+), 10 deletions(-) diff --git a/R/get_access_token.R b/R/get_access_token.R index eda4182..d5f79fb 100644 --- a/R/get_access_token.R +++ b/R/get_access_token.R @@ -30,6 +30,16 @@ get_access_token <- "iasset_password" %in% dplyr::pull(keyring::key_list(), "service") ## check if a username is set iasset_username_missing <- is.na(get_username()) + # check that only one keyring is set + number_of_keyrings <- nrow(keyring::key_list(service = "iasset_password")) + assertthat::assert_that(number_of_keyrings <= 1, + msg = paste( + "iassetR currently only supports storing one iAsset account at a time.", + "Delete any other accounts using", + 'keyring::key_delete(service = "iasset_password",', + 'username = "username_to_delete")' + ) + ) # prompt user for credentials if password or username is missing if(!iasset_keyring_exists | iasset_username_missing){ @@ -47,16 +57,6 @@ get_access_token <- ) } - # check that only one keyring is set - number_of_keyrings <- nrow(keyring::key_list(service = "iasset_password")) - assertthat::assert_that(number_of_keyrings <= 1, - msg = paste( - "iassetR currently only supports storing one iAsset account at a time.", - "Delete any other accounts using", - 'keyring::key_delete(service = "iasset_password",', - 'username = "username_to_delete")' - ) - ) # fetch the username, we'll fetch the password in line to avoid storing it username <- get_username() # check that the fetched username is a string From 8055e725342afc183b7da8a2684c6e063d50c375 Mon Sep 17 00:00:00 2001 From: Pieter Huybrechts <48065851+PietrH@users.noreply.github.com> Date: Tue, 2 Apr 2024 14:12:44 +0200 Subject: [PATCH 29/34] a failing username can also be `characterj(0)` --- R/get_access_token.R | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/R/get_access_token.R b/R/get_access_token.R index d5f79fb..5892c4f 100644 --- a/R/get_access_token.R +++ b/R/get_access_token.R @@ -29,7 +29,8 @@ get_access_token <- iasset_keyring_exists <- "iasset_password" %in% dplyr::pull(keyring::key_list(), "service") ## check if a username is set - iasset_username_missing <- is.na(get_username()) + iasset_username_missing <- !rlang::is_string(get_username()) + # check that only one keyring is set number_of_keyrings <- nrow(keyring::key_list(service = "iasset_password")) assertthat::assert_that(number_of_keyrings <= 1, From e801285ad86a5921370432ee2dbb0918f3fce722 Mon Sep 17 00:00:00 2001 From: Pieter Huybrechts <48065851+PietrH@users.noreply.github.com> Date: Tue, 2 Apr 2024 14:13:33 +0200 Subject: [PATCH 30/34] add whitespace for readability --- R/get_access_token.R | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/R/get_access_token.R b/R/get_access_token.R index 5892c4f..d8d8f58 100644 --- a/R/get_access_token.R +++ b/R/get_access_token.R @@ -23,11 +23,14 @@ get_access_token <- # check input params assertthat::assert_that(assertthat::is.flag(quiet)) + # check for keyring support assertthat::assert_that(keyring::has_keyring_support()) + # check if a keyring exists iasset_keyring_exists <- "iasset_password" %in% dplyr::pull(keyring::key_list(), "service") + ## check if a username is set iasset_username_missing <- !rlang::is_string(get_username()) @@ -60,6 +63,7 @@ get_access_token <- # fetch the username, we'll fetch the password in line to avoid storing it username <- get_username() + # check that the fetched username is a string assertthat::assert_that(assertthat::is.string(username)) @@ -76,10 +80,12 @@ get_access_token <- ) %>% httr2::req_perform() %>% httr2::resp_body_json(check_type = FALSE) + # print success if (!quiet) { message(login_response$returndata[[1]]$success_msg) } + # return access token invisible(purrr::chuck(login_response, "returndata", 1, "access_token")) } From e86c68a1eb89e9850a7df38b6f3801c65b02f2b8 Mon Sep 17 00:00:00 2001 From: Pieter Huybrechts <48065851+PietrH@users.noreply.github.com> Date: Tue, 2 Apr 2024 14:18:53 +0200 Subject: [PATCH 31/34] hardcode what username to get the pwd for Even if we only support storing one set of iasset credentials at the moment, if this ever changes in the future this line will ensure we'll be fetching the right one. --- R/get_access_token.R | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/R/get_access_token.R b/R/get_access_token.R index d8d8f58..6722ca6 100644 --- a/R/get_access_token.R +++ b/R/get_access_token.R @@ -74,7 +74,8 @@ get_access_token <- login_response <- login_request %>% httr2::req_body_form( username = username, - password = openssl::md5(keyring::key_get("iasset_password")), + password = openssl::md5(keyring::key_get("iasset_password", + username = username)), domain = "riparias", version = "9.7" ) %>% From ea7ed7dc80eb29f75e732eb85955eeca5460fbbb Mon Sep 17 00:00:00 2001 From: Pieter Huybrechts <48065851+PietrH@users.noreply.github.com> Date: Tue, 2 Apr 2024 14:22:01 +0200 Subject: [PATCH 32/34] update documentation --- R/get_access_token.R | 5 ++--- man/get_access_token.Rd | 5 ++--- 2 files changed, 4 insertions(+), 6 deletions(-) diff --git a/R/get_access_token.R b/R/get_access_token.R index 6722ca6..17a05fb 100644 --- a/R/get_access_token.R +++ b/R/get_access_token.R @@ -9,9 +9,8 @@ #' @details #' This function uses keyring to retrieve the password. If no password has been #' set using keyring you'll be prompted to enter your password using askpass. -#' Setting the password using keyring should best be done interactively -#' (in the console) using -#' `keyring::key_set("iasset_password", username = "my_username")`. +#' This password - username combination will then be stored in the system +#' credential store. #' Keyring uses secret environment variables on GitHub Actions. #' #' @import keyring diff --git a/man/get_access_token.Rd b/man/get_access_token.Rd index c64197c..7c8347c 100644 --- a/man/get_access_token.Rd +++ b/man/get_access_token.Rd @@ -20,9 +20,8 @@ Login to iAsset and retreive access token \details{ This function uses keyring to retrieve the password. If no password has been set using keyring you'll be prompted to enter your password using askpass. -Setting the password using keyring should best be done interactively -(in the console) using -\code{keyring::key_set("iasset_password", username = "my_username")}. +This password - username combination will then be stored in the system +credential store. Keyring uses secret environment variables on GitHub Actions. } \examples{ From 4f9c60b55ab440ffdf542be70f0d9090095dde40 Mon Sep 17 00:00:00 2001 From: Pieter Huybrechts <48065851+PietrH@users.noreply.github.com> Date: Tue, 2 Apr 2024 14:26:40 +0200 Subject: [PATCH 33/34] remove keyring from namespace + devtools::document() --- NAMESPACE | 1 - R/get_access_token.R | 1 - 2 files changed, 2 deletions(-) diff --git a/NAMESPACE b/NAMESPACE index 723ef42..bff2f42 100644 --- a/NAMESPACE +++ b/NAMESPACE @@ -5,6 +5,5 @@ export(get_access_token) export(get_fields) export(get_records) export(get_username) -import(keyring) importFrom(magrittr,"%>%") importFrom(rlang,.data) diff --git a/R/get_access_token.R b/R/get_access_token.R index 17a05fb..d8aa115 100644 --- a/R/get_access_token.R +++ b/R/get_access_token.R @@ -13,7 +13,6 @@ #' credential store. #' Keyring uses secret environment variables on GitHub Actions. #' -#' @import keyring #' @examples \dontrun{ #' get_access_token("my_username") #' } From f504bd3ab6335220d44ea5fd15e1e05bcb342ec6 Mon Sep 17 00:00:00 2001 From: Sander Devisscher Date: Thu, 4 Apr 2024 12:29:56 +0200 Subject: [PATCH 34/34] expand multiple usernames error #38 --- R/get_access_token.R | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/R/get_access_token.R b/R/get_access_token.R index d8aa115..8dbf661 100644 --- a/R/get_access_token.R +++ b/R/get_access_token.R @@ -38,8 +38,11 @@ get_access_token <- msg = paste( "iassetR currently only supports storing one iAsset account at a time.", "Delete any other accounts using", + 'keyring::key_list(service = "iasset_password")$username', + "to get a list of linked usernames &", 'keyring::key_delete(service = "iasset_password",', - 'username = "username_to_delete")' + 'username = "username_to_delete")', + "to delete the unneeded username" ) )