diff --git a/DESCRIPTION b/DESCRIPTION index 9313a83..c4ad4d8 100644 --- a/DESCRIPTION +++ b/DESCRIPTION @@ -54,7 +54,8 @@ Imports: Suggests: knitr, rmarkdown, - testthat + testthat, + withr VignetteBuilder: knitr Remotes: @@ -69,4 +70,4 @@ Encoding: UTF-8 Language: en-US LazyData: true Roxygen: list(markdown = TRUE) -RoxygenNote: 7.3.2 +Config/roxygen2/version: 8.1.1 diff --git a/NAMESPACE b/NAMESPACE index 8b353f6..3834e56 100644 --- a/NAMESPACE +++ b/NAMESPACE @@ -44,103 +44,135 @@ export(write_log) import(htmltools) import(pkgdown) importFrom(assertthat,assert_that) -importFrom(cli,ansi_strip) -importFrom(cli,cat_line) -importFrom(cli,cli_abort) -importFrom(cli,cli_alert_info) -importFrom(cli,cli_alert_success) -importFrom(cli,cli_alert_warning) -importFrom(cli,cli_bullets) -importFrom(cli,cli_progress_bar) -importFrom(cli,cli_progress_done) -importFrom(cli,cli_progress_update) -importFrom(cli,cli_warn) -importFrom(cli,tree) +importFrom(cli, + ansi_strip, + cat_line, + cli_abort, + cli_alert_info, + cli_alert_success, + cli_alert_warning, + cli_bullets, + cli_progress_bar, + cli_progress_done, + cli_progress_update, + cli_warn, + tree +) importFrom(config,get) -importFrom(covr,package_coverage) -importFrom(covr,report) +importFrom(covr, + package_coverage, + report +) importFrom(covrpage,covrpage) importFrom(digest,digest) -importFrom(dplyr,mutate) -importFrom(dplyr,pull) -importFrom(fs,dir_create) -importFrom(fs,dir_exists) -importFrom(fs,dir_info) -importFrom(fs,dir_ls) -importFrom(fs,file_create) -importFrom(fs,file_exists) -importFrom(fs,file_move) -importFrom(fs,path) -importFrom(fs,path_dir) -importFrom(fs,path_ext) -importFrom(fs,path_package) -importFrom(gargle,gargle_oauth_client_from_json) -importFrom(gargle,init_AuthState) +importFrom(dplyr, + mutate, + pull +) +importFrom(fs, + dir_create, + dir_exists, + dir_info, + dir_ls, + file_create, + file_exists, + file_move, + path, + path_dir, + path_ext, + path_package +) +importFrom(gargle, + gargle_oauth_client_from_json, + init_AuthState +) importFrom(gitdown,git_down) importFrom(glue,glue) -importFrom(htmltools,HTML) -importFrom(htmltools,htmlDependency) -importFrom(htmltools,tagList) -importFrom(htmltools,tags) -importFrom(httr,GET) -importFrom(httr,content) -importFrom(httr2,req_auth_bearer_token) -importFrom(httr2,req_headers) -importFrom(httr2,req_method) -importFrom(httr2,req_perform) -importFrom(httr2,req_url_path_append) -importFrom(httr2,request) -importFrom(httr2,resp_body_json) +importFrom(htmltools, + HTML, + htmlDependency, + tagList, + tags +) +importFrom(httr, + GET, + content +) +importFrom(httr2, + req_auth_bearer_token, + req_headers, + req_method, + req_perform, + req_url_path_append, + request, + resp_body_json +) importFrom(jsonlite,fromJSON) importFrom(lubridate,weeks) importFrom(markdown,markdownToHTML) -importFrom(metathis,meta) -importFrom(metathis,meta_social) +importFrom(metathis, + meta, + meta_social +) importFrom(parallel,detectCores) -importFrom(pdftools,pdf_data) -importFrom(pdftools,pdf_text) +importFrom(pdftools, + pdf_data, + pdf_text +) importFrom(pkgdown,build_site) importFrom(pkgload,pkg_name) -importFrom(purrr,map) -importFrom(purrr,map_chr) -importFrom(purrr,pluck) -importFrom(purrr,pmap_chr) -importFrom(purrr,pwalk) -importFrom(rlang,abort) -importFrom(rlang,cnd_entrace) -importFrom(rlang,current_env) -importFrom(rlang,entrace) -importFrom(rlang,error_cnd) -importFrom(rlang,trace_back) -importFrom(stringr,boundary) -importFrom(stringr,str_detect) -importFrom(stringr,str_extract) -importFrom(stringr,str_match) -importFrom(stringr,str_replace) -importFrom(stringr,str_replace_all) -importFrom(stringr,str_to_lower) -importFrom(stringr,str_trim) +importFrom(purrr, + map, + map_chr, + pluck, + pmap_chr, + pwalk +) +importFrom(rlang, + abort, + cnd_entrace, + current_env, + entrace, + error_cnd, + trace_back +) +importFrom(stringr, + boundary, + str_detect, + str_extract, + str_match, + str_replace, + str_replace_all, + str_to_lower, + str_trim +) importFrom(testdown,test_down) importFrom(tibble,tibble) -importFrom(tibblify,tib_chr) -importFrom(tibblify,tib_dbl) -importFrom(tibblify,tib_df) -importFrom(tibblify,tib_lgl) -importFrom(tibblify,tib_row) -importFrom(tibblify,tib_unspecified) -importFrom(tibblify,tibblify) -importFrom(tibblify,tspec_object) +importFrom(tibblify, + tib_chr, + tib_dbl, + tib_df, + tib_lgl, + tib_row, + tib_unspecified, + tibblify, + tspec_object +) importFrom(tidyr,unnest) -importFrom(togglr,get_time_entries) -importFrom(togglr,get_toggl_api_token) -importFrom(togglr,set_toggl_api_token) -importFrom(togglr,toggl_start) -importFrom(togglr,toggl_stop) +importFrom(togglr, + get_time_entries, + get_toggl_api_token, + set_toggl_api_token, + toggl_start, + toggl_stop +) importFrom(usethis,use_news_md) importFrom(utils,assignInMyNamespace) -importFrom(xml2,read_html) -importFrom(xml2,xml_children) -importFrom(xml2,xml_find_all) -importFrom(xml2,xml_find_first) -importFrom(xml2,xml_name) -importFrom(xml2,xml_text) +importFrom(xml2, + read_html, + xml_children, + xml_find_all, + xml_find_first, + xml_name, + xml_text +) diff --git a/R/auth0_jwt.R b/R/auth0_jwt.R index ad0ca2b..d260496 100644 --- a/R/auth0_jwt.R +++ b/R/auth0_jwt.R @@ -7,9 +7,30 @@ get_auth0_jwt <- function( rlang::abort("No Auth0 configuration found") } - httr2::curl_translate( - "curl --request POST --url https://jimbrig.us.auth0.com/oauth/token --header 'content-type: application/json' --data '{\"client_id\":\"tCLHUNuGdBXlaeZ94OrHfjVOUVuFPRte\",\"client_secret\":\"te2n7aYvXPYflth3OFMi8QK5c7obtjoJPNxfBfs1LKQIx2j9YfobR2rPKWsbEz-3\",\"audience\":\"https://auth-api.noclocks.dev\",\"grant_type\":\"client_credentials\"}'" + required_fields <- c( + "jwt_url", "client_id", "client_secret", "audience", "grant_type" ) + if (!is.list(auth0_config)) { + rlang::abort("Auth0 configuration must be a named list") + } + invalid_fields <- required_fields[!vapply( + required_fields, + function(field) { + value <- auth0_config[[field]] + is.character(value) && length(value) == 1L && + !is.na(value) && nzchar(trimws(value)) + }, + logical(1) + )] + if (length(invalid_fields) > 0L) { + rlang::abort(paste0( + "Auth0 configuration requires non-empty string values for: ", + paste(invalid_fields, collapse = ", ") + )) + } + if (!grepl("^https://[^/?#[:space:]]+", auth0_config$jwt_url)) { + rlang::abort("Auth0 jwt_url must use HTTPS") + } base_url <- auth0_config$jwt_url client_id <- auth0_config$client_id diff --git a/inst/config/.gitignore b/inst/config/.gitignore index 6ed01a4..75925c8 100644 --- a/inst/config/.gitignore +++ b/inst/config/.gitignore @@ -2,6 +2,7 @@ !.gitignore !README.md !auth0.yml +!auth0-jwt.template.yml !config.template.yml !credentials.template.json !keeper.config.template.json diff --git a/inst/config/README.md b/inst/config/README.md index a89d84f..6e5b9fd 100644 --- a/inst/config/README.md +++ b/inst/config/README.md @@ -1,5 +1,27 @@ # Configurations +## Auth0 JWT requests + +`get_auth0_jwt()` reads the `auth0` section of `config::get()`, or accepts an +explicit configuration list with `jwt_url`, `client_id`, `client_secret`, +`audience`, and `grant_type`. Existing lists with those fields remain supported. +The token endpoint must use HTTPS, and every field must be a non-empty string. + +Copy `auth0-jwt.template.yml` to the ignored project-root `config.yml`, or merge +its `auth0` section into an existing configuration. Populate `AUTH0_JWT_URL`, +`AUTH0_CLIENT_ID`, `AUTH0_CLIENT_SECRET`, and `AUTH0_AUDIENCE` through your secret +manager or deployment environment. The template uses the `client_credentials` +grant. Do not commit populated configuration, environment files, or tokens. + +The separate `auth0.yml` template uses a different configuration structure and +does not supply the fields required by `get_auth0_jwt()`. + +Source cleanup does not invalidate previously exposed secrets. Before rotating +an application secret in Auth0, inventory its consumers and arrange secure +configuration updates. After rotation, verify token issuance with the replacement +and rejection of the old secret after propagation. Previously issued access +tokens require a separate containment assessment until they expire. + ## Integrations - Slack: diff --git a/inst/config/auth0-jwt.template.yml b/inst/config/auth0-jwt.template.yml new file mode 100644 index 0000000..8c7923b --- /dev/null +++ b/inst/config/auth0-jwt.template.yml @@ -0,0 +1,9 @@ +# Copy to the ignored project-root config.yml, or merge into an existing config. +# Populate environment variables through a secret manager or deployment settings. +default: + auth0: + jwt_url: !expr Sys.getenv("AUTH0_JWT_URL") + client_id: !expr Sys.getenv("AUTH0_CLIENT_ID") + client_secret: !expr Sys.getenv("AUTH0_CLIENT_SECRET") + audience: !expr Sys.getenv("AUTH0_AUDIENCE") + grant_type: client_credentials diff --git a/man/build_pkgdown_with_reports.Rd b/man/build_pkgdown_with_reports.Rd index 5585949..2985870 100644 --- a/man/build_pkgdown_with_reports.Rd +++ b/man/build_pkgdown_with_reports.Rd @@ -35,7 +35,7 @@ overwritten. Default is \code{TRUE}.} None Generate a pkgdown with test and coverage reports } \description{ -Build a \code{\link[pkgdown:pkgdown-package]{pkgdown::pkgdown()}} site with additional options to include +Build a \code{\link[pkgdown:pkgdown]{pkgdown::pkgdown()}} site with additional options to include extra reports. The reports that can be included are: diff --git a/man/dot-noclocks_colors.Rd b/man/dot-noclocks_colors.Rd index 5314e12..e17789b 100644 --- a/man/dot-noclocks_colors.Rd +++ b/man/dot-noclocks_colors.Rd @@ -1,16 +1,11 @@ % Generated by roxygen2: do not edit by hand % Please edit documentation in R/colors.R -\docType{data} \name{.noclocks_colors} \alias{.noclocks_colors} \title{No Clocks Color Palettes} -\format{ -An object of class \code{list} of length 11. -} \usage{ .noclocks_colors } \description{ Collection of color palettes for use in No Clocks, LLC projects. } -\keyword{datasets} diff --git a/man/noclocksr-package.Rd b/man/noclocksr-package.Rd index 471841b..5989937 100644 --- a/man/noclocksr-package.Rd +++ b/man/noclocksr-package.Rd @@ -21,5 +21,10 @@ Useful links: \author{ \strong{Maintainer}: Jimmy Briggs \email{jimmy.briggs@jimbrig.com} (\href{https://orcid.org/0000-0002-7489-8787}{ORCID}) +Authors: +\itemize{ + \item Jimmy Briggs \email{jimmy.briggs@jimbrig.com} (\href{https://orcid.org/0000-0002-7489-8787}{ORCID}) +} + } \keyword{internal} diff --git a/man/process_pdfs.Rd b/man/process_pdfs.Rd index 40718fa..3349873 100644 --- a/man/process_pdfs.Rd +++ b/man/process_pdfs.Rd @@ -35,7 +35,7 @@ parsing the content, and saving the files to the output directory. } \details{ This function implements PDF extraction by extracting the PDF content via -\code{\link[pdftools:pdftools]{pdftools::pdf_text()}} and parsing the extracted text into the following +\code{\link[pdftools:pdf_text]{pdftools::pdf_text()}} and parsing the extracted text into the following components: \itemize{ \item Document Type (Receipt or Invoice) @@ -66,5 +66,5 @@ process_pdfs( } \seealso{ \code{\link[=extract_pdf_content]{extract_pdf_content()}}, \code{\link[=parse_pdf_content]{parse_pdf_content()}} -\code{\link[pdftools:pdftools]{pdftools::pdf_text()}}, \code{\link[pdftools:pdftools]{pdftools::pdf_data()}} +\code{\link[pdftools:pdf_text]{pdftools::pdf_text()}}, \code{\link[pdftools:pdf_data]{pdftools::pdf_data()}} } diff --git a/man/reexports.Rd b/man/reexports.Rd index c93fe33..e816a9a 100644 --- a/man/reexports.Rd +++ b/man/reexports.Rd @@ -19,12 +19,12 @@ These objects are imported from other packages. Follow the links below to see their documentation. \describe{ - \item{cli}{\code{\link[cli]{ansi_strip}}, \code{\link[cli]{tree}}} + \item{cli}{\code{\link[cli:ansi_strip]{ansi_strip()}}, \code{\link[cli:tree]{tree()}}} - \item{digest}{\code{\link[digest]{digest}}} + \item{digest}{\code{\link[digest:digest]{digest()}}} - \item{parallel}{\code{\link[parallel]{detectCores}}} + \item{parallel}{\code{\link[parallel:detectCores]{detectCores()}}} - \item{rlang}{\code{\link[rlang]{abort}}, \code{\link[rlang:entrace]{cnd_entrace}}, \code{\link[rlang]{entrace}}, \code{\link[rlang:cnd]{error_cnd}}, \code{\link[rlang]{trace_back}}} + \item{rlang}{\code{\link[rlang:abort]{abort()}}, \code{\link[rlang:cnd_entrace]{cnd_entrace()}}, \code{\link[rlang:entrace]{entrace()}}, \code{\link[rlang:error_cnd]{error_cnd()}}, \code{\link[rlang:trace_back]{trace_back()}}} }} diff --git a/man/use_noclocks_meta.Rd b/man/use_noclocks_meta.Rd index 96ff4a6..bd895d7 100644 --- a/man/use_noclocks_meta.Rd +++ b/man/use_noclocks_meta.Rd @@ -60,7 +60,7 @@ use_noclocks_meta( \item{...}{Additional arguments} } \value{ -HTML via \code{\link[htmltools:builder]{htmltools::tags()}} and \verb{} tags via +HTML via \code{\link[htmltools:tags]{htmltools::tags()}} and \verb{} tags via \code{\link[metathis:meta]{metathis::meta()}} } \description{ diff --git a/tests/testthat/test-auth0_jwt.R b/tests/testthat/test-auth0_jwt.R new file mode 100644 index 0000000..107fd0a --- /dev/null +++ b/tests/testthat/test-auth0_jwt.R @@ -0,0 +1,78 @@ +auth0_test_config <- function() { + list( + jwt_url = "https://tenant.example.com/oauth/token", + client_id = "test-client-id", + client_secret = "test-client-secret", + audience = "https://api.example.com", + grant_type = "client_credentials" + ) +} + +test_that("Auth0 requests use only the supplied configuration", { + settings <- auth0_test_config() + captured <- NULL + httr2::local_mocked_responses(function(req) { + captured <<- req + httr2::response( + headers = list(`content-type` = "application/json"), + body = charToRaw('{"access_token":"test-token","token_type":"Bearer"}') + ) + }) + testthat::local_mocked_bindings( + curl_translate = function(...) stop("Example code must not run"), + .package = "httr2" + ) + + result <- get_auth0_jwt(settings) + + expect_equal(captured$url, settings$jwt_url) + expect_equal(captured$method, "POST") + expect_equal(captured$body$data, settings[c( + "client_id", "client_secret", "audience", "grant_type" + )]) + expect_equal(result, list(access_token = "test-token", token_type = "Bearer")) +}) + +test_that("missing or malformed configuration fails before any request", { + httr2::local_mocked_responses(function(req) { + stop("No network request should be attempted") + }) + expect_error(get_auth0_jwt(NULL), "No Auth0 configuration found") + expect_error(get_auth0_jwt("invalid"), "must be a named list") + + for (field in names(auth0_test_config())) { + for (invalid in list(NULL, "", " ", NA_character_, character(), c("a", "b"), 1)) { + settings <- auth0_test_config() + settings[field] <- list(invalid) + expect_error(get_auth0_jwt(settings), field, fixed = TRUE) + } + } + settings <- auth0_test_config() + settings$jwt_url <- "http://tenant.example.com/oauth/token" + expect_error(get_auth0_jwt(settings), "must use HTTPS") +}) + +test_that("HTTP failures still propagate", { + httr2::local_mocked_responses(list(httr2::response(status_code = 401))) + expect_error(get_auth0_jwt(auth0_test_config()), class = "httr2_http_401") +}) + +test_that("the default configuration is resolved from environment values", { + withr::local_envvar(c( + AUTH0_JWT_URL = "https://tenant.example.com/oauth/token", + AUTH0_CLIENT_ID = "test-client-id", + AUTH0_CLIENT_SECRET = "test-client-secret", + AUTH0_AUDIENCE = "https://api.example.com" + )) + template <- testthat::test_path("..", "..", "inst", "config", "auth0-jwt.template.yml") + withr::local_envvar(c(R_CONFIG_FILE = normalizePath(template))) + httr2::local_mocked_responses(function(req) { + expect_equal(req$body$data$client_secret, Sys.getenv("AUTH0_CLIENT_SECRET")) + httr2::response( + headers = list(`content-type` = "application/json"), + body = charToRaw('{"access_token":"test-token"}') + ) + }) + + expect_equal(get_auth0_jwt()$access_token, "test-token") +})