Class: Stree::Signature

Inherits:
Object
  • Object
show all
Defined in:
lib/stree/signature.rb

Overview

Class responsible for generating signatures to requests.

Implements algorithm defined by Amazon Web Services to sign request with secret private credentials

See:

docs.amazonwebservices.com/AmazonS3/latest/index.html?RESTAuthentication.html

Class Method Summary collapse

Class Method Details

.generate(options) ⇒ Object

Generates signature for given parameters

Parameters:

options: a hash that contains options listed below

Options:

host: hostname request: Net::HTTPRequest object with correct headers access_key_id: access key id secret_access_key: secret access key

Returns:

Generated signature for given hostname and request



26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
# File 'lib/stree/signature.rb', line 26

def self.generate(options)
  request = options[:request]
  host = options[:host]
  access_key_id = options[:access_key_id]
  secret_access_key = options[:secret_access_key]

  http_verb = request.method
  content_md5 = request["content-md5"] || ""
  content_type = request["content-type"] || ""
  date = request["x-amz-date"].nil? ? request["date"] : ""
  canonicalized_resource = canonicalized_resource(host, request)
  canonicalized_amz_headers = canonicalized_amz_headers(request)

  string_to_sign = ""
  string_to_sign << http_verb
  string_to_sign << "\n"
  string_to_sign << content_md5
  string_to_sign << "\n"
  string_to_sign << content_type
  string_to_sign << "\n"
  string_to_sign << date
  string_to_sign << "\n"
  string_to_sign << canonicalized_amz_headers
  string_to_sign << canonicalized_resource

  digest = OpenSSL::Digest::Digest.new('sha1')
  hmac = OpenSSL::HMAC.digest(digest, secret_access_key, string_to_sign)
  base64 = Base64.encode64(hmac)
  signature = base64.chomp

  "AWS #{access_key_id}:#{signature}"
end