class HTTPClient::SSPINegotiateAuth

Authentication filter for handling Negotiate/NTLM negotiation. Used in ProxyAuth.

SSPINegotiateAuth depends on 'win32/sspi' module.

Attributes

scheme[R]

Authentication scheme.

Public Class Methods

new() click to toggle source

Creates new SSPINegotiateAuth filter.

Calls superclass method
# File lib/httpclient/auth.rb, line 602
def initialize
  super
  @challenge = {}
  @scheme = "Negotiate"
end

Public Instance Methods

challenge(uri, param_str) click to toggle source

Challenge handler: remember URL and challenge token for response.

# File lib/httpclient/auth.rb, line 664
def challenge(uri, param_str)
  synchronize {
    if param_str.nil? or @challenge[uri].nil?
      c = @challenge[uri] = {}
      c[:state] = :init
      c[:authenticator] = nil
      c[:authphrase] = ""
    else
      c = @challenge[uri]
      c[:state] = :response
      c[:authphrase] = param_str
    end
    true
  }
end
get(req) click to toggle source

Response handler: returns credential. See win32/sspi for negotiation state transition.

# File lib/httpclient/auth.rb, line 630
def get(req)
  target_uri = req.header.request_uri
  synchronize {
    domain_uri, param = @challenge.find { |uri, v|
      Util.uri_part_of(target_uri, uri)
    }
    return nil unless param
    Util.try_require('win32/sspi') || Util.try_require('gssapi') || return
    state = param[:state]
    authenticator = param[:authenticator]
    authphrase = param[:authphrase]
    case state
    when :init
      if defined?(Win32::SSPI)
        authenticator = param[:authenticator] = Win32::SSPI::NegotiateAuth.new
        return authenticator.get_initial_token(@scheme)
      else # use GSSAPI
        authenticator = param[:authenticator] = GSSAPI::Simple.new(domain_uri.host, 'HTTP')
        # Base64 encode the context token
        return [authenticator.init_context].pack('m').gsub(/\n/,'')
      end
    when :response
      @challenge.delete(domain_uri)
      if defined?(Win32::SSPI)
        return authenticator.complete_authentication(authphrase)
      else # use GSSAPI
        return authenticator.init_context(authphrase.unpack('m').pop)
      end
    end
    nil
  }
end
reset_challenge() click to toggle source

Resets challenge state. Do not send '*Authorization' header until the server sends '*Authentication' again.

# File lib/httpclient/auth.rb, line 610
def reset_challenge
  synchronize do
    @challenge.clear
  end
end
set(*args) click to toggle source

Set authentication credential. NOT SUPPORTED: username and necessary data is retrieved by win32/sspi. See win32/sspi for more details.

# File lib/httpclient/auth.rb, line 619
def set(*args)
  # not supported
end
set?() click to toggle source

Check always (not effective but it works)

# File lib/httpclient/auth.rb, line 624
def set?
  !@challenge.empty?
end