Sha256: 8a4d64aa22e2af4137b41a4329d31553b24194f5d3e2b41c1f009ae1cf9dd3d3

Contents?: true

Size: 1.7 KB

Versions: 12

Compression:

Stored size: 1.7 KB

Contents

require 'faraday'
require 'forwardable'

module FaradayMiddleware
  # Public: A simple middleware that adds an access token to each request.
  #
  # The token is added as both "access_token" query parameter and the
  # "Authorization" HTTP request header. However, an explicit "access_token"
  # parameter or "Authorization" header for the current request are not
  # overriden.
  #
  # Examples
  #
  #   # configure default token:
  #   OAuth2.new(app, 'abc123')
  #
  #   # configure query parameter name:
  #   OAuth2.new(app, 'abc123', :param_name => 'my_oauth_token')
  #
  #   # default token value is optional:
  #   OAuth2.new(app, :param_name => 'my_oauth_token')
  class OAuth2 < Faraday::Middleware

    PARAM_NAME  = 'access_token'.freeze
    AUTH_HEADER = 'Authorization'.freeze

    attr_reader :param_name

    extend Forwardable
    def_delegators :'Faraday::Utils', :parse_query, :build_query

    def call(env)
      params = { param_name => @token }.update query_params(env[:url])

      if token = params[param_name] and !token.empty?
        env[:url].query = build_query params
        env[:request_headers][AUTH_HEADER] ||= %(Token token="#{token}")
      end

      @app.call env
    end

    def initialize(app, token = nil, options = {})
      super(app)
      options, token = token, nil if token.is_a? Hash
      @token = token && token.to_s
      @param_name = options.fetch(:param_name, PARAM_NAME).to_s
      raise ArgumentError, ":param_name can't be blank" if @param_name.empty?
    end

    def query_params(url)
      if url.query.nil? or url.query.empty?
        {}
      else
        parse_query url.query
      end
    end
  end
end

# deprecated alias
Faraday::Request::OAuth2 = FaradayMiddleware::OAuth2

Version data entries

12 entries across 12 versions & 2 rubygems

Version Path
agiley-faraday_middleware-0.9.1 lib/faraday_middleware/request/oauth2.rb
agiley-faraday_middleware-0.9.0 lib/faraday_middleware/request/oauth2.rb
faraday_middleware-0.9.0 lib/faraday_middleware/request/oauth2.rb
faraday_middleware-0.8.8 lib/faraday_middleware/request/oauth2.rb
faraday_middleware-0.8.7 lib/faraday_middleware/request/oauth2.rb
faraday_middleware-0.8.6 lib/faraday_middleware/request/oauth2.rb
faraday_middleware-0.8.5 lib/faraday_middleware/request/oauth2.rb
agiley-faraday_middleware-0.8.3.2 lib/faraday_middleware/request/oauth2.rb
agiley-faraday_middleware-0.8.3.1 lib/faraday_middleware/request/oauth2.rb
faraday_middleware-0.8.4 lib/faraday_middleware/request/oauth2.rb
agiley-faraday_middleware-0.8.3 lib/faraday_middleware/request/oauth2.rb
faraday_middleware-0.8.3 lib/faraday_middleware/request/oauth2.rb