| 1 |  |  | <?php | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 3 |  |  | declare(strict_types=1); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 4 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 5 |  |  | namespace WsdlToPhp\WsSecurity; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 6 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 7 |  |  | class Password extends Element | 
            
                                                                                                            
                            
            
                                    
            
            
                | 8 |  |  | { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 9 |  |  |     public const NAME = 'Password'; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 10 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 11 |  |  |     public const ATTRIBUTE_TYPE = 'Type'; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 12 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 13 |  |  |     public const TYPE_PASSWORD_DIGEST = 'http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-username-token-profile-1.0#PasswordDigest'; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 14 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 15 |  |  |     public const TYPE_PASSWORD_TEXT = 'http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-username-token-profile-1.0#PasswordText'; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 16 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 17 |  |  |     protected string $typeValue; | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 18 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 19 | 18 |  |     public function __construct(string $password, string $typeValue = self::TYPE_PASSWORD_TEXT, int $timestampValue = 0, string $namespace = self::NS_WSSE) | 
            
                                                                        
                            
            
                                    
            
            
                | 20 |  |  |     { | 
            
                                                                        
                            
            
                                    
            
            
                | 21 |  |  |         $this | 
            
                                                                        
                            
            
                                    
            
            
                | 22 | 18 |  |             ->setTypeValue($typeValue) | 
            
                                                                        
                            
            
                                    
            
            
                | 23 | 18 |  |             ->setTimestampValue($timestampValue ? $timestampValue : time()) | 
            
                                                                        
                            
            
                                    
            
            
                | 24 | 18 |  |             ->setNonceValue((string) mt_rand()) | 
            
                                                                        
                            
            
                                    
            
            
                | 25 |  |  |         ; | 
            
                                                                        
                            
            
                                    
            
            
                | 26 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 27 | 18 |  |         parent::__construct(self::NAME, $namespace, $this->convertPassword($password), [ | 
            
                                                                        
                            
            
                                    
            
            
                | 28 |  |  |             self::ATTRIBUTE_TYPE => $typeValue, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 29 |  |  |         ]); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 30 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 31 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 32 | 18 |  |     public function convertPassword(string $password): string | 
            
                                                                                                            
                            
            
                                    
            
            
                | 33 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 34 | 18 |  |         if (self::TYPE_PASSWORD_DIGEST === $this->getTypeValue()) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 35 | 2 |  |             $password = $this->digestPassword($password); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 36 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 37 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 38 | 18 |  |         return $password; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 39 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 40 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 41 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 42 |  |  |      * When generating the password digest, we define values (nonce and timestamp) that can be used in other place. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 43 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 44 | 2 |  |     public function digestPassword(string $password): string | 
            
                                                                                                            
                            
            
                                    
            
            
                | 45 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 46 | 2 |  |         $packedNonce = pack('H*', $this->getNonceValue()); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 47 | 2 |  |         $packedTimestamp = pack('a*', $this->getTimestampValue(true)); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 48 | 2 |  |         $packedPassword = pack('a*', $password); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 49 | 2 |  |         $hash = sha1($packedNonce.$packedTimestamp.$packedPassword); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 50 | 2 |  |         $packedHash = pack('H*', $hash); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 51 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 52 | 2 |  |         return base64_encode($packedHash); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 53 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 54 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 55 | 18 |  |     public function getTypeValue(): string | 
            
                                                                                                            
                            
            
                                    
            
            
                | 56 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 57 | 18 |  |         return $this->typeValue; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 58 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 59 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 60 | 18 |  |     public function setTypeValue(string $typeValue): self | 
            
                                                                                                            
                            
            
                                    
            
            
                | 61 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 62 | 18 |  |         $this->typeValue = $typeValue; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 63 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 64 | 18 |  |         return $this; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 65 |  |  |     } | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 66 |  |  | } | 
            
                                                        
            
                                    
            
            
                | 67 |  |  |  |