Class: Saddle::Middleware::Authentication::OAuth1

Inherits:
Faraday::Middleware
  • Object
show all
Defined in:
lib/saddle/middleware/authentication/oauth1.rb

Overview

Add OAuth 1.0 authentication tokens to requests

Constant Summary collapse

TYPE_URLENCODED =
'application/x-www-form-urlencoded'.freeze

Instance Method Summary collapse

Instance Method Details

#body_params(env) ⇒ Object



34
35
36
37
38
39
40
41
42
43
44
# File 'lib/saddle/middleware/authentication/oauth1.rb', line 34

def body_params(env)
  # Only process body params if it's url-encoded or missing it's Content-Type
  # see RFC 5489, section 3.4.1.3.1 for details
  if !(type = env[:request_headers]['Content-Type']) or type == TYPE_URLENCODED
    if env[:body].respond_to?(:to_str)
      Faraday::Utils::parse_nested_query(env[:body])
    else
      env[:body]
    end
  end || {}
end

#call(env) ⇒ Object



16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
# File 'lib/saddle/middleware/authentication/oauth1.rb', line 16

def call(env)
  if env[:request][:client_options][:oauth1] &&
     env[:request][:client_options][:oauth1][:consumer_key] &&
     env[:request][:client_options][:oauth1][:consumer_secret] &&
     env[:request][:client_options][:oauth1][:token] &&
     env[:request][:client_options][:oauth1][:token_secret]

    env[:request_headers]['Authorization'] ||= SimpleOAuth::Header.new(
      env[:method],
      env[:url].to_s,
      filtered_body_params(env),
      env[:request][:client_options][:oauth1]
    ).to_s
  end

  @app.call(env)
end

#filtered_body_params(env) ⇒ Object



46
47
48
# File 'lib/saddle/middleware/authentication/oauth1.rb', line 46

def filtered_body_params(env)
  body_params(env).reject {|k,v| v.respond_to?(:content_type) }
end