May21
Ruby NTLM Mechanize
Today I was trying to get mechanize work with NTLM, but I found that NTLM authentication is different from basic authentication and in the mechanize code there is a decision whether to use basic or somethings else.
so I decided to fix this and I grabbed the rubyntlm gem and edited the mechanize code to use rubyntlm stuff. However I also found that when trying to do this I had to patch the rubyntlm code in order to make using it from net/http easier.
For convenience and because I am using this code where I work on various different machines I have packaged up the ntlm and mechanize code into a gem called mechanize-ntlm. If you just want to use the ruby ntlm-http stuff by itself standalone I packaged that as ntlm-http.
So now in mechanize you can do this:
require 'rubygems'
require 'mechanize-ntlm'
agent = WWW::Mechanize.new
agent.basic_auth("username","password")
page = agent.get('http://some/url')
page.links.each do |link|
puts link.text
end
and it will correctly realize that you want to use NTLM authentication and work just fine.
Get the mechanize-ntlm gem here: mechanize-ntlm-0.9.1.gem
Get the ntlm-http gem here: ntlm-http-0.1.1.gem
The ntlm-http stuff just helps if you want to do ntlm from the net/http world – for example consider the following:
when :ntlm; request.ntlm_auth('username', 'password')
when :basic; request.basic_auth('username', 'password')
You now are able to call ntlm_auth on the Net::HTTP request object as well as the already existing basic_auth.
Latest Comments
"Highly descriptive article, I e..."
rc heli / 31.Dec.2011 at 11:04pm
"Glad to visit this blog, keep i..."
rc helicopters / 28.Dec.2011 at 07:16am
"Very nice. Used to access
nick xidis / 20.Jul.2009 at 11:29pm
"Thanks Mark – I have put ..."
kingsley / 24.Jun.2009 at 11:10am
"Nice work! Could you publish th..."
mark yoon / 10.Jun.2009 at 05:03pm