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