-
Notifications
You must be signed in to change notification settings - Fork 151
/
net_http.rb
199 lines (164 loc) · 6.51 KB
/
net_http.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
require "uri"
require "httpi/adapter/base"
require "httpi/response"
require 'kconv'
require 'socket'
require "socksify"
require 'socksify/http'
module HTTPI
module Adapter
# = HTTPI::Adapter::NetHTTP
#
# Adapter for the Net::HTTP client.
# http://ruby-doc.org/stdlib/libdoc/net/http/rdoc/
class NetHTTP < Base
register :net_http, :deps => %w(net/https)
def initialize(request)
check_net_ntlm_version!
@request = request
@client = create_client
end
attr_reader :client
# Executes arbitrary HTTP requests.
# @see HTTPI.request
def request(method)
# Determine if Net::HTTP supports the method using reflection
unless Net::HTTP.const_defined?(:"#{method.to_s.capitalize}") &&
Net::HTTP.const_get(:"#{method.to_s.capitalize}").class == Class
raise NotSupportedError, "Net::HTTP does not support "\
"#{method.to_s.upcase}"
end
do_request(method) do |http, http_request|
http_request.body = @request.body
if @request.on_body then
perform(http, http_request) do |res|
res.read_body do |seg|
@request.on_body.call(seg)
end
end
else
perform(http, http_request)
end
end
rescue OpenSSL::SSL::SSLError
raise SSLError
rescue Errno::ECONNREFUSED # connection refused
$!.extend ConnectionError
raise
end
private
def check_net_ntlm_version!
begin
require 'net/ntlm'
require 'net/ntlm/version' unless Net::NTLM.const_defined?(:VERSION)
unless Net::NTLM::VERSION::STRING >= '0.3.2'
raise ArgumentError, 'Invalid version of rubyntlm. Please use v0.3.2+.'
end
rescue LoadError
end
end
def perform(http, http_request, &block)
http.request http_request, &block
end
def create_client
proxy_url = @request.proxy || URI("")
if URI(proxy_url).scheme == 'socks'
proxy =Net::HTTP.SOCKSProxy(proxy_url.host, proxy_url.port)
else
proxy = Net::HTTP::Proxy(proxy_url.host, proxy_url.port, proxy_url.user, proxy_url.password)
end
proxy.new(@request.url.host, @request.url.port)
end
def do_request(type, &requester)
setup
response = @client.start do |http|
negotiate_ntlm_auth(http, &requester) if @request.auth.ntlm?
requester.call(http, request_client(type))
end
respond_with(response)
end
def setup
setup_client
setup_ssl_auth if @request.auth.ssl? || @request.ssl?
end
def negotiate_ntlm_auth(http, &requester)
unless Net.const_defined?(:NTLM)
raise NotSupportedError, 'Net::NTLM is not available. Install via gem install rubyntlm.'
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'] && nego_auth_response.headers['www-authenticate'].include?('Negotiate')
auth_method = 'Negotiate'
elsif nego_auth_response.headers['www-authenticate'] && nego_auth_response.headers['www-authenticate'].include?('NTLM')
auth_method = 'NTLM'
else
auth_method = 'NTLM'
HTTPI.logger.debug 'Server does not support NTLM/Negotiate. Trying NTLM anyway'
end
# initiate a request is to authenticate (exchange secret and auth) using the method determined above...
ntlm_message_type1 = Net::NTLM::Message::Type1.new
%w(workstation domain).each do |a|
ntlm_message_type1.send("#{a}=",'')
ntlm_message_type1.enable(a.to_sym)
end
@request.headers["Authorization"] = "#{auth_method} #{ntlm_message_type1.encode64}"
auth_response = respond_with(requester.call(http, request_client(:head)))
# build an authentication request based on the token provided by the server
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]
message_builder[:password] = @request.auth.ntlm[1]
# we need to provide a domain in the packet if an only if it was provided by the user in the auth request
if @request.auth.ntlm[2]
message_builder[:domain] = @request.auth.ntlm[2].upcase
else
message_builder[:domain] = ''
end
ntlm_response = ntlm_message.response(message_builder ,
{:ntlmv2 => true})
# Finally add header of Authorization
@request.headers["Authorization"] = "#{auth_method} #{ntlm_response.encode64}"
end
nil
end
def setup_client
@client.use_ssl = @request.ssl?
@client.open_timeout = @request.open_timeout if @request.open_timeout
@client.read_timeout = @request.read_timeout if @request.read_timeout
end
def setup_ssl_auth
ssl = @request.auth.ssl
if @request.auth.ssl?
unless ssl.verify_mode == :none
@client.ca_file = ssl.ca_cert_file if ssl.ca_cert_file
end
# Send client-side certificate regardless of state of SSL verify mode
@client.key = ssl.cert_key
@client.cert = ssl.cert
@client.verify_mode = ssl.openssl_verify_mode
end
@client.ssl_version = ssl.ssl_version if ssl.ssl_version
end
def request_client(type)
request_class = Net::HTTP.const_get(:"#{type.to_s.capitalize}")
request_client = request_class.new @request.url.request_uri, @request.headers
request_client.basic_auth(*@request.auth.credentials) if @request.auth.basic?
if @request.auth.digest?
raise NotSupportedError, "Net::HTTP does not support HTTP digest authentication"
end
request_client
end
def respond_with(response)
headers = response.to_hash
headers.each do |key, value|
headers[key] = value[0] if value.size <= 1
end
body = (response.body.kind_of?(Net::ReadAdapter) ? "" : response.body)
Response.new response.code, headers, body
end
end
end
end