Class: FaradayMiddleware::OAuth
- Inherits:
-
Faraday::Middleware
- Object
- Faraday::Middleware
- FaradayMiddleware::OAuth
- Defined in:
- lib/faraday_middleware/request/oauth.rb
Overview
Public: Uses the simple_oauth library to sign requests according the OAuth protocol.
The options for this middleware are forwarded to SimpleOAuth::Header: :consumer_key, :consumer_secret, :token, :token_secret. All these parameters are optional.
The signature is added to the “Authorization” HTTP request header. If the value for this header already exists, it is not overriden.
For requests that have parameters in the body, such as POST, this middleware expects them to be in Hash form, i.e. not encoded to string. This means this middleware has to be positioned on the stack before any encoding middleware such as UrlEncoded.
Constant Summary collapse
- AUTH_HEADER =
'Authorization'.freeze
Instance Method Summary collapse
- #body_params(env) ⇒ Object
- #call(env) ⇒ Object
-
#initialize(app, options) ⇒ OAuth
constructor
A new instance of OAuth.
- #oauth_header(env) ⇒ Object
- #oauth_options(env) ⇒ Object
- #sign_request?(env) ⇒ Boolean
- #signature_params(params) ⇒ Object
Constructor Details
#initialize(app, options) ⇒ OAuth
Returns a new instance of OAuth.
23 24 25 26 |
# File 'lib/faraday_middleware/request/oauth.rb', line 23 def initialize(app, ) super(app) @options = end |
Instance Method Details
#body_params(env) ⇒ Object
52 53 54 |
# File 'lib/faraday_middleware/request/oauth.rb', line 52 def body_params(env) env[:body] || {} end |
#call(env) ⇒ Object
28 29 30 31 |
# File 'lib/faraday_middleware/request/oauth.rb', line 28 def call(env) env[:request_headers][AUTH_HEADER] ||= oauth_header(env).to_s if sign_request?(env) @app.call(env) end |
#oauth_header(env) ⇒ Object
33 34 35 36 37 38 |
# File 'lib/faraday_middleware/request/oauth.rb', line 33 def oauth_header(env) SimpleOAuth::Header.new env[:method], env[:url].to_s, signature_params(body_params(env)), (env) end |
#oauth_options(env) ⇒ Object
44 45 46 47 48 49 50 |
# File 'lib/faraday_middleware/request/oauth.rb', line 44 def (env) if extra = env[:request][:oauth] and extra.is_a? Hash and !extra.empty? @options.merge extra else @options end end |
#sign_request?(env) ⇒ Boolean
40 41 42 |
# File 'lib/faraday_middleware/request/oauth.rb', line 40 def sign_request?(env) !!env[:request].fetch(:oauth, true) end |
#signature_params(params) ⇒ Object
56 57 58 59 |
# File 'lib/faraday_middleware/request/oauth.rb', line 56 def signature_params(params) params.empty? ? params : params.reject {|k,v| v.respond_to?(:content_type) } end |