class RaptorIO::Protocol::HTTP::Request::Manipulators::Authenticator

Implements automatic HTTP authentication.

@author Tasos Laskos

Public Instance Methods

run() click to toggle source
# File lib/raptor-io/protocol/http/request/manipulators/authenticator.rb, line 29
def run
  datastore[:tries] ||= 0
  return if skip?

  callbacks = request.callbacks.dup
  request.clear_callbacks

  # We need to block until authentication is complete, that's why we requeue
  # and run.

  requeue
  request.on_complete do |response|
    auth_type = type( response )

    if !failed? && response.code == 401 && supported?( auth_type )
      retry_with_auth( auth_type, response )
    else
      request.callbacks = callbacks
      request.handle_response response
      request.clear_callbacks
    end
  end
  client.run
end

Private Instance Methods

failed?() click to toggle source

@note Set by one of the authenticators, not ‘self`. @return [Bool] `true` if authentication failed, `false` otherwise.

# File lib/raptor-io/protocol/http/request/manipulators/authenticator.rb, line 58
def failed?
  !!datastore[:failed]
end
remove_client_authenticators() click to toggle source

Removes all enabled authenticators.

# File lib/raptor-io/protocol/http/request/manipulators/authenticator.rb, line 101
def remove_client_authenticators
  client.manipulators.reject!{ |k, _| k.start_with? 'authenticator' }
end
requeue() click to toggle source

Requeues the request after the proper authenticator has been enabled.

# File lib/raptor-io/protocol/http/request/manipulators/authenticator.rb, line 78
def requeue
  client.queue( request, shortname => { skip: true } )
end
retry_with_auth( type, response ) click to toggle source

Retries the request with authentication.

@param [Symbol] type Authenticator to use. @param [RaptorIO::Protocol::HTTP::Response] response

Response signaling the need to authenticate.
# File lib/raptor-io/protocol/http/request/manipulators/authenticator.rb, line 67
def retry_with_auth( type, response )
  datastore[:tries] += 1

  remove_client_authenticators if ![:ntlm, :negotiate].include?( type )
  client.manipulators.merge!({
    "authenticators/#{type}" => options.merge( response: response )
  })
  requeue
end
skip?() click to toggle source
# File lib/raptor-io/protocol/http/request/manipulators/authenticator.rb, line 89
def skip?
  failed? || !!options[:skip]
end
supported?( type ) click to toggle source

@param [Symbol] type Authentication type to check. @return [Bool]

`true` if the authentication `type` is supported, `false` otherwise.
# File lib/raptor-io/protocol/http/request/manipulators/authenticator.rb, line 96
def supported?( type )
  Request::Manipulators.exist? "authenticators/#{type}"
end
type( response ) click to toggle source

@param [RaptorIO::Protocol::HTTP::Response] response

Response signaling the need to authenticate.

@return [Symbol] Authentication type.

# File lib/raptor-io/protocol/http/request/manipulators/authenticator.rb, line 85
def type( response )
  response.headers['www-authenticate'].to_s.split( ' ' ).first.to_s.downcase.to_s.to_sym
end