From e8127f458212f525b52f8207bcedd76be8282dc4 Mon Sep 17 00:00:00 2001 From: Tiago Fernandes Date: Sat, 5 Dec 2009 11:36:47 +0000 Subject: [PATCH 1/2] handsoap path: set up ssl client cert --- lib/handsoap/http/drivers/http_client_driver.rb | 2 ++ lib/handsoap/http/request.rb | 12 +++++++++++- lib/handsoap/service.rb | 17 +++++++++++++---- 3 files changed, 26 insertions(+), 5 deletions(-) diff --git a/lib/handsoap/http/drivers/http_client_driver.rb b/lib/handsoap/http/drivers/http_client_driver.rb index efd99b4..c6f92bc 100644 --- a/lib/handsoap/http/drivers/http_client_driver.rb +++ b/lib/handsoap/http/drivers/http_client_driver.rb @@ -16,6 +16,8 @@ def send_http_request(request) domain = request.url.match(/^(http(s?):\/\/[^\/]+\/)/)[1] http_client.set_auth(domain, request.username, request.password) end + http_client.ssl_config.set_trust_ca(request.trust_ca_file) if request.trust_ca_file + http_client.ssl_config.set_client_cert_file(request.client_cert_file,request.client_cert_key_file) if request.client_cert_file and request.client_cert_key_file # pack headers headers = request.headers.inject([]) do |arr, (k,v)| arr + v.map {|x| [k,x] } diff --git a/lib/handsoap/http/request.rb b/lib/handsoap/http/request.rb index 049cfd2..04f2e5f 100644 --- a/lib/handsoap/http/request.rb +++ b/lib/handsoap/http/request.rb @@ -5,7 +5,7 @@ module Http # Represents a HTTP Request. class Request - attr_reader :url, :http_method, :headers, :body, :username, :password + attr_reader :url, :http_method, :headers, :body, :username, :password, :trust_ca_file, :client_cert_file, :client_cert_key_file attr_writer :body, :http_method def initialize(url, http_method = :get) @url = url @@ -14,6 +14,16 @@ def initialize(url, http_method = :get) @body = nil @username = nil @password = nil + @trust_ca_file = nil + @client_cert_file = nil + @client_cert_key_file = nil + end + def set_trust_ca_file(ca_file) + @trust_ca_file = ca_file + end + def set_client_cert_files(client_cert_file,client_cert_key_file) + @client_cert_file = client_cert_file + @client_cert_key_file = client_cert_key_file end def set_auth(username, password) @username = username diff --git a/lib/handsoap/service.rb b/lib/handsoap/service.rb index 99b49e8..4c3388e 100644 --- a/lib/handsoap/service.rb +++ b/lib/handsoap/service.rb @@ -203,7 +203,11 @@ def invoke(action, options = { :soap_action => :auto }, &block) # :yields: Hands elsif options[:soap_action] == :none options[:soap_action] = nil end - doc = make_envelope do |body| + doc = make_envelope do |body,header| + options[:soap_header].each_pair do |k,v| + header.add k,v + end + body.add action end if block_given? @@ -215,7 +219,7 @@ def invoke(action, options = { :soap_action => :auto }, &block) # :yields: Hands } headers["SOAPAction"] = options[:soap_action] unless options[:soap_action].nil? on_before_dispatch - request = make_http_request(self.uri, doc.to_s, headers) + request = make_http_request(self.uri, doc.to_s, headers,options[:http_options]) response = http_driver_instance.send_http_request(request) parse_http_response(response) end @@ -347,8 +351,13 @@ def debug(message = nil) #:nodoc: end end - def make_http_request(uri, post_body, headers) + def make_http_request(uri, post_body, headers,http_options=nil) request = Handsoap::Http::Request.new(uri, :post) + + # SSL CA AND CLIENT CERTIFICATES + request.set_trust_ca_file(http_options[:trust_ca_file]) + request.set_client_cert_files(http_options[:client_cert_file],http_options[:client_cert_key_file]) + headers.each do |key, value| request.add_header(key, value) end @@ -399,7 +408,7 @@ def make_envelope # :yields: Handsoap::XmlMason::Element self.class.fire_on_create_document doc # deprecated .. use instance method on_create_document(doc) if block_given? - yield doc.find("Body") + yield doc.find("Body"),doc.find("Header") end return doc end From 445214a366225fef1cb6232686dd94e058a52025 Mon Sep 17 00:00:00 2001 From: Tiago Fernandes Date: Mon, 18 Oct 2010 20:25:01 +0100 Subject: [PATCH 2/2] add ssl_verify_mode options. its possible to set the ssl verify mode from http_options with :ssl_verify_mode => OpenSSL::SSL::VERIFY_NONE --- lib/handsoap/http/drivers/http_client_driver.rb | 1 + lib/handsoap/http/request.rb | 6 +++++- lib/handsoap/service.rb | 1 + 3 files changed, 7 insertions(+), 1 deletion(-) diff --git a/lib/handsoap/http/drivers/http_client_driver.rb b/lib/handsoap/http/drivers/http_client_driver.rb index c6f92bc..c86e1a9 100644 --- a/lib/handsoap/http/drivers/http_client_driver.rb +++ b/lib/handsoap/http/drivers/http_client_driver.rb @@ -18,6 +18,7 @@ def send_http_request(request) end http_client.ssl_config.set_trust_ca(request.trust_ca_file) if request.trust_ca_file http_client.ssl_config.set_client_cert_file(request.client_cert_file,request.client_cert_key_file) if request.client_cert_file and request.client_cert_key_file + http_client.ssl_config.verify_mode = request.ssl_verify_mode if request.ssl_verify_mode # pack headers headers = request.headers.inject([]) do |arr, (k,v)| arr + v.map {|x| [k,x] } diff --git a/lib/handsoap/http/request.rb b/lib/handsoap/http/request.rb index 04f2e5f..db13fa7 100644 --- a/lib/handsoap/http/request.rb +++ b/lib/handsoap/http/request.rb @@ -5,7 +5,7 @@ module Http # Represents a HTTP Request. class Request - attr_reader :url, :http_method, :headers, :body, :username, :password, :trust_ca_file, :client_cert_file, :client_cert_key_file + attr_reader :url, :http_method, :headers, :body, :username, :password, :trust_ca_file, :client_cert_file, :client_cert_key_file,:ssl_verify_mode attr_writer :body, :http_method def initialize(url, http_method = :get) @url = url @@ -17,6 +17,7 @@ def initialize(url, http_method = :get) @trust_ca_file = nil @client_cert_file = nil @client_cert_key_file = nil + @ssl_verify_mode = nil end def set_trust_ca_file(ca_file) @trust_ca_file = ca_file @@ -25,6 +26,9 @@ def set_client_cert_files(client_cert_file,client_cert_key_file) @client_cert_file = client_cert_file @client_cert_key_file = client_cert_key_file end + def set_ssl_verify_mode(mode) + @ssl_verify_mode = mode + end def set_auth(username, password) @username = username @password = password diff --git a/lib/handsoap/service.rb b/lib/handsoap/service.rb index c057896..93b429c 100644 --- a/lib/handsoap/service.rb +++ b/lib/handsoap/service.rb @@ -406,6 +406,7 @@ def make_http_request(uri, post_body, headers, http_options=nil) if http_options request.set_trust_ca_file(http_options[:trust_ca_file]) if http_options[:trust_ca_file] request.set_client_cert_files(http_options[:client_cert_file], http_options[:client_cert_key_file]) if http_options[:client_cert_file] && http_options[:client_cert_key_file] + request.set_ssl_verify_mode(http_options[:ssl_verify_mode]) if http_options[:ssl_verify_mode] end headers.each do |key, value|