Axis2C, WS-Security & Perl
I've been wanting to build a Perl client to consume an Axis2C web service protected by Rampart/C for a while now. So over the past couple days I took a few hours to figure out how. First I needed a username/password protected service to test with. I decided to use the "math" sample service that comes with the axis2c download. I got axis2c, along with a bunch of other related stuff from WSO2's <a href="http://wso2.com/products/web-services-framework/c/" >Web Services Framework for C</a>. For the most part, this just packages up a lot of Apache open source software like Axis2C and Rampart/C into one tidy tar file. Anyway, the math sample service doesn't come configured with rampart security right out of the gate, so the first thing I had to do was modify the services.xml file in $WSFC_HOME/services/math to add it. Here's what my file looked like after adding the entries for Rampart:
<parameter name="ServiceClass" locked="xsd:false">math</parameter>
<description>
This is a testing service, named 'math' to test multiple operations in the same service
</description>
<module ref="rampart"/> <wsp:Policy xmlns:wsp="http://schemas.xmlsoap.org/ws/2004/09/policy">
<wsp:ExactlyOne>
<wsp:All>
<sp:AsymmetricBinding xmlns:sp="http://schemas.xmlsoap.org/ws/2005/07/securitypolicy">
<wsp:Policy>
<sp:InitiatorToken>
<wsp:Policy>
<sp:X509Token sp:IncludeToken="http://schemas.xmlsoap.org/ws/2005/07/securitypolicy/IncludeToken/Never">
<wsp:Policy>
<sp:WssX509V3Token10/>
</wsp:Policy>
</sp:X509Token>
</wsp:Policy>
</sp:InitiatorToken>
<sp:RecipientToken>
<wsp:Policy>
<sp:X509Token sp:IncludeToken="http://schemas.xmlsoap.org/ws/2005/07/securitypolicy/IncludeToken/Never">
<wsp:Policy>
<sp:WssX509V3Token10/>
</wsp:Policy>
</sp:X509Token>
</wsp:Policy>
</sp:RecipientToken>
<sp:Layout>
<wsp:Policy>
<sp:Strict/>
</wsp:Policy>
</sp:Layout>
</wsp:Policy>
</sp:AsymmetricBinding>
<sp:SignedSupportingTokens xmlns:sp="http://schemas.xmlsoap.org/ws/2005/07/securitypolicy">
<wsp:Policy>
<sp:UsernameToken sp:IncludeToken="http://schemas.xmlsoap.org/ws/2005/07/securitypolicy/IncludeToken/Once"/>
</wsp:Policy>
</sp:SignedSupportingTokens>
<rampc:RampartConfig xmlns:rampc="http://ws.apache.org/rampart/c/policy">
<rampc:PasswordCallbackClass>
/PATH/TO/CALLBACK_LIBRARY.SO
</rampc:PasswordCallbackClass>
</rampc:RampartConfig>
</wsp:All>
</wsp:ExactlyOne>
</wsp:Policy>
/PATH/TO/CALLBACK_LIBRARY.SO
</rampc:PasswordCallbackClass>
-> uri('http://ws.apache.org/axis2/services/math')
-> proxy('http://localhost:9099/axis2/services/math/add');my $param1Element = SOAP::Data->name('param1')->value('1');my $param2Element = SOAP::Data->name('param2')->value('2');my $response = $service_call->add(
$param1Element
, $param2Element
);print "The result is " . $response->dataof('//result')->value . "\n";
-> uri('http://ws.apache.org/axis2/services/math')
-> proxy('http://localhost:9099/axis2/services/math/add');my $param1Element = SOAP::Data->name('param1')->value('1');my $param2Element = SOAP::Data->name('param2')->value('2');my $username = 'alice';
my $password = 'password';my $security=SOAP::Header->name(
"wsse:Security"
)->attr(
{'soap:mustUnderstand'=>1,'xmlns:wsse'=>'http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-secext-1.0.xsd'}
);
my $userToken =SOAP::Header->name(
"wsse:UsernameToken" => \SOAP::Header->value(
SOAP::Header->name(
'wsse:Username'
)->value(
$username
)->type(
''
)
, SOAP::Header->name(
'wsse:Password'
)->value(
$password
)->type(
''
)->attr(
{'Type'=>'http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-username-token-profile-1.0#PasswordText'}
)
)
)->attr({'xmlns:wsu'=>'http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-utility-1.0.xsd'});my $response = $service_call->add(
$param1Element
, $param2Element
, $security->value(
\$userToken
)
);print "The result is " . $response->dataof('//result')->value . "\n";