| 1 |  |  | <?php | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 3 |  |  | namespace LE_ACME2\Authorizer; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 4 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 5 |  |  | use LE_ACME2\Response; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 6 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 7 |  |  | use LE_ACME2\Cache; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 8 |  |  | use LE_ACME2\Utilities; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 9 |  |  | use LE_ACME2\Exception; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 10 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 11 |  |  | use LE_ACME2\Account; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 12 |  |  | use LE_ACME2\Order; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 13 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 14 |  |  | abstract class AbstractAuthorizer { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 15 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 16 |  |  |     protected $_account; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 17 |  |  |     protected $_order; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 18 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 19 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 20 |  |  |      * AbstractAuthorizer constructor. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 21 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 22 |  |  |      * @param Account $account | 
            
                                                                                                            
                            
            
                                    
            
            
                | 23 |  |  |      * @param Order $order | 
            
                                                                                                            
                            
            
                                    
            
            
                | 24 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 25 |  |  |      * @throws Exception\InvalidResponse | 
            
                                                                                                            
                            
            
                                    
            
            
                | 26 |  |  |      * @throws Exception\RateLimitReached | 
            
                                                                                                            
                            
            
                                    
            
            
                | 27 |  |  |      * @throws Exception\ExpiredAuthorization | 
            
                                                                                                            
                            
            
                                    
            
            
                | 28 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 29 |  |  |     public function __construct(Account $account, Order $order) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 30 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 31 |  |  |         $this->_account = $account; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 32 |  |  |         $this->_order = $order; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 33 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 34 |  |  |         $this->_fetchAuthorizationResponses(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 35 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 36 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 37 |  |  |     /** @var Response\Authorization\Get[] $_authorizationResponses */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 38 |  |  |     protected $_authorizationResponses = []; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 39 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 40 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 41 |  |  |      * @throws Exception\InvalidResponse | 
            
                                                                                                            
                            
            
                                    
            
            
                | 42 |  |  |      * @throws Exception\RateLimitReached | 
            
                                                                                                            
                            
            
                                    
            
            
                | 43 |  |  |      * @throws Exception\ExpiredAuthorization | 
            
                                                                                                            
                            
            
                                    
            
            
                | 44 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 45 |  |  |     protected function _fetchAuthorizationResponses() { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 46 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 47 |  |  |         if(!file_exists($this->_order->getKeyDirectoryPath() . 'private.pem')) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 48 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 49 |  |  |             Utilities\Logger::getInstance()->add( | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 50 |  |  |                 Utilities\Logger::LEVEL_DEBUG, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 51 |  |  |                 get_class() . '::' . __FUNCTION__ . ' result suppressed (Order has finished already)', | 
            
                                                                                                            
                            
            
                                    
            
            
                | 52 |  |  |             ); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 53 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 54 |  |  |             return; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 55 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 56 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 57 |  |  |         $orderResponse = Cache\OrderResponse::getInstance()->get($this->_order); | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 58 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 59 |  |  |         foreach($orderResponse->getAuthorizations() as $authorization) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 60 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 61 |  |  |             $this->_authorizationResponses[] = Cache\OrderAuthorizationResponse::getInstance() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 62 |  |  |                 ->get($this->_order, $authorization, $this->_getChallengeType()); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 63 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 64 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 65 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 66 |  |  |     protected function _hasValidAuthorizationResponses() : bool { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 67 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 68 |  |  |         return count($this->_authorizationResponses) > 0; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 69 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 70 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 71 |  |  |     public function shouldStartAuthorization() : bool { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 72 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 73 |  |  |         foreach($this->_authorizationResponses as $response) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 74 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 75 |  |  |             $challenge = $response->getChallenge($this->_getChallengeType()); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 76 |  |  |             if($challenge && $challenge->status == Response\Authorization\Struct\Challenge::STATUS_PENDING) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 77 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 78 |  |  |                 Utilities\Logger::getInstance()->add( | 
            
                                                                                                            
                            
            
                                    
            
            
                | 79 |  |  |                     Utilities\Logger::LEVEL_DEBUG, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 80 |  |  |                     get_class() . '::' . __FUNCTION__ . ' "Pending challenge found', | 
            
                                                                                                            
                            
            
                                    
            
            
                | 81 |  |  |                     [get_class($challenge) => $challenge] | 
            
                                                                                                            
                            
            
                                    
            
            
                | 82 |  |  |                 ); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 83 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 84 |  |  |                 return true; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 85 |  |  |             } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 86 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 87 |  |  |         return false; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 88 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 89 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 90 |  |  |     abstract protected function _getChallengeType() : string; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 91 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 92 |  |  |     private $_progressed = false; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 93 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 94 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 95 |  |  |      * @throws Exception\AuthorizationInvalid | 
            
                                                                                                            
                            
            
                                    
            
            
                | 96 |  |  |      * @throws Exception\InvalidResponse | 
            
                                                                                                            
                            
            
                                    
            
            
                | 97 |  |  |      * @throws Exception\RateLimitReached | 
            
                                                                                                            
                            
            
                                    
            
            
                | 98 |  |  |      * @throws Exception\ExpiredAuthorization | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 99 |  |  |      */ | 
            
                                                                        
                            
            
                                    
            
            
                | 100 |  |  |     public function progress() { | 
            
                                                                        
                            
            
                                    
            
            
                | 101 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 102 |  |  |         if($this->_progressed) { | 
            
                                                                        
                            
            
                                    
            
            
                | 103 |  |  |             return; | 
            
                                                                        
                            
            
                                    
            
            
                | 104 |  |  |         } | 
            
                                                                        
                            
            
                                    
            
            
                | 105 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 106 |  |  |         $this->_progressed = true; | 
            
                                                                        
                            
            
                                    
            
            
                | 107 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 108 |  |  |         if(!$this->_hasValidAuthorizationResponses()) | 
            
                                                                        
                            
            
                                    
            
            
                | 109 |  |  |             return; | 
            
                                                                        
                            
            
                                    
            
            
                | 110 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 111 |  |  |         $existsNotValidChallenges = false; | 
            
                                                                        
                            
            
                                    
            
            
                | 112 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 113 |  |  |         foreach($this->_authorizationResponses as $authorizationResponse) { | 
            
                                                                        
                            
            
                                    
            
            
                | 114 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 115 |  |  |             $challenge = $authorizationResponse->getChallenge($this->_getChallengeType()); | 
            
                                                                        
                            
            
                                    
            
            
                | 116 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 117 |  |  |             if($challenge && $this->_existsNotValidChallenges($challenge, $authorizationResponse)) { | 
            
                                                                        
                            
            
                                    
            
            
                | 118 |  |  |                 $existsNotValidChallenges = true; | 
            
                                                                        
                            
            
                                    
            
            
                | 119 |  |  |             } | 
            
                                                                        
                            
            
                                    
            
            
                | 120 |  |  |         } | 
            
                                                                        
                            
            
                                    
            
            
                | 121 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 122 |  |  |         $this->_finished = !$existsNotValidChallenges; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 123 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 124 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 125 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 126 |  |  |      * @param Response\Authorization\Struct\Challenge $challenge | 
            
                                                                                                            
                            
            
                                    
            
            
                | 127 |  |  |      * @param Response\Authorization\Get $authorizationResponse | 
            
                                                                                                            
                            
            
                                    
            
            
                | 128 |  |  |      * @return bool | 
            
                                                                                                            
                            
            
                                    
            
            
                | 129 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 130 |  |  |      * @throws Exception\AuthorizationInvalid | 
            
                                                                                                            
                            
            
                                    
            
            
                | 131 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 132 |  |  |     protected function _existsNotValidChallenges(Response\Authorization\Struct\Challenge $challenge, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 133 |  |  |                                                  Response\Authorization\Get $authorizationResponse | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 134 |  |  |     ) : bool { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 135 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 136 |  |  |         if($challenge->status == Response\Authorization\Struct\Challenge::STATUS_PENDING) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 137 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 138 |  |  |             Utilities\Logger::getInstance()->add( | 
            
                                                                                                            
                            
            
                                    
            
            
                | 139 |  |  |                 Utilities\Logger::LEVEL_DEBUG, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 140 |  |  |                 get_class() . '::' . __FUNCTION__ . ' "Non valid challenge found', | 
            
                                                                                                            
                            
            
                                    
            
            
                | 141 |  |  |                 [get_class($challenge) => $challenge] | 
            
                                                                                                            
                            
            
                                    
            
            
                | 142 |  |  |             ); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 143 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 144 |  |  |             return true; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 145 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 146 |  |  |         else if($challenge->status == Response\Authorization\Struct\Challenge::STATUS_PROGRESSING) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 147 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 148 |  |  |             // Should come back later | 
            
                                                                                                            
                            
            
                                    
            
            
                | 149 |  |  |             return true; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 150 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 151 |  |  |         else if($challenge->status == Response\Authorization\Struct\Challenge::STATUS_VALID) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 152 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 153 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 154 |  |  |         else if($challenge->status == Response\Authorization\Struct\Challenge::STATUS_INVALID) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 155 |  |  |             throw new Exception\AuthorizationInvalid( | 
            
                                                                                                            
                            
            
                                    
            
            
                | 156 |  |  |                 'Received status "' . Response\Authorization\Struct\Challenge::STATUS_INVALID . '" while challenge should be verified' | 
            
                                                                                                            
                            
            
                                    
            
            
                | 157 |  |  |             ); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 158 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 159 |  |  |         else { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 160 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 161 |  |  |             throw new \RuntimeException('Challenge status "' . $challenge->status . '" is not implemented'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 162 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 163 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 164 |  |  |         return false; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 165 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 166 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 167 |  |  |     protected $_finished = false; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 168 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 169 |  |  |     public function hasFinished() : bool { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 170 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 171 |  |  |         Utilities\Logger::getInstance()->add( | 
            
                                                                                                            
                            
            
                                    
            
            
                | 172 |  |  |             Utilities\Logger::LEVEL_DEBUG, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 173 |  |  |             get_called_class() . '::' . __FUNCTION__, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 174 |  |  |             ['finished' => $this->_finished] | 
            
                                                                                                            
                            
            
                                    
            
            
                | 175 |  |  |         ); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 176 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 177 |  |  |         return $this->_finished; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 178 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 179 |  |  | } | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 180 |  |  |  | 
            
                                                        
            
                                    
            
            
                | 181 |  |  |  |