Sha256: 40048a2e8feebcc8f26d4d0cfb09a0111e0d4cbe3148c78e64c366589da11222

Contents?: true

Size: 1.38 KB

Versions: 4

Compression:

Stored size: 1.38 KB

Contents

module FlexmlsApi

  module Authentication
    
    module OAuth2Impl
      
      #==OAuth2 Faraday response middleware
      # HTTP Response after filter to package oauth2 responses and bubble up basic api errors.
      class Middleware < Faraday::Response::ParseJson
        def on_complete(finished_env)
          body = parse(finished_env[:body])
          FlexmlsApi.logger.debug("Response Body: #{body.inspect}")
          unless body.is_a?(Hash)
            raise InvalidResponse, "The server response could not be understood"
          end
          case finished_env[:status]
          when 200..299
            FlexmlsApi.logger.debug("Success!")
            session = OAuthSession.new(body)
          else 
            # Handle the WWW-Authenticate Response Header Field if present. This can be returned by 
            # OAuth2 implementations and wouldn't hurt to log.
            auth_header_error = finished_env[:request_headers]["WWW-Authenticate"]
            FlexmlsApi.logger.warn("Authentication error #{auth_header_error}") unless auth_header_error.nil?
            raise ClientError, {:message => body["error"], :code =>0, :status => finished_env[:status]}
          end
          FlexmlsApi.logger.debug("Session= #{session.inspect}")
          finished_env[:body] = session
        end
    
        def initialize(app)
          super(app)
        end
        
      end
    end
  end
end

Version data entries

4 entries across 4 versions & 1 rubygems

Version Path
flexmls_api-0.7.3 lib/flexmls_api/authentication/oauth2_impl/middleware.rb
flexmls_api-0.7.0 lib/flexmls_api/authentication/oauth2_impl/middleware.rb
flexmls_api-0.6.5 lib/flexmls_api/authentication/oauth2_impl/middleware.rb
flexmls_api-0.6.4 lib/flexmls_api/authentication/oauth2_impl/middleware.rb