Search code examples
ruby-on-railsweb-servicesbasic-authenticationdigest-authenticationhttparty

Trying to connect to a "digest authentication" webservice using HTTParty or Net:HTTP (or etc)


I have been trying to connect to a web service that is using digest authentication.

I am able to connect in Safari using user:[email protected]/endpoint

I have tried in Ruby and Rails to connect using HTTParty and Net:HTTP using the "basic"auth" options, but have not had any luck.

Wondering if the HTTParty/Net:HTTP "basic_auth" option is not going to be compatible with a "digest auth" service?

If not, is there another way that I might connect?


Solution

  • HTTParty basic auth is apparently not compatible with digest_auth. I found this Net:HTTP extension: https://codesnippets.joyent.com/posts/show/1075 and am writing a method to handle this, with the help of the Crack gem http://github.com/jnunemaker/crack:

     def self.decode vin
        url = URI.parse(APP_CONFIG[:vinlink_url])
        Net::HTTP.start(url.host) do |http|
          res = http.head(url.request_uri)
          req = Net::HTTP::Get.new("/report?type=basic&vin=#{vin}")
          req.digest_auth(APP_CONFIG[:vinlink_login], APP_CONFIG[:vinlink_password], res)
          @response = http.request(req)
        end
        if @response.code == "200"
          hash = Crack::XML.parse(@response.body).recursive_downcase_keys!.recursive_symbolize_keys!
        end
      end