Fix token authentication method (#3)

Fixes #1, closes #2
This commit is contained in:
Hong Ooi 2020-10-15 04:46:14 +11:00 коммит произвёл GitHub
Родитель 32a2603bc4
Коммит b37fd24345
Не найден ключ, соответствующий данной подписи
Идентификатор ключа GPG: 4AEE18F83AFDEB23
11 изменённых файлов: 51 добавлений и 31 удалений

Просмотреть файл

@ -20,5 +20,5 @@ Suggests:
knitr,
testthat
VignetteBuilder: knitr
Roxygen: list(markdown=TRUE)
RoxygenNote: 6.1.1
Roxygen: list(markdown=TRUE, r6=FALSE, old_usage=TRUE)
RoxygenNote: 7.1.1

Просмотреть файл

@ -1,5 +1,6 @@
# AzureCognitive 1.0.0.9000
- Fix token authentication method for Text Translation (reported by @quickcoffee).
- Change maintainer email address.
# AzureCognitive 1.0.0

Просмотреть файл

@ -45,7 +45,7 @@ public=list(
regen_key=function(key=1)
{
body=list(keyName=paste0("Key", key))
body <- list(keyName=paste0("Key", key))
unlist(private$res_op("regenerateKey", body=body, http_verb="POST"))
},

Просмотреть файл

@ -26,16 +26,16 @@
#' @examples
#' \dontrun{
#'
#' cognitive_service("https://myvisionservice.api.cognitive.azure.com",
#' cognitive_endpoint("https://myvisionservice.api.cognitive.azure.com",
#' service_type="Computervision", key="key")
#'
#' cognitive_service("https://mylangservice.api.cognitive.azure.com",
#' cognitive_endpoint("https://mylangservice.api.cognitive.azure.com",
#' service_type="LUIS", key="key")
#'
#' # authenticating with AAD
#' token <- AzureAuth::get_azure_token("https://cognitiveservices.azure.com",
#' tenant="mytenant", app="app_id", password="password")
#' cognitive_service("https://myvisionservice.api.cognitive.azure.com",
#' cognitive_endpoint("https://myvisionservice.api.cognitive.azure.com",
#' service_type="Computervision", aad_token=token)
#'
#' }
@ -92,7 +92,7 @@ print.cognitive_endpoint <- function(x, ...)
#' @examples
#' \dontrun{
#'
#' endp <- cognitive_service("https://myvisionservice.api.cognitive.azure.com",
#' endp <- cognitive_endpoint("https://myvisionservice.api.cognitive.azure.com",
#' service_type="Computervision", key="key")
#'
#' # analyze an online image

Просмотреть файл

@ -2,15 +2,19 @@
#'
#' @param key The subscription key for the service.
#' @param region The Azure region where the service is located.
#' @param token_host Optionally, the URL for the token endpoint.
#' @param token_url Optionally, the URL for the token endpoint.
#' @export
get_cognitive_token <- function(key, region, token_host=NULL)
get_cognitive_token <- function(key, region="global", token_url=NULL)
{
if(is.null(token_host))
token_host <- sprintf("https://%s.api.cognitive.microsoft.com/sts/v1.0/issueToken", normalize_region(region))
if(is.null(token_url))
{
token_url <- if(region != "global")
sprintf("https://%s.api.cognitive.microsoft.com/sts/v1.0/issueToken", normalize_region(region))
else "https://api.cognitive.microsoft.com/sts/v1.0/issueToken"
}
hdrs <- httr::add_headers(`Ocp-Apim-Subscription-Key`=unname(key))
res <- httr::POST(token_host, encode="form", hdrs)
res <- httr::POST(token_url, encode="form", hdrs)
rawToChar(process_cognitive_response(res, "stop"))
}

Просмотреть файл

@ -4,10 +4,6 @@
\name{az_cognitive_service}
\alias{az_cognitive_service}
\title{Azure Cognitive Service resource class}
\format{An object of class \code{R6ClassGenerator} of length 24.}
\usage{
az_cognitive_service
}
\description{
Class representing a cognitive service resource, exposing methods for working with it.
}
@ -52,4 +48,3 @@ cogsvc$get_endpoint()
\seealso{
\link{cognitive_endpoint}, \link{create_cognitive_service}, \link{get_cognitive_service}
}
\keyword{datasets}

Просмотреть файл

@ -8,8 +8,8 @@
call_cognitive_endpoint(endpoint, ...)
\method{call_cognitive_endpoint}{cognitive_endpoint}(endpoint, operation,
options = list(), headers = list(), body = NULL, encode = NULL,
..., http_verb = c("GET", "POST", "PUT", "PATCH", "DELETE", "HEAD"),
options = list(), headers = list(), body = NULL, encode = NULL, ...,
http_verb = c("GET", "POST", "PUT", "PATCH", "DELETE", "HEAD"),
http_status_handler = c("stop", "warn", "message", "pass"))
}
\arguments{
@ -29,7 +29,7 @@ call_cognitive_endpoint(endpoint, ...)
\item{http_verb}{The HTTP verb for the REST call.}
\item{http_status_handler}{How to handle a failed REST call. \code{stop}, \code{warn} and \code{message} will call the corresponding \code{*_for_status} handler in the httr package; \code{pass} will return the raw response object unchanged. The last one is mostly intended for debugging purposes.}
\item{http_status_handler}{How to handle a failed REST call. \code{stop}, \code{warn} and \code{message} will call the corresponding \verb{*_for_status} handler in the httr package; \code{pass} will return the raw response object unchanged. The last one is mostly intended for debugging purposes.}
}
\value{
For a successful REST call, the contents of the response. This will usually be a list, obtained by translating the raw JSON body into R. If the call returns a non-success HTTP status code, based on the \code{http_status_handler} argument.
@ -43,7 +43,7 @@ This function does the low-level work of constructing a HTTP request and then ca
\examples{
\dontrun{
endp <- cognitive_service("https://myvisionservice.api.cognitive.azure.com",
endp <- cognitive_endpoint("https://myvisionservice.api.cognitive.azure.com",
service_type="Computervision", key="key")
# analyze an online image

Просмотреть файл

@ -43,17 +43,17 @@ Currently, \code{cognitive_endpoint} recognises the following service types:
\examples{
\dontrun{
cognitive_service("https://myvisionservice.api.cognitive.azure.com",
cognitive_endpoint("https://myvisionservice.api.cognitive.azure.com",
service_type="Computervision", key="key")
cognitive_service("https://mylangservice.api.cognitive.azure.com",
cognitive_endpoint("https://mylangservice.api.cognitive.azure.com",
service_type="LUIS", key="key")
# authenticating with AAD
token <- AzureAuth::get_azure_token("https://cognitiveservices.azure.com",
tenant="mytenant", app="app_id", password="password")
cognitive_service("https://myvisionservice.api.cognitive.azure.com",
service_type="Computervision", aad_token=token))
cognitive_endpoint("https://myvisionservice.api.cognitive.azure.com",
service_type="Computervision", aad_token=token)
}
}

Просмотреть файл

@ -4,14 +4,14 @@
\alias{get_cognitive_token}
\title{Obtain authentication token for a cognitive service}
\usage{
get_cognitive_token(key, region, token_host = NULL)
get_cognitive_token(key, region = "global", token_url = NULL)
}
\arguments{
\item{key}{The subscription key for the service.}
\item{region}{The Azure region where the service is located.}
\item{token_host}{Optionally, the URL for the token endpoint.}
\item{token_url}{Optionally, the URL for the token endpoint.}
}
\description{
Obtain authentication token for a cognitive service

Просмотреть файл

@ -68,6 +68,7 @@ test_that("Endpoint AAD authentication works",
endp <- cognitive_endpoint(cogsvc$properties$endpoint, service_type="ComputerVision", aad_token=tok)
expect_is(endp, "cognitive_endpoint")
Sys.sleep(30) # let AAD role assignment complete
img_url <- httr::parse_url(storage)
img_url$path <- "cognitive/bill.jpg"
res <- call_cognitive_endpoint(endp, "analyze", body=list(url=httr::build_url(img_url)), http_verb="POST")

Просмотреть файл

@ -12,7 +12,6 @@ if(tenant == "" || app == "" || password == "" || subscription == "" || storage
skip("Tests skipped: credentials not set")
rgname <- paste0(sample(letters, 10, TRUE), collapse="")
svcname <- paste0(sample(letters, 20, TRUE), collapse="")
az <- AzureRMR::az_rm$new(tenant=tenant, app=app, password=password)
sub <- az$get_subscription(subscription)
@ -21,18 +20,38 @@ rg <- sub$create_resource_group(rgname, location="australiaeast")
test_that("Endpoint cogsvc token authentication works",
{
svcname <- paste0(sample(letters, 20, TRUE), collapse="")
cogsvc <- rg$create_cognitive_service(svcname, service_type="TextTranslation", service_tier="S1", location="global")
# pause for Azure to catch up
Sys.sleep(5)
key <- cogsvc$list_keys()[1]
tok <- get_cognitive_token(key, token_host=cogsvc$properties$endpoint)
tok <- get_cognitive_token(key, cogsvc$location)
endp <- cognitive_endpoint(cogsvc$properties$endpoint, service_type="TextTranslation", cognitive_token=tok)
expect_is(endp, "cognitive_endpoint")
res <- call_cognitive_endpoint(endp, "translate",
options=list(`api-version`="3.0", from="en", to="de"),
body=list(list(Text="Hello world")),
http_verb="POST")
expect_type(res, "list")
})
test_that("Endpoint cogsvc token authentication works with local region",
{
svcname <- paste0(sample(letters, 20, TRUE), collapse="")
cogsvc <- rg$create_cognitive_service(svcname, service_type="TextTranslation", service_tier="S1",
location="australiaeast")
# pause for Azure to catch up
Sys.sleep(5)
key <- cogsvc$list_keys()[1]
tok <- get_cognitive_token(key, cogsvc$location)
endp <- cognitive_endpoint(cogsvc$properties$endpoint, service_type="TextTranslation", cognitive_token=tok)
expect_is(endp, "cognitive_endpoint")
# manual hacking of text translation endpoint
endp$url <- httr::parse_url("https://api.cognitive.microsofttranslator.com")
res <- call_cognitive_endpoint(endp, "translate",
options=list(`api-version`="3.0", from="en", to="de"),
body=list(list(Text="Hello world")),