Friday, May 7, 2010

Adding WSSE security headers in PHP native webservices

I have seen lot of people extending the native soap class to append the wsse security header in the soap header. But didn't see any solution how to do it without extending the soap client native library, and here's the solutions how to do that!!

Step1: Create two classes to create a structure for WSSE headers

class clsWSSEAuth {
private $Username;
private $Password;
function __construct($username, $password) {
$this->Username=$username;
$this->Password=$password;
}
}

class clsWSSEToken {
private $UsernameToken;
function __construct ($innerVal){
$this->UsernameToken = $innerVal;
}
}


Step2: Create Soap Variables for UserName and Password
$username = 1111;
$password = 1111;

//Check with your provider which security name-space they are using.
$strWSSENS = "http://schemas.xmlsoap.org/ws/2002/07/secext";

$objSoapVarUser = new SoapVar($username, XSD_STRING, NULL, $strWSSENS, NULL, $strWSSENS);
$objSoapVarPass = new SoapVar($password, XSD_STRING, NULL, $strWSSENS, NULL, $strWSSENS);


Step3: Create Object for Auth Class and pass in soap var

$objWSSEAuth = new clsWSSEAuth($objSoapVarUser, $objSoapVarPass);


Step4: Create SoapVar out of object of Auth class


$objSoapVarWSSEAuth = new SoapVar($objWSSEAuth, SOAP_ENC_OBJECT, NULL, $strWSSENS, 'UsernameToken', $strWSSENS);


Step5: Create object for Token Class

$objWSSEToken = new clsWSSEToken($objSoapVarWSSEAuth);


Step6: Create SoapVar out of object of Token class
$objSoapVarWSSEToken = new SoapVar($objWSSEToken, SOAP_ENC_OBJECT, NULL, $strWSSENS, 'UsernameToken', $strWSSENS);


Step7: Create SoapVar for 'Security' node
$objSoapVarHeaderVal=new SoapVar($objSoapVarWSSEToken, SOAP_ENC_OBJECT, NULL, $strWSSENS, 'Security', $strWSSENS);


Step8: Create header object out of security soapvar
$objSoapVarWSSEHeader = new SoapHeader($strWSSENS, 'Security', $objSoapVarHeaderVal,true, 'http://abce.com');

//Third parameter here makes 'mustUnderstand=1
//Forth parameter generates 'actor="http://abce.com"'


Step9: Create object of Soap Client
$objClient = new SoapClient($WSDL, $arrOptions);


Step10: Set headers for soapclient object

$objClient->__setSoapHeaders(array($objSoapVarWSSEHeader));


Step 11: Final call to method
$objResponse = $objClient->__soapCall($strMethod, $requestPayloadString);


Same could be found at PHP.net site as comment
http://php.net/manual/en/soapclient.soapclient.php

And you are done!! Donuts !!!