Skip to content
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

Add option to retrieve oauth2.0 token using basic authentication #310

Merged
merged 4 commits into from
Jan 7, 2016
Merged
Show file tree
Hide file tree
Changes from 2 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
2 changes: 1 addition & 1 deletion DESCRIPTION
Original file line number Diff line number Diff line change
Expand Up @@ -27,4 +27,4 @@ Suggests:
VignetteBuilder: knitr
License: MIT + file LICENSE
URL: https://github.com/hadley/httr
RoxygenNote: 5.0.0
RoxygenNote: 5.0.1
3 changes: 3 additions & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,9 @@

* `build_url()` now collapses vector `path` with `/` (#280, @artemklevtsov).

* `oauth2.0_token()` gains a `use_basic_auth` argument which allows you to pick
the type of http authentication used to retrieve the token (#310, @grahamrp).

# httr 1.0.0

* httr no longer uses the RCurl package. Instead it uses the curl package,
Expand Down
33 changes: 25 additions & 8 deletions R/oauth-init.R
Original file line number Diff line number Diff line change
Expand Up @@ -51,11 +51,16 @@ init_oauth1.0 <- function(endpoint, app, permission = NULL,
#' code. Defaults to the of the \code{"httr_oob_default"} default,
#' or \code{TRUE} if \code{httpuv} is not installed.
#' @param is_interactive Is the current environment interactive?
#' @param use_basic_auth if \code{TRUE} use http basic authentication to
#' retrieve the token. Some authorization servers require this.
#' If \code{FALSE}, the default, retrieve the token by including the
#' app key and secret in the request body.
#' @export
#' @keywords internal
init_oauth2.0 <- function(endpoint, app, scope = NULL, type = NULL,
use_oob = getOption("httr_oob_default"),
is_interactive = interactive()) {
is_interactive = interactive(),
use_basic_auth = FALSE) {
if (!use_oob && !is_installed("httpuv")) {
message("httpuv not installed, defaulting to out-of-band authentication")
use_oob <- TRUE
Expand Down Expand Up @@ -85,13 +90,25 @@ init_oauth2.0 <- function(endpoint, app, scope = NULL, type = NULL,
}

# Use authorisation code to get (temporary) access token
req <- POST(endpoint$access, encode = "form",
body = list(
client_id = app$key,
client_secret = app$secret,
redirect_uri = redirect_uri,
grant_type = "authorization_code",
code = code))
# Send credentials using HTTP Basic or as parameters in the request body
# See https://tools.ietf.org/html/rfc6749#section-2.3 (Client Authentication)
if (isTRUE(use_basic_auth)){
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could you add a comment with link to the relevant section of the RFC?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It feels like there's a little too much duplication here. Could you please pull out the common body?

req <- POST(endpoint$access, encode = "form",
body = list(
client_id = app$key,
redirect_uri = redirect_uri,
grant_type = "authorization_code",
code = code),
authenticate(app$key, app$secret, type = "basic"))
} else {
req <- POST(endpoint$access, encode = "form",
body = list(
client_id = app$key,
client_secret = app$secret,
redirect_uri = redirect_uri,
grant_type = "authorization_code",
code = code))
}

stop_for_status(req)
content(req, type = type)
Expand Down
10 changes: 6 additions & 4 deletions R/oauth-token.r
Original file line number Diff line number Diff line change
Expand Up @@ -181,18 +181,20 @@ Token1.0 <- R6::R6Class("Token1.0", inherit = Token, list(
#' caching policies used to store credentials across sessions.
#'
#' @inheritParams init_oauth2.0
#' @param as_header If \code{TRUE}, the default, sends oauth in bearer header.
#' If \code{FALSE}, adds as parameter to url.
#' @param as_header If \code{TRUE}, the default, configures the token to add
#' itself to the bearer header of subsequent requests.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Missing indent

#' If \code{FALSE}, configures the token to add itself as a url parameter of subsequent requests.
#' @inheritParams oauth1.0_token
#' @return A \code{Token2.0} reference class (RC) object.
#' @family OAuth
#' @export
oauth2.0_token <- function(endpoint, app, scope = NULL, type = NULL,
use_oob = getOption("httr_oob_default"),
as_header = TRUE,
use_basic_auth = FALSE,
cache = getOption("httr_oauth_cache")) {
params <- list(scope = scope, type = type, use_oob = use_oob,
as_header = as_header)
as_header = as_header, use_basic_auth = use_basic_auth)
Token2.0$new(app = app, endpoint = endpoint, params = params,
cache_path = cache)
}
Expand All @@ -203,7 +205,7 @@ Token2.0 <- R6::R6Class("Token2.0", inherit = Token, list(
init_credentials = function() {
self$credentials <- init_oauth2.0(self$endpoint, self$app,
scope = self$params$scope, type = self$params$type,
use_oob = self$params$use_oob)
use_oob = self$params$use_oob, use_basic_auth = self$params$use_basic_auth)
},
can_refresh = function() {
!is.null(self$credentials$refresh_token)
Expand Down
8 changes: 7 additions & 1 deletion man/init_oauth2.0.Rd

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

12 changes: 9 additions & 3 deletions man/oauth2.0_token.Rd

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