Module: Awis::Utils::Request
- Included in:
- Connection
- Defined in:
- lib/awis/utils/request.rb
Instance Method Summary collapse
- #algorithm ⇒ Object
- #authorization_header ⇒ Object
- #aws4 ⇒ Object
- #aws4_request ⇒ Object
- #canonical_request ⇒ Object
- #credential_scope ⇒ Object
- #datestamp ⇒ Object
- #digest_sha256_hexdigest(key) ⇒ Object
- #encryption_method ⇒ Object
-
#escapeRFC3986(str) ⇒ Object
escape str to RFC 3986.
- #handle_response(response) ⇒ Object
- #headers ⇒ Object
- #headers_lst ⇒ Object
- #headers_str ⇒ Object
- #openssl_hmac_digest(method, key, secret) ⇒ Object
- #payload_hash ⇒ Object
- #query_str ⇒ Object
- #request ⇒ Object
- #signature ⇒ Object
- #signature_key(key, date_stamp, region_name, service_name) ⇒ Object
- #signing_key ⇒ Object
- #string_to_sign ⇒ Object
- #timestamp ⇒ Object
- #uri ⇒ Object
- #url ⇒ Object
- #url_params ⇒ Object
Instance Method Details
#algorithm ⇒ Object
60 61 62 |
# File 'lib/awis/utils/request.rb', line 60 def algorithm "AWS4-HMAC-SHA256" end |
#authorization_header ⇒ Object
80 81 82 83 84 85 |
# File 'lib/awis/utils/request.rb', line 80 def algorithm + " " + "Credential=" + Awis.config.access_key_id + "/" + credential_scope + ", " + "SignedHeaders=" + headers_lst + ", " + "Signature=" + signature; end |
#aws4 ⇒ Object
124 125 126 |
# File 'lib/awis/utils/request.rb', line 124 def aws4 "AWS4" end |
#aws4_request ⇒ Object
128 129 130 |
# File 'lib/awis/utils/request.rb', line 128 def aws4_request "aws4_request" end |
#canonical_request ⇒ Object
56 57 58 |
# File 'lib/awis/utils/request.rb', line 56 def canonical_request "GET" + "\n" + Awis::SERVICE_URI + "\n" + query_str + "\n" + headers_str + "\n" + headers_lst + "\n" + payload_hash end |
#credential_scope ⇒ Object
64 65 66 |
# File 'lib/awis/utils/request.rb', line 64 def credential_scope datestamp + "/" + Awis::SERVICE_REGION + "/" + Awis::SERVICE_NAME + "/" + aws4_request end |
#datestamp ⇒ Object
29 30 31 |
# File 'lib/awis/utils/request.rb', line 29 def datestamp Time::now.utc.strftime("%Y%m%d") end |
#digest_sha256_hexdigest(key) ⇒ Object
148 149 150 |
# File 'lib/awis/utils/request.rb', line 148 def digest_sha256_hexdigest(key) Digest::SHA256.hexdigest key end |
#encryption_method ⇒ Object
132 133 134 |
# File 'lib/awis/utils/request.rb', line 132 def encryption_method 'sha256' end |
#escapeRFC3986(str) ⇒ Object
escape str to RFC 3986
21 22 23 |
# File 'lib/awis/utils/request.rb', line 21 def escapeRFC3986(str) URI.escape(str, /[^A-Za-z0-9\-_.~]/) end |
#handle_response(response) ⇒ Object
108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 |
# File 'lib/awis/utils/request.rb', line 108 def handle_response(response) case response.code.to_i when 200...300 response when 300...600 if response.body.nil? raise ResponseError.new(nil, response) else = MultiXml.parse(response.body).deep_find('ErrorCode') raise ResponseError.new(, response) end else raise ResponseError.new("Unknown code: #{respnse.code}", response) end end |
#headers ⇒ Object
33 34 35 36 37 38 |
# File 'lib/awis/utils/request.rb', line 33 def headers { "host" => Awis::SERVICE_ENDPOINT, "x-amz-date" => } end |
#headers_lst ⇒ Object
48 49 50 |
# File 'lib/awis/utils/request.rb', line 48 def headers_lst headers.sort.map{|k,v| k}.join(";") end |
#headers_str ⇒ Object
44 45 46 |
# File 'lib/awis/utils/request.rb', line 44 def headers_str headers.sort.map{|k,v| k + ":" + v}.join("\n") + "\n" end |
#openssl_hmac_digest(method, key, secret) ⇒ Object
144 145 146 |
# File 'lib/awis/utils/request.rb', line 144 def openssl_hmac_digest(method, key, secret) OpenSSL::HMAC.digest(method, key, secret) end |
#payload_hash ⇒ Object
52 53 54 |
# File 'lib/awis/utils/request.rb', line 52 def payload_hash digest_sha256_hexdigest "" end |
#query_str ⇒ Object
40 41 42 |
# File 'lib/awis/utils/request.rb', line 40 def query_str params.sort.map{|k,v| k + "=" + escapeRFC3986(v.to_s())}.join('&') end |
#request ⇒ Object
91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 |
# File 'lib/awis/utils/request.rb', line 91 def request req = Net::HTTP::Get.new(uri.to_s) req["Accept"] = "application/xml" req["Content-Type"] = "application/xml" req["x-amz-date"] = req["Authorization"] = response = Net::HTTP.start(uri.host, uri.port, use_ssl: uri.scheme == 'https', ssl_timeout: @timeout, open_timeout: @open_timeout) { |http| http.request(req) } response end |
#signature ⇒ Object
76 77 78 |
# File 'lib/awis/utils/request.rb', line 76 def signature OpenSSL::HMAC.hexdigest(encryption_method, signing_key, string_to_sign) end |
#signature_key(key, date_stamp, region_name, service_name) ⇒ Object
12 13 14 15 16 17 18 |
# File 'lib/awis/utils/request.rb', line 12 def signature_key(key, date_stamp, region_name, service_name) kDate = openssl_hmac_digest(encryption_method, aws4 + key, date_stamp) kRegion = openssl_hmac_digest(encryption_method, kDate, region_name) kService = openssl_hmac_digest(encryption_method, kRegion, service_name) kSigning = openssl_hmac_digest(encryption_method, kService, aws4_request) kSigning end |
#signing_key ⇒ Object
72 73 74 |
# File 'lib/awis/utils/request.rb', line 72 def signing_key signature_key(Awis.config.secret_access_key, datestamp, Awis::SERVICE_REGION, Awis::SERVICE_NAME) end |
#string_to_sign ⇒ Object
68 69 70 |
# File 'lib/awis/utils/request.rb', line 68 def string_to_sign algorithm + "\n" + + "\n" + credential_scope + "\n" + digest_sha256_hexdigest(canonical_request) end |
#timestamp ⇒ Object
25 26 27 |
# File 'lib/awis/utils/request.rb', line 25 def Time::now.utc.strftime("%Y%m%dT%H%M%SZ") end |
#uri ⇒ Object
87 88 89 |
# File 'lib/awis/utils/request.rb', line 87 def uri URI(url + url_params) end |
#url ⇒ Object
136 137 138 |
# File 'lib/awis/utils/request.rb', line 136 def url protocol + '://' + Awis::SERVICE_HOST + Awis::SERVICE_URI end |
#url_params ⇒ Object
140 141 142 |
# File 'lib/awis/utils/request.rb', line 140 def url_params '?' + query_str end |