More information

xWeb: Ruby

Authenticate

XWeb: Authenticate Ruby Sample

This is a case study for xWeb: Authenticate.

Calling Authenticate with Ruby

The code below demonstrates two ways of executing soap requests. The first fetches the WSDL file from the server each time and parses it, and the second relies on a static hand-coded definition. Both use the same authentication token handling class.

Code

require "soap/rpc/driver"
require 'soap/header/simplehandler'
require 'soap/wsdlDriver'
 
#The following should be set according to the client installation
client_server = 'http://clientserver/xweb/Secure/netFORUMXML.asmx'
client_namespace = "http://www.avectra.com/2005/"
client_userName = 'xWebUserName'
client_password = 'xWebUserPassword'
 
#this is for debugging purposes
show_traffic = false
 
#when parsing the WSDL some errors will be generated, and we may want to hide them
def suppress_warning
back = $VERBOSE
$VERBOSE = nil
begin
yield
ensure
$VERBOSE = back
end
end
 
#This is the basic class that will handle catching, storing, and injecting the authentication token
#into the auth headers
class ClientAuthHeaderHandler < SOAP::Header::SimpleHandler
AuthTokenHeader = XSD::QName.new("http://www.avectra.com/2005/", "AuthorizationToken")
 
def initialize(token = nil)
super(AuthTokenHeader)
@authtoken = token
end
 
def on_simple_outbound
if @authtoken
{ "Token" => @authtoken }
end
end
 
def on_simple_inbound(header, mustunderstand)
@authtoken = header["Token"]
end
end
 
wiredump_dev=STDERR if show_traffic == true
 
print "BEGINNING WSDL REQUEST\n"
wsdl = suppress_warning{
SOAP::WSDLDriverFactory.new(client_server+"?WSDL").create_rpc_driver
}
wsdl.default_encodingstyle = SOAP::EncodingStyle::ASPDotNetHandler::Namespace
wsdl.headerhandler << ClientAuthHeaderHandler.new()
wsdl.wiredump_dev=wiredump_dev
 
wsdl.Authenticate({'userName'=>client_userName,'password'=>client_password})
#note that the parameters come in a named hash, cf example below
result = wsdl.GetIndividualInformation({'IndividualKey'=>"292aaf95-6f9c-4d89-bee0-6a8f6877c7a9"})
print("Result: ",result.inspect,"\n\n")
#Note that when using the wsdl the result comes in an object called RequestNameResult, as opposed to the response using the bare request below
print result['GetIndividualInformationResult']['IndividualObjects']['IndividualObject']['ind_first_name']
print "\n\n"
 
print "\nBEGINNING BARE REQUEST\n"
service = SOAP::RPC::Driver.new(client_server, client_namespace)
service.default_encodingstyle = SOAP::EncodingStyle::ASPDotNetHandler::Namespace
#Because we aren't using the wsdl, we have to identify the methods to use and their parameters
service.add_method_with_soapaction('Authenticate',client_namespace+"Authenticate", 'userName','password')
service.add_method_with_soapaction('GetIndividualInformation',client_namespace+"GetIndividualInformation", 'IndividualKey')
service.wiredump_dev=wiredump_dev
service.headerhandler << ClientAuthHeaderHandler.new()
 
#note that in this case, the parameters can be sent directly, no hash required
service.Authenticate(client_userName, client_password)
 
result = service.GetIndividualInformation("292aaf95-6f9c-4d89-bee0-6a8f6877c7a9")
print("Result: ",result.inspect,"\n\n")
#As promised, this time there is no GetIndividualInformationResult object.
#Since variables with leading capital letters are special in ruby, the soap mapping lowercases the first letter
#either style of access works fine, it is individual preference
print result.individualObjects.individualObject.ind_first_name
print "\n\n"