Class: MessageMediaMessages::AuthManager

Inherits:
Object
  • Object
show all
Defined in:
lib/message_media_messages/http/auth/auth_manager.rb

Overview

Utility class for basic authorization.

Class Method Summary collapse

Class Method Details

.apply(http_request, url, body = nil) ⇒ Object

Add authentication to the request. be added.

Parameters:

  • The (HttpRequest)

    HttpRequest object to which authentication will

  • The (String)

    url of the request.

  • The (String)

    body of the request. None for GET requests.



16
17
18
19
20
21
22
23
# File 'lib/message_media_messages/http/auth/auth_manager.rb', line 16

def self.apply(http_request, url, body = nil)
  if Configuration.hmac_auth_user_name.nil? ||
     Configuration.hmac_auth_password.nil?
    AuthManager.apply_basic_auth(http_request)
  else
    AuthManager.apply_hmac_auth(http_request, url, body)
  end
end

.apply_basic_auth(http_request) ⇒ Object

Add basic authentication to the request. be added.

Parameters:

  • The (HttpRequest)

    HttpRequest object to which authentication will



28
29
30
31
32
33
34
# File 'lib/message_media_messages/http/auth/auth_manager.rb', line 28

def self.apply_basic_auth(http_request)
  username = Configuration.basic_auth_user_name
  password = Configuration.basic_auth_password
  value = Base64.strict_encode64("#{username}:#{password}")
  header_value = "Basic #{value}"
  http_request.headers['Authorization'] = header_value
end

.apply_hmac_auth(http_request, url, body) ⇒ Object

Add hmac authentication to the request. be added.

Parameters:

  • The (HttpRequest)

    HttpRequest object to which authentication will

  • The (String)

    url of the request.

  • The (String)

    body of the request. None for GET requests.



41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
# File 'lib/message_media_messages/http/auth/auth_manager.rb', line 41

def self.apply_hmac_auth(http_request, url, body)
  username = Configuration.hmac_auth_user_name

  content_signature = ''
  content_header = ''

  now = Time.now.utc.strftime('%a, %d %b %Y %H:%M:%S GMT')

  date_header = now

  request_type = 'GET'

  unless body.nil?
    request_type = 'POST'

    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 = AuthManager.create_signature(date_header,
                                                content_signature, url,
                                                request_type)

  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, request_type) ⇒ Object



78
79
80
81
82
83
84
85
86
87
88
# File 'lib/message_media_messages/http/auth/auth_manager.rb', line 78

def self.create_signature(date, content_signature, url, request_type)
  signing_string = "date: #{date}\n#{content_signature}#{request_type} " \
                   "#{url} HTTP/1.1"
  hmac = OpenSSL::HMAC.digest(OpenSSL::Digest.new('sha1'),
                              Configuration.hmac_auth_password.encode(
                                Encoding::UTF_8
                              ),
                              signing_string.encode(Encoding::UTF_8))

  Base64.encode64(hmac).chomp
end