Skip to content

Add support for viewer-based credentials on Posit Connect #217

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Feb 3, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions DESCRIPTION
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ Imports:
lifecycle,
rlang (>= 1.0.0)
Suggests:
connectcreds,
covr,
knitr,
rmarkdown,
Expand Down
3 changes: 3 additions & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,9 @@
* `gh()` now uses a cache provided by httr2. This cache lives in `tools::R_user_dir("gh", "cache")`, maxes out at 100 MB, and can be disabled by setting `options(gh_cache = FALSE)` (#203).
* Removes usage of mockery (@tanho63, #197)

* `gh_token()` can now pick up on the viewer's GitHub credentials (if any) when
running on Posit Connect (@atheriel, #217).

# gh 1.4.1

* `gh_next()`, `gh_prev()`, `gh_first()` and `gh_last()`
Expand Down
20 changes: 10 additions & 10 deletions R/gh_token.R
Original file line number Diff line number Diff line change
Expand Up @@ -46,8 +46,16 @@
gh_token <- function(api_url = NULL) {
api_url <- api_url %||% default_api_url()
stopifnot(is.character(api_url), length(api_url) == 1)
host_url <- get_hosturl(api_url)
# Check for credentials supplied by Posit Connect.
if (is_installed("connectcreds")) {
if (connectcreds::has_viewer_token(host_url)) {
token <- connectcreds::connect_viewer_token(host_url)
return(gh_pat(token$access_token))
}
}
token <- tryCatch(
gitcreds::gitcreds_get(get_hosturl(api_url)),
gitcreds::gitcreds_get(host_url),
error = function(e) NULL
)
gh_pat(token$password %||% "")
Expand All @@ -56,15 +64,7 @@ gh_token <- function(api_url = NULL) {
#' @export
#' @rdname gh_token
gh_token_exists <- function(api_url = NULL) {
api_url <- api_url %||% default_api_url()
tryCatch(
{
token <- gitcreds::gitcreds_get(get_hosturl(api_url))
gh_pat(token$password)
TRUE
} ,
error = function(e) FALSE
)
tryCatch(nzchar(gh_token(api_url)), error = function(e) FALSE)
}

gh_auth <- function(token) {
Expand Down
8 changes: 8 additions & 0 deletions tests/testthat/test-gh_token.R
Original file line number Diff line number Diff line change
Expand Up @@ -163,3 +163,11 @@ test_that("get_apiurl() works", {
expect_equal(get_apiurl("https://github.acme.com/OWNER/REPO"), x)
expect_equal(get_apiurl("https://github.acme.com/api/v3"), x)
})

test_that("tokens can be requested from a Connect server", {
skip_if_not_installed("connectcreds")

token <- strrep("a", 40)
connectcreds::local_mocked_connect_responses(token = token)
expect_equal(gh_token(), gh_pat(token))
})
Loading