Module: SimpleAWS::Signing::Version2

Overview

Implementation of "Signature Version 2" signing

Instance Method Summary collapse

Instance Method Details

#finish_and_sign_request(request) ⇒ Object



15
16
17
18
19
20
21
22
23
24
25
26
27
# File 'lib/simple_aws/signing/version2.rb', line 15

def finish_and_sign_request(request)
  request.params.merge!({
    "AWSAccessKeyId" => self.access_key,
    "SignatureMethod" => "HmacSHA256",
    "SignatureVersion" => "2",
    "Timestamp" => Time.now.utc.strftime("%Y-%m-%dT%H:%M:%SZ"),
    "Version" => self.version
  })

  request.params["Signature"] = Base64.encode64(sign_request(request)).chomp

  request
end

#sign_request(request) ⇒ Object



29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
# File 'lib/simple_aws/signing/version2.rb', line 29

def sign_request(request)
  signing_params = request.params.clone

  list = signing_params.map {|k, v| [k, Util.uri_escape(v.to_s)] }
  list.sort! do |a, b|
    if a[0] == "AWSAccessKeyId"
      -1
    else
      a[0] <=> b[0]
    end
  end

  plain_host = URI.parse(request.host).host

  to_sign = "POST\n#{plain_host}\n#{request.path}\n#{list.map {|p| p.join("=") }.join("&")}"
  OpenSSL::HMAC.digest("sha256", self.secret_key, to_sign)
end