Sha256: 813a5b913774b082a9fe935d2d0cb1ef6bb531571ea9e8494a732de8abc3df8c

Contents?: true

Size: 1.16 KB

Versions: 2

Compression:

Stored size: 1.16 KB

Contents

module Restforce

  # Faraday middleware that allows for on the fly authentication of requests.
  # When a request fails (ie. A status of 401 is returned). The middleware
  # will attempt to either reauthenticate (username and password) or refresh
  # the oauth access token (if a refresh token is present).
  class Middleware::Authentication < Restforce::Middleware

    def call(env)
      begin
        return authenticate! if force_authenticate?(env)
        @app.call(env)
      rescue Restforce::UnauthorizedError
        authenticate!
        @app.call(env)
      end
    end

    def authenticate!
      raise 'must subclass'
    end

    def connection
      @connection ||= Faraday.new(:url => "https://#{@options[:host]}") do |builder|
        builder.response :json
        builder.use Restforce::Middleware::Logger, Restforce.configuration.logger if Restforce.log?
        builder.adapter Faraday.default_adapter
      end
    end

    def force_authenticate?(env)
      env[:request_headers] && env[:request_headers]['X-ForceAuthenticate']
    end

    def error_message(response)
      "#{response.body['error']}: #{response.body['error_description']}"
    end
  
  end

end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
restforce-0.0.5 lib/restforce/middleware/authentication.rb
restforce-0.0.4 lib/restforce/middleware/authentication.rb