Skip to content

Commit

Permalink
work on gen_auth_* and README
Browse files Browse the repository at this point in the history
  • Loading branch information
yannikbuhl committed Dec 12, 2024
1 parent a267457 commit 02b80de
Show file tree
Hide file tree
Showing 3 changed files with 114 additions and 117 deletions.
87 changes: 49 additions & 38 deletions R/gen_auth.R
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
#' @description Save credentials of the different databases for further convenient use
#'
#' @param database Character string. The database to store credentials for ('all', 'genesis', 'zensus' or 'regio').
#' @param use_token Boolean. Do you want to (if possible) set an API token instead of password + username? Note: This is not supported by regionalstatistik.de. Defaults to FALSE.
#'
#' @details Username and password are encrypted and saved as RDS in the
#' package config directory. A random string is generated and stored in the
Expand All @@ -21,52 +22,56 @@
#' gen_auth_save("zensus")
#' }
#'
gen_auth_save <- function(database = c("all", "genesis", "zensus", "regio")) {
gen_auth_save <- function(database = c("all", "genesis", "zensus", "regio"),
use_token = FALSE) {

if (missing(database)) stop("You have to specify a value for parameter 'database'.",
call. = FALSE)

if (database == "regio" & isTRUE(use_token)) {

warning("regionalstatistik.de does not support API tokens. Defaulting to username and password.",
call. = FALSE)

}

#-----------------------------------------------------------------------------

if (database %in% c("genesis", "regio")) {
if (database == "regio") {

insert_and_save_credentials(database)
insert_and_save_credentials(database, use_token)

gen_logincheck(database = database)

#-----------------------------------------------------------------------------

} else if (database == "zensus"){
} else if (database %in% c("zensus", "genesis")) {

insert_and_save_credentials("zensus")
insert_and_save_credentials(database, use_token)

gen_logincheck(database = "zensus")
gen_logincheck(database = database)

} else if (database == "all"){

#---------------------------------------------------------------------------

message("~~ Saving credentials for the 'genesis' database.")

insert_and_save_credentials("genesis")

gen_logincheck(database = "genesis")

#-------------------------------------------------------------------------
walk_arguments <- list(ui_menu_database = c("GENESIS", "Zensus 2022", "regionalstatistik.de"),
db_names = c("genesis", "zensus", "regio"),
use_token = use_token)

message("~~ Saving credentials for the Zensus 2022 database.\n")
purrr::pwalk(.l = walk_arguments,
.f = function(ui_menu_database,
db_names,
use_token) {

insert_and_save_credentials("zensus")
message("~~ Saving credentials for the ", ui_menu_database, " database.")

gen_logincheck(database = "zensus")
insert_and_save_credentials(database = db_names,
use_token = use_token)

#-------------------------------------------------------------------------
gen_logincheck(database = db_names)

message("~~ Saving credentials for regionalstatistik.de database.")

insert_and_save_credentials("regio")

gen_logincheck(database = "regio")
})

#-----------------------------------------------------------------------------

Expand Down Expand Up @@ -192,44 +197,50 @@ gen_auth_get <- function(database = c("all", "genesis", "zensus", "regio")) {

if (!(file.exists(auth_path) && nzchar(Sys.getenv("GENESIS_KEY")))) {

stop(paste0("GENESIS database credentials not found. ",
"Please run 'gen_auth_save()' to store GENESIS database username and password."),
call. = FALSE)
warning(paste0("GENESIS database credentials not found. ",
"Please run 'gen_auth_save()' to store GENESIS database username and password."),
call. = FALSE)

}
} else {

message("Credentials for database GENESIS:\n")
print(httr2::secret_read_rds(auth_path, "GENESIS_KEY"))
message("Credentials for database GENESIS:\n")
print(httr2::secret_read_rds(auth_path, "GENESIS_KEY"))

}

#---------------------------------------------------------------------------

auth_path <- gen_auth_path("auth_zensus.rds")

if (!(file.exists(auth_path) && nzchar(Sys.getenv("ZENSUS_KEY")))) {

stop(paste0("Zensus 2022 database credentials not found. ",
warning(paste0("Zensus 2022 database credentials not found. ",
"Please run 'gen_auth_save()' to store Zensus 2022 database username and password."),
call. = FALSE)
call. = FALSE)

}
} else {

message("Credentials for database Zensus 2022:\n")
print(httr2::secret_read_rds(auth_path, "ZENSUS_KEY"))
message("Credentials for database Zensus 2022:\n")
print(httr2::secret_read_rds(auth_path, "ZENSUS_KEY"))

}

#---------------------------------------------------------------------------

auth_path <- gen_auth_path("auth_regio.rds")

if (!(file.exists(auth_path) && nzchar(Sys.getenv("REGIO_KEY")))) {

stop(paste0("regionalstatistik.de database credentials not found. ",
warning(paste0("regionalstatistik.de database credentials not found. ",
"Please run 'gen_auth_save()' to store regionalstatistik.de database username and password."),
call. = FALSE)
call. = FALSE)

}
} else {

message("Credentials for database regionalstatistik.de:\n")
print(httr2::secret_read_rds(auth_path, "REGIO_KEY"))
message("Credentials for database regionalstatistik.de:\n")
print(httr2::secret_read_rds(auth_path, "REGIO_KEY"))

}

} # End of 'else if (database == "all")'

Expand Down
141 changes: 63 additions & 78 deletions R/utils_httr2.R
Original file line number Diff line number Diff line change
Expand Up @@ -589,112 +589,97 @@ logincheck_stop_or_warn <- function(response,
#' insert_and_save_credentials
#'
#' @param database The database to specify credentials for
#' @param use_token Boolean. Do you want to (if possible) set an API token instead of password + username? Defaults to FALSE.
#'
insert_and_save_credentials <- function(database) {
insert_and_save_credentials <- function(database,
use_token) {

if (database %in% c("genesis", "regio")) {
if (database == "regio") {

username <- gen_auth_ask("username")
password <- gen_auth_ask("password")
if (isTRUE(use_token)) use_token <- FALSE

auth_path <- gen_auth_path(paste0("auth_", database, ".rds"))

key <- httr2::secret_make_key()

key_name <- paste0(toupper(database), "_KEY")

do.call("Sys.setenv", setNames(list(key), key_name))

message(paste0("Saving '", database, "' database credentials to "),
auth_path,
"\n\n",
"Please add the following line to your .Renviron, ",
"e.g. via `usethis::edit_r_environ()`, ",
"to use the specified username and password across sessions:\n\n",
paste0(key_name, "="),
key,
"\n\n")

dir.create(gen_auth_path(), showWarnings = FALSE, recursive = TRUE)

httr2::secret_write_rds(list(username = username,
password = password),
path = auth_path,
key = key_name)
set_credentials_auth(path = "auth_regio.rds",
sys_env = "REGIO_KEY",
ui_menu_database = "regionalstatistik.de",
use_token = use_token)

#-----------------------------------------------------------------------------

} else if (database == "zensus") {

want_token_resp <- menu(choices = c("Zensus 2022 API token",
"mail address + password"),
graphics = FALSE,
title = "Do you want to specifiy a Zensus 2022 API token or regular credentials for access?")

want_token <- ifelse(want_token_resp == 1L, TRUE, FALSE)
set_credentials_auth(path = "auth_zensus.rds",
sys_env = "ZENSUS_KEY",
ui_menu_database = "Zensus 2022",
use_token = use_token)

if (isTRUE(want_token)) {
#-----------------------------------------------------------------------------

username <- gen_auth_ask("API token")
password <- ""
} else if (database == "genesis") {

auth_path <- gen_auth_path("auth_zensus.rds")
set_credentials_auth(path = "auth_genesis.rds",
sys_env = "GENESIS_KEY",
ui_menu_database = "GENESIS",
use_token = use_token)

key <- httr2::secret_make_key()
} else {

Sys.setenv(ZENSUS_KEY = key)
stop("Misspecification of parameter 'database' in function 'insert_and_save_credentials'.",
call. = FALSE)

message("Saving Zensus 2022 database credentials to ",
auth_path,
"\n\n",
"Please add the following line to your .Renviron, ",
"e.g. via `usethis::edit_r_environ()`, ",
"to use the specified username and password across sessions:\n\n",
"ZENSUS_KEY=",
key,
"\n\n")
}

dir.create(gen_auth_path(), showWarnings = FALSE, recursive = TRUE)
}

httr2::secret_write_rds(list(username = username,
password = password),
path = auth_path,
key = "ZENSUS_KEY")
#-------------------------------------------------------------------------------

} else {
#' set_credentials_auth
#'
#' @param path Path for the .rds file
#' @param sys_env System environment variable name for the key
#' @param ui_menu_database The database for the auth request ('GENESIS' or 'Zensus 2022')
#' @param use_token Boolean. Do you want to (if possible) set an API token instead of password + username? Defaults to FALSE.
#'
set_credentials_auth <- function(path,
sys_env,
ui_menu_database,
use_token) {

username <- gen_auth_ask("username")
password <- gen_auth_ask("password")
if(isTRUE(use_token)) {

auth_path <- gen_auth_path("auth_zensus.rds")
username <- gen_auth_ask(paste0("API token for ", ui_menu_database))
password <- ""

key <- httr2::secret_make_key()
} else {

Sys.setenv(ZENSUS_KEY = key)
username <- gen_auth_ask(paste0("username for ", ui_menu_database))
password <- gen_auth_ask(paste0("password for ", ui_menu_database))

message("Saving Zensus 2022 database credentials to ",
auth_path,
"\n\n",
"Please add the following line to your .Renviron, ",
"e.g. via `usethis::edit_r_environ()`, ",
"to use the specified username and password across sessions:\n\n",
"ZENSUS_KEY=",
key,
"\n\n")
}

dir.create(gen_auth_path(), showWarnings = FALSE, recursive = TRUE)
auth_path <- gen_auth_path(path)

httr2::secret_write_rds(list(username = username, password = password),
path = auth_path,
key = "ZENSUS_KEY")
key <- httr2::secret_make_key()

}
do.call(Sys.setenv, setNames(list(key), sys_env))

} else {
message(paste0("Your credentials for the ", ui_menu_database, " database have been saved to "),
auth_path,
".",
"\n\n",
"The key to the credentials file has been set as environment variable, which is lost after the R session is closed.",
"\n\n",
"If you want to keep the key saved for future sessions, please add the following line to your .Renviron file, ",
"e.g. via the function `usethis::edit_r_environ()`:\n\n",
paste0(sys_env, "="),
key,
"\n\n")

stop("Misspecification of parameter 'database' in function 'insert_and_save_credentials'.",
call. = FALSE)
dir.create(gen_auth_path(), showWarnings = FALSE, recursive = TRUE)

}
httr2::secret_write_rds(list(username = username, password = password),
path = auth_path,
key = sys_env)

}

#-------------------------------------------------------------------------------
3 changes: 2 additions & 1 deletion README.Rmd
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,10 @@ knitr::opts_chunk$set(
[![R-CMD-check](https://github.com/CorrelAid/restatis/actions/workflows/R-CMD-check.yaml/badge.svg)](https://github.com/CorrelAid/restatis/actions/workflows/R-CMD-check.yaml)
[![Codecov test coverage](https://codecov.io/gh/CorrelAid/restatis/branch/main/graph/badge.svg)](https://app.codecov.io/gh/CorrelAid/restatis?branch=main)
[![Lifecycle: experimental](https://img.shields.io/badge/lifecycle-experimental-orange.svg)](https://lifecycle.r-lib.org/articles/stages.html#experimental)
[![Awesome](https://cdn.rawgit.com/sindresorhus/awesome/d7305f38d29fed78fa85652e3a63e154dd8e8829/media/badge.svg)](https://github.com/sindresorhus/awesome)
<!-- badges: end -->

restatis is a wrapper around the RESTful API that provides access to the three main databases of German official statistics:
**restatis** is a wrapper around the RESTful API that provides access to the three main databases of German official statistics:

- The [**GENESIS database** of the Federal Statistical Office of Germany (Destatis)](https://www-genesis.destatis.de/genesis/online).
- [**regionalstatistik.de** which is the database of the German Länder (Regionaldatenbank)](https://www.regionalstatistik.de/genesis/online/).
Expand Down

0 comments on commit 02b80de

Please sign in to comment.