Class: Faraday::NTLMAuth::Middleware

Inherits:
Middleware
  • Object
show all
Defined in:
lib/faraday/ntlm_auth/middleware.rb

Overview

This class provides the main implementation for your middleware. Your middleware can implement any of the following methods:

  • on_request - called when the request is being prepared

  • on_complete - called when the response is being processed

Optionally, you can also override the following methods from Faraday::Middleware

Instance Method Summary collapse

Instance Method Details

#on_request(env) ⇒ Object

This method will be called when the request is being prepared. You can alter it as you like, accessing things like request_body, request_headers, and more. Refer to Faraday::Env for a list of accessible fields: github.com/lostisland/faraday/blob/main/lib/faraday/options/env.rb

Parameters:

  • env (Faraday::Env)

    the environment of the request being processed



26
27
28
29
30
31
32
33
34
# File 'lib/faraday/ntlm_auth/middleware.rb', line 26

def on_request(env)
  ntlm_message = Net::NTLM::Message
  env[:request_headers]['Authorization'] = 'NTLM ' + ntlm_message::Type1.new.encode64
  response = @app.call(env.dup)
  challenge = response.env[:response_headers]['www-authenticate'][/(?:NTLM|Negotiate) (.*)$/, 1]
  message = ntlm_message::Type2.decode64(challenge)
  ntlm_auth = message.response([:user, :password, :domain].zip(@options[:auth]).to_h)
  env[:request_headers]['Authorization'] = "NTLM #{ntlm_auth.encode64}"
end