-
Notifications
You must be signed in to change notification settings - Fork 704
Expand file tree
/
Copy pathConfig.php
More file actions
488 lines (437 loc) · 18.3 KB
/
Copy pathConfig.php
File metadata and controls
488 lines (437 loc) · 18.3 KB
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
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
<?php
declare(strict_types=1);
namespace SimpleSAML\Module\admin\Controller;
use SimpleSAML\Configuration;
use SimpleSAML\Event\Dispatcher\ModuleEventDispatcherFactory;
use SimpleSAML\Locale\Translate;
use SimpleSAML\Logger;
use SimpleSAML\Module;
use SimpleSAML\Module\admin\Event\ConfigPageEvent;
use SimpleSAML\Module\admin\Event\SanityCheckEvent;
use SimpleSAML\Session;
use SimpleSAML\Utils;
use SimpleSAML\XHTML\Template;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\StreamedResponse;
use Symfony\Contracts\HttpClient\Exception\ExceptionInterface;
use function explode;
use function function_exists;
use function ltrim;
use function phpversion;
use function version_compare;
/**
* Controller class for the admin module.
*
* This class serves the configuration views available in the module.
*
* @package SimpleSAML\Module\admin
*/
class Config
{
public const string LATEST_VERSION_STATE_KEY = 'admin:latest_simplesamlphp_version';
public const string RELEASES_API = 'https://api.github.com/repos/simplesamlphp/simplesamlphp/releases/latest';
/** @var \SimpleSAML\Utils\Auth */
protected Utils\Auth $authUtils;
/** @var \SimpleSAML\Utils\HTTP */
protected Utils\HTTP $httpUtils;
/** @var \SimpleSAML\Module\admin\Controller\Menu */
protected Menu $menu;
/**
* ConfigController constructor.
*
* @param \SimpleSAML\Configuration $config The configuration to use.
* @param \SimpleSAML\Session $session The current user session.
*/
public function __construct(
protected Configuration $config,
protected Session $session,
) {
$this->menu = new Menu();
$this->authUtils = new Utils\Auth();
$this->httpUtils = new Utils\HTTP();
}
/**
* Inject the \SimpleSAML\Utils\Auth dependency.
*
* @param \SimpleSAML\Utils\Auth $authUtils
*/
public function setAuthUtils(Utils\Auth $authUtils): void
{
$this->authUtils = $authUtils;
}
/**
* Display basic diagnostic information on hostname, port and protocol.
*
* @param \Symfony\Component\HttpFoundation\Request $request The current request.
*
* @return \SimpleSAML\XHTML\Template
*/
public function diagnostics(Request $request): Template
{
$this->authUtils->requireAdmin();
$t = new Template($this->config, 'admin:diagnostics.twig');
$t->data = [
'remaining' => $this->session->getAuthData('admin', 'Expire') - time(),
'logouturl' => $this->authUtils->getAdminLogoutURL(),
'items' => [
'HTTP_HOST' => [$request->getHost()],
'HTTPS' => $request->isSecure() ? ['on'] : [],
'SERVER_PROTOCOL' => [$request->getProtocolVersion()],
'getBaseURL()' => [$this->httpUtils->getBaseURL()],
'getSelfHost()' => [$this->httpUtils->getSelfHost()],
'getSelfHostWithNonStandardPort()' => [$this->httpUtils->getSelfHostWithNonStandardPort()],
'getSelfURLHost()' => [$this->httpUtils->getSelfURLHost()],
'getSelfURLNoQuery()' => [$this->httpUtils->getSelfURLNoQuery()],
'getSelfHostWithPath()' => [$this->httpUtils->getSelfHostWithPath()],
'getSelfURL()' => [$this->httpUtils->getSelfURL()],
],
];
$this->menu->addOption('logout', $t->data['logouturl'], Translate::noop('Log out'));
return $this->menu->insert($t);
}
/**
* Display the main admin page.
*
* @param \Symfony\Component\HttpFoundation\Request $request The current request.
*
* @return \SimpleSAML\XHTML\Template
*/
public function main(/** @scrutinizer ignore-unused */ Request $request): Template
{
$this->authUtils->requireAdmin();
$t = new Template($this->config, 'admin:config.twig');
$t->data = [
'warnings' => $this->getWarnings(),
'directory' => $this->config->getBaseDir(),
'version' => $this->config->getVersion(),
'links' => [
[
'href' => Module::getModuleURL('admin/diagnostics'),
'text' => Translate::noop('Diagnostics on hostname, port and protocol'),
],
[
'href' => Module::getModuleURL('admin/phpinfo'),
'text' => Translate::noop('Information on your PHP installation'),
],
],
'enablematrix' => [
'saml20idp' => $this->config->getOptionalBoolean('enable.saml20-idp', false),
],
'funcmatrix' => $this->getPrerequisiteChecks(),
'logouturl' => $this->authUtils->getAdminLogoutURL(),
'modulelist' => $this->getModuleList(),
];
$eventDispatcher = ModuleEventDispatcherFactory::getInstance();
/** @var \SimpleSAML\Module\admin\Controller\CronEvent $event */
$event = $eventDispatcher->dispatch(new ConfigPageEvent($t));
$t = $event->getTemplate();
Module::callHooks('configpage', $t);
$this->menu->addOption('logout', $this->authUtils->getAdminLogoutURL(), Translate::noop('Log out'));
return $this->menu->insert($t);
}
/**
* @return array
*/
protected function getModuleList(): array
{
$modules = Module::getModules();
$modulestates = [];
foreach ($modules as $module) {
$modulestates[$module] = Module::isModuleEnabled($module);
}
ksort($modulestates);
return $modulestates;
}
/**
* Display the output of phpinfo().
*
* @param \Symfony\Component\HttpFoundation\Request $request The current request.
*
* @return \Symfony\Component\HttpFoundation\StreamedResponse
*/
public function phpinfo(/** @scrutinizer ignore-unused */ Request $request): StreamedResponse
{
$this->authUtils->requireAdmin();
$response = new StreamedResponse('phpinfo');
$response->headers->set(
'Content-Security-Policy',
"default-src 'self'; style-src 'self' 'unsafe-inline'; img-src 'self' data:; frame-ancestors 'self';",
);
return $response;
}
/**
* Perform a list of checks on the current installation, and return the results as an array.
*
* The elements in the array returned are also arrays with the following keys:
*
* - required: Whether this prerequisite is mandatory or not. One of "required" or "optional".
* - descr: A translatable text that describes the prerequisite. If the text uses parameters, the value must be an
* array where the first value is the text to translate, and the second is a hashed array containing the
* parameters needed to properly translate the text.
* - enabled: True if the prerequisite is met, false otherwise.
*
* @return array
*/
protected function getPrerequisiteChecks(): array
{
$matrix = [
[
'required' => 'required',
'descr' => [
Translate::noop('PHP %minimum% or newer is needed. You are running: %current%'),
[
'%minimum%' => '8.3',
'%current%' => explode('-', phpversion())[0],
],
],
'enabled' => version_compare(phpversion(), '8.3', '>='),
],
];
$store = $this->config->getOptionalString('store.type', null);
$checkforupdates = $this->config->getOptionalBoolean('admin.checkforupdates', true);
// check dependencies used via normal functions
$functions = [
'time' => [
'required' => 'required',
'descr' => [
'required' => Translate::noop('Date/Time Extension'),
],
],
'hash' => [
'required' => 'required',
'descr' => [
'required' => Translate::noop('Hashing function'),
],
],
'gzinflate' => [
'required' => 'required',
'descr' => [
'required' => Translate::noop('ZLib'),
],
],
'openssl_sign' => [
'required' => 'required',
'descr' => [
'required' => Translate::noop('OpenSSL'),
],
],
'dom_import_simplexml' => [
'required' => 'required',
'descr' => [
'required' => Translate::noop('XML DOM'),
],
],
'preg_match' => [
'required' => 'required',
'descr' => [
'required' => Translate::noop('Regular expression support'),
],
],
'intl_get_error_code' => [
'required' => 'optional',
'descr' => [
'optional' => Translate::noop('PHP intl extension'),
],
],
'json_decode' => [
'required' => 'required',
'descr' => [
'required' => Translate::noop('JSON support'),
],
],
'class_implements' => [
'required' => 'required',
'descr' => [
'required' => Translate::noop('Standard PHP library (SPL)'),
],
],
'mb_strlen' => [
'required' => 'required',
'descr' => [
'required' => Translate::noop('Multibyte String extension'),
],
],
'curl_init' => [
'required' => ($checkforupdates === true) ? 'required' : 'optional',
'descr' => [
'optional' => Translate::noop(
'cURL (might be required by some modules)',
),
'required' => Translate::noop(
'cURL (required if automatic version checks are used, also by some modules)',
),
],
],
'session_start' => [
'required' => $store === 'phpsession' ? 'required' : 'optional',
'descr' => [
'optional' => Translate::noop('Session extension (required if PHP sessions are used)'),
'required' => Translate::noop('Session extension'),
],
],
'pdo_drivers' => [
'required' => $store === 'sql' ? 'required' : 'optional',
'descr' => [
'optional' => Translate::noop('PDO Extension (required if a database backend is used)'),
'required' => Translate::noop('PDO extension'),
],
],
'ldap_bind' => [
'required' => Module::isModuleEnabled('ldap') ? 'required' : 'optional',
'descr' => [
'optional' => Translate::noop('LDAP extension (required if an LDAP backend is used)'),
'required' => Translate::noop('LDAP extension'),
],
],
];
foreach ($functions as $function => $description) {
$matrix[] = [
'required' => $description['required'],
'descr' => $description['descr'][$description['required']],
'enabled' => function_exists($function),
];
}
// check object-oriented external libraries and extensions
$libs = [
[
'classes' => ['\Predis\Client'],
'required' => $store === 'redis' ? 'required' : 'optional',
'descr' => [
'optional' => Translate::noop('predis/predis (required if the redis data store is used)'),
'required' => Translate::noop('predis/predis library'),
],
],
[
'classes' => ['\Memcache', '\Memcached'],
'required' => $store === 'memcache' ? 'required' : 'optional',
'descr' => [
'optional' => Translate::noop(
'Memcache or Memcached extension (required if the memcache backend is used)',
),
'required' => Translate::noop('Memcache or Memcached extension'),
],
],
];
foreach ($libs as $lib) {
$enabled = false;
foreach ($lib['classes'] as $class) {
/** @psalm-suppress InvalidOperand - See https://github.com/vimeo/psalm/issues/1340 */
$enabled |= class_exists($class);
}
$matrix[] = [
'required' => $lib['required'],
'descr' => $lib['descr'][$lib['required']],
'enabled' => $enabled,
];
}
// perform some basic configuration checks
$technicalcontact = $this->config->getOptionalString('technicalcontact_email', 'na@example.org');
$matrix[] = [
'required' => 'optional',
'descr' => Translate::noop('The <code>technicalcontact_email</code> configuration option should be set'),
'enabled' => $technicalcontact !== 'na@example.org',
];
$matrix[] = [
'required' => 'required',
'descr' => Translate::noop('The auth.adminpassword configuration option must be set'),
'enabled' => $this->config->getOptionalString('auth.adminpassword', '123') !== '123',
];
// Add module specific checks via the sanitycheck hook that a module can provide.
$eventDispatcher = ModuleEventDispatcherFactory::getInstance();
/** @var \SimpleSAML\Module\admin\Event\SanityCheckEvent $event */
$event = $eventDispatcher->dispatch(new SanityCheckEvent());
$hookinfo = [ 'info' => [], 'errors' => [] ];
Module::callHooks('sanitycheck', $hookinfo);
// Merge results from the event into $hookinfo. Can be removed when hook infrastructure is removed.
$hookinfo = [
'info' => array_merge(
$event->getInfo(),
$hookinfo['info'],
),
'errors' => array_merge(
$event->getErrors(),
$hookinfo['errors'],
),
];
foreach (['info', 'errors'] as $resulttype) {
foreach ($hookinfo[$resulttype] as $result) {
$matrix[] = [
'required' => 'required',
'descr' => $result,
'enabled' => $resulttype === 'info',
];
}
}
return $matrix;
}
/**
* Compile a list of warnings about the current deployment.
*
* The returned array can contain either strings that can be translated directly, or arrays. If an element is an
* array, the first value in that array is a string that can be translated, and the second value will be a hashed
* array that contains the substitutions that must be applied to the translation, with its corresponding value. This
* can be used in twig like this, assuming an element called "e":
*
* {{ e[0]|trans(e[1])|raw }}
*
* @return array
*/
protected function getWarnings(): array
{
$warnings = [];
// make sure we're using HTTPS
if (!$this->httpUtils->isHTTPS()) {
$warnings[] = Translate::noop(
'<strong>You are not using HTTPS</strong> to protect communications with your users. HTTP works fine ' .
'for testing purposes, but in a production environment you should use HTTPS. <a ' .
'href="https://simplesamlphp.org/docs/stable/simplesamlphp-maintenance">Read more about the ' .
'maintenance of SimpleSAMLphp</a>.',
);
}
// make sure we have a secret salt set
$secretSalt = $this->config->getString('secretsalt');
if ($secretSalt === 'defaultsecretsalt') {
$warnings[] = Translate::noop(
'<strong>The configuration uses the default secret salt</strong>. Make sure to modify the <code>' .
'secretsalt</code> option in the SimpleSAMLphp configuration in production environments. <a ' .
'href="https://simplesamlphp.org/docs/stable/simplesamlphp-install">Read more about the ' .
'maintenance of SimpleSAMLphp</a>.',
);
} elseif (str_contains($secretSalt, '%')) {
$warnings[] = Translate::noop(
'The "secretsalt" configuration option may not contain a `%` sign.',
);
}
/**
* Check for updates. Store the remote result in the session so we don't need to fetch it on every access to
* this page.
*/
$checkforupdates = $this->config->getOptionalBoolean('admin.checkforupdates', true);
if (($checkforupdates === true) && $this->config->getVersion() !== 'master') {
$latest = $this->session->getData(self::LATEST_VERSION_STATE_KEY, "version");
if (!$latest) {
$client = $this->httpUtils->createHttpClient(['timeout' => 3]);
$response = $client->request('GET', self::RELEASES_API);
try {
$latest = $response->toArray();
$this->session->setData(self::LATEST_VERSION_STATE_KEY, 'version', $latest);
} catch (ExceptionInterface $e) {
Logger::warning(sprintf("Unable to check for updates; %s", $e->getMessage()));
$warnings[] = Translate::noop("Unable to check for updates; see logs for details.");
}
}
if ($latest && version_compare($this->config->getVersion(), ltrim($latest['tag_name'], 'v'), 'lt')) {
$warnings[] = [
Translate::noop(
'You are running an outdated version of SimpleSAMLphp. Please update to <a href="' .
'%latest%">the latest version</a> as soon as possible.',
),
[
'%latest%' => $latest['html_url'],
],
];
}
}
return $warnings;
}
}