[Home] You are not logged in. You can create an account here or login here

Just my stuff

Consume SSL protected Web Services with soap4r

After hours of google this deserves a blog post. I did not find a clear example about this, so I am writing one.
I had the need to call a .NET Web Service over https with mutual authentication and basic authentication.
First of all I installed the soap4r gem, then the httpclient gem (because that one supports basic authentication).
Then I made a folder called “certs” with all the certificates and key files I had:
- ca.cer – the certificate of the certification authority that signed the server certificate
- server.cer – the certificate of the server (signed by the guys who own ca.cer)
- client.cer – the client certificate I need to send along the request to get the content
- client.key – the key file for the client certificate
That’s all the certs and key files I needed.
Now it was time to try to get the wsdl:
require 'http-access2'

url = 'https://secure.example.com/web_service/wsdl'

client = HTTPAccess2::Client.new()
client.ssl_config.set_client_cert_file('certs/client.cer', 'certs/client.key')
client.ssl_config.set_trust_ca('certs/ca.cer')
client.set_basic_auth(url, 'username', 'password')
puts client.get(url).content

This worked.
Time to try soap4r:
require 'rubygems'  #if you installed httpclient with rubygems you need this
require 'soap/wsdlDriver'

#this validates the server certificate
#so you can be sure that the server you are
#sending data to is the server you have the
#certificate of in certs/server.cer
def validate_certificate(is_ok, ctx)
  cert = ctx.current_cert
  unless (cert.subject.to_s == cert.issuer.to_s) #check the server certificate only
    is_ok &&= File.open('certs/server.cer').read == ctx.current_cert.to_pem
  end
  is_ok
end 

wsdl = 'https://secure.example.com/web_service/wsdl'
driver = SOAP::WSDLDriverFactory.new(wsdl).create_rpc_driver
#driver.wiredump_dev = STDOUT

driver.options['protocol.http.ssl_config.verify_callback'] = method(:validate_certificate)

results = driver.web_service_method(arg1, arg2)
p results

To tell soap4r that you want basic authentication and where the certificate files are, you need to create a soap/property file with the following content:
client.protocol.http.basic_auth.1.url = https://secure.example.com/web_service/wsdl
client.protocol.http.basic_auth.1.userid = username
client.protocol.http.basic_auth.1.password = password

client.protocol.http.ssl_config.client_cert = certs/client.cer
client.protocol.http.ssl_config.client_key = certs/client.key
client.protocol.http.ssl_config.ca_file = certs/ca.cer
client.protocol.http.ssl_config.verify_mode = OpenSSL::SSL::VERIFY_PEER
client.protocol.http.ssl_config.ciphers = ALL
client.protocol.http.ssl_config.verify_depth = 1

This file is loaded at startup (you can find other options in soap/lib/soap/httpconfigloader.rb), and configures the ssl and basic auth stuff for soap4r.

in_place_editor on rails 2.0 and rjs templates

All right. The in_place_editor is gone from rails 2.0 as everyone knows, so now we need to install it as a plugin. No problem. But I needed something particular this time: On this site you can upload pictures and make them password protected. So I had the in_place_editor for the password, and right below it a link pointing to the picture with the password (if you sign up you can see it) like this:


[Picture is password protected:] (in_place_editor) verydifficultpassword
[Link to this picture with password:] click


When a user updates the password, I wanted the link to update as well.
To do it I had to call the in_place_editor with :script => true like this
<span>
Picture is password protected: 
<span id="picture_pass_prot">
<%=h @picture.password_protected||'-no-' %>
</span>
</span>

<span id="picture_pass_prot_url">
<% if @picture.password_protected %>
<%= render :partial => 'pic_pass_url' %>
<% end %>
</span>

<%= in_place_editor 'picture_pass_prot', 
    {:url => url_for (:action => 'update_password_protected', 
                      :id => @picture), 
    :script => true} %>

the pic_pass_url partial only renders the url for the password protected image.
The update_password_protected action looks something like this
    @picture = Picture.find(params[:id])
    @picture.password_protected = value
    @picture.save

and the update_password_protected.rjs looks like this
page[:picture_pass_prot].replace_html(@picture.password_protected)
page[:picture_pass_prot_url].replace_html :partial => 'pic_pass_url'

So we did 2 things now: we replaced 2 elements in the html, and one of them with a partial.
Oh… and to make this work on rails 2.0.x you actually have to patch the in_place_editor def in in_place_macros_helper.rb, adding the line
    js_options['clickToEditText'] = %('#{options[:click_to_edit_text]}') if options[:click_to_edit_text]
    #ADD THIS LINE HERE
    js_options['htmlResponse'] = !options[:script] if options[:script]
    function << (', ' + options_for_javascript(js_options)) unless js_options.empty?

because “rails’ “in_place_editor” doesn’t let you specify htmlResponse for the underlying Ajax.InPlaceEditor call, which needs to be set to false.” – see here for the details.
Ok… now finally we have our in_place_editor that updates two (or more) html elements using rjs.