Class: SchoologyClient::OAuth
- Inherits:
-
Faraday::Middleware
- Object
- Faraday::Middleware
- SchoologyClient::OAuth
- Extended by:
- Forwardable
- Defined in:
- lib/schoology_client/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.
If no Content-Type header is specified, this middleware assumes that request body parameters should be included while signing the request. Otherwise, it only includes them if the Content-Type is “application/x-www-form-urlencoded”, as per OAuth 1.0.
For better performance while signing requests, this middleware should be positioned before UrlEncoded middleware on the stack, but after any other body-encoding middleware (such as EncodeJson).
Constant Summary collapse
- AUTH_HEADER =
'Authorization'
- CONTENT_TYPE =
'Content-Type'
- TYPE_URLENCODED =
'application/x-www-form-urlencoded'
Instance Method Summary collapse
- #body_params(env) ⇒ Object
- #call(env) ⇒ Object
- #include_body_params?(env) ⇒ Boolean
-
#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.
32 33 34 35 |
# File 'lib/schoology_client/oauth.rb', line 32 def initialize(app, ) super(app) @options = end |
Instance Method Details
#body_params(env) ⇒ Object
61 62 63 64 65 66 67 68 69 |
# File 'lib/schoology_client/oauth.rb', line 61 def body_params(env) if include_body_params?(env) if env[:body].respond_to?(:to_str) parse_nested_query env[:body] else env[:body] end end || {} end |
#call(env) ⇒ Object
37 38 39 40 |
# File 'lib/schoology_client/oauth.rb', line 37 def call(env) env[:request_headers][AUTH_HEADER] ||= oauth_header(env).to_s if sign_request?(env) @app.call(env) end |
#include_body_params?(env) ⇒ Boolean
71 72 73 74 |
# File 'lib/schoology_client/oauth.rb', line 71 def include_body_params?(env) # see RFC 5849, section 3.4.1.3.1 for details !(type = env[:request_headers][CONTENT_TYPE]) || (type == TYPE_URLENCODED) end |
#oauth_header(env) ⇒ Object
42 43 44 45 46 47 |
# File 'lib/schoology_client/oauth.rb', line 42 def oauth_header(env) SimpleOAuth::Header.new env[:method], env[:url].to_s, signature_params(body_params(env)), (env) end |
#oauth_options(env) ⇒ Object
53 54 55 56 57 58 59 |
# File 'lib/schoology_client/oauth.rb', line 53 def (env) if (extra = env[:request][:oauth]) && extra.is_a?(Hash) && !extra.empty? @options.merge extra else @options end end |
#sign_request?(env) ⇒ Boolean
49 50 51 |
# File 'lib/schoology_client/oauth.rb', line 49 def sign_request?(env) !!env[:request].fetch(:oauth, true) end |
#signature_params(params) ⇒ Object
76 77 78 79 80 81 82 |
# File 'lib/schoology_client/oauth.rb', line 76 def signature_params(params) if params.empty? params else params.reject { |_k, v| v.respond_to?(:content_type) } end end |