Sha256: e9a48582ba1cf661cb095d8b2fdd7ccf5be711722d2594ab9b147bd65720342e

Contents?: true

Size: 1.28 KB

Versions: 7

Compression:

Stored size: 1.28 KB

Contents

require 'uri'
require 'openssl'

module AWS
  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

7 entries across 7 versions & 1 rubygems

Version Path
simple_aws-1.0.0 lib/aws/signing/version2.rb
simple_aws-1.0.0.pre3 lib/aws/signing/version2.rb
simple_aws-1.0.0.pre2 lib/aws/signing/version2.rb
simple_aws-1.0.0.pre1 lib/aws/signing/version2.rb
simple_aws-0.0.1d lib/aws/signing/version2.rb
simple_aws-0.0.1c lib/aws/signing/version2.rb
simple_aws-0.0.1b lib/aws/signing/version2.rb