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.
3 Comments
Nice work! Could you publish this on rubyforge or github, or your own gemserver, so that we can use your gem in our app (as a rails 2 gem dependency). Thanks
Thanks Mark – I have put the gems here: rubyforge ntlm gems
Very nice. Used to access REST services from a VersionOne system that uses Windows integrated auth. It works like a charm.
Sorry, comments are closed for this article.