I'm starting with SOAP / PHP and have to send a security. IT's drving me crazy, I have tried a dozen approachesd found here but nothing works. Here is the header excpected by the service - how can I send it?
<soapenv:Envelope
xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/"
xmlns:v1="http://v1.cc.b2c.ws.rcs.buergel.de/">
<soapenv:Header>
<wsse:Security soapenv:mustUnderstand="1"
xmlns:wsse="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-secext-1.0.xsd"
xmlns:wsu="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-utility-1.0.xsd">
<wsse:UsernameToken wsu:Id="UsernameToken-6">
<wsse:Username>99999999</wsse:Username>
<wsse:Password Type="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-username-token-profile-1.0#PasswordText">
99999999
</wsse:Password>
<wsse:Nonce EncodingType="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-soap-message-security-1.0#Base64Binary">
920ffm0dBhbpa4/Q7ZYGsQ==
</wsse:Nonce>
<wsu:Created>2013-07-18T11:01:27.312Z</wsu:Created>
</wsse:UsernameToken>
</wsse:Security>
</soapenv:Header>
</soapenv:Envelope>
I used this approach last: Connecting to WS-Security protected Web Service with PHP and get following error:
public 'faultstring' => string 'An error was discovered processing the header' (length=61) public 'faultcode' => string 'ns1:InvalidSecurity' (length=19)
I really appreciate every help - thanks in advance.
Here is my current php code.
/**
* This function implements a WS-Security digest authentification for PHP.
*
* @access private
* @param string $user
* @param string $password
* @return string
*/
function generateWSSecurity($user, $password)
{
// Creating date using yyyy-mm-ddThh:mm:ssZ format
$tm_created = gmdate('Y-m-d\TH:i:s\Z');
$tm_expires = gmdate('Y-m-d\TH:i:s\Z', gmdate('U') + 180);
// Generating, packing and encoding a random number
$simple_nonce = mt_rand();
$encoded_nonce = base64_encode(pack('H*', $simple_nonce));
// Compiling WSS string
$passdigest = base64_encode(pack('H*',sha1(pack('H*', $simple_nonce) . pack('a*', $tm_created) . pack('a*', $password))));
// Initializing namespaces
$ns_envelope = 'http://schemas.xmlsoap.org/soap/envelope/';
$ns_wsse = 'http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-secext-1.0.xsd';
$ns_wsu = 'http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-utility-1.0.xsd';
$password_type = 'http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-username-token-profile-1.0#PasswordText';
$encoding_type = 'http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-soap-message-security-1.0#Base64Binary';
// Creating WSS identification header using SimpleXML
$root = new \SimpleXMLElement('<root/>');
$envelope = $root->addChild('soapenv:Envelope', null, $ns_envelope);
$soapheader = $envelope->addChild('soapenv:Header');
$security = $soapheader->addChild('wsse:Security', null, $ns_wsse);
$security->addAttribute('soapenv:mustUnderstand','1');
$usernameToken = $security->addChild('wsse:UsernameToken', null, $ns_wsse);
$usernameToken->addChild('wsse:Username', $user, $ns_wsse);
$password = $usernameToken->addChild('wsse:Password', $passdigest, $ns_wsse);
$password->addAttribute('Type', $password_type);
$nonce = $usernameToken->addChild('wsse:Nonce', $encoded_nonce, $ns_wsse);
$nonce->addAttribute('EncodingType', $encoding_type);
$usernameToken->addChild('wsu:Created', $tm_created, $ns_wsu);
// Recovering XML value from that object
$root->registerXPathNamespace('soapenv', $ns_envelope);
$full = $root->xpath('/root/soapenv:Envelope');
$auth = $full[0]->asXML();
return $auth;
}
function soapClientWSSecurityHeader($user, $password)
{
return new \SoapHeader('http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-secext-1.0.xsd',
'Security', new \SoapVar(generateWSSecurity($user, $password), XSD_ANYXML), true
);
}
$client = new \SoapClient('https://webservice?wsdl');
$client->__setSoapHeaders(soapClientWSSecurityHeader('user', 'pass'));
It works so far, but now I get this if I call any operation:
public 'faultstring' => string 'Bad credentials' (length=15) public 'faultcode' => string 'soap:Server' (length=11)
Any ideas?