Class: ApiAuth::RequestDrivers::FaradayEnv

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

Overview

Internally, Faraday uses the class Faraday::Env to represent requests. The class is not meant to be directly exposed to users, but this is what Faraday middlewares work with. See <lostisland.github.io/faraday/middleware/>.

Instance Method Summary collapse

Methods included from Helpers

#b64_encode, #capitalize_keys, #md5_base64digest, #sha256_base64digest

Constructor Details

#initialize(env) ⇒ FaradayEnv

Returns a new instance of FaradayEnv.



9
10
11
# File 'lib/api_auth/request_drivers/faraday_env.rb', line 9

def initialize(env)
  @env = env
end

Instance Method Details

#authorization_headerObject



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

def authorization_header
  find_header(%w[Authorization AUTHORIZATION HTTP_AUTHORIZATION])
end

#bodyObject



80
81
82
83
84
85
86
87
88
89
# File 'lib/api_auth/request_drivers/faraday_env.rb', line 80

def body
  body_source = @env.request_body
  if body_source.respond_to?(:read)
    result = body_source.read
    body_source.rewind
    result
  else
    body_source.to_s
  end
end

#calculated_hashObject



18
19
20
# File 'lib/api_auth/request_drivers/faraday_env.rb', line 18

def calculated_hash
  sha256_base64digest(body)
end

#content_hashObject



56
57
58
# File 'lib/api_auth/request_drivers/faraday_env.rb', line 56

def content_hash
  find_header(%w[X-AUTHORIZATION-CONTENT-SHA256])
end

#content_hash_mismatch?Boolean

Returns:

  • (Boolean)


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

def content_hash_mismatch?
  if %w[POST PUT PATCH].include?(http_method)
    calculated_hash != content_hash
  else
    false
  end
end

#content_typeObject



40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
# File 'lib/api_auth/request_drivers/faraday_env.rb', line 40

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

  # When sending a body-less POST request, the Content-Type is set at the last minute by the
  # Net::HTTP adapter, which states in the documentation for Net::HTTP#post:
  #
  # > You should set Content-Type: header field for POST. If no Content-Type: field given,
  # > this method uses “application/x-www-form-urlencoded” by default.
  #
  # The same applies to PATCH and PUT. Hopefully the other HTTP adapters behave similarly.
  #
  type ||= 'application/x-www-form-urlencoded' if %w[POST PATCH PUT].include?(http_method)

  type
end

#fetch_headersObject



91
92
93
# File 'lib/api_auth/request_drivers/faraday_env.rb', line 91

def fetch_headers
  capitalize_keys @env.request_headers
end

#http_methodObject



36
37
38
# File 'lib/api_auth/request_drivers/faraday_env.rb', line 36

def http_method
  @env.method.to_s.upcase
end

#original_uriObject



60
61
62
# File 'lib/api_auth/request_drivers/faraday_env.rb', line 60

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

#populate_content_hashObject



22
23
24
25
26
# File 'lib/api_auth/request_drivers/faraday_env.rb', line 22

def populate_content_hash
  return unless %w[POST PUT PATCH].include?(http_method)

  @env.request_headers['X-Authorization-Content-SHA256'] = calculated_hash
end

#request_uriObject



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

def request_uri
  @env.url.request_uri
end

#set_auth_header(header) ⇒ Object



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

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

#set_dateObject



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

def set_date
  @env.request_headers['Date'] = Time.now.utc.httpdate
end

#timestampObject



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

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