Skip to content
This repository has been archived by the owner on Oct 31, 2019. It is now read-only.

Commit

Permalink
Use assert_that() to catch errors #61
Browse files Browse the repository at this point in the history
  • Loading branch information
andrie committed May 23, 2017
1 parent 9fa84cd commit c8f38fd
Show file tree
Hide file tree
Showing 75 changed files with 580 additions and 671 deletions.
1 change: 1 addition & 0 deletions DESCRIPTION
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ URL: https://github.com/Microsoft/AzureSMR
BugReports: https://github.com/Microsoft/AzureSMR/issues
NeedsCompilation: no
Imports:
assertthat,
httr,
jsonlite,
XML,
Expand Down
2 changes: 2 additions & 0 deletions NAMESPACE
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,8 @@ importFrom(XML,htmlParse)
importFrom(XML,xmlValue)
importFrom(XML,xpathApply)
importFrom(XML,xpathSApply)
importFrom(assertthat,"on_failure<-")
importFrom(assertthat,assert_that)
importFrom(base64enc,base64decode)
importFrom(base64enc,base64encode)
importFrom(digest,hmac)
Expand Down
46 changes: 17 additions & 29 deletions R/AzureAuthenticate.R
Original file line number Diff line number Diff line change
Expand Up @@ -6,34 +6,22 @@
#' @note See \url{https://azure.microsoft.com/en-us/documentation/articles/resource-group-create-service-principal-portal/} for instructions to set up an Active Directory application
#' @references \url{https://azure.microsoft.com/en-us/documentation/articles/resource-group-create-service-principal-portal/}
#'
#' @return Returns Azure token and sets AzureContext token
#' @return If successful, returns TRUE
#' @family Azure resource functions
#'
#' @importFrom utils URLencode
#' @export
azureAuthenticate <- function(azureActiveContext, tenantID, clientID, authKey, verbose = FALSE) {
assert_that(is.azureActiveContext(azureActiveContext))

if (missing(tenantID)) {
tenantID <- azureActiveContext$tenantID
} else (tenantID <- tenantID)
if (missing(clientID)) {
clientID <- azureActiveContext$clientID
} else (clientID <- clientID)
if (missing(authKey)) {
authKey <- azureActiveContext$authKey
} else (authKey <- authKey)
if (missing(tenantID)) tenantID <- azureActiveContext$tenantID
if (missing(clientID)) clientID <- azureActiveContext$clientID
if (missing(authKey)) authKey <- azureActiveContext$authKey

assert_that(is_tenant_id(tenantID))
assert_that(is_client_id(clientID))
assert_that(is_authKey(authKey))
verbosity <- set_verbosity(verbose)

if (!length(tenantID)) {
stop("Error: No tenantID provided: Use tenantID argument or set in AzureContext")
}
if (!length(clientID)) {
stop("Error: No clientID provided: Use clientID argument or set in AzureContext")
}
if (!length(authKey)) {
stop("Error: No authKey provided: Use authKey argument or set in AzureContext")
}
verbosity <- if (verbose)
httr::verbose(TRUE) else NULL

URLGT <- paste0("https://login.microsoftonline.com/", tenantID, "/oauth2/token?api-version=1.0")

Expand All @@ -48,19 +36,19 @@ azureAuthenticate <- function(azureActiveContext, tenantID, clientID, authKey, v
`Content-type` = "application/x-www-form-urlencoded")),
body = bodyGT,
verbosity)
stopWithAzureError(r)

j1 <- content(r, "parsed", encoding = "UTF-8")
if (status_code(r) != 200) stopWithAzureError(r)

AT <- paste("Bearer", j1$access_token)

azToken <- paste("Bearer", j1$access_token)

azureActiveContext$Token <- AT
azureActiveContext$Token <- azToken
azureActiveContext$tenantID <- tenantID
azureActiveContext$clientID <- clientID
azureActiveContext$authKey <- authKey
azureActiveContext$EXPIRY <- Sys.time() + 3598
SUBS <- azureListSubscriptions(azureActiveContext)
if(verbose) message("Authentication succeeded: key obtained")
azureListSubscriptions(azureActiveContext) # this sets the subscription ID
#if(verbose) message("Authentication succeeded: key obtained")
return(TRUE)
}

Expand All @@ -73,7 +61,7 @@ azureAuthenticate <- function(azureActiveContext, tenantID, clientID, authKey, v
#' @family Azure resource functions
#' @export
azureCheckToken <- function(azureActiveContext) {
if (missing(azureActiveContext) || is.null(azureActiveContext)) return(NA)
if (missing(azureActiveContext) || is.null(azureActiveContext)) return(FALSE)
if (is.null(azureActiveContext$EXPIRY))
stop("Not authenticated: Use azureAuthenticate()")

Expand Down
Loading

0 comments on commit c8f38fd

Please sign in to comment.