Class: Amazon::MWS::Authentication::Signature

Inherits:
String show all
Extended by:
Memoizable
Defined in:
lib/amazon/mws/authentication/signature.rb

Overview

:nodoc:

Constant Summary collapse

VERSION =
'2'
METHOD =
'HmacSHA256'

Instance Method Summary collapse

Methods included from Memoizable

memoize

Methods inherited from String

#camelize, #to_boolean, #underscore, #valid_utf8?

Constructor Details

#initialize(queryparams = {}, params = {}) ⇒ Signature

Returns a new instance of Signature.



9
10
11
12
13
14
15
# File 'lib/amazon/mws/authentication/signature.rb', line 9

def initialize(queryparams = {}, params = {})
  verb   = params[:verb]
  secret = params[:secret_access_key]
  # Create the string to sign
  string = string_to_sign(verb, canonical_querystring(queryparams))
  self << sign(string, secret)
end

Instance Method Details

#canonical_querystring(params) ⇒ Object



34
35
36
37
38
# File 'lib/amazon/mws/authentication/signature.rb', line 34

def canonical_querystring(params)
  # Make sure we have string keys, otherwise the sort does not work
  params = Hash.keys_to_s(params)
  params.sort.collect { |key, value| [CGI.escape(key.to_s), CGI.escape(value.to_s)].join('=') }.join('&')
end

#sign(string, secret_access_key) ⇒ Object

Returns a signed string



18
19
20
21
22
23
# File 'lib/amazon/mws/authentication/signature.rb', line 18

def sign(string, secret_access_key)
  hmac = HMAC::SHA256.new(secret_access_key)
  hmac.update(string)
  # chomp is important!  the base64 encoded version will have a newline at the end
  Base64.encode64(hmac.digest).chomp
end

#string_to_sign(verb, querystring) ⇒ Object



27
28
29
30
# File 'lib/amazon/mws/authentication/signature.rb', line 27

def string_to_sign(verb, querystring)
  verb   = verb.to_s.upcase
  string = "#{verb}\n#{Amazon::MWS::DEFAULT_HOST}\n/\n#{querystring}"
end