| 1 |  |  | <?php | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 3 |  |  | namespace roaresearch\yii2\oauth2server\models; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 4 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 5 |  |  | use Yii; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 6 |  |  | use yii\db\IntegrityException; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 7 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 8 |  |  | class AuthForm extends \yii\base\Model | 
            
                                                                                                            
                            
            
                                    
            
            
                | 9 |  |  | { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 10 |  |  |     public ?bool $authorized = null; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 11 |  |  |     public ?string $client_id = null; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 12 |  |  |     public ?string $scopes = null; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 13 |  |  |     public ?string $response_type = null; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 14 |  |  |     public ?string $state = null; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 15 |  |  |     public ?string $redirect_uri = null; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 16 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 17 |  |  |     protected ?OauthClients $clientModel = null; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 18 |  |  |     protected array $scopesList = []; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 19 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 20 |  |  |     public function rules() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 21 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 22 |  |  |         $validatedClient = fn () => !$this->hasErrors('client_id'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 23 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 24 |  |  |         return [ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 25 |  |  |             [ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 26 |  |  |                 [ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 27 |  |  |                     'authorized', | 
            
                                                                                                            
                            
            
                                    
            
            
                | 28 |  |  |                     'client_id', | 
            
                                                                                                            
                            
            
                                    
            
            
                | 29 |  |  |                     'response_type', | 
            
                                                                                                            
                            
            
                                    
            
            
                | 30 |  |  |                     'state', | 
            
                                                                                                            
                            
            
                                    
            
            
                | 31 |  |  |                     'redirect_uri', | 
            
                                                                                                            
                            
            
                                    
            
            
                | 32 |  |  |                 ], | 
            
                                                                                                            
                            
            
                                    
            
            
                | 33 |  |  |                 'required', | 
            
                                                                                                            
                            
            
                                    
            
            
                | 34 |  |  |             ], | 
            
                                                                                                            
                            
            
                                    
            
            
                | 35 |  |  |             [['authorized'], 'boolean'], | 
            
                                                                                                            
                            
            
                                    
            
            
                | 36 |  |  |             [['redirect_uri'], 'url'], | 
            
                                                                                                            
                            
            
                                    
            
            
                | 37 |  |  |             [ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 38 |  |  |                 [ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 39 |  |  |                     'client_id', | 
            
                                                                                                            
                            
            
                                    
            
            
                | 40 |  |  |                     'scopes', | 
            
                                                                                                            
                            
            
                                    
            
            
                | 41 |  |  |                     'response_type', | 
            
                                                                                                            
                            
            
                                    
            
            
                | 42 |  |  |                     'state', | 
            
                                                                                                            
                            
            
                                    
            
            
                | 43 |  |  |                     'redirect_uri', | 
            
                                                                                                            
                            
            
                                    
            
            
                | 44 |  |  |                 ], | 
            
                                                                                                            
                            
            
                                    
            
            
                | 45 |  |  |                 'string', | 
            
                                                                                                            
                            
            
                                    
            
            
                | 46 |  |  |             ], | 
            
                                                                                                            
                            
            
                                    
            
            
                | 47 |  |  |             [ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 48 |  |  |                 ['client_id'], | 
            
                                                                                                            
                            
            
                                    
            
            
                | 49 |  |  |                 'exist', | 
            
                                                                                                            
                            
            
                                    
            
            
                | 50 |  |  |                 'targetClass' => OauthClients::class, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 51 |  |  |             ], | 
            
                                                                                                            
                            
            
                                    
            
            
                | 52 |  |  |             [ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 53 |  |  |                 ['scopes'], | 
            
                                                                                                            
                            
            
                                    
            
            
                | 54 |  |  |                 function ($attribute) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 55 |  |  |                     try { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 56 |  |  |                          $this->getScopesList(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 57 |  |  |                     } catch (IntegrityException $e) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 58 |  |  |                          $this->addError($atribute, $e->getMessage()); | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 59 |  |  |                     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 60 |  |  |                 }, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 61 |  |  |                 'when' => $validatedClient, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 62 |  |  |             ], | 
            
                                                                                                            
                            
            
                                    
            
            
                | 63 |  |  |             [ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 64 |  |  |                 ['redirect_uri'], | 
            
                                                                                                            
                            
            
                                    
            
            
                | 65 |  |  |                 function ($attribute) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 66 |  |  |                     if ( | 
            
                                                                                                            
                            
            
                                    
            
            
                | 67 |  |  |                         !$this->getClientModel() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 68 |  |  |                             ->validateUri($this->redirect_uri) | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 69 |  |  |                     ) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 70 |  |  |                         $this->addError( | 
            
                                                                                                            
                            
            
                                    
            
            
                | 71 |  |  |                             $attribute, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 72 |  |  |                             "Redirection URI not recognized by client." | 
            
                                                                                                            
                            
            
                                    
            
            
                | 73 |  |  |                         ); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 74 |  |  |                     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 75 |  |  |                 }, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 76 |  |  |                 'when' => $validatedClient, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 77 |  |  |             ], | 
            
                                                                                                            
                            
            
                                    
            
            
                | 78 |  |  |         ]; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 79 |  |  |     } | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 80 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 81 |  |  |     public function getClientModel(): ?OauthClients | 
            
                                                                        
                            
            
                                    
            
            
                | 82 |  |  |     { | 
            
                                                                        
                            
            
                                    
            
            
                | 83 |  |  |         if (empty($this->client_id) || isset($this->clientModel)) { | 
            
                                                                        
                            
            
                                    
            
            
                | 84 |  |  |             return $this->clientModel; | 
            
                                                                        
                            
            
                                    
            
            
                | 85 |  |  |         } | 
            
                                                                        
                            
            
                                    
            
            
                | 86 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 87 |  |  |         $this->clientModel = OauthClients::findOne($this->client_id) | 
            
                                                                        
                            
            
                                    
            
            
                | 88 |  |  |             ?: throw new IntegrityException( | 
            
                                                                        
                            
            
                                    
            
            
                | 89 |  |  |                 "Unknown client '{$this->client_id}'" | 
            
                                                                        
                            
            
                                    
            
            
                | 90 |  |  |             ); | 
            
                                                                        
                            
            
                                    
            
            
                | 91 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 92 |  |  |         return $this->clientModel; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 93 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 94 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 95 |  |  |     public function getScopesList(): array | 
            
                                                                                                            
                            
            
                                    
            
            
                | 96 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 97 |  |  |         if (empty($this->scopes) || !empty($this->scopesList)) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 98 |  |  |             return $this->scopesList; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 99 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 100 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 101 |  |  |         $clientModel = $this->getClientModel(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 102 |  |  |         foreach (explode(' ', $this->scopes) as $scope) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 103 |  |  |             $this->scopesList[$scope] = $clientModel->assureScope($scope); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 104 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 105 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 106 |  |  |         return $this->scopesList; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 107 |  |  |     } | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 108 |  |  | } | 
            
                                                        
            
                                    
            
            
                | 109 |  |  |  |