Class: FaradayMiddleware::FollowOAuthRedirects

Inherits:
FollowRedirects
  • Object
show all
Defined in:
lib/faraday_middleware/follow_oauth_redirects.rb

Constant Summary collapse

AUTH_HEADER =
OAuth::AUTH_HEADER
CONTENT_TYPE =
OAuth::CONTENT_TYPE
TYPE_URLENCODED =
OAuth::TYPE_URLENCODED

Instance Method Summary collapse

Instance Method Details

#body_params(env) ⇒ Object



45
46
47
48
49
50
51
52
53
# File 'lib/faraday_middleware/follow_oauth_redirects.rb', line 45

def body_params(env)
  if include_body_params?(env)
    if env[:body].respond_to?(:to_str)
      ::Faraday::Utils.parse_nested_query env[:body]
    else
      env[:body]
    end
  end || {}
end

#include_body_params?(env) ⇒ Boolean

Returns:

  • (Boolean)


55
56
57
58
59
# File 'lib/faraday_middleware/follow_oauth_redirects.rb', line 55

def include_body_params?(env)
  # see RFC 5849, section 3.4.1.3.1 for details
  !(type = env[:request_headers][OAuth::CONTENT_TYPE]) ||
    type == OAuth::TYPE_URLENCODED
end

#oauth_header(env) ⇒ Object



25
26
27
28
29
30
# File 'lib/faraday_middleware/follow_oauth_redirects.rb', line 25

def oauth_header(env)
  SimpleOAuth::Header.new env[:method],
                          env[:url].to_s,
                          signature_params(body_params(env)),
                          oauth_options(env)
end

#oauth_options(env) ⇒ Object



36
37
38
39
40
41
42
43
# File 'lib/faraday_middleware/follow_oauth_redirects.rb', line 36

def oauth_options(env)
  extra = env[:request][:oauth]
  if extra.present? and extra.is_a? Hash and !extra.empty?
    @options.merge extra
  else
    @options
  end
end

#oauth_signed_request?(env) ⇒ Boolean

Returns:

  • (Boolean)


32
33
34
# File 'lib/faraday_middleware/follow_oauth_redirects.rb', line 32

def oauth_signed_request?(env)
  env[:request].oauth
end

#signature_params(params) ⇒ Object



61
62
63
64
# File 'lib/faraday_middleware/follow_oauth_redirects.rb', line 61

def signature_params(params)
  return params if params.empty?
  params.reject { |_k, v| v.respond_to?(:content_type) }
end

#update_env(env, request_body, response) ⇒ Object



14
15
16
17
18
19
20
21
22
23
# File 'lib/faraday_middleware/follow_oauth_redirects.rb', line 14

def update_env(env, request_body, response)
  env = super(env, request_body, response)

  # update Authentication Header
  if oauth_signed_request?(env)
    env[:request_headers][OAuth::AUTH_HEADER] = oauth_header(env).to_s
  end

  env
end