Module: AWS::SigV4

Defined in:
lib/aws/sig_v4.rb

Class Method Summary collapse

Class Method Details

.canonical_request(host, payload, time_stamp) ⇒ Object



8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
# File 'lib/aws/sig_v4.rb', line 8

def self.canonical_request(host, payload, time_stamp)
	headers = {
		'Content-Encoding' => 'amz-1.0',
		'Host' => host,
		'X-Amz-Content-Sha256' => OpenSSL::Digest::SHA256.hexdigest(payload),
		'X-Amz-Date' => time_stamp,
		'X-Amz-Target' => "com.amazon.paapi5.v1.ProductAdvertisingAPIv1.GetItems",
	}
	[
		"POST",
		"/paapi5/getitems",
		'',
		headers.keys.sort.map{|k|"#{k.downcase}:#{headers[k]}"}.join("\n") + "\n",
		headers.keys.sort.map{|k|k.downcase}.join(';'),
		OpenSSL::Digest::SHA256.hexdigest(payload)
	].join("\n")
end

.hmac(key, value) ⇒ Object



41
42
43
# File 'lib/aws/sig_v4.rb', line 41

def self.hmac(key, value)
	OpenSSL::HMAC.digest(OpenSSL::Digest.new('sha256'), key, value)
end

.signature(secret, time_stamp, region, host, payload) ⇒ Object



33
34
35
36
37
38
39
40
# File 'lib/aws/sig_v4.rb', line 33

def self.signature(secret, time_stamp, region, host, payload)
	k_secret = secret
	k_date = hmac("AWS4" + k_secret, time_stamp[0,8])
	k_region = hmac(k_date, region)
	k_service = hmac(k_region, 'ProductAdvertisingAPI')
	k_credential = hmac(k_service, "aws4_request")
	OpenSSL::HMAC.hexdigest(OpenSSL::Digest.new('sha256'), k_credential, string2sign(host, payload, time_stamp))
end

.string2sign(host, payload, time_stamp) ⇒ Object



25
26
27
28
29
30
31
32
# File 'lib/aws/sig_v4.rb', line 25

def self.string2sign(host, payload, time_stamp)
	[
		'AWS4-HMAC-SHA256',
		time_stamp,
		"#{time_stamp[0,8]}/us-west-2/ProductAdvertisingAPI/aws4_request",
		OpenSSL::Digest::SHA256.hexdigest(canonical_request(host, payload, time_stamp))
	].join("\n")
end