Sha256: 8053e47778f0536657be118d0fb6b407c7e6f05bcf99fce560523fa2f1359f4c

Contents?: true

Size: 1.29 KB

Versions: 5

Compression:

Stored size: 1.29 KB

Contents

require 'uri'
require 'openssl'

module SimpleAWS
  module Signing
    ##
    # Implementation of "Signature Version 2" signing
    ##
    module Version2

      ##
      # Build and sign the final request, as per the rules here:
      # http://docs.amazonwebservices.com/AWSEC2/latest/UserGuide/index.html?using-query-api.html
      ##
      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

      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

    end
  end
end

Version data entries

5 entries across 5 versions & 1 rubygems

Version Path
simple_aws-1.2.3 lib/simple_aws/signing/version2.rb
simple_aws-1.2.2 lib/simple_aws/signing/version2.rb
simple_aws-1.2.1 lib/simple_aws/signing/version2.rb
simple_aws-1.2.0 lib/simple_aws/signing/version2.rb
simple_aws-1.1.0 lib/simple_aws/signing/version2.rb