| 1 |  |  | <?php | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 3 |  |  | declare(strict_types=1); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 4 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 5 |  |  | namespace SimpleSAML\Module\casserver\Controller; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 6 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 7 |  |  | use SimpleSAML\Auth\Simple; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 8 |  |  | use SimpleSAML\Compat\SspContainer; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 9 |  |  | use SimpleSAML\Configuration; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 10 |  |  | use SimpleSAML\Logger; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 11 |  |  | use SimpleSAML\Module; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 12 |  |  | use SimpleSAML\Module\casserver\Cas\Factories\TicketFactory; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 13 |  |  | use SimpleSAML\Module\casserver\Controller\Traits\UrlTrait; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 14 |  |  | use SimpleSAML\Session; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 15 |  |  | use Symfony\Component\HttpFoundation\RedirectResponse; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 16 |  |  | use Symfony\Component\HttpFoundation\Request; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 17 |  |  | use Symfony\Component\HttpKernel\Attribute\AsController; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 18 |  |  | use Symfony\Component\HttpKernel\Attribute\MapQueryParameter; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 19 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 20 |  |  | #[AsController] | 
            
                                                                                                            
                            
            
                                    
            
            
                | 21 |  |  | class LogoutController | 
            
                                                                                                            
                            
            
                                    
            
            
                | 22 |  |  | { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 23 |  |  |     use UrlTrait; | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 24 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 25 |  |  |     /** @var Logger */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 26 |  |  |     protected Logger $logger; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 27 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 28 |  |  |     /** @var Configuration */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 29 |  |  |     protected Configuration $casConfig; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 30 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 31 |  |  |     /** @var TicketFactory */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 32 |  |  |     protected TicketFactory $ticketFactory; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 33 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 34 |  |  |     /** @var Simple  */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 35 |  |  |     protected Simple $authSource; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 36 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 37 |  |  |     /** @var SspContainer */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 38 |  |  |     protected SspContainer $container; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 39 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 40 |  |  |     // this could be any configured ticket store | 
            
                                                                                                            
                            
            
                                    
            
            
                | 41 |  |  |     /** @var mixed */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 42 |  |  |     protected mixed $ticketStore; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 43 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 44 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 45 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 46 |  |  |      * @param   Configuration|null  $casConfig | 
            
                                                                                                            
                            
            
                                    
            
            
                | 47 |  |  |      * @param   Simple|null         $source | 
            
                                                                                                            
                            
            
                                    
            
            
                | 48 |  |  |      * @param   SspContainer|null   $container | 
            
                                                                                                            
                            
            
                                    
            
            
                | 49 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 50 |  |  |      * @throws \Exception | 
            
                                                                                                            
                            
            
                                    
            
            
                | 51 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 52 |  |  |     public function __construct( | 
            
                                                                                                            
                            
            
                                    
            
            
                | 53 |  |  |         // Facilitate testing | 
            
                                                                                                            
                            
            
                                    
            
            
                | 54 |  |  |         Configuration $casConfig = null, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 55 |  |  |         Simple $source = null, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 56 |  |  |         SspContainer $container = null, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 57 |  |  |     ) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 58 |  |  |         $this->casConfig = $casConfig ?? Configuration::getConfig('module_casserver.php'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 59 |  |  |         /* Instantiate ticket factory */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 60 |  |  |         $this->ticketFactory = new TicketFactory($this->casConfig); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 61 |  |  |         /* Instantiate ticket store */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 62 |  |  |         $ticketStoreConfig = $this->casConfig->getOptionalValue( | 
            
                                                                                                            
                            
            
                                    
            
            
                | 63 |  |  |             'ticketstore', | 
            
                                                                                                            
                            
            
                                    
            
            
                | 64 |  |  |             ['class' => 'casserver:FileSystemTicketStore'], | 
            
                                                                                                            
                            
            
                                    
            
            
                | 65 |  |  |         ); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 66 |  |  |         $ticketStoreClass = 'SimpleSAML\\Module\\casserver\\Cas\\Ticket\\' | 
            
                                                                                                            
                            
            
                                    
            
            
                | 67 |  |  |             . explode(':', $ticketStoreConfig['class'])[1]; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 68 |  |  |         $this->ticketStore = new $ticketStoreClass($this->casConfig); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 69 |  |  |         $this->authSource = $source ?? new Simple($this->casConfig->getValue('authsource')); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 70 |  |  |         $this->container = $container ?? new SspContainer(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 71 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 72 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 73 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 74 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 75 |  |  |      * @param   Request      $request | 
            
                                                                                                            
                            
            
                                    
            
            
                | 76 |  |  |      * @param   string|null  $url | 
            
                                                                                                            
                            
            
                                    
            
            
                | 77 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 78 |  |  |      * @return RedirectResponse|null | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 79 |  |  |      */ | 
            
                                                                        
                            
            
                                    
            
            
                | 80 |  |  |     public function logout( | 
            
                                                                        
                            
            
                                    
            
            
                | 81 |  |  |         Request $request, | 
            
                                                                        
                            
            
                                    
            
            
                | 82 |  |  |         #[MapQueryParameter] ?string $url = null, | 
            
                                                                        
                            
            
                                    
            
            
                | 83 |  |  |     ): RedirectResponse|null { | 
            
                                                                        
                            
            
                                    
            
            
                | 84 |  |  |         if (!$this->casConfig->getOptionalValue('enable_logout', false)) { | 
            
                                                                        
                            
            
                                    
            
            
                | 85 |  |  |             $this->handleExceptionThrown('Logout not allowed'); | 
            
                                                                        
                            
            
                                    
            
            
                | 86 |  |  |         } | 
            
                                                                        
                            
            
                                    
            
            
                | 87 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 88 |  |  |         // Skip Logout Page configuration | 
            
                                                                        
                            
            
                                    
            
            
                | 89 |  |  |         $skipLogoutPage = $this->casConfig->getOptionalValue('skip_logout_page', false); | 
            
                                                                        
                            
            
                                    
            
            
                | 90 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 91 |  |  |         if ($skipLogoutPage && $url === null) { | 
            
                                                                        
                            
            
                                    
            
            
                | 92 |  |  |             $this->handleExceptionThrown('Required URL query parameter [url] not provided. (CAS Server)'); | 
            
                                                                        
                            
            
                                    
            
            
                | 93 |  |  |         } | 
            
                                                                        
                            
            
                                    
            
            
                | 94 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 95 |  |  |         // Construct the logout redirect url | 
            
                                                                        
                            
            
                                    
            
            
                | 96 |  |  |         if ($skipLogoutPage) { | 
            
                                                                        
                            
            
                                    
            
            
                | 97 |  |  |             $logoutRedirectUrl = $url; | 
            
                                                                        
                            
            
                                    
            
            
                | 98 |  |  |             $params = []; | 
            
                                                                        
                            
            
                                    
            
            
                | 99 |  |  |         } else { | 
            
                                                                        
                            
            
                                    
            
            
                | 100 |  |  |             $logoutRedirectUrl = Module::getModuleURL('casserver/loggedOut.php'); | 
            
                                                                        
                            
            
                                    
            
            
                | 101 |  |  |             $params =  $url === null ? [] | 
            
                                                                        
                            
            
                                    
            
            
                | 102 |  |  |                 : ['url' => $url]; | 
            
                                                                        
                            
            
                                    
            
            
                | 103 |  |  |         } | 
            
                                                                        
                            
            
                                    
            
            
                | 104 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 105 |  |  |         // Delete the ticket from the session | 
            
                                                                        
                            
            
                                    
            
            
                | 106 |  |  |         $session = $this->getSession(); | 
            
                                                                        
                            
            
                                    
            
            
                | 107 |  |  |         if ($session !== null) { | 
            
                                                                        
                            
            
                                    
            
            
                | 108 |  |  |             $this->ticketStore->deleteTicket($session->getSessionId()); | 
            
                                                                        
                            
            
                                    
            
            
                | 109 |  |  |         } | 
            
                                                                        
                            
            
                                    
            
            
                | 110 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 111 |  |  |         // Redirect | 
            
                                                                        
                            
            
                                    
            
            
                | 112 |  |  |         if (!$this->authSource->isAuthenticated()) { | 
            
                                                                        
                            
            
                                    
            
            
                | 113 |  |  |             $this->container->redirect($logoutRedirectUrl, $params); | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                        
                            
            
                                    
            
            
                | 114 |  |  |         } | 
            
                                                                        
                            
            
                                    
            
            
                | 115 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 116 |  |  |         // Logout and redirect | 
            
                                                                        
                            
            
                                    
            
            
                | 117 |  |  |         $this->authSource->logout($logoutRedirectUrl); | 
            
                                                                        
                            
            
                                    
            
            
                | 118 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 119 |  |  |         // We should never get here | 
            
                                                                        
                            
            
                                    
            
            
                | 120 |  |  |         return null; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 121 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 122 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 123 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 124 |  |  |      * @param   string  $message | 
            
                                                                                                            
                            
            
                                    
            
            
                | 125 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 126 |  |  |      * @return void | 
            
                                                                                                            
                            
            
                                    
            
            
                | 127 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 128 |  |  |     protected function handleExceptionThrown(string $message): void | 
            
                                                                                                            
                            
            
                                    
            
            
                | 129 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 130 |  |  |         Logger::debug('casserver:' . $message); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 131 |  |  |         throw new \RuntimeException($message); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 132 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 133 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 134 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 135 |  |  |      * Get the Session | 
            
                                                                                                            
                            
            
                                    
            
            
                | 136 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 137 |  |  |      * @return Session|null | 
            
                                                                                                            
                            
            
                                    
            
            
                | 138 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 139 |  |  |     protected function getSession(): ?Session | 
            
                                                                                                            
                            
            
                                    
            
            
                | 140 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 141 |  |  |         return Session::getSession(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 142 |  |  |     } | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 143 |  |  | } | 
            
                                                        
            
                                    
            
            
                | 144 |  |  |  |