Class: S3Lib::AuthenticatedRequest

Inherits:
Object
  • Object
show all
Defined in:
lib/s3_authenticator.rb,
lib/put_with_curl_test.rb,
lib/s3_authenticator_dev.rb,
lib/s3_authenticator_dev_private.rb

Constant Summary collapse

POSITIONAL_HEADERS =
['content-md5', 'content-type', 'date']
AMAZON_HEADER_PREFIX =
'x-amz-'
HOST =
's3.amazonaws.com'
BUCKET_LIST_PARAMS =
[:max_keys, :prefix, :marker, :delimiter]
SUB_RESOURCE_TYPES =
['acl', 'torrent', 'logging']

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#headersObject (readonly)

Returns the value of attribute headers.



35
36
37
# File 'lib/s3_authenticator_dev.rb', line 35

def headers
  @headers
end

Instance Method Details

#authorization_stringObject



108
109
110
111
112
113
# File 'lib/s3_authenticator.rb', line 108

def authorization_string
  generator = OpenSSL::Digest::Digest.new('sha1')
  encoded_canonical = Base64.encode64(OpenSSL::HMAC.digest(generator, @amazon_secret, canonical_string)).strip

  "AWS #{@amazon_id}:#{encoded_canonical}"
end

#canonicalized_amazon_headersObject



125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
# File 'lib/s3_authenticator.rb', line 125

def canonicalized_amazon_headers

  # select all headers that start with x-amz-
  amazon_headers = @headers.select do |header, value|
    header =~ /^x-amz-/
  end

  # Sort them alpabetically by key
  amazon_headers = amazon_headers.sort do |a, b|
    a[0] <=> b[0]
  end

  # Collect all of the amazon headers like this:
  # {key}:{value}\n
  # The value has to have any whitespace on the left stripped from it 
  # and any new-lines replaced by a single space.
  # Finally, return the headers joined together as a single string and return it.
  amazon_headers.collect do |header, value|
    "#{header}:#{value.lstrip.gsub("\n"," ")}\n"
  end.join
end

#canonicalized_resourceObject



147
148
149
150
151
152
# File 'lib/s3_authenticator.rb', line 147

def canonicalized_resource
  canonicalized_resource_string = "/"
  canonicalized_resource_string += @bucket
  canonicalized_resource_string += @request_path  
  canonicalized_resource_string  
end

#get_bucket_list_paramsObject



91
92
93
94
95
96
# File 'lib/s3_authenticator.rb', line 91

def get_bucket_list_params
  @bucket_list_params = {}
  @headers.each do |key, value|
    @bucket_list_params[key] = @headers.delete(key) if BUCKET_LIST_PARAMS.include?(key)
  end
end

#get_bucket_nameObject



69
70
71
72
73
74
75
76
77
78
79
# File 'lib/s3_authenticator.rb', line 69

def get_bucket_name
  @bucket = ""
  return unless @headers.has_key?('host')
  @headers['host'] = @headers['host'].downcase
  return if @headers['host'] == 's3.amazonaws.com'
  if @headers['host'] =~ /^([^.]+)(:\d\d\d\d)?\.#{HOST}$/
    @bucket = $1.gsub(/\/$/,'') + '/'
  else
    @bucket = @headers['host'].gsub(/(:\d\d\d\d)$/, '').gsub(/\/$/,'') + '/'
  end    
end

#make_authenticated_request(verb, request_path, headers = {}) ⇒ Object



50
51
52
53
54
55
56
57
58
59
60
61
62
63
# File 'lib/s3_authenticator.rb', line 50

def make_authenticated_request(verb, request_path, headers = {})
  @verb = verb
  @request_path = request_path.gsub(/^\//,'') # Strip off the leading '/'

  @amazon_id = ENV['AMAZON_ACCESS_KEY_ID']
  @amazon_secret = ENV['AMAZON_SECRET_ACCESS_KEY']    

  @headers = headers.downcase_keys.join_values    
  get_bucket_list_params          
  get_bucket_name
  fix_date
  
  req = open(uri_with_bucket_list_params, @headers.merge(:method => @verb, 'Authorization' => authorization_string))
end

#public_authorization_stringObject



8
9
10
# File 'lib/put_with_curl_test.rb', line 8

def public_authorization_string
  authorization_string
end

#uriObject



86
87
88
89
# File 'lib/s3_authenticator.rb', line 86

def uri
  host = @headers['host'] || HOST
  "http://" + File.join(host, URI.escape(@request_path))
end

#uri_with_bucket_list_paramsObject



98
99
100
101
102
103
104
105
106
# File 'lib/s3_authenticator.rb', line 98

def uri_with_bucket_list_params
  return uri if @bucket_list_params.empty?
  uri_with_params = uri
  bucket_list_string = @bucket_list_params.collect {|key, value| "#{key.to_s.gsub('_', '-')}=#{value}"}.join('&')
  uri_with_params.sub(/\/$/, '') # remove trailing slash
  uri_with_params += '?' unless uri =~ /\?$/ # Add trailing ?
  uri_with_params += bucket_list_string # add bucket list params
  uri_with_params
end