-
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 user_params to oauth2.0_token to enable endpoint-specific access parameters. #312
Changes from 4 commits
68f9455
7a9a8d4
5cfdb15
bcb72e5
98e5fcf
1600fa8
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 |
---|---|---|
|
@@ -46,15 +46,17 @@ init_oauth1.0 <- function(endpoint, app, permission = NULL, | |
#' @inheritParams init_oauth1.0 | ||
#' @param type content type used to override incorrect server response | ||
#' @param scope a character vector of scopes to request. | ||
#' @param user_params List of named values holding endpoint specific parameters to pass to | ||
#' the server when posting the request for obtaining or refreshing the access token. | ||
#' @param use_oob if FALSE, use a local webserver for the OAuth dance. | ||
#' Otherwise, provide a URL to the user and prompt for a validation | ||
#' 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? | ||
#' @export | ||
#' @keywords internal | ||
init_oauth2.0 <- function(endpoint, app, scope = NULL, type = NULL, | ||
use_oob = getOption("httr_oob_default"), | ||
init_oauth2.0 <- function(endpoint, app, scope = NULL, user_params = NULL, | ||
type = NULL, use_oob = getOption("httr_oob_default"), | ||
is_interactive = interactive()) { | ||
if (!use_oob && !is_installed("httpuv")) { | ||
message("httpuv not installed, defaulting to out-of-band authentication") | ||
|
@@ -85,13 +87,16 @@ 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)) | ||
req_params <- c( | ||
list( | ||
client_id = app$key, | ||
client_secret = app$secret, | ||
redirect_uri = redirect_uri, | ||
grant_type = "authorization_code", | ||
code = code | ||
), | ||
user_params) | ||
req <- POST(endpoint$access, encode = "form", body=req_params) | ||
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. Need a space around last |
||
|
||
stop_for_status(req) | ||
content(req, type = type) | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
# !!! The special redirect URI "urn:ietf:wg:oauth:2.0:oob used | ||
# !!! by httr in case httuv is not installed is currently not | ||
# !!! supported by Azure Active Directory (AAD). | ||
# !!! Therefore it is required to install httpuv to make this work. | ||
|
||
# 1. Register an app app in AAD, e.g. as a "Native app", with | ||
# redirect URI <http://localhost:1410>. | ||
# 2. Insert the App name: | ||
app_name <- 'myapp' # not important for authorization grant flow | ||
# 3. Insert the created apps client ID which was issued after app creation: | ||
client_id <- 'aaaaaaaa-bbbb-cccc-dddd-eeeeeeeeeeee' | ||
# In case your app was registered as a web app instead of a native app, | ||
# you might have to add your secret key string here: | ||
client_secret <- NULL | ||
# API resource ID to request access for, e.g. Power BI: | ||
resource_uri <- 'https://analysis.windows.net/powerbi/api' | ||
|
||
# Obtain OAuth2 endpoint settings for azure: | ||
# This uses the "common" endpoint. | ||
# To use a tenant url, create an | ||
# oauth_endpoint(authorize = "https://login.windows.net/<tenant_id>/oauth2/authorize", | ||
# access = "https://login.windows.net/<tenant_id>/oauth2/token") | ||
# with <tenant_id> replaced by your endpoint ID. | ||
azure_endpoint <- oauth_endpoints('azure') | ||
|
||
# Create the app instance. | ||
myapp <- oauth_app(appname = app_name, | ||
key = client_id, | ||
secret = client_secret) | ||
|
||
# Step through the authorization chain: | ||
# 1. You will be redirected to you authorization endpoint via web browser. | ||
# 2. Once you responded to the request, the endpoint will redirect you to | ||
# the local address specified by httr. | ||
# 3. httr will acquire the authorization code (or error) from the data | ||
# posted to the redirect URI. | ||
# 4. If a code was acquired, httr will contact your authorized token access | ||
# endpoint to obtain the token. | ||
mytoken <- oauth2.0_token(azure_endpoint, myapp, | ||
user_params = list(resource = resource_uri), | ||
use_oob = FALSE) | ||
if (('error' %in% names(mytoken$credentials)) && (nchar(mytoken$credentials$error) > 0)) { | ||
errorMsg <- paste('Error while acquiring token.', | ||
paste('Error message:', mytoken$credentials$error), | ||
paste('Error description:', mytoken$credentials$error_description), | ||
paste('Error code:', mytoken$credentials$error_codes), | ||
sep = '\n') | ||
stop(errorMsg) | ||
} | ||
|
||
# Resource API can be accessed through "mytoken" at this point. | ||
|
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.
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.
I think it would be slightly better to use
stats::modifyList()
and change the order of the argument so thatuser_params
"loses" if a name is defined in both places.