Skip to content

Commit 0e79a8a

Browse files
committed
Address review feedback
1 parent 99704ad commit 0e79a8a

File tree

7 files changed

+98
-60
lines changed

7 files changed

+98
-60
lines changed

NAMESPACE

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,7 @@ export(get_image)
8080
export(get_job)
8181
export(get_jobs)
8282
export(get_my_permission)
83+
export(get_oauth_credentials)
8384
export(get_procs)
8485
export(get_tag_data)
8586
export(get_tags)

NEWS.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
## Enhancements and fixes
44

5-
- Implement `connect$oauth_credentials` for interacting with Connect's
5+
- Implement `get_oauth_credentials()` for interacting with Connect's
66
`/v1/oauth/integrations/credentials` endpoint. This endpoint allows
77
content running on Posit Connect to obtain the content viewer's OAuth access token.
88

R/connect.R

Lines changed: 0 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -787,36 +787,6 @@ Connect <- R6::R6Class(
787787
return(res[["schedules"]])
788788
},
789789

790-
# oauth integrations --------------------------------------------
791-
792-
#' @description Perform an OAuth credential exchange to obtain a
793-
#' viewer's OAuth access token.
794-
#' @param user_session_token The content viewer's session token. This token
795-
#' can only be obtained when the content is running on a Connect server. The token
796-
#' identifies the user who is viewing the content interactively on the Connect server.
797-
#'
798-
#' Read this value from the HTTP header: `Posit-Connect-User-Session-Token`
799-
#'
800-
#' For example, to read the token from a Shiny session:
801-
#' `user_session_token <- session$request$HTTP_POSIT_CONNECT_USER_SESSION_TOKEN`
802-
#' or, to read the token from an inbound HTTP request with Plumbler:
803-
#' `user_session_token <- req$HTTP_POSIT_CONNECT_USER_SESSION_TOKEN`
804-
oauth_credentials = function(user_session_token) {
805-
url <- v1_url("oauth", "integrations", "credentials")
806-
body <- c(
807-
list(
808-
grant_type = "urn:ietf:params:oauth:grant-type:token-exchange",
809-
subject_token_type = "urn:posit:connect:user-session-token",
810-
subject_token = user_session_token
811-
)
812-
)
813-
self$POST(
814-
url,
815-
encode = "form",
816-
body = body
817-
)
818-
},
819-
820790
# misc utilities --------------------------------------------
821791

822792
#' @description Get documentation.

R/get.R

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -652,3 +652,53 @@ get_procs <- function(src) {
652652

653653
return(tbl_data)
654654
}
655+
656+
#' Perform an OAuth credential exchange to obtain a viewer's OAuth access token.
657+
#'
658+
#' @param client A Connect R6 object.
659+
#' @param user_session_token The content viewer's session token. This token
660+
#' can only be obtained when the content is running on a Connect server. The token
661+
#' identifies the user who is viewing the content interactively on the Connect server.
662+
#'
663+
#' Read this value from the HTTP header: `Posit-Connect-User-Session-Token`
664+
#'
665+
#' @examples
666+
#' \dontrun{
667+
#' library(connectapi)
668+
#' library(plumber)
669+
#' client <- connect()
670+
#'
671+
#' #* @get /do
672+
#' function(req){
673+
#' user_session_token <- req$HTTP_POSIT_CONNECT_USER_SESSION_TOKEN
674+
#' credentials <- get_oauth_credentials(client, user_session_token)
675+
#'
676+
#' # ... do something with `credentials$access_token` ...
677+
#'
678+
#' "done"
679+
#' }
680+
#' }
681+
#'
682+
#' @return The OAuth credential exchange response.
683+
#'
684+
#' @details
685+
#' Please see https://docs.posit.co/connect/user/oauth-integrations/#obtaining-a-viewer-oauth-access-token
686+
#' for more information.
687+
#'
688+
#' @export
689+
get_oauth_credentials = function(connect, user_session_token) {
690+
validate_R6_class(connect, "Connect")
691+
url <- v1_url("oauth", "integrations", "credentials")
692+
body <- c(
693+
list(
694+
grant_type = "urn:ietf:params:oauth:grant-type:token-exchange",
695+
subject_token_type = "urn:posit:connect:user-session-token",
696+
subject_token = user_session_token
697+
)
698+
)
699+
connect$POST(
700+
url,
701+
encode = "form",
702+
body = body
703+
)
704+
}

man/PositConnect.Rd

Lines changed: 0 additions & 28 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

man/get_oauth_credentials.Rd

Lines changed: 45 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

tests/testthat/test-oauth.R

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ with_mock_api({
22
test_that("we can retrieve the oauth credentials", {
33
con <- Connect$new(server = "https://connect.example", api_key = "fake")
44
expect_true(validate_R6_class(con, "Connect"))
5-
credentials <- con$oauth_credentials("user-session-token")
5+
credentials <- get_oauth_credentials(con, user_session_token = "user-session-token")
66
expect_equal(credentials,
77
list(
88
access_token="access-token",

0 commit comments

Comments
 (0)