Module: Passwordstate::NetHTTPExtensions

Defined in:
lib/passwordstate/util.rb

Overview

Extensions on Net::HTTP to allow NTLM digest auth

Examples:

Using NTLM auth

uri = URI('https://example.com/some_object')
Net::HTTP.start uri.host, uri.port { |http|
  req = Net::HTTP::Get.new uri
  req.ntlm_auth 'username', 'password'

  http.request req
}

Instance Method Summary collapse

Instance Method Details

#request(req, body = nil, &block) ⇒ Object



65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
# File 'lib/passwordstate/util.rb', line 65

def request(req, body = nil, &block)
  return super(req, body, &block) if req.ntlm_auth_information.nil?

  unless started?
    @last_body = req.body
    req.body = nil
    start do
      req.delete('connection')
      return request(req, body, &block)
    end
  end

  type1 = Net::NTLM::Message::Type1.new
  req['authorization'] = "NTLM #{type1.encode64}"
  res = super(req, body)

  challenge = res['www-authenticate'][/(?:NTLM|Negotiate) (.+)/, 1]

  if challenge && res.code == '401'
    type2 = Net::NTLM::Message.decode64 challenge
    type3 = type2.response(req.ntlm_auth_information, req.ntlm_auth_options.dup)

    req['authorization'] = "NTLM #{type3.encode64}"
    req.body_stream.rewind if req.body_stream
    req.body = @last_body if @last_body

    super(req, body, &block)
  else
    yield res if block_given?
    res
  end
end