Skip to content

Commit b63b8db

Browse files
authored
Add initial oauth integration credential exchange impl (#292)
1 parent ca4b646 commit b63b8db

File tree

7 files changed

+121
-1
lines changed

7 files changed

+121
-1
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: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,12 @@
11
# Unreleased
22

3+
## Enhancements and fixes
4+
35
- Fixed a bug where timestamps from Connect not in UTC were parsed as `NA` (#290)
46
- Fixed a bug where timestamps sent to Connect may have added the difference between the local time zone and UTC (#291)
7+
- Implement `get_oauth_credentials()` for interacting with Connect's
8+
`/v1/oauth/integrations/credentials` endpoint. This endpoint allows
9+
content running on Posit Connect to obtain the content viewer's OAuth access token. (#297)
510

611
# connectapi 0.2.0
712

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 connect 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/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/__api__/v1/content-064d19.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -168,4 +168,4 @@
168168
"last_name": "User"
169169
}
170170
}
171-
]
171+
]
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
{
2+
"access_token": "access-token",
3+
"issued_token_type": "urn:ietf:params:oauth:token-type:access_token",
4+
"token_type": "Bearer"
5+
}

tests/testthat/test-oauth.R

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
with_mock_api({
2+
test_that("we can retrieve the oauth credentials", {
3+
con <- Connect$new(server = "https://connect.example", api_key = "fake")
4+
expect_true(validate_R6_class(con, "Connect"))
5+
credentials <- get_oauth_credentials(con, user_session_token = "user-session-token")
6+
expect_equal(credentials,
7+
list(
8+
access_token="access-token",
9+
issued_token_type="urn:ietf:params:oauth:token-type:access_token",
10+
token_type="Bearer"
11+
)
12+
)
13+
})
14+
})

0 commit comments

Comments
 (0)