Class: ApiAuth::RequestDrivers::RackRequest

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

Overview

:nodoc:

Instance Method Summary collapse

Methods included from Helpers

#b64_encode, #capitalize_keys, #md5_base64digest

Constructor Details

#initialize(request) ⇒ RackRequest

Returns a new instance of RackRequest.


6
7
8
9
10
# File 'lib/api_auth/request_drivers/rack.rb', line 6

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

Instance Method Details

#authorization_headerObject


76
77
78
# File 'lib/api_auth/request_drivers/rack.rb', line 76

def authorization_header
  find_header %w[Authorization AUTHORIZATION HTTP_AUTHORIZATION]
end

#calculated_md5Object


18
19
20
21
22
23
24
25
26
# File 'lib/api_auth/request_drivers/rack.rb', line 18

def calculated_md5
  if @request.body
    body = @request.body.read
    @request.body.rewind
  else
    body = ''
  end
  md5_base64digest(body)
end

#content_md5Object


55
56
57
# File 'lib/api_auth/request_drivers/rack.rb', line 55

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

#content_typeObject


51
52
53
# File 'lib/api_auth/request_drivers/rack.rb', line 51

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

#fetch_headersObject


43
44
45
# File 'lib/api_auth/request_drivers/rack.rb', line 43

def fetch_headers
  @headers = capitalize_keys @request.env
end

#http_methodObject


47
48
49
# File 'lib/api_auth/request_drivers/rack.rb', line 47

def http_method
  @request.request_method.upcase
end

#md5_mismatch?Boolean

Returns:

  • (Boolean)

35
36
37
38
39
40
41
# File 'lib/api_auth/request_drivers/rack.rb', line 35

def md5_mismatch?
  if %w[POST PUT].include?(@request.request_method)
    calculated_md5 != content_md5
  else
    false
  end
end

#original_uriObject


59
60
61
# File 'lib/api_auth/request_drivers/rack.rb', line 59

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

#populate_content_md5Object


28
29
30
31
32
33
# File 'lib/api_auth/request_drivers/rack.rb', line 28

def populate_content_md5
  return unless %w[POST PUT].include?(@request.request_method)

  @request.env['Content-MD5'] = calculated_md5
  fetch_headers
end

#request_uriObject


63
64
65
# File 'lib/api_auth/request_drivers/rack.rb', line 63

def request_uri
  @request.fullpath
end

#set_auth_header(header) ⇒ Object


12
13
14
15
16
# File 'lib/api_auth/request_drivers/rack.rb', line 12

def set_auth_header(header)
  @request.env['Authorization'] = header
  fetch_headers
  @request
end

#set_dateObject


67
68
69
70
# File 'lib/api_auth/request_drivers/rack.rb', line 67

def set_date
  @request.env['DATE'] = Time.now.utc.httpdate
  fetch_headers
end

#timestampObject


72
73
74
# File 'lib/api_auth/request_drivers/rack.rb', line 72

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