3

Can Rest Client do NTLM authentication?

I didn't see any options in the documentation for authentication types:

require 'rest_client'

resource = RestClient::Resource.new 'http://website', :auth_type => 'ntlm', :user => 'USERNAME', :password => 'PASSWORD'
results = resource.get

:auth_type => 'ntlm' doesn't work, and I couldn't find anything on the documentation or IRC room either.

the Tin Man
  • 158,662
  • 42
  • 215
  • 303
nictrix
  • 1,483
  • 1
  • 17
  • 34

2 Answers2

2

The NTLM requirement really narrows down what HTTP software you can use due to it being so specific to Microsoft.

You might want to look at "NTLM Authentication for Ruby with Typhoeus and Curl", then look into using Typhoeus instead of rest-client.

the Tin Man
  • 158,662
  • 42
  • 215
  • 303
  • Thanks, I'll probably end up using: https://github.com/maccman/nestful or just curl. Believe me I didn't want to use NTLM if I didn't have to :) – nictrix Nov 09 '10 at 21:17
  • interesting story Greg, sounds like fun over there......here ya go: http://scottw.com/accessing-restful-service-ruby-via - good enough to do everything I need it to do, luckily I won't be accessing the RestAPI behind NTLM too much – nictrix Nov 10 '10 at 07:04
  • @nictrix - Your link appears to be broken. This worked for me: http://dev.scottw.com/accessing-restful-service-ruby-via – Roy Tinker Aug 25 '16 at 20:14
0

Technically speaking, you can make it do so using the before_execution_proc arg which lets you access the internal Net::HTTP request objects. If you're using the ruby-ntlm gem it adds a ntlm_auth method to Net::HTTP requests.

require 'ntlm/http'
require 'rest-client'
require 'json'

# Quick monkey patch to rest client payloads since for some reason Net/NTLM insists on playing payload streams backwards.
class RestClient::Payload::Base
  def rewind
    @stream.rewind
  end
end

auth_proc = ->(req, _args){ req.ntlm_auth(username, domain, password)}
res = RestClient::Request.new(method: :post, url: url, payload: payload}, before_execution_proc: auth_proc ).execute
res