Class: ApiAuth::RequestDrivers::RestClientRequest

Inherits:
Object
  • Object
show all
Includes:
Helpers
Defined in:
lib/api_auth/request_drivers/rest_client.rb

Overview

:nodoc:

Instance Method Summary collapse

Methods included from Helpers

#b64_encode, #capitalize_keys, #md5_base64digest

Constructor Details

#initialize(request) ⇒ RestClientRequest

Returns a new instance of RestClientRequest.


9
10
11
12
13
# File 'lib/api_auth/request_drivers/rest_client.rb', line 9

def initialize(request)
  @request = request
  @headers = fetch_headers
  true
end

Instance Method Details

#authorization_headerObject


79
80
81
# File 'lib/api_auth/request_drivers/rest_client.rb', line 79

def authorization_header
  find_header %w[Authorization AUTHORIZATION HTTP_AUTHORIZATION]
end

#calculated_md5Object


21
22
23
24
25
26
27
28
29
# File 'lib/api_auth/request_drivers/rest_client.rb', line 21

def calculated_md5
  if @request.payload
    body = @request.payload.read
    @request.payload.instance_variable_get(:@stream).seek(0)
  else
    body = ''
  end
  md5_base64digest(body)
end

#content_md5Object


58
59
60
# File 'lib/api_auth/request_drivers/rest_client.rb', line 58

def content_md5
  find_header(%w[CONTENT-MD5 CONTENT_MD5])
end

#content_typeObject


54
55
56
# File 'lib/api_auth/request_drivers/rest_client.rb', line 54

def content_type
  find_header(%w[CONTENT-TYPE CONTENT_TYPE HTTP_CONTENT_TYPE])
end

#fetch_headersObject


46
47
48
# File 'lib/api_auth/request_drivers/rest_client.rb', line 46

def fetch_headers
  capitalize_keys @request.processed_headers
end

#http_methodObject


50
51
52
# File 'lib/api_auth/request_drivers/rest_client.rb', line 50

def http_method
  @request.method.to_s.upcase
end

#md5_mismatch?Boolean

Returns:

  • (Boolean)

38
39
40
41
42
43
44
# File 'lib/api_auth/request_drivers/rest_client.rb', line 38

def md5_mismatch?
  if %w[post put].include?(@request.method.to_s)
    calculated_md5 != content_md5
  else
    false
  end
end

#original_uriObject


62
63
64
# File 'lib/api_auth/request_drivers/rest_client.rb', line 62

def original_uri
  find_header(%w[X-ORIGINAL-URI X_ORIGINAL_URI HTTP_X_ORIGINAL_URI])
end

#populate_content_md5Object


31
32
33
34
35
36
# File 'lib/api_auth/request_drivers/rest_client.rb', line 31

def populate_content_md5
  return unless %w[post put].include?(@request.method.to_s)

  @request.headers['Content-MD5'] = calculated_md5
  save_headers
end

#request_uriObject


66
67
68
# File 'lib/api_auth/request_drivers/rest_client.rb', line 66

def request_uri
  @request.url
end

#set_auth_header(header) ⇒ Object


15
16
17
18
19
# File 'lib/api_auth/request_drivers/rest_client.rb', line 15

def set_auth_header(header)
  @request.headers['Authorization'] = header
  save_headers # enforce update of processed_headers based on last updated headers
  @request
end

#set_dateObject


70
71
72
73
# File 'lib/api_auth/request_drivers/rest_client.rb', line 70

def set_date
  @request.headers['DATE'] = Time.now.utc.httpdate
  save_headers
end

#timestampObject


75
76
77
# File 'lib/api_auth/request_drivers/rest_client.rb', line 75

def timestamp
  find_header(%w[DATE HTTP_DATE])
end