Sha256: e0cb81f4b1f25d4e1af43a2f8872a1cb7cc6937654ff3fee76f0d4e49fb99938

Contents?: true

Size: 1.21 KB

Versions: 1

Compression:

Stored size: 1.21 KB

Contents

class Amazon::MWS::Authentication

  class Signature < String#:nodoc:
    extend Memoizable

    VERSION = '2'
    METHOD  = 'HmacSHA256'

    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), params[:server], params[:path])
      self << sign(string, secret)
    end

    # Returns a signed string
    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

    memoize :sign

    def string_to_sign(verb, querystring, server, path)
      verb   = verb.to_s.upcase
      string = "#{verb}\n#{server}\n#{path}\n#{querystring}"
    end

    memoize :string_to_sign

    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

    memoize :canonical_querystring
  end

end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
amazon-mws-plus-0.1.3 lib/amazon/mws/authentication/signature.rb