lib/net/http/ntlm/transport.rb
# Replaces the original request method, and tries to negotiate an NTLM authentication
#
# @param req [Net::HTTPGenericRequest] A Net::HTTP request
# @param body [String] Request body
# @yield [Net::HTTPResponse] Passes the HTTP response object to the block
# @return [Net::HTTPResponse] The HTTP response object
Replaces the original request method, and tries to negotiate an NTLM authentication
Replaces the original request method and tries to negotiate an NTLM authentication
def request_with_ntlm(req, body = nil, &block)
    return request_without_ntlm_auth(req, body, &block) unless req.ntlm_auth_user
    unless started?
      @original_body = req.body
      req.body = nil
      start do
        req.delete('connection')
        return request_with_ntlm(req, body, &block)
      end
    end
    negotiation_message = Net::NTLM::Message::Type1.new
    negotiation_message.domain = req.ntlm_auth_domain
    req['authorization'] = "NTLM #{negotiation_message.encode64}"
    res = request_without_ntlm_auth(req, body)
    www_auth_headers = Array(res.get_fields('www-authenticate'))
    ntlm_challenge_header = www_auth_headers.find { |h| h.start_with?('NTLM') }
    challenge = ntlm_challenge_header&.slice(/NTLM (.*)/, 1)
    if challenge && res.code == '401'
      challenge_message = ::Net::NTLM::Message.decode64(challenge)
      authorization_message = challenge_message.response(req.ntlm_auth_params, ntlmv2: true)
      req['authorization'] = "NTLM #{authorization_message.encode64}"
      req.body_stream&.rewind
      req.body = @original_body
      request_without_ntlm_auth(req, body, &block)
    else
      yield res if block_given?
      res
    end
  end
def request_with_ntlm ( req , body = nil , & block ) return request_without_ntlm_auth ( req , body , & block ) unless req . ntlm_auth_user unless started? @original_body = req . body req . body = nil start do req . delete ( 'connection' ) return request_with_ntlm ( req , body , & block ) end end negotiation_message = Net :: NTLM :: Message :: Type1 . new negotiation_message . domain = req . ntlm_auth_domain req [ 'authorization' ] = "NTLM #{negotiation_message.encode64}" res = request_without_ntlm_auth ( req , body ) www_auth_headers = Array ( res . get_fields ( 'www-authenticate' ) ) ntlm_challenge_header = www_auth_headers . find { | h | h . start_with? ( 'NTLM' ) } challenge = ntlm_challenge_header &. slice ( / NTLM (.*) / , 1 ) if challenge && res . code == '401' challenge_message = :: Net :: NTLM :: Message . decode64 ( challenge ) authorization_message = challenge_message . response ( req . ntlm_auth_params , ntlmv2 : true ) req [ 'authorization' ] = "NTLM #{authorization_message.encode64}" req . body_stream &. rewind req . body = @original_body request_without_ntlm_auth ( req , body , & block ) else yield res if block_given? res end end
Replaces the original request method, and tries to negotiate an NTLM authentication
Replaces the original request method and tries to negotiate an NTLM authentication
# If we don't have NTLM credentials, handle as regular request # Try to negotiate NTLM first # Try to extract NTLM challenge from the response # We got a challenge and correct response code, sending an authorized request # We must re-use the connection. # If we can't find a challenge or no authentication is required, return the response.
The HTTP response object
The HTTP response object
Net::HTTPResponse
req
A Net::HTTP request
A Net :: HTTP request
body
None
block
<not_specific>
]
<not_specific>
Passes the HTTP response object to the block
Passes the HTTP response object to the block