Sha256: 1decca6684fe3266bfcec4c29eae17d7985c2c094bcbaa5412faf1747f348fea

Contents?: true

Size: 1.52 KB

Versions: 15

Compression:

Stored size: 1.52 KB

Contents

require 'faraday'
require 'simple_oauth'



module Saddle
  module Middleware
    module Authentication

      ## Add OAuth 1.0 authentication tokens to requests
      #
      class OAuth1 < Faraday::Middleware

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

        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

        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

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

      end

    end
  end
end

Version data entries

15 entries across 15 versions & 1 rubygems

Version Path
saddle-0.0.53 lib/saddle/middleware/authentication/oauth1.rb
saddle-0.0.51 lib/saddle/middleware/authentication/oauth1.rb
saddle-0.0.50 lib/saddle/middleware/authentication/oauth1.rb
saddle-0.0.49 lib/saddle/middleware/authentication/oauth1.rb
saddle-0.0.48 lib/saddle/middleware/authentication/oauth1.rb
saddle-0.0.46 lib/saddle/middleware/authentication/oauth1.rb
saddle-0.0.45 lib/saddle/middleware/authentication/oauth1.rb
saddle-0.0.44 lib/saddle/middleware/authentication/oauth1.rb
saddle-0.0.43 lib/saddle/middleware/authentication/oauth1.rb
saddle-0.0.42 lib/saddle/middleware/authentication/oauth1.rb
saddle-0.0.41 lib/saddle/middleware/authentication/oauth1.rb
saddle-0.0.38 lib/saddle/middleware/authentication/oauth1.rb
saddle-0.0.37 lib/saddle/middleware/authentication/oauth1.rb
saddle-0.0.36 lib/saddle/middleware/authentication/oauth1.rb
saddle-0.0.35 lib/saddle/middleware/authentication/oauth1.rb