ILIAS  trunk Revision v11.0_alpha-3011-gc6b235a2e85
class.ilAuthUtils.php
Go to the documentation of this file.
1<?php
2
19declare(strict_types=1);
20
22
24{
25 public const int LOCAL_PWV_FULL = 1;
26 public const int LOCAL_PWV_NO = 2;
27 public const int LOCAL_PWV_USER = 3;
28
29 public const int AUTH_LOCAL = 1;
30 public const int AUTH_LDAP = 2;
31 public const int AUTH_SCRIPT = 4;
32 public const int AUTH_SHIBBOLETH = 5;
33 public const int AUTH_SOAP = 7;
34 public const int AUTH_HTTP = 8; // Used for WebDAV
35 public const int AUTH_ECS = 9;
36 public const int AUTH_APACHE = 11;
37 public const int AUTH_SAML = 12;
38 public const int AUTH_OPENID_CONNECT = 15;
39 //TODO this is not used anywhere, can it be removed
40 private const int AUTH_INACTIVE = 18;
41 //TODO this is not used anywhere, can it be removed
42 private const int AUTH_MULTIPLE = 20;
43 //TODO this is not used anywhere, can it be removed
44 private const int AUTH_SESSION = 21;
45 public const int AUTH_PROVIDER_LTI = 22;
46
47 //TODO this is not used anywhere, can it be removed
48 private const int AUTH_SOAP_NO_ILIAS_USER = -100;
49 //TODO this is not used anywhere, can it be removed
50 private const int AUTH_LDAP_NO_ILIAS_USER = -200;
51 // apache auhtentication failed...
52 // maybe no (valid) certificate or
53 // username could not be extracted
54 //TODO this is not used anywhere, can it be removed
55 private const int AUTH_APACHE_FAILED = -500;
56 //TODO this is not used anywhere, can it be removed
57 private const int AUTH_SAML_FAILED = -501;
58 //TODO this is not used anywhere, can it be removed
59 private const int AUTH_MODE_INACTIVE = -1000;
60 // an external user cannot be found in ilias, but his email address
61 // matches one or more ILIAS users
62 //TODO this is not used anywhere, can it be removed?
63 private const int AUTH_SOAP_NO_ILIAS_USER_BUT_EMAIL = -101;
64 // ilUser validation (no login)
65 //TODO All these are is not used anywhere, can it be removed?
66 private const int AUTH_USER_WRONG_IP = -600;
67 private const int AUTH_USER_INACTIVE = -601;
68 private const int AUTH_USER_TIME_LIMIT_EXCEEDED = -602;
69 private const int AUTH_USER_SIMULTANEOUS_LOGIN = -603;
70
72 public const array REGEX_DELIMITERS = ['/', '~', '@', ';', '%', '`', '#'];
73
74 public static function isAuthenticationForced(): bool
75 {
76 //TODO rework forced authentication concept
77 global $DIC;
78 $query_wrapper = $DIC->http()->wrapper()->query();
79 return $query_wrapper->has('ecs_hash') || $query_wrapper->has('ecs_hash_url');
80 }
81
82 public static function handleForcedAuthentication(): void
83 {
84 global $DIC;
85 $query_wrapper = $DIC->http()->wrapper()->query();
86 $string_refinery = $DIC->refinery()->kindlyTo()->string();
87 if ($query_wrapper->has('ecs_hash') || $query_wrapper->has('ecs_hash_url')) {
88 $credentials = new ilAuthFrontendCredentials();
89 $credentials->setUsername($query_wrapper->retrieve('ecs_login', $string_refinery));
90 $credentials->setAuthMode((string) self::AUTH_ECS);
91
92 $provider_factory = new ilAuthProviderFactory();
93 $providers = $provider_factory->getProviders($credentials);
94
95 $status = ilAuthStatus::getInstance();
96
97 $frontend_factory = new ilAuthFrontendFactory();
98 $frontend_factory->setContext(ilAuthFrontendFactory::CONTEXT_STANDARD_FORM);
99 $frontend = $frontend_factory->getFrontend(
100 $GLOBALS['DIC']['ilAuthSession'],
101 $status,
102 $credentials,
103 $providers
104 );
105 $frontend->authenticate();
106
107 switch ($status->getStatus()) {
109 return;
110
113 return;
114 }
115 }
116 }
117
121 public static function _getAuthMode(?string $a_auth_mode)
122 {
123 global $DIC;
124
125 $ilSetting = $DIC['ilSetting'];
126
127 if ($a_auth_mode === null) {
128 return $ilSetting->get('auth_mode');
129 }
130
131 if (str_contains($a_auth_mode, '_')) {
132 $auth_arr = explode('_', $a_auth_mode);
133 $auth_switch = $auth_arr[0];
134 } else {
135 $auth_switch = $a_auth_mode;
136 }
137 switch ($auth_switch) {
138 case 'local':
139 return self::AUTH_LOCAL;
140 break;
141
142 case 'ldap':
143 return ilLDAPServer::getKeyByAuthMode($a_auth_mode);
144
145 case 'lti':
146 return ilAuthProviderLTI::getKeyByAuthMode($a_auth_mode);
147
148 case 'script':
149 return self::AUTH_SCRIPT;
150 break;
151
152 case 'shibboleth':
154 break;
155
156 case 'oidc':
158 break;
159
160 case 'saml':
161 return ilSamlIdp::getKeyByAuthMode($a_auth_mode);
162
163 case 'soap':
164 return self::AUTH_SOAP;
165 break;
166
167 case 'ecs':
168 return self::AUTH_ECS;
169
170 case 'apache':
171 return self::AUTH_APACHE;
172
173 default:
174 return $ilSetting->get('auth_mode');
175 break;
176 }
177 }
178
182 public static function _getAuthModeName($a_auth_key): string
183 {
184 switch ((int) $a_auth_key) {
185 case self::AUTH_LOCAL:
186 return 'local';
187 break;
188
189 case self::AUTH_LDAP:
190 // begin-patch ldap_multiple
191 return ilLDAPServer::getAuthModeByKey($a_auth_key);
192 // end-patch ldap_multiple
193
195 return ilAuthProviderLTI::getAuthModeByKey($a_auth_key);
196
198 return 'script';
199 break;
200
202 return 'shibboleth';
203 break;
204
205 case self::AUTH_SAML:
206 return ilSamlIdp::getAuthModeByKey($a_auth_key);
207
208 case self::AUTH_SOAP:
209 return 'soap';
210 break;
211
212 case self::AUTH_ECS:
213 return 'ecs';
214
216 return 'apache';
217
219 return 'oidc';
220 break;
221
222 default:
223 return 'default';
224 break;
225 }
226 }
227
231 public static function _getActiveAuthModes(): array
232 {
233 global $DIC;
234
235 $ilSetting = $DIC['ilSetting'];
236
237 $modes = [
238 'default' => $ilSetting->get('auth_mode'),
239 'local' => self::AUTH_LOCAL
240 ];
241
242 foreach (ilLDAPServer::_getActiveServerList() as $sid) {
243 $modes['ldap_' . $sid] = (self::AUTH_LDAP . '_' . $sid);
244 }
245
246 foreach (ilAuthProviderLTI::getAuthModes() as $sid) {
247 $modes['lti_' . $sid] = (self::AUTH_PROVIDER_LTI . '_' . $sid);
248 }
249
250 if (ilOpenIdConnectSettings::getInstance()->getActive()) {
251 $modes['oidc'] = self::AUTH_OPENID_CONNECT;
252 }
253
254 if ($ilSetting->get('shib_active')) {
255 $modes['shibboleth'] = self::AUTH_SHIBBOLETH;
256 }
257 if ($ilSetting->get('script_active')) {
258 $modes['script'] = self::AUTH_SCRIPT;
259 }
260 if ($ilSetting->get('soap_auth_active')) {
261 $modes['soap'] = self::AUTH_SOAP;
262 }
263 if ($ilSetting->get('apache_active')) {
264 $modes['apache'] = self::AUTH_APACHE;
265 }
266
267 if (ilECSServerSettings::getInstance()->activeServerExists()) {
268 $modes['ecs'] = self::AUTH_ECS;
269 }
270
271 foreach (ilSamlIdp::getActiveIdpList() as $idp) {
272 $idpId = $idp->getIdpId();
273 $modes['saml_' . $idpId] = self::AUTH_SAML . '_' . $idpId;
274 }
275
276 // begin-path auth_plugin
277 foreach (self::getAuthPlugins() as $pl) {
278 foreach ($pl->getAuthIds() as $auth_id) {
279 if ($pl->isAuthActive($auth_id)) {
280 $modes[$pl->getAuthName($auth_id)] = $auth_id;
281 }
282 }
283 }
284 // end-path auth_plugin
285 return $modes;
286 }
287
291 public static function _getAllAuthModes(): array
292 {
293 $modes = [
303 ];
304 $ret = [];
305 foreach ($modes as $mode) {
306 if ($mode === self::AUTH_PROVIDER_LTI) {
307 foreach (ilAuthProviderLTI::getAuthModes() as $sid) {
308 $id = self::AUTH_PROVIDER_LTI . '_' . $sid;
310 }
311 continue;
312 }
313
314 // multi ldap implementation
315 if ($mode === self::AUTH_LDAP) {
316 foreach (ilLDAPServer::_getServerList() as $ldap_id) {
317 $id = self::AUTH_LDAP . '_' . $ldap_id;
319 }
320 continue;
321 }
322
323 if ($mode === self::AUTH_SAML) {
324 foreach (ilSamlIdp::getAllIdps() as $idp) {
325 $id = self::AUTH_SAML . '_' . $idp->getIdpId();
327 }
328 continue;
329 }
330 $ret[$mode] = self::_getAuthModeName($mode);
331 }
332 return $ret;
333 }
334
339 public static function _generateLogin(string $a_login): string
340 {
341 global $DIC;
342
343 $ilDB = $DIC['ilDB'];
344
345 // Check if username already exists
346 $found = false;
347 $postfix = 0;
348 $c_login = $a_login;
349 while (!$found) {
350 $r = $ilDB->query(
351 'SELECT login FROM usr_data WHERE login = ' .
352 $ilDB->quote($c_login)
353 );
354 if ($r->numRows() > 0) {
355 $postfix++;
356 $c_login = $a_login . $postfix;
357 } else {
358 $found = true;
359 }
360 }
361
362 return $c_login;
363 }
364
365 public static function _hasMultipleAuthenticationMethods(): bool
366 {
368 return true;
369 }
370
371 global $DIC;
372
373 $ilSetting = $DIC['ilSetting'];
374
375 if ($ilSetting->get('apache_active')) {
376 return true;
377 }
378
379 // begin-patch auth_plugin
380 foreach (self::getAuthPlugins() as $pl) {
381 foreach ($pl->getAuthIds() as $auth_id) {
382 if ($pl->getMultipleAuthModeOptions($auth_id)) {
383 return true;
384 }
385 }
386 }
387 // end-patch auth_plugin
388
389 return false;
390 }
391
395 public static function _getMultipleAuthModeOptions(ilLanguage $lng): array
396 {
397 global $DIC;
398
399 $ilSetting = $DIC['ilSetting'];
400 $options = [];
401 // in the moment only ldap is activated as additional authentication method
402
403 $options[self::AUTH_LOCAL]['txt'] = $lng->txt('authenticate_ilias');
404
405
406 foreach (ilLDAPServer::_getActiveServerList() as $sid) {
408 $options[self::AUTH_LDAP . '_' . $sid]['txt'] = $server->getName();
409 }
410
411 if ($ilSetting->get('apache_active')) {
412 global $DIC;
413
414 $lng = $DIC['lng'];
415 $apache_settings = new ilSetting('apache_auth');
416 $options[self::AUTH_APACHE]['txt'] = $apache_settings->get('name', $lng->txt('apache_auth'));
417 $options[self::AUTH_APACHE]['hide_in_ui'] = true;
418 }
419
420 if ($ilSetting->get('auth_mode', (string) self::AUTH_LOCAL) === (string) self::AUTH_LDAP) {
421 $default = self::AUTH_LDAP;
422 } else {
423 $default = self::AUTH_LOCAL;
424 }
425
426 $default = $ilSetting->get('default_auth_mode', (string) $default);
427
428 // begin-patch auth_plugin
429 $pls = self::getAuthPlugins();
430 foreach ($pls as $pl) {
431 $auths = $pl->getAuthIds();
432 foreach ($auths as $auth_id) {
433 $pl_auth_option = $pl->getMultipleAuthModeOptions($auth_id);
434 if ($pl_auth_option) {
435 $options += $pl_auth_option;
436 }
437 }
438 }
439 // end-patch auth_plugins
440
441 if (array_key_exists($default, $options)) {
442 $options[$default]['checked'] = true;
443 }
444
445 return $options;
446 }
447
452 public static function _isExternalAccountEnabled(): bool
453 {
454 global $DIC;
455
456 $ilSetting = $DIC['ilSetting'];
457
458 if ($ilSetting->get('soap_auth_active')) {
459 return true;
460 }
461 if ($ilSetting->get('shib_active')) {
462 return true;
463 }
465 return true;
466 }
467
469 return true;
470 }
471
472 if (count(ilSamlIdp::getActiveIdpList()) > 0) {
473 return true;
474 }
475
476 if (ilOpenIdConnectSettings::getInstance()->getActive()) {
477 return true;
478 }
479
480 // begin-path auth_plugin
481 foreach (self::getAuthPlugins() as $pl) {
482 foreach ($pl->getAuthIds() as $auth_id) {
483 if ($pl->isAuthActive($auth_id) && $pl->isExternalAccountNameRequired($auth_id)) {
484 return true;
485 }
486 }
487 }
488 // end-path auth_plugin
489
490 return false;
491 }
492
497 public static function _allowPasswordModificationByAuthMode($a_auth_mode): bool
498 {
499 switch ((int) $a_auth_mode) {
500 case self::AUTH_LDAP:
501 case self::AUTH_ECS:
504 return false;
505 default:
506 return true;
507 }
508 }
509
515 public static function _needsExternalAccountByAuthMode($a_auth_mode): bool
516 {
517 switch ($a_auth_mode) {
518 case self::AUTH_LOCAL:
520 return false;
521 default:
522 return true;
523 }
524 }
525
526 public static function isPasswordModificationHidden(): bool
527 {
528 global $DIC;
529
531 $password_setting = $DIC['user']->getSettings()->getSettingByDefinitionClass(Password::class);
532
533 return !$password_setting->isChangeableByUser();
534 }
535
540 public static function isLocalPasswordEnabledForAuthMode($a_authmode): bool
541 {
542 global $DIC;
543
544 $ilSetting = $DIC->settings();
545
546 switch ((int) $a_authmode) {
547 // always enabled
548 case self::AUTH_LOCAL:
550 return true;
551
552 // No local passwords for these auth modes
553 case self::AUTH_LDAP:
554 case self::AUTH_ECS:
558 return false;
559
560 case self::AUTH_SAML:
562 return $idp->isActive() && $idp->allowLocalAuthentication();
563
565 return (bool) $ilSetting->get('shib_auth_allow_local', '0');
566 case self::AUTH_SOAP:
567 return (bool) $ilSetting->get('soap_auth_allow_local', '0');
568 }
569 return false;
570 }
571
572
577 public static function isPasswordModificationEnabled($a_authmode): bool
578 {
579 if (self::isPasswordModificationHidden()) {
580 return false;
581 }
582
583 return self::isLocalPasswordEnabledForAuthMode($a_authmode);
584 }
585
591 public static function supportsLocalPasswordValidation($a_authmode): int
592 {
593 switch ((int) $a_authmode) {
594 case self::AUTH_LDAP:
595 case self::AUTH_LOCAL:
597
600 case self::AUTH_SAML:
601 case self::AUTH_SOAP:
602 if (!self::isPasswordModificationEnabled($a_authmode)) {
603 return self::LOCAL_PWV_NO;
604 }
607 case self::AUTH_ECS:
610 default:
612 }
613 }
614
618 public static function getAuthPlugins(): Iterator
619 {
620 return $GLOBALS['DIC']['component.factory']->getActivePluginsInSlot('authhk');
621 }
622
623 public static function getAuthModeTranslation(string $a_auth_key, string $auth_name = ''): ?string
624 {
625 global $DIC;
626
627 $lng = $DIC['lng'];
628
629 //TODO fix casting strings like 2_1 (auth_key for first ldap server) to int to get it to 2
630 switch ((int) $a_auth_key) {
631 case self::AUTH_LDAP:
632 $sid = ilLDAPServer::getServerIdByAuthMode($a_auth_key);
633 return ilLDAPServer::getInstanceByServerId($sid)->getName();
634
638
639
640 case self::AUTH_SAML:
641 $idp_id = ilSamlIdp::getIdpIdByAuthMode($a_auth_key);
642 return ilSamlIdp::getInstanceByIdpId($idp_id)->getEntityId();
643
644 default:
645 $lng->loadLanguageModule('auth');
646 if (!empty($auth_name)) {
647 return $lng->txt('auth_' . $auth_name);
648 }
649
650 return $lng->txt('auth_' . self::_getAuthModeName($a_auth_key));
651 }
652 }
653}
$id
plugin.php for ilComponentBuildPluginInfoObjectiveTest::testAddPlugins
Definition: plugin.php:23
const int CONTEXT_STANDARD_FORM
Authentication with id and password.
static getServerIdByAuthMode(string $a_auth_mode)
Get auth id by auth mode.
static getActiveAuthModes()
get all active authmode server ids
static getKeyByAuthMode(string $a_auth_mode)
Get auth id by auth mode.
static lookupConsumer(int $a_sid)
Lookup consumer title.
static getAuthModeByKey(string $a_auth_key)
Get auth mode by key.
const int STATUS_AUTHENTICATION_FAILED
const int STATUS_AUTHENTICATED
static getInstance()
Get status instance.
const int AUTH_ECS
static _getMultipleAuthModeOptions(ilLanguage $lng)
static _allowPasswordModificationByAuthMode($a_auth_mode)
Allow password modification.
const int AUTH_SHIBBOLETH
const int AUTH_APACHE_FAILED
const int AUTH_LDAP_NO_ILIAS_USER
static isAuthenticationForced()
static _getActiveAuthModes()
const array REGEX_DELIMITERS
const int AUTH_LOCAL
const int AUTH_SOAP_NO_ILIAS_USER
static supportsLocalPasswordValidation($a_authmode)
Check if local password validation is supported.
const int AUTH_INACTIVE
const int AUTH_USER_INACTIVE
const int AUTH_LDAP
static isLocalPasswordEnabledForAuthMode($a_authmode)
Check if local password validation is enabled for a specific auth_mode.
const int AUTH_SAML
const int AUTH_APACHE
const int AUTH_USER_SIMULTANEOUS_LOGIN
static _isExternalAccountEnabled()
Check if an external account name is required.
static getAuthModeTranslation(string $a_auth_key, string $auth_name='')
const int AUTH_SESSION
static getAuthPlugins()
const int AUTH_SOAP_NO_ILIAS_USER_BUT_EMAIL
const int AUTH_SOAP
const int AUTH_SCRIPT
const int AUTH_OPENID_CONNECT
const int AUTH_MULTIPLE
static _hasMultipleAuthenticationMethods()
const int LOCAL_PWV_USER
static isPasswordModificationEnabled($a_authmode)
Check if password modification is enabled.
static _generateLogin(string $a_login)
generate free login by starting with a default string and adding postfix numbers
const int AUTH_PROVIDER_LTI
static _needsExternalAccountByAuthMode($a_auth_mode)
Check if chosen auth mode needs an external account entry.
const int AUTH_HTTP
const int AUTH_USER_TIME_LIMIT_EXCEEDED
static _getAuthMode(?string $a_auth_mode)
static handleForcedAuthentication()
const int LOCAL_PWV_NO
const int AUTH_SAML_FAILED
const int AUTH_USER_WRONG_IP
static _getAllAuthModes()
const int AUTH_MODE_INACTIVE
const int LOCAL_PWV_FULL
static _getAuthModeName($a_auth_key)
static getInstance()
Get singleton instance.
static goToPublicSection()
go to public section
static _getServerList()
Get list of all configured servers.
static getAuthModeByKey(string $a_auth_key)
get auth mode by key
static _getActiveServerList()
Get active server list.
static getInstanceByServerId(int $a_server_id)
Get instance by server id.
static getServerIdByAuthMode(string $a_auth_mode)
Get auth id by auth mode.
static getKeyByAuthMode(string $a_auth_mode)
Get auth id by auth mode.
language handling
static getActiveIdpList()
static getIdpIdByAuthMode(string $a_auth_mode)
static getAuthModeByKey(string $a_auth_key)
static getInstanceByIdpId(int $a_idp_id)
static getAllIdps()
static getKeyByAuthMode(string $a_auth_mode)
ILIAS Setting Class.
global $lng
Definition: privfeed.php:31
global $ilSetting
Definition: privfeed.php:31
global $DIC
Definition: shib_login.php:26
$server
Definition: shib_login.php:28
$GLOBALS["DIC"]
Definition: wac.php:54