-
Notifications
You must be signed in to change notification settings - Fork 2k
added ability to pass down url query parameters to initial oauth2.0 authorization page #503
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
Changes from 19 commits
d6521ff
05e38c3
df9923b
935f560
79fd5fd
f883c30
a3478e3
1d85300
d19e1a2
da325ec
b66806b
2a55408
1d45bf7
81b6017
07dd312
9e2d5d9
f56170c
1acdfac
b10ca89
a330073
d92d7e9
54b922b
89a1c42
9799af1
bfc3274
54008e6
6c70919
b12b426
4ce00ab
5971fda
f51cfa9
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 |
|---|---|---|
|
|
@@ -66,6 +66,10 @@ init_oauth1.0 <- function(endpoint, app, permission = NULL, | |
| #' @param client_credentials Default to \code{FALSE}. Set to \code{TRUE} to use | ||
| #' \emph{Client Credentials Grant} instead of \emph{Authorization | ||
| #' Code Grant}. See \url{https://tools.ietf.org/html/rfc6749#section-4.4}. | ||
| #' @param auth_page_query_params Default to \code{list()}. Set to named list | ||
| #' holding query parameters to append to initial auth page query. Useful for | ||
| #' some APIs (e.g. Fitbit; see Authorization page URI parameters at | ||
| #' \url{https://dev.fitbit.com/build/reference/web-api/oauth2/}.) | ||
| #' @export | ||
| #' @keywords internal | ||
| init_oauth2.0 <- function(endpoint, app, scope = NULL, | ||
|
|
@@ -75,7 +79,8 @@ init_oauth2.0 <- function(endpoint, app, scope = NULL, | |
| is_interactive = interactive(), | ||
| use_basic_auth = FALSE, | ||
| config_init = list(), | ||
| client_credentials = FALSE | ||
| client_credentials = FALSE, | ||
| auth_page_query_params = list() | ||
| ) { | ||
|
|
||
| scope <- check_scope(scope) | ||
|
|
@@ -99,7 +104,8 @@ init_oauth2.0 <- function(endpoint, app, scope = NULL, | |
| app, | ||
| scope = scope, | ||
| redirect_uri = redirect_uri, | ||
| state = state | ||
| state = state, | ||
| auth_page_query_params = auth_page_query_params | ||
|
||
| ) | ||
| code <- oauth_authorize(authorize_url, use_oob) | ||
| } | ||
|
|
@@ -113,23 +119,33 @@ init_oauth2.0 <- function(endpoint, app, scope = NULL, | |
| type = type, | ||
| redirect_uri = redirect_uri, | ||
| client_credentials = client_credentials, | ||
| config = config_init | ||
| config = config_init, | ||
| auth_page_query_params=auth_page_query_params | ||
cosmomeese marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| ) | ||
| } | ||
|
|
||
| #' @export | ||
| #' @rdname init_oauth2.0 | ||
| oauth2.0_authorize_url <- function(endpoint, app, scope, | ||
| redirect_uri = app$redirect_uri, | ||
| state = nonce() | ||
| state = nonce(), | ||
| auth_page_query_params = auth_page_query_params | ||
cosmomeese marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| ) { | ||
| modify_url(endpoint$authorize, query = compact(list( | ||
| client_id = app$key, | ||
| scope = scope, | ||
| redirect_uri = redirect_uri, | ||
| response_type = "code", | ||
| state = state) | ||
| )) | ||
| #TODO might need to put some before and some after... | ||
| modify_url(endpoint$authorize, | ||
cosmomeese marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| query = compact( | ||
| c( | ||
| list( | ||
| client_id = app$key, | ||
| scope = scope, | ||
| redirect_uri = redirect_uri, | ||
| response_type = "code", | ||
| state = state | ||
| ), auth_page_query_params | ||
| ) | ||
| ) | ||
| ) | ||
|
|
||
| } | ||
|
|
||
| #' @export | ||
|
|
@@ -142,7 +158,8 @@ oauth2.0_access_token <- function(endpoint, | |
| use_basic_auth = FALSE, | ||
| redirect_uri = app$redirect_uri, | ||
| client_credentials = FALSE, | ||
| config = list() | ||
| config = list(), | ||
| auth_page_query_params=list() | ||
cosmomeese marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| ) { | ||
|
|
||
| req_params <- compact(list( | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -202,6 +202,10 @@ Token1.0 <- R6::R6Class("Token1.0", inherit = Token, list( | |
| #' requests. | ||
| #' @param credentials Advanced use only: allows you to completely customise | ||
| #' token generation. | ||
| #' @param auth_page_query_params Default to \code{list()}. Set to named list | ||
|
||
| #' holding query parameters to append to initial auth page query. Useful for | ||
| #' some APIs (e.g. Fitbit; see Authorization page URI parameters at | ||
| #' \url{https://dev.fitbit.com/build/reference/web-api/oauth2/}.) | ||
| #' @inheritParams oauth1.0_token | ||
| #' @return A \code{Token2.0} reference class (RC) object. | ||
| #' @family OAuth | ||
|
|
@@ -214,7 +218,8 @@ oauth2.0_token <- function(endpoint, app, scope = NULL, user_params = NULL, | |
| cache = getOption("httr_oauth_cache"), | ||
| config_init = list(), | ||
| client_credentials = FALSE, | ||
| credentials = NULL | ||
| credentials = NULL, | ||
| auth_page_query_params = list() | ||
| ) { | ||
| params <- list( | ||
| scope = scope, | ||
|
|
@@ -224,7 +229,8 @@ oauth2.0_token <- function(endpoint, app, scope = NULL, user_params = NULL, | |
| as_header = as_header, | ||
| use_basic_auth = use_basic_auth, | ||
| config_init = config_init, | ||
| client_credentials = client_credentials | ||
| client_credentials = client_credentials, | ||
| auth_page_query_params = auth_page_query_params | ||
| ) | ||
|
|
||
| Token2.0$new( | ||
|
|
@@ -245,7 +251,9 @@ Token2.0 <- R6::R6Class("Token2.0", inherit = Token, list( | |
| type = self$params$type, use_oob = self$params$use_oob, | ||
| use_basic_auth = self$params$use_basic_auth, | ||
| config_init = self$params$config_init, | ||
| client_credentials = self$params$client_credentials) | ||
| client_credentials = self$params$client_credentials, | ||
| auth_page_query_params = self$params$auth_page_query_params | ||
| ) | ||
| }, | ||
| 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.
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,67 @@ | ||
| context("OAuth-query-params") | ||
cosmomeese marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
|
|
||
| test_that("oauth2.0 authorize url appends query params", { | ||
| app <- oauth_app("x", "y", "z") | ||
| scope <- httr:::check_scope(c("foo", "bar")) | ||
cosmomeese marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| queryParams <- list( | ||
| foo = "bar", | ||
| paul = "isCool" | ||
cosmomeese marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| ) | ||
| authURL <- oauth2.0_authorize_url( | ||
| endpoint = oauth_endpoints("google"), | ||
| app = app, | ||
| scope = scope, | ||
| auth_page_query_params = queryParams | ||
| ) | ||
|
|
||
| url <- parse_url(authURL) | ||
| paramNames <- names(queryParams) | ||
cosmomeese marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| for (qpIndex in 1:length(queryParams)) | ||
| { | ||
| paramName <- paramNames[qpIndex] | ||
| expect_equal(url$query[paramName], queryParams[qpIndex]) | ||
| } | ||
| }) | ||
|
|
||
| test_that("oauth2.0 authorize url does not append empty query params", { | ||
cosmomeese marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| testAuthorizeUrlWithEmptyInput <- function(emptyInput) { | ||
| app <- oauth_app("x", "y", "z") | ||
| scope <- NULL | ||
| authURL <- oauth2.0_authorize_url( | ||
| endpoint = oauth_endpoints("google"), | ||
| app = app, | ||
| scope = scope, | ||
| state = "testing-nonce", | ||
| auth_page_query_params = emptyInput | ||
| ) | ||
| urlQuery <- parse_url(authURL)$query | ||
|
|
||
| queryParams <- list( | ||
| foo = "bar", | ||
| paul = "isCool" | ||
| ) | ||
| authURL2 <- oauth2.0_authorize_url( | ||
| endpoint = oauth_endpoints("google"), | ||
| app = app, | ||
| scope = scope, | ||
| state = "testing-nonce", | ||
| auth_page_query_params = queryParams | ||
| ) | ||
| urlQuery2 <- parse_url(authURL2)$query | ||
|
|
||
| appendedQueryParamsIn2 <- urlQuery2[!(urlQuery2 %in% urlQuery)] | ||
|
|
||
| expect_equal(appendedQueryParamsIn2, queryParams) | ||
|
|
||
| appendedQueryParamsIn1 <- urlQuery[!(urlQuery %in% urlQuery2)] | ||
|
|
||
| emptyNamedList <- list() | ||
| attr(emptyNamedList, "names") <- character(0) | ||
|
|
||
| expect_equal(appendedQueryParamsIn1, emptyNamedList) | ||
| } | ||
|
|
||
| testAuthorizeUrlWithEmptyInput(list()) | ||
| testAuthorizeUrlWithEmptyInput(NULL) | ||
|
|
||
| }) | ||
Uh oh!
There was an error while loading. Please reload this page.