Class: ApiAuth::RequestDrivers::GrapeRequest

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

Overview

:nodoc:

Instance Method Summary collapse

Methods included from Helpers

#b64_encode, #capitalize_keys, #md5_base64digest

Constructor Details

#initialize(request) ⇒ GrapeRequest

Returns a new instance of GrapeRequest.


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

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

Instance Method Details

#authorization_headerObject


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

def authorization_header
  find_header %w[HTTP_X_HMAC_AUTHORIZATION HTTP_X_AUTHORIZATION Authorization AUTHORIZATION HTTP_AUTHORIZATION]
end

#calculated_md5Object


18
19
20
21
22
# File 'lib/api_auth/request_drivers/grape_request.rb', line 18

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

#content_md5Object


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

def content_md5
  find_header %w[HTTP_X_HMAC_CONTENT_MD5 HTTP_X_CONTENT_MD5 CONTENT-MD5 CONTENT_MD5 HTTP_CONTENT_MD5]
end

#content_typeObject


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

def content_type
  find_header %w[HTTP_X_HMAC_CONTENT_TYPE HTTP_X_CONTENT_TYPE CONTENT-TYPE CONTENT_TYPE HTTP_CONTENT_TYPE]
end

#fetch_headersObject


39
40
41
# File 'lib/api_auth/request_drivers/grape_request.rb', line 39

def fetch_headers
  capitalize_keys @request.env
end

#http_methodObject


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

def http_method
  @request.request_method.upcase
end

#md5_mismatch?Boolean

Returns:

  • (Boolean)

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

def md5_mismatch?
  if @request.put? || @request.post?
    calculated_md5 != content_md5
  else
    false
  end
end

#original_uriObject


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

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

#populate_content_md5Object


24
25
26
27
28
29
# File 'lib/api_auth/request_drivers/grape_request.rb', line 24

def populate_content_md5
  return if !@request.put? && !@request.post?

  @request.env['HTTP_CONTENT_MD5'] = calculated_md5
  save_headers
end

#request_uriObject


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

def request_uri
  @request.url
end

#set_auth_header(header) ⇒ Object


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

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

#set_dateObject


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

def set_date
  @request.env['HTTP_DATE'] = Time.now.utc.httpdate
  save_headers
end

#timestampObject


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

def timestamp
  find_header %w[HTTP_X_HMAC_DATE HTTP_X_DATE DATE HTTP_DATE]
end