Permalink
Browse files

Abstracted the creation of the signup URL so that all we have

to do is pass in the parameters we care about and the library will do
the rest for us.
  • Loading branch information...
1 parent 6542dbd commit 130e7440aba5608d332ada86b81095fae6bc8489 @csaunders csaunders committed May 14, 2012
Showing with 30 additions and 24 deletions.
  1. +17 −20 lib/active_fulfillment/fulfillment/services/amazon_mws.rb
  2. +13 −4 test/unit/services/amazon_mws_test.rb
View
37 lib/active_fulfillment/fulfillment/services/amazon_mws.rb
@@ -9,6 +9,8 @@ class AmazonMarketplaceWebService < Service
APPLICATION_IDENTIFIER = "active_merchant_mws/0.01 (Language=ruby)"
+ REGISTRATION_URI = URI.parse("https://sellercentral.amazon.com/gp/mws/registration/register.html")
+
SIGNATURE_VERSION = 2
SIGNATURE_METHOD = "SHA256"
VERSION = "2010-10-01"
@@ -106,7 +108,6 @@ def self.shipping_methods
def initialize(options = {})
requires!(options, :login, :password)
@seller_id = options[:seller_id]
- options
super
end
@@ -287,32 +288,28 @@ def parse_error(http_response)
def sign(http_verb, uri, options)
string_to_sign = "#{http_verb.to_s.upcase}\n"
string_to_sign += "#{uri.host}\n"
- string_to_sign += uri.path.length <= 0 ? "/\n" : "#{uri.path}"
+ string_to_sign += uri.path.length <= 0 ? "/\n" : "#{uri.path}\n"
string_to_sign += build_query(options)
# remove trailing newline created by encode64
escape(Base64.encode64(OpenSSL::HMAC.digest(SIGNATURE_METHOD, @options[:password], string_to_sign)).chomp)
end
- def amazon_request?(uri, body)
- if @options[:base_url]
- base_url = "#{uri.scheme}://#{@options[:base_url]}"
- else
- base_url = "#{uri.scheme}://#{uri.host}"
- end
- return_path_and_params = uri.to_s.gsub(base_url, '')
-
- signature_match = body.match(/&?Signature=([a-zA-Z0-9\%]+)/)
- body = body.gsub(signature_match[0], '')
- signature = signature_match[1]
-
- string_to_sign = "POST\n"
- string_to_sign += "#{base_url}\n"
- string_to_sign += "#{return_path_and_params}\n"
- string_to_sign += body
+ def amazon_request?(signed_string, expected_signature)
+ calculated_signature = escape(Base64.encode64(OpenSSL::HMAC.digest(SIGNATURE_METHOD, @options[:password], signed_string)).chomp)
+ calculated_signature == expected_signature
+ end
- calculated_signature = escape(Base64.encode64(OpenSSL::HMAC.digest(SIGNATURE_METHOD, @options[:password], string_to_sign)).chomp)
- calculated_signature == signature
+ def registration_url(options)
+ opts = {
+ "returnPathAndParameters" => options["returnPathAndParameters"],
+ "id" => @options[:app_id],
+ "AWSAccessKeyId" => @options[:login],
+ "SignatureMethod" => "Hmac#{SIGNATURE_METHOD}",
+ "SignatureVersion" => SIGNATURE_VERSION
+ }
+ signature = sign(:get, REGISTRATION_URI, opts)
+ "#{REGISTRATION_URI.to_s}?#{build_query(opts)}&Signature=#{signature}"
end
def md5_content(content)
View
17 test/unit/services/amazon_mws_test.rb
@@ -89,10 +89,10 @@ def test_create_signature
end
def test_verify_amazon_response
- service = AmazonMarketplaceWebService.new(:login => "AKIAFJPPO5KLY6G4XO7Q", :password => "aaa", :base_url => "www.vendor.com/mwsApp1")
- callback_url = URI.parse("https://www.vendor.com/mwsApp1/orders/listRecentOrders.jsp?sessionId=123")
- response_body = "AWSAccessKeyId=AKIAFJPPO5KLY6G4XO7Q&Marketplace=ATVPDKIKX0DER&Merchant=A047950713KM6AGKQCBRD&SignatureMethod=HmacSHA256&SignatureVersion=2&Signature=b0hxWov1RfBOqNk77UDfNRRZmf3tkdM7vuNa%2FolfnWg%3D"
- assert service.amazon_request?(callback_url, response_body)
+ service = AmazonMarketplaceWebService.new(:login => "AKIAFJPPO5KLY6G4XO7Q", :password => "aaa")
+ string_signed_by_amazon = "POST\nhttps://www.vendor.com/mwsApp1\n/orders/listRecentOrders.jsp?sessionId=123"
+ string_signed_by_amazon += "\nAWSAccessKeyId=AKIAFJPPO5KLY6G4XO7Q&Marketplace=ATVPDKIKX0DER&Merchant=A047950713KM6AGKQCBRD&SignatureMethod=HmacSHA256&SignatureVersion=2"
+ assert service.amazon_request?(string_signed_by_amazon, "b0hxWov1RfBOqNk77UDfNRRZmf3tkdM7vuNa%2FolfnWg%3D")
end
def test_build_address
@@ -108,6 +108,15 @@ def test_build_address
assert_equal expected_items, @service.build_address(@address)
end
+ def test_integrated_registration_url_creation
+ service = AmazonMarketplaceWebService.new(:login => "AKIAFJPPO5KLY6G4XO7Q", :password => "aaa", :app_id => "1014f5ad-c359-4e86-8e50-bb8f8e431a9")
+ options = {
+ "returnPathAndParameters" => "/orders/listRecentOrders.jsp?sessionId=123"
+ }
+ expected_registration_url = "#{AmazonMarketplaceWebService::REGISTRATION_URI.to_s}?AWSAccessKeyId=AKIAFJPPO5KLY6G4XO7Q&SignatureMethod=HmacSHA256&SignatureVersion=2&id=1014f5ad-c359-4e86-8e50-bb8f8e431a9&returnPathAndParameters=%2Forders%2FlistRecentOrders.jsp%3FsessionId%3D123&Signature=zpZyHd8rMf5gg5rpO5ri5RGUi0kks03ZkhAtPm4npVk%3D"
+ assert_equal expected_registration_url, service.registration_url(options)
+ end
+
def test_build_items
expected_items = {
"Items.member.1.DisplayableComment" => "Awesome",

0 comments on commit 130e744

Please sign in to comment.