Class: MessageMediaMessages::HmacAuth
- Inherits:
-
Object
- Object
- MessageMediaMessages::HmacAuth
- Defined in:
- lib/message_media_lookups/http/auth/hmac_auth.rb
Overview
Utility class for basic authorization.
Class Method Summary collapse
-
.apply(http_request, url = nil, body = nil) ⇒ Object
Add basic authentication to the request.
- .create_signature(date, content_signature, url, body = nil) ⇒ Object
Class Method Details
.apply(http_request, url = nil, body = nil) ⇒ Object
Add basic authentication to the request. be added.
23 24 25 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 |
# File 'lib/message_media_lookups/http/auth/hmac_auth.rb', line 23 def self.apply(http_request, url=nil, body=nil) username = Configuration.hmac_auth_user_name content_signature = "" content_header = "" now = DateTime.now.new_offset(0).to_time.strftime("%a, %d %b %Y %H:%M:%S GMT") date_header = now if body != nil md5 = Digest::MD5.new md5.update(body) content_hash = md5.hexdigest content_signature = "x-Content-MD5: #{content_hash}\n" content_header = "x-Content-MD5 " http_request.headers["x-Content-MD5"] = content_hash end http_request.headers["date"] = date_header hmac_signature = HmacAuth.create_signature(date_header, content_signature, url, body) joined = "username=\"#{username}\", algorithm=\"hmac-sha1\", headers=\"date #{content_header}request-line\", signature=\"#{hmac_signature}\"" header_value = "hmac #{joined}" http_request.headers["Authorization"] = header_value end |
.create_signature(date, content_signature, url, body = nil) ⇒ Object
7 8 9 10 11 12 13 14 15 16 17 18 |
# File 'lib/message_media_lookups/http/auth/hmac_auth.rb', line 7 def self.create_signature(date, content_signature, url, body=nil) request_type = "GET" if body != nil request_type = "POST" end signing_string = "date: #{date}\n#{content_signature}#{request_type} #{url} HTTP/1.1" hmac = OpenSSL::HMAC.digest(OpenSSL::Digest::Digest.new('sha1'), Configuration.hmac_auth_password.encode("ASCII"), signing_string.encode("ASCII")) return Base64.encode64(hmac).chomp end |