2

I want to create a WSS header to authentificate on secured web services.

I can do it using an ugly :

    $auth = '
     <wsse:Security SOAP-ENV:mustUnderstand="1" xmlns:wsse="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-secext-1.0.xsd">
        <wsu:Timestamp wsu:Id="Timestamp-28" xmlns:wsu="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-utility-1.0.xsd">
            <wsu:Created>' . $timestamp . '</wsu:Created>
            <wsu:Expires>' . $timestampExpires . '</wsu:Expires>
        </wsu:Timestamp>
        <wsse:UsernameToken wsu:Id="UsernameToken-27" xmlns:wsu="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-utility-1.0.xsd">
            <wsse:Username>' . $user . '</wsse:Username>
            <wsse:Password Type="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-username-token-profile-1.0#PasswordDigest">' . $passdigest . '</wsse:Password>
            <wsse:Nonce EncodingType="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-soap-message-security-1.0#Base64Binary">' . $encodedNonce . '</wsse:Nonce>
            <wsu:Created>' . $timestamp . '</wsu:Created>
        </wsse:UsernameToken>
     </wsse:Security>';

I am now trying to do it cleaner, using SimpleXML.

But if I try to do a simple :

    $xml = new SimpleXMLElement('<wsse:Security/>', 0, false, 'wsse');

I get :

warning: SimpleXMLElement::__construct() [simplexmlelement.--construct]: namespace error : Namespace prefix wsse on Security is not defined in

I think I miss something with the way to create namespaced xmls, can you give me some hints?

Alain Tiemblo
  • 36,099
  • 17
  • 121
  • 153

1 Answers1

0

I found a way to solve my problem :

$root = new SimpleXMLElement('<root/>');

$security = $root->addChild('wsse:Security', 'test', 'http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-secext-1.0.xsd');

$root->registerXPathNamespace('wsse', 'http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-secext-1.0.xsd');
$auth = $root->xpath('/root/wsse:Security');
echo htmlentities($auth[0]->asXML());

Displays :

<wsse:Security xmlns:wsse="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-secext-1.0.xsd">test</wsse:Security> 

And also, there is a mistake in my XML, I put a SOAP-ENV:mustUnderstand="1" but I never define the SOAP-ENV namespace.

Alain Tiemblo
  • 36,099
  • 17
  • 121
  • 153