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

Constructor Details

#initialize(request) ⇒ RackRequest

Returns a new instance of RackRequest.


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

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

Instance Method Details

#authorization_headerObject


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

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/rack.rb', line 21

def calculated_md5
  if @request.body
    body = @request.body.read
    @request.body.rewind
  else
    body = ''
  end
  Digest::MD5.base64digest(body)
end

#content_md5Object


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

def content_md5
  value = find_header(%w(CONTENT-MD5 CONTENT_MD5 HTTP-CONTENT-MD5 HTTP_CONTENT_MD5))
  value.nil? ? "" : value
end

#content_typeObject


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

def content_type
  value = find_header(%w(CONTENT-TYPE CONTENT_TYPE HTTP_CONTENT_TYPE))
  value.nil? ? "" : value
end

#fetch_headersObject


45
46
47
# File 'lib/api_auth/request_drivers/rack.rb', line 45

def fetch_headers
  capitalize_keys @request.env
end

#md5_mismatch?Boolean

Returns:

  • (Boolean)

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

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

#populate_content_md5Object


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

def populate_content_md5
  if ['POST', 'PUT'].include?(@request.request_method)
    @request.env["Content-MD5"] = calculated_md5
  end
end

#request_uriObject


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

def request_uri
  @request.url
end

#set_auth_header(header) ⇒ Object


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

def set_auth_header(header)
  @request.env.merge!({ "Authorization" => header })
  @headers = fetch_headers
  @request
end

#set_dateObject


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

def set_date
  @request.env.merge!({ "DATE" => Time.now.utc.httpdate })
end

#timestampObject


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

def timestamp
  value = find_header(%w(DATE HTTP_DATE))
  value.nil? ? "" : value
end