Skip to content

Commit 3556823

Browse files
pkopereksoodoku
authored andcommitted
Change signature v2 to v4 (#11)
* change signature_v2 -> signature v4 * bump up version * fix imports from signature package * run roxygen2 on the repo * include missing function parameters rerun roxygen2
1 parent 2b0d112 commit 3556823

File tree

5 files changed

+73
-18
lines changed

5 files changed

+73
-18
lines changed

DESCRIPTION

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
Package: aws.alexa
22
Title: Client for the Amazon Alexa Web Information Services API
3-
Version: 0.1.6
3+
Version: 0.1.7
44
Authors@R: c(person("Gaurav", "Sood", email = "[email protected]", role = c("aut", "cre")),
55
person("Thomas", "Leeper", email = "[email protected]", role = "ctb"))
66
Description: Use the Amazon Alexa Web Information Services API to
@@ -22,4 +22,4 @@ VignetteBuilder: knitr
2222
License: MIT + file LICENSE
2323
Encoding: UTF-8
2424
LazyData: true
25-
RoxygenNote: 6.0.1
25+
RoxygenNote: 6.1.0

NAMESPACE

+3-1
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,11 @@ export(in_links)
66
export(set_secret_key)
77
export(traffic_history)
88
export(url_info)
9-
importFrom(aws.signature,signature_v2_auth)
9+
importFrom(aws.signature,locate_credentials)
10+
importFrom(aws.signature,signature_v4_auth)
1011
importFrom(dplyr,bind_rows)
1112
importFrom(httr,GET)
13+
importFrom(httr,add_headers)
1214
importFrom(httr,content)
1315
importFrom(httr,stop_for_status)
1416
importFrom(stats,setNames)

R/aws.alexa.package.R

+55-12
Original file line numberDiff line numberDiff line change
@@ -13,10 +13,10 @@
1313
#' And set these using \code{\link{set_secret_key}}
1414
#'
1515
#' @importFrom stats setNames
16-
#' @importFrom httr GET content stop_for_status
16+
#' @importFrom httr GET content stop_for_status add_headers
1717
#' @importFrom xml2 read_xml as_list
1818
#' @importFrom dplyr bind_rows
19-
#' @importFrom aws.signature signature_v2_auth
19+
#' @importFrom aws.signature signature_v4_auth locate_credentials
2020
#' @docType package
2121
#' @author Gaurav Sood
2222
NULL
@@ -31,27 +31,70 @@ NULL
3131
#' @param query query list
3232
#' @param key A character string containing an AWS Access Key ID. The default is retrieved from \code{Sys.getenv("AWS_ACCESS_KEY_ID")}.
3333
#' @param secret A character string containing an AWS Secret Access Key. The default is retrieved from \code{Sys.getenv("AWS_SECRET_ACCESS_KEY")}.
34+
#' @param verbose A logical indicating whether to be verbose. Default is given by \code{options("verbose")}.
35+
#' @param session_token Optionally, a character string containing an AWS temporary Session Token. If missing, defaults to value stored in environment variable \env{AWS_SESSION_TOKEN}.
36+
#' @param region A character string containing the AWS region. If missing, defaults to \dQuote{us-west-1}.
37+
#' @param headers A list of request headers for the REST call.
3438
#' @param \dots Additional arguments passed to \code{\link[httr]{GET}}.
3539
#' @return list
3640

37-
alexa_GET <- function(query, key = Sys.getenv("AWS_ACCESS_KEY_ID"),
38-
secret = Sys.getenv("AWS_SECRET_ACCESS_KEY"), ...) {
41+
alexa_GET <- function(query,
42+
key = Sys.getenv("AWS_ACCESS_KEY_ID"),
43+
secret = Sys.getenv("AWS_SECRET_ACCESS_KEY"),
44+
verbose = getOption("verbose", FALSE),
45+
session_token = NULL,
46+
region = 'us-west-1',
47+
headers = list(),
48+
...) {
3949

4050
if (identical(key, "") | identical(secret, "")) {
4151
stop("Please set application id and password using set_secret_key(key='key',
4252
secret='secret')).")
4353
}
44-
45-
sig <- signature_v2_auth(datetime = format(Sys.time(),
46-
format = "%Y-%m-%dT%H:%M:%SZ", tz = "UTC"),
54+
55+
# locate and validate credentials
56+
credentials <- locate_credentials(key = key,
57+
secret = secret,
58+
session_token = session_token,
59+
region = region,
60+
verbose = verbose)
61+
62+
key <- credentials[["key"]]
63+
secret <- credentials[["secret"]]
64+
session_token <- credentials[["session_token"]]
65+
region <- credentials[["region"]]
66+
67+
hostname <- paste0("awis.", region, ".amazonaws.com")
68+
current <- Sys.time()
69+
header_timestamp <- format(current, "%Y-%m-%dT%H:%M:%SZ", tz = "UTC")
70+
canonical_headers <- c(list(host = hostname,
71+
`x-amz-date` = header_timestamp), headers)
72+
73+
# generate signatures
74+
Sig <- signature_v4_auth(datetime = format(current,
75+
"%Y%m%dT%H%M%SZ", tz = "UTC"),
76+
region = region,
77+
service = "awis",
4778
verb = "GET",
48-
service = "awis.amazonaws.com",
49-
path = "/",
79+
action = "/api",
5080
query_args = query,
81+
canonical_headers = canonical_headers,
82+
request_body = "",
5183
key = key,
52-
secret = secret)
53-
54-
res <- GET("http://awis.amazonaws.com", query = sig$Query, ...)
84+
secret = secret,
85+
session_token = session_token,
86+
verbose = verbose)
87+
88+
headers[["x-amz-date"]] <- header_timestamp
89+
headers[["x-amz-content-sha256"]] <- Sig$BodyHash
90+
if (!is.null(session_token) && session_token != "") {
91+
headers[["x-amz-security-token"]] <- session_token
92+
}
93+
headers[["Authorization"]] <- Sig[["SignatureHeader"]]
94+
H <- do.call(add_headers, headers)
95+
96+
res <- GET("https://awis.amazonaws.com/api", H, query = query)
97+
5598
alexa_check(res)
5699
res <- as_list(read_xml(content(res, as = "text", encoding = "utf-8")))
57100

man/alexa_GET.Rd

+11-1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

man/category_listing.Rd

+2-2
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)