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

added ability to pass down url query parameters to initial oauth2.0 authorization page #503

Merged
merged 31 commits into from
Dec 4, 2018
Merged
Show file tree
Hide file tree
Changes from 8 commits
Commits
Show all changes
31 commits
Select commit Hold shift + click to select a range
d6521ff
added ability to pass url query parameters to initial authorization page
cosmomeese Jan 22, 2018
05e38c3
fixed syntax error for previous commit
cosmomeese Jan 22, 2018
df9923b
added ability to pass query params to authorization page
cosmomeese Jan 23, 2018
935f560
changed default auth_page_query_params from NULL to list() to fix unu…
cosmomeese Jan 23, 2018
79fd5fd
fixed error resulting from moving auth_page_query_params to end of li…
cosmomeese Jan 23, 2018
f883c30
removed unnecessary (& incorrect) reference to auth_page_query_params…
cosmomeese Jan 23, 2018
a3478e3
updated documentation + fixed auth_page_query_params so they all defa…
cosmomeese Jan 26, 2018
1d85300
added some unit tests
cosmomeese Jan 26, 2018
d19e1a2
updated code to match tidyverse style guide (using 'styler' package)
cosmomeese Nov 22, 2018
da325ec
Merge branch 'master' into master
cosmomeese Nov 22, 2018
b66806b
reversed unnecessary style changes
cosmomeese Nov 22, 2018
2a55408
updated style (using styler package & manual changes where necessary)
cosmomeese Nov 22, 2018
1d45bf7
Merge commit '5345aaf04486a507f5fcfe028225f4f4d7c8afa6'
cosmomeese Nov 22, 2018
81b6017
Merge branch 'master' into experiment
cosmomeese Nov 22, 2018
07dd312
re-styled (using 'styler') ugly code missed on first pass
cosmomeese Nov 22, 2018
9e2d5d9
re-styled (using 'styler') ugly code missed on second pass
cosmomeese Nov 22, 2018
f56170c
manual re-style for complicated code block
cosmomeese Nov 22, 2018
1acdfac
force travis CI rebuild
cosmomeese Nov 22, 2018
b10ca89
manual re-re-style for complicated code block
cosmomeese Nov 22, 2018
a330073
improved code smell
cosmomeese Nov 26, 2018
d92d7e9
dropped bracket
cosmomeese Nov 26, 2018
54b922b
updated man pages & made sure oauth2.0_authorize_url can still receiv…
cosmomeese Nov 26, 2018
89a1c42
corrected test
cosmomeese Nov 26, 2018
9799af1
updated man pages
cosmomeese Nov 26, 2018
bfc3274
clarify code
cosmomeese Nov 26, 2018
54008e6
added default param for query_extra
cosmomeese Nov 26, 2018
6c70919
updated manpages
cosmomeese Nov 26, 2018
b12b426
replaced oauth2.0_authorize_url test for empty input & updated style
cosmomeese Nov 27, 2018
4ce00ab
updated news
cosmomeese Nov 28, 2018
5971fda
updated news (again)
cosmomeese Nov 28, 2018
f51cfa9
Merge branch 'master' into master
cosmomeese Dec 3, 2018
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
37 changes: 25 additions & 12 deletions R/oauth-init.R
Original file line number Diff line number Diff line change
Expand Up @@ -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
cosmomeese marked this conversation as resolved.
Show resolved Hide resolved
#' 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,
Expand All @@ -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)
Expand All @@ -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
Copy link
Member

Choose a reason for hiding this comment

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

Needs alignment fix

)
code <- oauth_authorize(authorize_url, use_oob)
}
Expand All @@ -113,23 +119,29 @@ 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.
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
) {
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, query = compact(c(list(
client_id = app$key,
scope = scope,
redirect_uri = redirect_uri,
response_type = "code",
state = state
), auth_page_query_params
)
)
)
}

#' @export
Expand All @@ -142,7 +154,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.
Show resolved Hide resolved
) {

req_params <- compact(list(
Expand Down
13 changes: 10 additions & 3 deletions R/oauth-token.r
Original file line number Diff line number Diff line change
Expand Up @@ -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
Copy link
Member

Choose a reason for hiding this comment

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

You shouldn't need to copy and paste the docs here, but instead use @inheritParams

#' 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
Expand All @@ -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,
Expand All @@ -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(
Expand All @@ -245,7 +251,8 @@ 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)
Expand Down
13 changes: 10 additions & 3 deletions man/init_oauth2.0.Rd

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

8 changes: 7 additions & 1 deletion man/oauth2.0_token.Rd

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

63 changes: 63 additions & 0 deletions tests/testthat/test-oauth-query-params.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
context("OAuth-query-params")
cosmomeese marked this conversation as resolved.
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"))
queryParams <- list(foo = "bar",
paul = "isCool")
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.
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.
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)

})