| 1 |  |  | <?php | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 3 |  |  | declare(strict_types=1); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 4 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 5 |  |  | namespace WsdlToPhp\WsSecurity; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 6 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 7 |  |  | use SoapHeader; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 8 |  |  | use SoapVar; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 9 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 10 |  |  | class WsSecurity | 
            
                                                                                                            
                            
            
                                    
            
            
                | 11 |  |  | { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 12 |  |  |     protected Security $security; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 13 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 14 | 18 |  |     public function __construct( | 
            
                                                                                                            
                            
            
                                    
            
            
                | 15 |  |  |         string $username, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 16 |  |  |         string $password, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 17 |  |  |         bool $passwordDigest = false, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 18 |  |  |         int $addCreated = 0, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 19 |  |  |         int $addExpires = 0, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 20 |  |  |         bool $mustUnderstand = false, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 21 |  |  |         ?string $actor = null, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 22 |  |  |         ?string $usernameId = null, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 23 |  |  |         bool $addNonce = true, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 24 |  |  |         string $envelopeNamespace = Security::ENV_NAMESPACE | 
            
                                                                                                            
                            
            
                                    
            
            
                | 25 |  |  |     ) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 26 |  |  |         $this | 
            
                                                                                                            
                            
            
                                    
            
            
                | 27 | 18 |  |             ->initSecurity($mustUnderstand, $actor, $envelopeNamespace) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 28 | 18 |  |             ->setUsernameToken($username, $usernameId) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 29 | 18 |  |             ->setPassword($password, $passwordDigest, $addCreated) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 30 | 18 |  |             ->setNonce($addNonce) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 31 | 18 |  |             ->setCreated($addCreated) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 32 | 18 |  |             ->setTimestamp($addCreated, $addExpires) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 33 |  |  |         ; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 34 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 35 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 36 | 18 |  |     public function getSecurity(): ?Security | 
            
                                                                                                            
                            
            
                                    
            
            
                | 37 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 38 | 18 |  |         return $this->security; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 39 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 40 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 41 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 42 |  |  |      * @return SoapHeader|SoapVar | 
            
                                                                                                            
                            
            
                                    
            
            
                | 43 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 44 | 18 |  |     public static function createWsSecuritySoapHeader( | 
            
                                                                                                            
                            
            
                                    
            
            
                | 45 |  |  |         string $username, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 46 |  |  |         string $password, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 47 |  |  |         bool $passwordDigest = false, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 48 |  |  |         int $addCreated = 0, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 49 |  |  |         int $addExpires = 0, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 50 |  |  |         bool $returnSoapHeader = true, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 51 |  |  |         bool $mustUnderstand = false, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 52 |  |  |         ?string $actor = null, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 53 |  |  |         ?string $usernameId = null, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 54 |  |  |         bool $addNonce = true, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 55 |  |  |         string $envelopeNamespace = Security::ENV_NAMESPACE | 
            
                                                                                                            
                            
            
                                    
            
            
                | 56 |  |  |     ) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 57 | 18 |  |         $self = new WsSecurity($username, $password, $passwordDigest, $addCreated, $addExpires, $mustUnderstand, $actor, $usernameId, $addNonce, $envelopeNamespace); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 58 | 18 |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 59 | 14 |  |         return $self->getSoapHeader($returnSoapHeader, $mustUnderstand, $actor); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 60 | 8 |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 61 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 62 |  |  |     public function getSoapHeader(bool $returnSoapHeader = true, bool $mustUnderstand = false, ?string $actor = null): object | 
            
                                                                                                            
                            
            
                                    
            
            
                | 63 | 6 |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 64 |  |  |         if ($returnSoapHeader) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 65 |  |  |             if (!empty($actor)) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 66 | 4 |  |                 return new SoapHeader(Element::NS_WSSE, Security::NAME, new SoapVar($this->getSecurity()->toSend(), XSD_ANYXML), $mustUnderstand, $actor); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 67 |  |  |             } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 68 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 69 | 18 |  |             return new SoapHeader(Element::NS_WSSE, Security::NAME, new SoapVar($this->getSecurity()->toSend(), XSD_ANYXML), $mustUnderstand); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 70 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 71 | 18 |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 72 |  |  |         return new SoapVar($this->getSecurity()->toSend(), XSD_ANYXML); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 73 | 18 |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 74 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 75 |  |  |     protected function initSecurity(bool $mustUnderstand = false, ?string $actor = null, string $envelopeNamespace = Security::ENV_NAMESPACE): self | 
            
                                                                                                            
                            
            
                                    
            
            
                | 76 | 18 |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 77 |  |  |         $this->security = new Security($mustUnderstand, $actor, Security::NS_WSSE, $envelopeNamespace); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 78 | 18 |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 79 | 18 |  |         return $this; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 80 | 18 |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 81 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 82 | 18 |  |     protected function setUsernameToken(string $username, ?string $usernameId = null): self | 
            
                                                                                                            
                            
            
                                    
            
            
                | 83 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 84 |  |  |         $usernameToken = new UsernameToken($usernameId); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 85 | 18 |  |         $usernameToken->setUsername(new Username($username)); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 86 |  |  |         $this->security->setUsernameToken($usernameToken); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 87 | 18 |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 88 |  |  |         return $this; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 89 | 18 |  |     } | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 90 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 91 |  |  |     protected function setPassword(string $password, bool $passwordDigest = false, int $addCreated = 0): self | 
            
                                                                        
                            
            
                                    
            
            
                | 92 | 18 |  |     { | 
            
                                                                        
                            
            
                                    
            
            
                | 93 |  |  |         $this->getUsernameToken()->setPassword(new Password($password, $passwordDigest ? Password::TYPE_PASSWORD_DIGEST : Password::TYPE_PASSWORD_TEXT, $addCreated)); | 
            
                                                                        
                            
            
                                    
            
            
                | 94 | 18 |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 95 | 16 |  |         return $this; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 96 | 16 |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 97 | 16 |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 98 |  |  |     protected function setNonce(bool $addNonce): self | 
            
                                                                                                            
                            
            
                                    
            
            
                | 99 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 100 |  |  |         if ($addNonce) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 101 | 18 |  |             $nonceValue = $this->getPassword()->getNonceValue(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 102 |  |  |             if (!empty($nonceValue)) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 103 |  |  |                 $this->getUsernameToken()->setNonce(new Nonce($nonceValue)); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 104 | 18 |  |             } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 105 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 106 | 18 |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 107 | 18 |  |         return $this; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 108 | 18 |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 109 | 18 |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 110 |  |  |     protected function setCreated(int $addCreated): self | 
            
                                                                                                            
                            
            
                                    
            
            
                | 111 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 112 | 18 |  |         $passwordDigest = $this->getPassword()->getTypeValue(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 113 |  |  |         $timestampValue = $this->getPassword()->getTimestampValue(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 114 |  |  |         if (($addCreated || Password::TYPE_PASSWORD_DIGEST === $passwordDigest) && 0 < $timestampValue) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 115 | 18 |  |             $this->getUsernameToken()->setCreated(new Created($timestampValue)); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 116 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 117 | 18 |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 118 | 18 |  |         return $this; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 119 | 2 |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 120 | 2 |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 121 | 2 |  |     protected function setTimestamp(int $addCreated = 0, int $addExpires = 0): self | 
            
                                                                                                            
                            
            
                                    
            
            
                | 122 | 2 |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 123 |  |  |         $timestampValue = $this->getPassword()->getTimestampValue(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 124 |  |  |         if (!$timestampValue || (0 === $addCreated && 0 === $addExpires)) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 125 | 18 |  |             return $this; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 126 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 127 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 128 | 18 |  |         $timestamp = new Timestamp(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 129 |  |  |         if (0 < $addCreated) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 130 | 18 |  |             $timestamp->setCreated(new Created($timestampValue)); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 131 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 132 |  |  |         if (0 < $addExpires) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 133 | 18 |  |             $timestamp->setExpires(new Expires($timestampValue, $addExpires)); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 134 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 135 | 18 |  |         $this->security->setTimestamp($timestamp); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 136 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 137 |  |  |         return $this; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 138 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 139 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 140 |  |  |     protected function getUsernameToken(): ?UsernameToken | 
            
                                                                                                            
                            
            
                                    
            
            
                | 141 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 142 |  |  |         return $this->security->getUsernameToken(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 143 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 144 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 145 |  |  |     protected function getPassword(): ?Password | 
            
                                                                                                            
                            
            
                                    
            
            
                | 146 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 147 |  |  |         return $this->getUsernameToken()->getPassword(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 148 |  |  |     } | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 149 |  |  | } | 
            
                                                        
            
                                    
            
            
                | 150 |  |  |  |