Skip to content

Commit

Permalink
Pass auth_url param through to oauth_flow_device (#331)
Browse files Browse the repository at this point in the history
* Pass `auth_url` through to `oauth_flow_device()`
* Add example
* Extract out repeated example GitHub client
* Fix `oauth_flow_abort()` buglet
  • Loading branch information
taerwin committed Oct 8, 2023
1 parent f5bee37 commit 696e4fe
Show file tree
Hide file tree
Showing 10 changed files with 78 additions and 35 deletions.
1 change: 1 addition & 0 deletions NAMESPACE
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ export("%>%")
export(curl_help)
export(curl_translate)
export(default_redirect_uri)
export(example_github_client)
export(example_url)
export(jwt_claim)
export(jwt_encode_hmac)
Expand Down
6 changes: 6 additions & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
# httr2 (development version)

* `req_oauth_device()` now takes a `auth_url` parameter making it usable
(#331, @taerwin).

* OAuth errors containing a url now correctly display that URL
(instead of just uri).

* New `req_paginate()` and `paginate_req_perform()` to easily request paginated
APIs (@mgirlich, #8).

Expand Down
18 changes: 8 additions & 10 deletions R/oauth-flow-auth-code.R
Original file line number Diff line number Diff line change
Expand Up @@ -39,18 +39,16 @@
#' @returns A modified HTTP [request].
#' @inheritParams oauth_flow_auth_code
#' @examples
#' client <- oauth_client(
#' id = "28acfec0674bb3da9f38",
#' secret = obfuscated(paste0(
#' "J9iiGmyelHltyxqrHXW41ZZPZamyUNxSX1_uKnv",
#' "PeinhhxET_7FfUs2X0LLKotXY2bpgOMoHRCo"
#' )),
#' token_url = "https://github.com/login/oauth/access_token",
#' name = "hadley-oauth-test"
#' )
#' req_auth_github <- function(req) {
#' req_oauth_auth_code(
#' req,
#' client = example_github_client(),
#' auth_url = "https://github.com/login/oauth/authorize"
#' )
#' }
#'
#' request("https://api.github.com/user") %>%
#' req_oauth_auth_code(client, auth_url = "https://github.com/login/oauth/authorize")
#' req_auth_github()
req_oauth_auth_code <- function(req, client,
auth_url,
cache_disk = FALSE,
Expand Down
15 changes: 11 additions & 4 deletions R/oauth-flow-device.R
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,17 @@
#' @inheritParams req_oauth_auth_code
#' @returns A modified HTTP [request].
#' @examples
#' client <- oauth_client("example", "https://example.com/get_token")
#' req <- request("https://example.com")
#' req_auth_github <- function(req) {
#' req_oauth_device(
#' req,
#' client = example_github_client(),
#' auth_url = "https://github.com/login/device/code"
#' )
#' }
#'
#' req %>% req_oauth_device(client)
req_oauth_device <- function(req, client,
#' request("https://api.github.com/user") %>%
#' req_auth_github()
req_oauth_device <- function(req, client, auth_url,
cache_disk = FALSE,
cache_key = NULL,
scope = NULL,
Expand All @@ -26,6 +32,7 @@ req_oauth_device <- function(req, client,

params <- list(
client = client,
auth_url = auth_url,
scope = scope,
auth_params = auth_params,
token_params = token_params
Expand Down
2 changes: 1 addition & 1 deletion R/oauth-flow.R
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ oauth_flow_abort <- function(error,
c(
"OAuth failure [{error}]",
"*" = description,
i = if (!is.null(uri)) "Learn more at {.url uri}."
i = if (!is.null(uri)) "Learn more at {.url {uri}}."
),
code = error,
class = c(glue("httr2_oauth_{error}"), "httr2_oauth"),
Expand Down
23 changes: 21 additions & 2 deletions R/test.R
Original file line number Diff line number Diff line change
Expand Up @@ -30,14 +30,18 @@ request_pagination_test <- function(parse_resp = NULL,
}


#' URL to a local server that's useful for tests and examples
#' Code for examples
#'
#' Requires the webfakes package to be installed. It has the following endpoints:
#' @description
#' `example_url()` runs a simple websever using the webfakes package with the
#' following endpoints:
#'
#' * all the ones from the [webfakes::httpbin_app()]
#' * `/iris`: paginate through the iris dataset. It has the query parameters
#' `page` and `limit` to control the pagination.
#'
#' `example_github_client()` is an OAuth client for GitHub.
#'
#' @keywords internal
#' @export
example_url <- function() {
Expand Down Expand Up @@ -74,3 +78,18 @@ example_url <- function() {
)
the$test_app$url()
}

#' @export
#' @rdname example_url
example_github_client <- function() {
# <https://github.com/settings/applications/1636322>
oauth_client(
id = "28acfec0674bb3da9f38",
secret = obfuscated(paste0(
"J9iiGmyelHltyxqrHXW41ZZPZamyUNxSX1_uKnv",
"PeinhhxET_7FfUs2X0LLKotXY2bpgOMoHRCo"
)),
token_url = "https://github.com/login/oauth/access_token",
name = "hadley-oauth-test"
)
}
12 changes: 8 additions & 4 deletions man/example_url.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

18 changes: 8 additions & 10 deletions man/req_oauth_auth_code.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

16 changes: 13 additions & 3 deletions man/req_oauth_device.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion tests/testthat/_snaps/oauth-flow-auth-code.md
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@
Error in `oauth_flow_auth_code_parse()`:
! OAuth failure [123]
* A bad error
i Learn more at <uri>.
i Learn more at <http://example.com>.
Code
oauth_flow_auth_code_parse(query3, "abc")
Condition
Expand Down

0 comments on commit 696e4fe

Please sign in to comment.