Creating a SOAP header with PHP using SoapClient with security mode "TransportWithMessageCredential“

How can I construct a SOAP header using the "TransportWithMessageCredential" mode in PHP using the SoapClient. I'm using the SoapClient as this seems to be the best solution. The following is from the given documentation:

The Webservice uses the security mode "TransportWithMessageCredential“. For secure transmission an SSL certificate is being used. Furthermore, for the security of the message exchange, a combination of username and password is required. The username and password are transmitted in the SOAP-Header.

Example:

<soapenv:Header>
    <wsse:Security soapenv:mustUnderstand="1" xmlns:wsse="http://docs.oasisopen.
    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 wssecurityutility-1.0.xsd">
        <wsse:UsernameToken wsu:Id="UsernameToken-37">
            <wsse:Username>MyUsername</wsse:Username>
            <wsse:Password Type="http://docs.oasisopen.org/wss/2004/01/oasis-200401-wss-username-token-profile-1.0#PasswordText">MyPassword!</wsse:Password>
        <wsse:Nonce EncodingType="http://docs.oasisopen.org/wss/2004/01/oasis-200401-wss-soap-message-security-1.0#Base64Binary">vAvnhyzl+yP8Yb8ZVdKnMw==</wsse:Nonce>
        <wsu:Created>2014-03-17T13:08:02.795Z</wsu:Created>
        </wsse:UsernameToken>
    </wsse:Security>
</soapenv:Header>  

Where "MyUserName" and "MyPassword!" ofc. is interchanged with the actual login information.

wsdl's are available for every functionality offered.

Upvotes: 1

Views: 1486

Answers (1)

This class returns a header with the above defined format:)

class WsseAuthHeader extends SoapHeader
{
    private $wssNs = 'http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-secext-1.0.xsd';
    private $wsuNs = 'http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-utility-1.0.xsd';
    private $passType = 'http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-username-token-profile-1.0#PasswordText';
    private $nonceType = 'http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-soap-message-security-1.0#Base64Binary';
    private $username = 'Username';
    private $password = 'Password';


    function __construct()
    {
        $created = gmdate('Y-m-d\TH:i:s\Z');
        $nonce = mt_rand();
        $encodedNonce = base64_encode(pack('H*', sha1(pack('H*', $nonce) . pack('a*', $created) . pack('a*', $this->password))));

        // Creating WSS identification header using SimpleXML
        $root = new SimpleXMLElement('<root/>');

        $security = $root->addChild('wsse:Security', null, $this->wssNs);

        $usernameToken = $security->addChild('wsse:UsernameToken', null, $this->wssNs);
        $usernameToken->addChild('wsse:Username', $this->username, $this->wssNs);
        $passNode = $usernameToken->addChild('wsse:Password', htmlspecialchars($this->password, ENT_XML1, 'UTF-8'), $this->wssNs);
        $passNode->addAttribute('Type', $this->passType);

        $nonceNode = $usernameToken->addChild('wsse:Nonce', $encodedNonce, $this->wssNs);
        $nonceNode->addAttribute('EncodingType', $this->nonceType);
        $usernameToken->addChild('wsu:Created', $created, $this->wsuNs);
        // Recovering XML value from that object
        $root->registerXPathNamespace('wsse', $this->wssNs);
        $full = $root->xpath('/root/wsse:Security');
        $auth = $full[0]->asXML();

        parent::SoapHeader($this->wssNs, 'Security', new SoapVar($auth, XSD_ANYXML), true);

    }
};

Upvotes: 1

Related Questions