/
AuthenticationSignon.php
273 lines (222 loc) · 8.4 KB
/
AuthenticationSignon.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
<?php
/**
* SignOn Authentication plugin for phpMyAdmin
*/
declare(strict_types=1);
namespace PhpMyAdmin\Plugins\Auth;
use PhpMyAdmin\Config;
use PhpMyAdmin\LanguageManager;
use PhpMyAdmin\Plugins\AuthenticationPlugin;
use PhpMyAdmin\ResponseRenderer;
use PhpMyAdmin\Util;
use function __;
use function array_merge;
use function defined;
use function file_exists;
use function in_array;
use function session_get_cookie_params;
use function session_id;
use function session_name;
use function session_set_cookie_params;
use function session_start;
use function session_write_close;
/**
* Handles the SignOn authentication method
*/
class AuthenticationSignon extends AuthenticationPlugin
{
/**
* Displays authentication form
*/
public function showLoginForm(): never
{
$response = ResponseRenderer::getInstance();
$response->disable();
unset($_SESSION['LAST_SIGNON_URL']);
$config = Config::getInstance();
if (empty($config->selectedServer['SignonURL'])) {
echo $this->template->render('error/generic', [
'lang' => $GLOBALS['lang'] ?? 'en',
'dir' => LanguageManager::$textDir,
'error_message' => 'You must set SignonURL!',
]);
$response->callExit();
} else {
$response->redirect($config->selectedServer['SignonURL']);
}
$response->callExit();
}
/**
* Set cookie params
*
* @param mixed[] $sessionCookieParams The cookie params
*/
public function setCookieParams(array|null $sessionCookieParams = null): void
{
/* Session cookie params from config */
$sessionCookieParams ??= Config::getInstance()->selectedServer['SignonCookieParams'];
foreach (['lifetime', 'path', 'domain', 'secure', 'httponly'] as $key) {
if (isset($sessionCookieParams[$key])) {
continue;
}
$sessionCookieParams[$key] = match ($key) {
'lifetime' => 0,
'path' => '/',
'domain' => '',
'secure', 'httponly' => false,
};
}
if (
isset($sessionCookieParams['samesite'])
&& ! in_array($sessionCookieParams['samesite'], ['Lax', 'Strict'], true)
) {
// Not a valid value for samesite
unset($sessionCookieParams['samesite']);
}
session_set_cookie_params($sessionCookieParams);
}
/**
* Gets authentication credentials
*/
public function readCredentials(): bool
{
$config = Config::getInstance();
/* Check if we're using same signon server */
$signonUrl = $config->selectedServer['SignonURL'];
if (isset($_SESSION['LAST_SIGNON_URL']) && $_SESSION['LAST_SIGNON_URL'] != $signonUrl) {
return false;
}
/* Script name */
$scriptName = $config->selectedServer['SignonScript'];
/* Session name */
$sessionName = $config->selectedServer['SignonSession'];
/* Current host */
$singleSignonHost = $config->selectedServer['host'];
/* Current port */
$singleSignonPort = $config->selectedServer['port'];
/* No configuration updates */
$singleSignonCfgUpdate = [];
/* Handle script based auth */
if ($scriptName !== '') {
if (! @file_exists($scriptName)) {
echo $this->template->render('error/generic', [
'lang' => $GLOBALS['lang'] ?? 'en',
'dir' => LanguageManager::$textDir,
'error_message' => __('Can not find signon authentication script:') . ' ' . $scriptName,
]);
ResponseRenderer::getInstance()->callExit();
}
include $scriptName;
[$this->user, $this->password] = get_login_credentials($config->selectedServer['user']);
} elseif (isset($_COOKIE[$sessionName])) { /* Does session exist? */
/* End current session */
$oldSession = session_name();
$oldId = session_id();
$oldCookieParams = session_get_cookie_params();
if (! defined('TESTSUITE')) {
session_write_close();
}
/* Load single signon session */
if (! defined('TESTSUITE')) {
$this->setCookieParams();
session_name($sessionName);
session_id($_COOKIE[$sessionName]);
session_start();
}
/* Clear error message */
unset($_SESSION['PMA_single_signon_error_message']);
/* Grab credentials if they exist */
if (isset($_SESSION['PMA_single_signon_user'])) {
$this->user = $_SESSION['PMA_single_signon_user'];
}
if (isset($_SESSION['PMA_single_signon_password'])) {
$this->password = $_SESSION['PMA_single_signon_password'];
}
if (isset($_SESSION['PMA_single_signon_host'])) {
$singleSignonHost = $_SESSION['PMA_single_signon_host'];
}
if (isset($_SESSION['PMA_single_signon_port'])) {
$singleSignonPort = $_SESSION['PMA_single_signon_port'];
}
if (isset($_SESSION['PMA_single_signon_cfgupdate'])) {
$singleSignonCfgUpdate = $_SESSION['PMA_single_signon_cfgupdate'];
}
/* Also get token as it is needed to access subpages */
if (isset($_SESSION['PMA_single_signon_token'])) {
/* No need to care about token on logout */
$pmaToken = $_SESSION['PMA_single_signon_token'];
}
$hmacSecret = $_SESSION['PMA_single_signon_HMAC_secret'] ?? Util::generateRandom(16);
/* End single signon session */
if (! defined('TESTSUITE')) {
session_write_close();
}
/* Restart phpMyAdmin session */
if (! defined('TESTSUITE')) {
$this->setCookieParams($oldCookieParams);
if ($oldSession !== false) {
session_name($oldSession);
}
if ($oldId !== '' && $oldId !== false) {
session_id($oldId);
}
session_start();
}
/* Set the single signon host */
$config->selectedServer['host'] = $singleSignonHost;
/* Set the single signon port */
$config->selectedServer['port'] = $singleSignonPort;
/* Configuration update */
$config->selectedServer = array_merge($config->selectedServer, $singleSignonCfgUpdate);
/* Restore our token */
if (! empty($pmaToken)) {
$_SESSION[' PMA_token '] = $pmaToken;
$_SESSION[' HMAC_secret '] = $hmacSecret;
}
/**
* Clear user cache.
*/
Util::clearUserCache();
}
// Returns whether we get authentication settings or not
if ($this->user === '') {
unset($_SESSION['LAST_SIGNON_URL']);
return false;
}
$_SESSION['LAST_SIGNON_URL'] = $config->selectedServer['SignonURL'];
return true;
}
/**
* User is not allowed to login to MySQL -> authentication failed
*
* @param string $failure String describing why authentication has failed
*/
public function showFailure(string $failure): never
{
parent::showFailure($failure);
/* Session name */
$sessionName = Config::getInstance()->selectedServer['SignonSession'];
/* Does session exist? */
if (isset($_COOKIE[$sessionName])) {
if (! defined('TESTSUITE')) {
/* End current session */
session_write_close();
/* Load single signon session */
$this->setCookieParams();
session_name($sessionName);
session_id($_COOKIE[$sessionName]);
session_start();
}
/* Set error message */
$_SESSION['PMA_single_signon_error_message'] = $this->getErrorMessage($failure);
}
$this->showLoginForm();
}
/**
* Returns URL for login form.
*/
public function getLoginFormURL(): string
{
return Config::getInstance()->selectedServer['SignonURL'];
}
}