From 821e1a23d8d7185c491ea4a069e8dc0efa442945 Mon Sep 17 00:00:00 2001 From: Toph Allen Date: Tue, 15 Apr 2025 17:28:57 -0400 Subject: [PATCH 1/4] first draft of implementation --- R/connect.R | 32 ++++++++++++++++++++++++++++++-- 1 file changed, 30 insertions(+), 2 deletions(-) diff --git a/R/connect.R b/R/connect.R index 770595970..2e32f51d9 100644 --- a/R/connect.R +++ b/R/connect.R @@ -981,8 +981,36 @@ connect <- function( } con <- Connect$new(server = server, api_key = api_key) + if (on_connect()) { + comp <- compare_connect_version(using_version, "2025.01.0") + if (comp < 0) { + if (!missing(token)) { + # If running on a too-old version of Connect and token was explicitly + # passed, error. + stop( + "Running content using the viewer's credentials ", + "requires Connect v2025.01.0 or later." + ) + } else { + # If running on too-old Connect and no token was explicitly passed, use + # the old behavior (run with publisher creds). + message( + "Content will run using publisher's credentials. ", + "Running content using the viewer's credentials ", + "requires Connect v2025.01.0 or later." + ) + } + } else { + if (missing(token) && exists("getDefaultReactiveDomain", mode = "function")) { + # If the session token was not provided, see if we can obtain it from + # the Shiny context. + session <- getDefaultReactiveDomain() + token <- session$request$HTTP_POSIT_CONNECT_USER_SESSION_TOKEN + } + } + } + if (!missing(token)) { - error_if_less_than(con$version, "2025.01.0") if (on_connect()) { visitor_creds <- get_oauth_credentials( con, @@ -991,11 +1019,11 @@ connect <- function( ) con <- connect(server = server, api_key = visitor_creds$access_token) } else { - con <- connect(server = server, api_key = token_local_testing_key) message(paste0( "Called with `token` but not running on Connect. ", "Continuing with fallback API key." )) + con <- connect(server = server, api_key = token_local_testing_key) } } From b2375d2532b19449941b99d85068ffc01dd22b04 Mon Sep 17 00:00:00 2001 From: Toph Allen Date: Tue, 15 Apr 2025 17:46:07 -0400 Subject: [PATCH 2/4] bugfix --- R/connect.R | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/R/connect.R b/R/connect.R index 2e32f51d9..7f005f750 100644 --- a/R/connect.R +++ b/R/connect.R @@ -982,7 +982,7 @@ connect <- function( con <- Connect$new(server = server, api_key = api_key) if (on_connect()) { - comp <- compare_connect_version(using_version, "2025.01.0") + comp <- compare_connect_version(con$version, "2025.01.0") if (comp < 0) { if (!missing(token)) { # If running on a too-old version of Connect and token was explicitly From 5061a6745cd5f358e0766dd6befa2046ee6baee5 Mon Sep 17 00:00:00 2001 From: Toph Allen Date: Tue, 15 Apr 2025 17:57:22 -0400 Subject: [PATCH 3/4] small bugfix --- R/connect.R | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/R/connect.R b/R/connect.R index 7f005f750..44aaeef31 100644 --- a/R/connect.R +++ b/R/connect.R @@ -1017,13 +1017,13 @@ connect <- function( user_session_token = token, requested_token_type = "urn:posit:connect:api-key" ) - con <- connect(server = server, api_key = visitor_creds$access_token) + con <- Connect$new(server = server, api_key = visitor_creds$access_token) } else { message(paste0( "Called with `token` but not running on Connect. ", "Continuing with fallback API key." )) - con <- connect(server = server, api_key = token_local_testing_key) + con <- Connect$new(server = server, api_key = token_local_testing_key) } } From d47d24706af93648b14088384f07756c6eb46f06 Mon Sep 17 00:00:00 2001 From: Toph Allen Date: Tue, 15 Apr 2025 18:09:08 -0400 Subject: [PATCH 4/4] account for explicit provision of API key --- R/connect.R | 13 ++++++++++++- 1 file changed, 12 insertions(+), 1 deletion(-) diff --git a/R/connect.R b/R/connect.R index 44aaeef31..419b6be8d 100644 --- a/R/connect.R +++ b/R/connect.R @@ -965,12 +965,19 @@ Connect <- R6::R6Class( #' @export connect <- function( server = Sys.getenv(paste0(prefix, "_SERVER"), NA_character_), - api_key = Sys.getenv(paste0(prefix, "_API_KEY"), NA_character_), + api_key, token, token_local_testing_key = api_key, prefix = "CONNECT", ..., .check_is_fatal = TRUE) { + if (!missing(api_key)) { + api_key_provided <- TRUE + } else { + api_key_provided <- FALSE + api_key <- Sys.getenv(paste0(prefix, "_API_KEY"), NA_character_) + } + if (is.null(api_key) || is.na(api_key) || nchar(api_key) == 0) { msg <- "Invalid (empty) API key. Please provide a valid API key" if (.check_is_fatal) { @@ -981,6 +988,10 @@ connect <- function( } con <- Connect$new(server = server, api_key = api_key) + if (api_key_provided) { + return(con) + } + if (on_connect()) { comp <- compare_connect_version(con$version, "2025.01.0") if (comp < 0) {