class HTTPClient::ProxyAuth
SSPINegotiateAuth requires ‘win32/sspi’ module.
NegotiateAuth requires ‘ruby/ntlm’ module.
and delegates some operations to it.
ProxyAuth has sub filters (BasicAuth, NegotiateAuth, and SSPINegotiateAuth)
traps HTTP request header for inserting necessary authentication header.
It traps HTTP response header and maintains authentication state, and
Authentication filter is implemented using request filter of HTTPClient.
generates ‘Proxy-Authorization’ header in request.
Proxy server. Parses ‘Proxy-Authentication’ header in response and
Authentication filter for handling authentication negotiation between
def filter_request(req)
Filter API implementation. Traps HTTP request and insert
def filter_request(req) @authenticator.each do |auth| next unless auth.set? # hasn't be set, don't use it if cred = auth.get(req) req.header.set('Proxy-Authorization', auth.scheme + " " + cred) return end end end
def filter_response(req, res)
Filter API implementation. Traps HTTP response and parses
def filter_response(req, res) command = nil if res.status == HTTP::Status::PROXY_AUTHENTICATE_REQUIRED if challenge = parse_authentication_header(res, 'proxy-authenticate') uri = req.header.request_uri challenge.each do |scheme, param_str| @authenticator.each do |auth| next unless auth.set? # hasn't be set, don't use it if scheme.downcase == auth.scheme.downcase challengeable = auth.challenge(uri, param_str) command = :retry if challengeable end end end # ignore unknown authentication scheme end end command end
def initialize
def initialize @basic_auth = BasicAuth.new @negotiate_auth = NegotiateAuth.new @ntlm_auth = NegotiateAuth.new('NTLM') @sspi_negotiate_auth = SSPINegotiateAuth.new # sort authenticators by priority @authenticator = [@negotiate_auth, @ntlm_auth, @sspi_negotiate_auth, @basic_auth] end
def reset_challenge
def reset_challenge @authenticator.each do |auth| auth.reset_challenge end end
def set_auth(user, passwd)
def set_auth(user, passwd) @authenticator.each do |auth| auth.set(nil, user, passwd) end reset_challenge end