module AWS

Public Class Methods

canonical_string(params, host, method="POST", base="/") click to toggle source

Builds the canonical string for signing requests. This strips out all '&', '?', and '=' from the query string to be signed. The parameters in the path passed in must already be sorted in case-insensitive alphabetical order and must not be url encoded.

@param [String] params the params that will be sorted and encoded as a canonical string. @param [String] host the hostname of the API endpoint. @param [String] method the HTTP method that will be used to submit the params. @param [String] base the URI path that this information will be submitted to. @return [String] the canonical request description string.

# File lib/AWS.rb, line 63
def AWS.canonical_string(params, host, method="POST", base="/")
  # Sort, and encode parameters into a canonical string.
  sorted_params = params.sort {|x,y| x[0] <=> y[0]}
  encoded_params = sorted_params.collect do |p|
    encoded = (CGI::escape(p[0].to_s) +
               "=" + CGI::escape(p[1].to_s))
    # Ensure spaces are encoded as '%20', not '+'
    encoded = encoded.gsub('+', '%20')
    # According to RFC3986 (the scheme for values expected by signing requests), '~' 
    # should not be encoded
    encoded = encoded.gsub('%7E', '~')
  end
  sigquery = encoded_params.join("&")

  # Generate the request description string
  req_desc =
    method + "\n" +
    host + "\n" +
    base + "\n" +
    sigquery

end
encode(secret_access_key, str, urlencode=true) click to toggle source

Encodes the given string with the secret_access_key by taking the hmac-sha1 sum, and then base64 encoding it. Optionally, it will also url encode the result of that to protect the string if it's going to be used as a query string parameter.

@param [String] secret_access_key the user's secret access key for signing. @param [String] str the string to be hashed and encoded. @param [Boolean] urlencode whether or not to url encode the result., true or false @return [String] the signed and encoded string.

# File lib/AWS.rb, line 95
def AWS.encode(secret_access_key, str, urlencode=true)
  digest = OpenSSL::Digest::Digest.new('sha1')
  b64_hmac =
    Base64.encode64(
      OpenSSL::HMAC.digest(digest, secret_access_key, str)).gsub("\n","")

  if urlencode
    return CGI::escape(b64_hmac)
  else
    return b64_hmac
  end
end