Class: Cognac::Signature

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

Overview

Constant Summary collapse

NEW_LINE =
"\n"
SECURE_HASH_ALGORITHM =
"sha1"
BLANK_STRING =
""
UTF8_ENCODING =
"UTF-8"
HTTP_PUT =
"PUT"

Instance Method Summary collapse

Constructor Details

#initialize(options = {}) ⇒ Signature

Returns a new instance of Signature.



40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
# File 'lib/cognac/signature.rb', line 40

def initialize(options = {})
  if Cognac.secret_access_key.nil?
    raise 'AWS Secret access key is not initialized. Refer README.md at https://bitbucket.org/bparanj/cognac'
  end

  if Cognac.aws_access_key.nil?
    raise 'AWS access key is not initialized. Refer README.md at https://bitbucket.org/bparanj/cognac'
  end
  
  @secret_access_key = Cognac.secret_access_key
  @aws_access_key = Cognac.aws_access_key
  @options = options
  @default = {
    http_verb:     HTTP_PUT,
    content_md5:   nil,
    content_type:  nil,
    date:          an_hour_from_now,
    amz_headers:   [],
    resource:      BLANK_STRING
  }
end

Instance Method Details

#base_encoded_hmac_digest(string_to_sign) ⇒ Object



87
88
89
# File 'lib/cognac/signature.rb', line 87

def base_encoded_hmac_digest(string_to_sign)
  Base64.encode64(OpenSSL::HMAC.digest(SECURE_HASH_ALGORITHM, @secret_access_key, string_to_sign)).strip
end

#build_s3_rest_signatureObject



71
72
73
74
# File 'lib/cognac/signature.rb', line 71

def build_s3_rest_signature
   string_to_sign = build_string_to_sign(@options).force_encoding(UTF8_ENCODING)
   base_encoded_hmac_digest(string_to_sign)
end

#build_s3_upload_url(end_point) ⇒ Object

This is called in the controller



63
64
65
# File 'lib/cognac/signature.rb', line 63

def build_s3_upload_url(end_point)
  "#{end_point}?AWSAccessKeyId=#{@aws_access_key}&Expires=#{expiration}&Signature=#{encoded_signature}"     
end

#build_string_to_sign(options = {}) ⇒ Object



76
77
78
79
80
81
82
83
84
85
# File 'lib/cognac/signature.rb', line 76

def build_string_to_sign(options = {})
   o = @default.merge(options)

   add_new_line(o[:http_verb]) +
   add_new_line(o[:content_md5]) +
   add_new_line(o[:content_type]) +
   add_new_line(o[:date]) +
   (o[:amz_headers].any? ? (o[:amz_headers].join(NEW_LINE) + NEW_LINE) : BLANK_STRING) +
   o[:resource]
end

#encoded_signatureObject



67
68
69
# File 'lib/cognac/signature.rb', line 67

def encoded_signature
  ERB::Util.url_encode(build_s3_rest_signature)
end

#generate_options_for_build_s3_upload_url(file_name, content_type) ⇒ Object

This is called in the controller and passed in to the build_s3_upload_url method as a parameter Hide this method



93
94
95
96
97
98
99
100
101
102
103
# File 'lib/cognac/signature.rb', line 93

def generate_options_for_build_s3_upload_url(file_name, content_type)
  if Cognac.aws_s3_bucket.nil?
    raise 'AWS S3 Bucket is not initialized. Refer README.md at https://bitbucket.org/bparanj/cognac' 
  end
  {
     http_verb:    HTTP_PUT, 
     date:         1.hours.from_now.to_i, 
     resource:     "/#{Cognac.aws_s3_bucket}/#{file_name}",
     content_type: content_type
  }
end