|
7 | 7 |
|
8 | 8 | class WsseAuthHeader extends SoapHeader |
9 | 9 | { |
| 10 | + // Namespaces |
| 11 | + private $ns_wsse = 'http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-secext-1.0.xsd'; |
| 12 | + private $ns_wsu = 'http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-utility-1.0.xsd'; |
| 13 | + private $password_type = 'http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-username-token-profile-1.0#PasswordDigest'; |
| 14 | + private $encoding_type = 'http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-soap-message-security-1.0#Base64Binary'; |
10 | 15 |
|
11 | | - private $wss_ns = 'http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-secext-1.0.xsd'; |
12 | | - private $wsu_ns = 'http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-utility-1.0.xsd'; |
13 | | - |
14 | | - function __construct($user, $pass) |
| 16 | + function __construct($username, $password, $wsse_username_token = null) |
15 | 17 | { |
16 | | - $created = gmdate('Y-m-d\TH:i:s\Z'); |
17 | | - $nonce = mt_rand(); |
18 | | - $passdigest = base64_encode(pack('H*', sha1(pack('H*', $nonce) . pack('a*', $created) . pack('a*', $pass)))); |
19 | | - |
20 | | - $auth = new \stdClass(); |
21 | | - $auth->Username = new SoapVar($user, XSD_STRING, null, $this->wss_ns, null, $this->wss_ns); |
22 | | - $auth->Password = new SoapVar($pass, XSD_STRING, null, $this->wss_ns, null, $this->wss_ns); |
23 | | - $auth->Nonce = new SoapVar($passdigest, XSD_STRING, null, $this->wss_ns, null, $this->wss_ns); |
24 | | - $auth->Created = new SoapVar($created, XSD_STRING, null, $this->wss_ns, null, $this->wsu_ns); |
25 | | - |
26 | | - $username_token = new \stdClass(); |
27 | | - $username_token->UsernameToken = |
28 | | - new SoapVar($auth, SOAP_ENC_OBJECT, null, $this->wss_ns, 'UsernameToken', $this->wss_ns); |
29 | | - |
30 | | - $security_sv = new SoapVar( |
31 | | - new SoapVar($username_token, SOAP_ENC_OBJECT, null, $this->wss_ns, 'UsernameToken', $this->wss_ns), |
32 | | - SOAP_ENC_OBJECT, null, $this->wss_ns, 'Security', $this->wss_ns); |
33 | | - parent::__construct($this->wss_ns, 'Security', $security_sv, true); |
| 18 | + $simple_nonce = mt_rand(); |
| 19 | + $created_at = gmdate('Y-m-d\TH:i:s\Z'); |
| 20 | + $encoded_nonce = base64_encode($simple_nonce); |
| 21 | + $password_digest = base64_encode(sha1($simple_nonce . $created_at . $password, true)); |
| 22 | + |
| 23 | + // Creating WSS identification header using SimpleXML |
| 24 | + $root = new \SimpleXMLElement('<root/>'); |
| 25 | + $security = $root->addChild('wsse:Security', null, $this->ns_wsse); |
| 26 | + $usernameToken = $security->addChild('wsse:UsernameToken', null, $this->ns_wsse); |
| 27 | + if($wsse_username_token){ |
| 28 | + $usernameToken->addAttribute('wsu:Id', $wsse_username_token, $this->ns_wsu); |
| 29 | + } |
| 30 | + $usernameToken->addChild('Username', $username, $this->ns_wsse); |
| 31 | + $usernameToken->addChild('Password', $password_digest, $this->ns_wsse)->addAttribute('Type', $this->password_type); |
| 32 | + $usernameToken->addChild('Nonce', $encoded_nonce, $this->ns_wsse)->addAttribute('EncodingType', $this->encoding_type); |
| 33 | + $usernameToken->addChild('Created', $created_at, $this->ns_wsu); |
| 34 | + |
| 35 | + // Recovering XML value from that object |
| 36 | + $root->registerXPathNamespace('wsse', $this->ns_wsse); |
| 37 | + $full = $root->xpath('/root/wsse:Security'); |
| 38 | + $auth = $full[0]->asXML(); |
| 39 | + parent::__construct($this->ns_wsse, 'Security', new SoapVar($auth, XSD_ANYXML), true); |
34 | 40 | } |
35 | 41 | } |
0 commit comments