Skip to content

Commit

Permalink
Merge pull request #108 from savonrb/ntlm-is-optional
Browse files Browse the repository at this point in the history
Make ruby-ntlm an optional dependency
  • Loading branch information
rogerleite committed Feb 5, 2014
2 parents d80ece0 + 7b324c3 commit b289cb4
Show file tree
Hide file tree
Showing 5 changed files with 36 additions and 9 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
### 2.2.0 (edge)

* Feature: [#108](https://github.com/savonrb/httpi/pull/108) Make `rubyntlm` gem, an optional dependency.

### 2.1.0 (2013-07-22)

* Feature: [#75](https://github.com/savonrb/httpi/pull/75) Rack adapter.
Expand Down
10 changes: 5 additions & 5 deletions httpi.gemspec
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,12 @@ Gem::Specification.new do |s|
s.license = 'MIT'

s.add_dependency 'rack'
s.add_dependency 'rubyntlm', '~> 0.3.2'

s.add_development_dependency 'rake', '~> 10.0'
s.add_development_dependency 'rspec', '~> 2.12'
s.add_development_dependency 'mocha', '~> 0.13'
s.add_development_dependency 'puma', '~> 2.3.2'
s.add_development_dependency 'rubyntlm', '~> 0.3.2'
s.add_development_dependency 'rake', '~> 10.0'
s.add_development_dependency 'rspec', '~> 2.12'
s.add_development_dependency 'mocha', '~> 0.13'
s.add_development_dependency 'puma', '~> 2.3.2'

s.files = `git ls-files`.split("\n")
s.require_path = 'lib'
Expand Down
18 changes: 15 additions & 3 deletions lib/httpi/adapter/net_http.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,18 @@

require "httpi/adapter/base"
require "httpi/response"
require 'net/ntlm'
require 'kconv'
require 'socket'

begin
require 'net/ntlm'
unless Net::NTLM::VERSION::STRING >= '0.3.2'
raise ArgumentError('Invalid version of rubyntlm. Please use v0.3.2+.')
end
rescue LoadError => e
HTTPI.logger.debug('Net::NTLM is not available. Install via gem install rubyntlm.')
end

module HTTPI
module Adapter

Expand Down Expand Up @@ -76,6 +84,10 @@ def setup
end

def negotiate_ntlm_auth(http, &requester)
unless Net.const_defined?(:NTLM)
HTTPI.logger.fatal('Cannot negotiate ntlm auth if net/ntlm is not present. Perhaps the net/ntlm gem is not installed?')
end

# first figure out if we should use NTLM or Negotiate
nego_auth_response = respond_with(requester.call(http, request_client(:head)))
if nego_auth_response.headers['www-authenticate'].include? 'Negotiate'
Expand All @@ -102,7 +114,7 @@ def negotiate_ntlm_auth(http, &requester)
if auth_response.headers["WWW-Authenticate"] =~ /(NTLM|Negotiate) (.+)/
auth_token = $2
ntlm_message = Net::NTLM::Message.decode64(auth_token)

message_builder = {}
# copy the username and password from the authorization parameters
message_builder[:user] = @request.auth.ntlm[0]
Expand All @@ -114,7 +126,7 @@ def negotiate_ntlm_auth(http, &requester)
else
message_builder[:domain] = ''
end

ntlm_response = ntlm_message.response(message_builder ,
{:ntlmv2 => true})
# Finally add header of Authorization
Expand Down
2 changes: 1 addition & 1 deletion lib/httpi/version.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
module HTTPI

VERSION = "2.1.0"
VERSION = "2.2.0"

end
11 changes: 11 additions & 0 deletions spec/httpi/adapter/net_http_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,17 @@
response = HTTPI.get(request, adapter)
response.body.should eq("ntlm-auth")
end

it 'fatal logs when net/ntlm is not available, but ntlm authentication was requested' do
Net.expects(:const_defined?).with(:NTLM).returns false

request = HTTPI::Request.new(@server.url + 'ntlm-auth')
request.auth.ntlm("testing", "failures")
HTTPI.logger.expects(:fatal)

response = HTTPI.get(request, adapter)
response.body.should eq("ntlm-auth")
end
end

# it does not support digest auth
Expand Down

0 comments on commit b289cb4

Please sign in to comment.