Class: Amazon::AwsProductSign
- Inherits:
-
Object
- Object
- Amazon::AwsProductSign
- Defined in:
- lib/amazon/aws_product_sign.rb
Instance Method Summary collapse
- #access_key ⇒ Object
- #access_key=(a) ⇒ Object
-
#add_signature(params) ⇒ Object
Pass in a hash representing params for a query string.
-
#add_signature!(params) ⇒ Object
Like #add_signature, but will mutate the hash passed in, adding a “Signature” key/value to hash passed in, and return hash too.
-
#canonical_querystring(params) ⇒ Object
param keys should be strings, not symbols please.
-
#hash_to_query(hash) ⇒ Object
Turns a hash into a query string, returns the query string.
-
#initialize(options = {}) ⇒ AwsProductSign
constructor
A new instance of AwsProductSign.
- #query_with_signature(hash) ⇒ Object
- #secret_key ⇒ Object
- #string_to_sign(query_string, options = {}) ⇒ Object
-
#url_encode(string) ⇒ Object
Insist on specific method of URL encoding, RFC3986.
Constructor Details
#initialize(options = {}) ⇒ AwsProductSign
Returns a new instance of AwsProductSign.
53 54 55 56 57 |
# File 'lib/amazon/aws_product_sign.rb', line 53 def initialize( = {}) @secret_key = [:secret_key] raise Exception.new("You must supply a :secret_key") unless @secret_key @access_key = [:access_key] end |
Instance Method Details
#access_key ⇒ Object
141 142 143 |
# File 'lib/amazon/aws_product_sign.rb', line 141 def access_key return @access_key end |
#access_key=(a) ⇒ Object
144 145 146 |
# File 'lib/amazon/aws_product_sign.rb', line 144 def access_key=(a) @access_key = a end |
#add_signature(params) ⇒ Object
Pass in a hash representing params for a query string.
param keys should be strings, not symbols please. Will return a param with the “Signature” key/value added, without modifying original.
67 68 69 70 |
# File 'lib/amazon/aws_product_sign.rb', line 67 def add_signature(params) # Make a copy to not modify original add_signature!( Hash[params] ) end |
#add_signature!(params) ⇒ Object
Like #add_signature, but will mutate the hash passed in, adding a “Signature” key/value to hash passed in, and return hash too.
75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 |
# File 'lib/amazon/aws_product_sign.rb', line 75 def add_signature!(params) # supply timestamp and access key if not already provided params["Timestamp"] ||= Time.now.iso8601 params["AWSAccessKeyId"] ||= access_key # Existing "Signature"? That's gotta go before we generate a new # signature and add it. params.delete("Signature") query_string = canonical_querystring(params) string_to_sign = string_to_sign(query_string) hmac = HMAC::SHA256.new( secret_key ) hmac.update( string_to_sign ) # chomp is important! the base64 encoded version will have a newline at the end signature = Base64.encode64(hmac.digest).chomp params["Signature"] = signature #order doesn't matter for the actual request, we return the hash #and let client turn it into a url. return params end |
#canonical_querystring(params) ⇒ Object
param keys should be strings, not symbols please. return a string joined by & in canonical order.
109 110 111 112 113 114 |
# File 'lib/amazon/aws_product_sign.rb', line 109 def canonical_querystring(params) # I hope this built-in sort sorts by byte order, that's what's required. values = params.keys.sort.collect {|key| [url_encode(key), url_encode(params[key].to_s)].join("=") } return values.join("&") end |
#hash_to_query(hash) ⇒ Object
Turns a hash into a query string, returns the query string. url-encodes everything to Amazon’s specifications.
130 131 132 133 134 135 136 |
# File 'lib/amazon/aws_product_sign.rb', line 130 def hash_to_query(hash) hash.collect do |key, value| url_encode(key) + "=" + url_encode(value) end.join("&") end |
#query_with_signature(hash) ⇒ Object
59 60 61 |
# File 'lib/amazon/aws_product_sign.rb', line 59 def query_with_signature(hash) return hash_to_query( add_signature(hash) ) end |
#secret_key ⇒ Object
138 139 140 |
# File 'lib/amazon/aws_product_sign.rb', line 138 def secret_key return @secret_key end |
#string_to_sign(query_string, options = {}) ⇒ Object
116 117 118 119 120 121 122 123 124 125 126 |
# File 'lib/amazon/aws_product_sign.rb', line 116 def string_to_sign(query_string, = {}) [:verb] = "GET" [:request_uri] = "/onca/xml" [:host] = "webservices.amazon.com" return [:verb] + "\n" + [:host].downcase + "\n" + [:request_uri] + "\n" + query_string end |
#url_encode(string) ⇒ Object
Insist on specific method of URL encoding, RFC3986.
101 102 103 104 105 |
# File 'lib/amazon/aws_product_sign.rb', line 101 def url_encode(string) # It's kinda like CGI.escape, except CGI.escape is encoding a tilde when # it ought not to be, so we turn it back. Also space NEEDS to be %20 not +. return CGI.escape(string).gsub("%7E", "~").gsub("+", "%20") end |