Skip to content

Instantly share code, notes, and snippets.

@coliver
Forked from rodrei/rubyntlm_with_net_http.rb
Created February 20, 2019 18:20
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save coliver/b34e0ca52fb7beeb0e408f1dcf9dc27d to your computer and use it in GitHub Desktop.
Save coliver/b34e0ca52fb7beeb0e408f1dcf9dc27d to your computer and use it in GitHub Desktop.
Example: NTLM Authentication with NetHTTP
uri = URI('https://host.com/ews/exchange.asmx')
user = ''
passwd = ''
Net::HTTP.start(uri.host, uri.port, :use_ssl => uri.scheme == 'https') do |http|
request = Net::HTTP::Get.new(uri.request_uri)
t1 = Net::NTLM::Message::Type1.new()
request['Authorization'] = 'NTLM ' + t1.encode64
response = http.request(request)
if /\A(NTLM|Negotiate) (.+)\z/ =~ response.header['www-authenticate']
msg = $2
end
t2 = Net::NTLM::Message.decode64(msg)
t3 = t2.response({:user => user, :password => passwd}, {:ntlmv2 => true})
request['Authorization'] = 'NTLM ' + t3.encode64
response = http.request(request)
puts response.msg
response.header.each_header do |h| puts h.inspect + " " + response.header[h] end
puts response.body
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment