Sha256: e18d6c8ad6bb1c4574b9eff5d3d46c17734b785f43922c7d24d0265dc6e7f52e

Contents?: true

Size: 1.79 KB

Versions: 5

Compression:

Stored size: 1.79 KB

Contents

module Vacuum
  module Request
    module Signature
      # Internal: Signs a request to an Amazon API with an HMAC-SHA256
      # signature.
      class Builder
        # Returns a Hash that contains info about the request.
        attr :env

        # Returns the String Amazon AWS access secret key.
        attr :secret

        # Initializes a new Builder.
        #
        # env - A Hash that contains info about the request.
        def initialize(env, secret)
          env[:url] = Addressable::URI.parse env[:url]
          @env, @secret = env, secret
        end

        # Returns the String name of the HTTP method used by the request.
        def method
          env[:method].to_s.upcase
        end

        # Signs the request.
        #
        # Returns self.
        def sign
          url.query = url.query.to_s + "&Signature=#{Utils.encode signature}"
          self
        end

        # Returns a String signature.
        def signature
          sha256 = OpenSSL::Digest::SHA256.new
          hash = OpenSSL::HMAC.digest sha256, secret, string_to_sign

          Base64.encode64(hash).chomp
        end

        # Sorts the URL query values of the request.
        #
        # Returns self.
        def sort_query
          url.query_values = url.query_values
          self
        end

        # Returns a String to sign based on pseudo-grammar specified by Amazon.
        def string_to_sign
          [method, url.host, url.path, url.query].join "\n"
        end

        # Timestamps the request.
        #
        # Returns self.
        def timestamp
          url.query = url.query.to_s + "&Timestamp=#{Utils.encode Time.now.utc.iso8601}"
          self
        end

        # Returns the Addressable::URI URL of the request.
        def url
          env[:url]
        end
      end
    end
  end
end

Version data entries

5 entries across 5 versions & 1 rubygems

Version Path
vacuum-0.2.2 lib/vacuum/request/signature/builder.rb
vacuum-0.2.1 lib/vacuum/request/signature/builder.rb
vacuum-0.2.0 lib/vacuum/request/signature/builder.rb
vacuum-0.2.0.pre.1 lib/vacuum/request/signature/builder.rb
vacuum-0.2.0.pre lib/vacuum/request/signature/builder.rb