Ruby, Soap4R, WSSE Authentication

I’ve been doing some work for a client, where we have to interact with a web service front end for Oracle (which also means that everything reflects Oracle’s “delightful” shortened table names, like UsrAddrsPhn…). I didn’t find a lot out there to do this in Ruby, except this helpful post:

However, it wasn’t quite enough for what we’re doing, as we have to deal with a nonce parameter as well.

Here’s the snippet of code I created:

require 'rubygems'
gem 'soap4r'
require 'base64'

require 'soap/rpc/driver'
require 'soap/wsdlDriver'

wsdl_url = ARGV.shift
proxy    =

# run ruby with -d to see SOAP wiredumps.
proxy.wiredump_dev = STDERR if $DEBUG

require 'soap/header/simplehandler'
require 'soap/element'
require 'xsd/datatypes'

class WsseAuthHeader < SOAP::Header::Handler
  USERNAME  = 'Not really the username'
  PASSWORD  = 'Put your own password here'

  def initialize()
    super(, 'Security'))

  def on_outbound
    security ='wsse:Security')
    security.extraattr['xmlns:wsse'] =
    unt ='wsse:UsernameToken')
    unt.extraattr['xmlns:wsu'] =
    un ='wsse:Username', USERNAME)
    pw ='wsse:Password', PASSWORD)
    pw.extraattr['Type'] =
    # Create a nonce...
    nonce ='wsse:Nonce', Base64.encode64((rand() * 1000000000).to_i.to_s))
    nonce.extraattr['EncodingType'] =
    # The time format returned by XSDDateTime isn't quite right, so we
    # fiddle with it a bit.
                         - 30).to_s.gsub(/..*/, "") + "Z"))
    return, true)

proxy.headerhandler <<

... call proxy methods to interact with web service ...

The key thing is to dress up the outgoing header with all the right XML, including Nonce and Created parameters. This code was created by matching the XML generated by the Java code used to interact with this “JAX-WS” web service.

All in all, it’s a bit of a mess compared to a nice REST style web service. Hopefully the above code will be useful to others who are in a similar situation.

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s