-
Notifications
You must be signed in to change notification settings - Fork 2k
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
Changes from 2 commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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 | ||
|
@@ -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)){ | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 |
||
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) | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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. | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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) | ||
} | ||
|
@@ -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) | ||
|
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
There was a problem hiding this comment.
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?