Module: UberS3::Authorization

Extended by:
Authorization
Included in:
Authorization
Defined in:
lib/uber-s3/authorization.rb

Instance Method Summary collapse

Instance Method Details

#sign(client, verb, path, headers = {}) ⇒ Object



4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
# File 'lib/uber-s3/authorization.rb', line 4

def sign(client, verb, path, headers={})
  req_verb                  = verb.to_s.upcase
  req_content_md5           = headers['Content-MD5']
  req_content_type          = headers['Content-Type']
  req_date                  = headers['Date']
  req_canonical_resource    = "/#{client.bucket}/#{path}".split('?').first
  req_canonical_amz_headers = ''

  headers.keys.sort.select {|k| k =~ /^x-amz-/ }.each do |amz_key|
    req_canonical_amz_headers << amz_key+':'+headers[amz_key]+"\n"
  end

  canonical_string_to_sign = "#{req_verb}\n"+
                             "#{req_content_md5}\n"+
                             "#{req_content_type}\n"+
                             "#{req_date}\n"+
                             "#{req_canonical_amz_headers}"+
                             "#{req_canonical_resource}"
  
  digest = OpenSSL::Digest::Digest.new('sha1')
  [OpenSSL::HMAC.digest(digest, client.connection.secret_access_key, canonical_string_to_sign)].pack("m").strip
end