| 1 |  |  | <?php | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2 |  |  | /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 3 |  |  |  * @author Tharanga Kothalawala <[email protected]> | 
            
                                                                                                            
                            
            
                                    
            
            
                | 4 |  |  |  * @date 17-02-2019 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 5 |  |  |  */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 6 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 7 |  |  | namespace TSK\SSO\ThirdParty; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 8 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 9 |  |  | use TSK\SSO\Storage\FileSystemThirdPartyStorageRepository; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 10 |  |  | use TSK\SSO\Storage\ThirdPartyStorageRepository; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 11 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 12 |  |  | /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 13 |  |  |  * @package TSK\SSO\ThirdParty | 
            
                                                                                                            
                            
            
                                    
            
            
                | 14 |  |  |  * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 15 |  |  |  * This class is capable of revoking vendor connection access | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 16 |  |  |  */ | 
            
                                                                        
                            
            
                                    
            
            
                | 17 |  |  | class VendorConnectionRevoker | 
            
                                                                        
                            
            
                                    
            
            
                | 18 |  |  | { | 
            
                                                                        
                            
            
                                    
            
            
                | 19 |  |  |     /** | 
            
                                                                        
                            
            
                                    
            
            
                | 20 |  |  |      * @var VendorConnection | 
            
                                                                        
                            
            
                                    
            
            
                | 21 |  |  |      */ | 
            
                                                                        
                            
            
                                    
            
            
                | 22 |  |  |     private $thirdPartyConnection; | 
            
                                                                        
                            
            
                                    
            
            
                | 23 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 24 |  |  |     /** | 
            
                                                                        
                            
            
                                    
            
            
                | 25 |  |  |      * @var ThirdPartyStorageRepository | 
            
                                                                        
                            
            
                                    
            
            
                | 26 |  |  |      */ | 
            
                                                                        
                            
            
                                    
            
            
                | 27 |  |  |     private $storageRepository; | 
            
                                                                        
                            
            
                                    
            
            
                | 28 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 29 |  |  |     /** | 
            
                                                                        
                            
            
                                    
            
            
                | 30 |  |  |      * @param VendorConnection $thirdPartyConnection vendor connection to use for the revoke action | 
            
                                                                        
                            
            
                                    
            
            
                | 31 |  |  |      * @param ThirdPartyStorageRepository $storageRepository [optional] the storage implementation | 
            
                                                                        
                            
            
                                    
            
            
                | 32 |  |  |      *        which was used to store the data for the first time. By default uses file system as the storage. | 
            
                                                                        
                            
            
                                    
            
            
                | 33 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 34 | 3 |  |     public function __construct( | 
            
                                                                                                            
                            
            
                                    
            
            
                | 35 |  |  |         VendorConnection $thirdPartyConnection, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 36 |  |  |         ThirdPartyStorageRepository $storageRepository = null | 
            
                                                                                                            
                            
            
                                    
            
            
                | 37 |  |  |     ) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 38 | 3 |  |         $this->thirdPartyConnection = $thirdPartyConnection; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 39 | 3 |  |         $this->storageRepository = is_null($storageRepository) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 40 | 3 |  |             ? new FileSystemThirdPartyStorageRepository() | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 41 |  |  |             : $storageRepository; | 
            
                                                                        
                            
            
                                    
            
            
                | 42 | 3 |  |     } | 
            
                                                                        
                            
            
                                    
            
            
                | 43 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 44 |  |  |     /** | 
            
                                                                        
                            
            
                                    
            
            
                | 45 |  |  |      * Use this to revoke the app's access to the third party and to remove the local vendor user mappings. | 
            
                                                                        
                            
            
                                    
            
            
                | 46 |  |  |      * | 
            
                                                                        
                            
            
                                    
            
            
                | 47 |  |  |      * @return bool | 
            
                                                                        
                            
            
                                    
            
            
                | 48 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 49 | 3 |  |     public function revoke($vendorEmail, $vendorName) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 50 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 51 |  |  |         // return false if no vendor user mapping found | 
            
                                                                                                            
                            
            
                                    
            
            
                | 52 | 3 |  |         $mappedUser = $this->storageRepository->getUser($vendorEmail, $vendorName); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 53 | 3 |  |         if (is_null($mappedUser)) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 54 | 1 |  |             return false; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 55 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 56 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 57 | 2 |  |         $isRevoked = $this->thirdPartyConnection->revokeAccess( | 
            
                                                                                                            
                            
            
                                    
            
            
                | 58 | 2 |  |             new CommonAccessToken($mappedUser->vendorToken(), $mappedUser->vendorName(), $mappedUser->vendorEmail()) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 59 | 2 |  |         ); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 60 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 61 | 2 |  |         if (!$isRevoked) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 62 | 1 |  |             return false; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 63 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 64 |  |  |  | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 65 | 1 |  |         return $this->storageRepository->remove($vendorEmail, $vendorName); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 66 |  |  |     } | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 67 |  |  | } | 
            
                                                        
            
                                    
            
            
                | 68 |  |  |  |