ILIAS  release_8 Revision v8.24
class.ilAuthUtils.php
Go to the documentation of this file.
1<?php
2
19declare(strict_types=1);
20
27{
28 public const LOCAL_PWV_FULL = 1;
29 public const LOCAL_PWV_NO = 2;
30 public const LOCAL_PWV_USER = 3;
31
32
33 public const AUTH_LOCAL = 1;
34 public const AUTH_LDAP = 2;
35 public const AUTH_SCRIPT = 4;
36 public const AUTH_SHIBBOLETH = 5;
37 public const AUTH_CAS = 6;
38 public const AUTH_SOAP = 7;
39 public const AUTH_HTTP = 8; // Used for WebDAV
40 public const AUTH_ECS = 9;
41
42 public const AUTH_APACHE = 11;
43 public const AUTH_SAML = 12;
44
45 public const AUTH_OPENID_CONNECT = 15;
46
47 //TODO this is not used anywhere, can it be removed
48 private const AUTH_INACTIVE = 18;
49
50 //TODO this is not used anywhere, can it be removed
51 private const AUTH_MULTIPLE = 20;
52
53 //TODO this is not used anywhere, can it be removed
54 private const AUTH_SESSION = 21;
55
56 public const AUTH_PROVIDER_LTI = 22;
57
58 //TODO this is not used anywhere, can it be removed
59 private const AUTH_SOAP_NO_ILIAS_USER = -100;
60 //TODO this is not used anywhere, can it be removed
61 private const AUTH_LDAP_NO_ILIAS_USER = -200;
62
63 // apache auhtentication failed...
64 // maybe no (valid) certificate or
65 // username could not be extracted
66 //TODO this is not used anywhere, can it be removed
67 private const AUTH_APACHE_FAILED = -500;
68
69 //TODO this is not used anywhere, can it be removed
70 private const AUTH_SAML_FAILED = -501;
71
72 //TODO this is not used anywhere, can it be removed
73 private const AUTH_MODE_INACTIVE = -1000;
74
75 // an external user cannot be found in ilias, but his email address
76 // matches one or more ILIAS users
77 //TODO this is not used anywhere, can it be removed?
79 //TODO this is not used anywhere, can it be removed?
80 private const AUTH_CAS_NO_ILIAS_USER = -90;
81
82 // ilUser validation (no login)
83 //TODO All these are is not used anywhere, can it be removed?
84 private const AUTH_USER_WRONG_IP = -600;
85 private const AUTH_USER_INACTIVE = -601;
86 private const AUTH_USER_TIME_LIMIT_EXCEEDED = -602;
87 private const AUTH_USER_SIMULTANEOUS_LOGIN = -603;
88
92 public static function isAuthenticationForced(): bool
93 {
94 //TODO rework forced authentication concept
95 global $DIC;
96 $query_wrapper = $DIC->http()->wrapper()->query();
97 return $query_wrapper->has('ecs_hash') || $query_wrapper->has('ecs_hash_url');
98 }
99
100 public static function handleForcedAuthentication(): void
101 {
102 global $DIC;
103 $query_wrapper = $DIC->http()->wrapper()->query();
104 $string_refinery = $DIC->refinery()->kindlyTo()->string();
105 if ($query_wrapper->has('ecs_hash') || $query_wrapper->has('ecs_hash_url')) {
106 $credentials = new ilAuthFrontendCredentials();
107 $credentials->setUsername($query_wrapper->retrieve('ecs_login', $string_refinery));
108 $credentials->setAuthMode((string) self::AUTH_ECS);
109
110 $provider_factory = new ilAuthProviderFactory();
111 $providers = $provider_factory->getProviders($credentials);
112
113 $status = ilAuthStatus::getInstance();
114
115 $frontend_factory = new ilAuthFrontendFactory();
116 $frontend_factory->setContext(ilAuthFrontendFactory::CONTEXT_STANDARD_FORM);
117 $frontend = $frontend_factory->getFrontend(
118 $GLOBALS['DIC']['ilAuthSession'],
119 $status,
120 $credentials,
121 $providers
122 );
123
124 $frontend->authenticate();
125
126 switch ($status->getStatus()) {
128 return;
129
132 return;
133 }
134 }
135 }
136
140 public static function _getAuthMode(?string $a_auth_mode)
141 {
142 global $DIC;
143
144 $ilSetting = $DIC['ilSetting'];
145
146 if (null === $a_auth_mode) {
147 return $ilSetting->get("auth_mode");
148 }
149
150 if (strpos($a_auth_mode, '_') !== false) {
151 $auth_arr = explode('_', $a_auth_mode);
152 $auth_switch = $auth_arr[0];
153 } else {
154 $auth_switch = $a_auth_mode;
155 }
156 switch ($auth_switch) {
157 case "local":
158 return self::AUTH_LOCAL;
159 break;
160
161 case "ldap":
162 return ilLDAPServer::getKeyByAuthMode($a_auth_mode);
163
164 case 'lti':
165 return ilAuthProviderLTI::getKeyByAuthMode($a_auth_mode);
166
167 case "script":
168 return self::AUTH_SCRIPT;
169 break;
170
171 case "shibboleth":
173 break;
174
175 case 'oidc':
177 break;
178
179 case 'saml':
180 return ilSamlIdp::getKeyByAuthMode($a_auth_mode);
181
182 case "cas":
183 return self::AUTH_CAS;
184 break;
185
186 case "soap":
187 return self::AUTH_SOAP;
188 break;
189
190 case 'ecs':
191 return self::AUTH_ECS;
192
193 case 'apache':
194 return self::AUTH_APACHE;
195
196 default:
197 return $ilSetting->get("auth_mode");
198 break;
199 }
200 }
201
205 public static function _getAuthModeName($a_auth_key): string
206 {
207 switch ((int) $a_auth_key) {
208 case self::AUTH_LOCAL:
209 return "local";
210 break;
211
212 case self::AUTH_LDAP:
213 // begin-patch ldap_multiple
214 return ilLDAPServer::getAuthModeByKey($a_auth_key);
215 // end-patch ldap_multiple
216
218 return ilAuthProviderLTI::getAuthModeByKey($a_auth_key);
219
220 case self::AUTH_CAS:
221 return "cas";
222 break;
223
225 return "script";
226 break;
227
229 return "shibboleth";
230 break;
231
232 case self::AUTH_SAML:
233 return ilSamlIdp::getAuthModeByKey($a_auth_key);
234
235 case self::AUTH_SOAP:
236 return "soap";
237 break;
238
239 case self::AUTH_ECS:
240 return 'ecs';
241
243 return 'apache';
244
246 return 'oidc';
247 break;
248
249 default:
250 return "default";
251 break;
252 }
253 }
254
258 public static function _getActiveAuthModes(): array
259 {
260 global $DIC;
261
262 $ilSetting = $DIC['ilSetting'];
263
264 $modes = [
265 'default' => $ilSetting->get("auth_mode"),
266 'local' => self::AUTH_LOCAL
267 ];
268
269 foreach (ilLDAPServer::_getActiveServerList() as $sid) {
270 $modes['ldap_' . $sid] = (self::AUTH_LDAP . '_' . $sid);
271 }
272
273 foreach (ilAuthProviderLTI::getAuthModes() as $sid) {
274 $modes['lti_' . $sid] = (self::AUTH_PROVIDER_LTI . '_' . $sid);
275 }
276
277 if (ilOpenIdConnectSettings::getInstance()->getActive()) {
278 $modes['oidc'] = self::AUTH_OPENID_CONNECT;
279 }
280
281 if ($ilSetting->get("shib_active")) {
282 $modes['shibboleth'] = self::AUTH_SHIBBOLETH;
283 }
284 if ($ilSetting->get("script_active")) {
285 $modes['script'] = self::AUTH_SCRIPT;
286 }
287 if ($ilSetting->get("cas_active")) {
288 $modes['cas'] = self::AUTH_CAS;
289 }
290 if ($ilSetting->get("soap_auth_active")) {
291 $modes['soap'] = self::AUTH_SOAP;
292 }
293 if ($ilSetting->get("apache_active")) {
294 $modes['apache'] = self::AUTH_APACHE;
295 }
296
297 if (ilECSServerSettings::getInstance()->activeServerExists()) {
298 $modes['ecs'] = self::AUTH_ECS;
299 }
300
301 foreach (ilSamlIdp::getActiveIdpList() as $idp) {
302 $idpId = $idp->getIdpId();
303 $modes['saml_' . $idpId] = self::AUTH_SAML . '_' . $idpId;
304 }
305
306 // begin-path auth_plugin
307 foreach (self::getAuthPlugins() as $pl) {
308 foreach ($pl->getAuthIds() as $auth_id) {
309 if ($pl->isAuthActive($auth_id)) {
310 $modes[$pl->getAuthName($auth_id)] = $auth_id;
311 }
312 }
313 }
314 // end-path auth_plugin
315 return $modes;
316 }
317
321 public static function _getAllAuthModes(): array
322 {
323 $modes = array(
324 self::AUTH_LOCAL,
325 self::AUTH_LDAP,
326 self::AUTH_SHIBBOLETH,
327 self::AUTH_SAML,
328 self::AUTH_CAS,
329 self::AUTH_SOAP,
330 self::AUTH_ECS,
331 self::AUTH_PROVIDER_LTI,
332 self::AUTH_OPENID_CONNECT,
333 self::AUTH_APACHE
334 );
335 $ret = array();
336 foreach ($modes as $mode) {
337 if ($mode === self::AUTH_PROVIDER_LTI) {
338 foreach (ilAuthProviderLTI::getAuthModes() as $sid) {
339 $id = self::AUTH_PROVIDER_LTI . '_' . $sid;
341 }
342 continue;
343 }
344
345 // multi ldap implementation
346 if ($mode === self::AUTH_LDAP) {
347 foreach (ilLDAPServer::_getServerList() as $ldap_id) {
348 $id = self::AUTH_LDAP . '_' . $ldap_id;
350 }
351 continue;
352 }
353
354 if ($mode === self::AUTH_SAML) {
355 foreach (ilSamlIdp::getAllIdps() as $idp) {
356 $id = self::AUTH_SAML . '_' . $idp->getIdpId();
358 }
359 continue;
360 }
361 $ret[$mode] = self::_getAuthModeName($mode);
362 }
363 return $ret;
364 }
365
370 public static function _generateLogin(string $a_login): string
371 {
372 global $DIC;
373
374 $ilDB = $DIC['ilDB'];
375
376 // Check if username already exists
377 $found = false;
378 $postfix = 0;
379 $c_login = $a_login;
380 while (!$found) {
381 $r = $ilDB->query("SELECT login FROM usr_data WHERE login = " .
382 $ilDB->quote($c_login));
383 if ($r->numRows() > 0) {
384 $postfix++;
385 $c_login = $a_login . $postfix;
386 } else {
387 $found = true;
388 }
389 }
390
391 return $c_login;
392 }
393
394 public static function _hasMultipleAuthenticationMethods(): bool
395 {
397 return true;
398 }
399
400 global $DIC;
401
402 $ilSetting = $DIC['ilSetting'];
403
404 if ($ilSetting->get('apache_active')) {
405 return true;
406 }
407
408 // begin-patch auth_plugin
409 foreach (self::getAuthPlugins() as $pl) {
410 foreach ($pl->getAuthIds() as $auth_id) {
411 if ($pl->getMultipleAuthModeOptions($auth_id)) {
412 return true;
413 }
414 }
415 }
416 // end-patch auth_plugin
417
418
419 return false;
420 }
421
426 public static function _getMultipleAuthModeOptions(ilLanguage $lng): array
427 {
428 global $DIC;
429
430 $ilSetting = $DIC['ilSetting'];
431 $options = [];
432 // in the moment only ldap is activated as additional authentication method
433
434 $options[self::AUTH_LOCAL]['txt'] = $lng->txt('authenticate_ilias');
435
436
437 foreach (ilLDAPServer::_getActiveServerList() as $sid) {
439 $options[self::AUTH_LDAP . '_' . $sid]['txt'] = $server->getName();
440 }
441
442 if ($ilSetting->get('apache_active')) {
443 global $DIC;
444
445 $lng = $DIC['lng'];
446 $apache_settings = new ilSetting('apache_auth');
447 $options[self::AUTH_APACHE]['txt'] = $apache_settings->get('name', $lng->txt('apache_auth'));
448 $options[self::AUTH_APACHE]['hide_in_ui'] = true;
449 }
450
451 if ($ilSetting->get('auth_mode', (string) self::AUTH_LOCAL) === (string) self::AUTH_LDAP) {
452 $default = self::AUTH_LDAP;
453 } else {
454 $default = self::AUTH_LOCAL;
455 }
456
457 $default = $ilSetting->get('default_auth_mode', (string) $default);
458
459 // begin-patch auth_plugin
460 $pls = self::getAuthPlugins();
461 foreach ($pls as $pl) {
462 $auths = $pl->getAuthIds();
463 foreach ($auths as $auth_id) {
464 $pl_auth_option = $pl->getMultipleAuthModeOptions($auth_id);
465 if ($pl_auth_option) {
466 $options += $pl_auth_option;
467 }
468 }
469 }
470 // end-patch auth_plugins
471
472 if (array_key_exists($default, $options)) {
473 $options[$default]['checked'] = true;
474 }
475
476 return $options;
477 }
478
483 public static function _isExternalAccountEnabled(): bool
484 {
485 global $DIC;
486
487 $ilSetting = $DIC['ilSetting'];
488
489 if ($ilSetting->get("cas_active")) {
490 return true;
491 }
492 if ($ilSetting->get("soap_auth_active")) {
493 return true;
494 }
495 if ($ilSetting->get("shib_active")) {
496 return true;
497 }
499 return true;
500 }
501
503 return true;
504 }
505
506 if (count(ilSamlIdp::getActiveIdpList()) > 0) {
507 return true;
508 }
509
510 if (ilOpenIdConnectSettings::getInstance()->getActive()) {
511 return true;
512 }
513
514 // begin-path auth_plugin
515 foreach (self::getAuthPlugins() as $pl) {
516 foreach ($pl->getAuthIds() as $auth_id) {
517 if ($pl->isAuthActive($auth_id) && $pl->isExternalAccountNameRequired($auth_id)) {
518 return true;
519 }
520 }
521 }
522 // end-path auth_plugin
523
524 return false;
525 }
526
531 public static function _allowPasswordModificationByAuthMode($a_auth_mode): bool
532 {
533 switch ((int) $a_auth_mode) {
534 case self::AUTH_LDAP:
535 case self::AUTH_ECS:
538 return false;
539 default:
540 return true;
541 }
542 }
543
549 public static function _needsExternalAccountByAuthMode($a_auth_mode): bool
550 {
551 switch ($a_auth_mode) {
552 case self::AUTH_LOCAL:
554 return false;
555 default:
556 return true;
557 }
558 }
559
563 public static function isPasswordModificationHidden(): bool
564 {
566 global $DIC;
567
568 $ilSetting = $DIC['ilSetting'];
569
570 return $ilSetting->get('usr_settings_hide_password') || $ilSetting->get('usr_settings_disable_password');
571 }
572
578 public static function isLocalPasswordEnabledForAuthMode($a_authmode): bool
579 {
580 global $DIC;
581
582 $ilSetting = $DIC->settings();
583
584 switch ((int) $a_authmode) {
585 // always enabled
586 case self::AUTH_LOCAL:
588 return true;
589
590 // No local passwords for these auth modes
591 case self::AUTH_LDAP:
592 case self::AUTH_ECS:
596 return false;
597
598 case self::AUTH_SAML:
600 return $idp->isActive() && $idp->allowLocalAuthentication();
601
603 return (bool) $ilSetting->get("shib_auth_allow_local", '0');
604 case self::AUTH_SOAP:
605 return (bool) $ilSetting->get("soap_auth_allow_local", '0');
606 case self::AUTH_CAS:
607 return (bool) $ilSetting->get("cas_allow_local", '0');
608
609 }
610 return false;
611 }
612
613
619 public static function isPasswordModificationEnabled($a_authmode): bool
620 {
621 if (self::isPasswordModificationHidden()) {
622 return false;
623 }
624
625 return self::isLocalPasswordEnabledForAuthMode($a_authmode);
626 }
627
633 public static function supportsLocalPasswordValidation($a_authmode): int
634 {
635 switch ((int) $a_authmode) {
636 case self::AUTH_LDAP:
637 case self::AUTH_LOCAL:
639
642 case self::AUTH_SAML:
643 case self::AUTH_SOAP:
644 case self::AUTH_CAS:
645 if (!self::isPasswordModificationEnabled($a_authmode)) {
646 return self::LOCAL_PWV_NO;
647 }
649
651 case self::AUTH_ECS:
654 default:
656 }
657 }
658
662 public static function getAuthPlugins(): \Iterator
663 {
664 return $GLOBALS['DIC']['component.factory']->getActivePluginsInSlot('authhk');
665 }
666
667 public static function getAuthModeTranslation(string $a_auth_key, string $auth_name = ''): ?string
668 {
669 global $DIC;
670
671 $lng = $DIC['lng'];
672
673 //TODO fix casting strings like 2_1 (auth_key for first ldap server) to int to get it to 2
674 switch ((int) $a_auth_key) {
675 case self::AUTH_LDAP:
676 $sid = ilLDAPServer::getServerIdByAuthMode($a_auth_key);
677 return ilLDAPServer::getInstanceByServerId($sid)->getName();
678
682
683
684 case self::AUTH_SAML:
685 $idp_id = ilSamlIdp::getIdpIdByAuthMode($a_auth_key);
686 return ilSamlIdp::getInstanceByIdpId($idp_id)->getEntityId();
687
688 default:
689 $lng->loadLanguageModule('auth');
690 if (!empty($auth_name)) {
691 return $lng->txt('auth_' . $auth_name);
692 }
693
694 return $lng->txt('auth_' . self::_getAuthModeName($a_auth_key));
695
696 }
697 }
698}
$id
plugin.php for ilComponentBuildPluginInfoObjectiveTest::testAddPlugins
Definition: plugin.php:23
if(!defined('PATH_SEPARATOR')) $GLOBALS['_PEAR_default_error_mode']
Definition: PEAR.php:64
Factory for auth frontend classes.
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.
static getInstance()
Get status instance.
const STATUS_AUTHENTICATION_FAILED
static utility functions used to manage authentication modes
static _getMultipleAuthModeOptions(ilLanguage $lng)
static _allowPasswordModificationByAuthMode($a_auth_mode)
Allow password modification.
const AUTH_USER_WRONG_IP
static isAuthenticationForced()
Check if authentication is should be forced.
const AUTH_CAS_NO_ILIAS_USER
const AUTH_SOAP_NO_ILIAS_USER_BUT_EMAIL
static _getActiveAuthModes()
const AUTH_OPENID_CONNECT
static supportsLocalPasswordValidation($a_authmode)
Check if local password validation is supported.
const AUTH_LDAP_NO_ILIAS_USER
static isLocalPasswordEnabledForAuthMode($a_authmode)
Check if local password validation is enabled for a specific auth_mode.
static _isExternalAccountEnabled()
Check if an external account name is required.
static getAuthModeTranslation(string $a_auth_key, string $auth_name='')
const AUTH_USER_SIMULTANEOUS_LOGIN
const AUTH_USER_INACTIVE
static getAuthPlugins()
Get active enabled auth plugins.
static _hasMultipleAuthenticationMethods()
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
static _needsExternalAccountByAuthMode($a_auth_mode)
Check if chosen auth mode needs an external account entry.
static _getAuthMode(?string $a_auth_mode)
const AUTH_USER_TIME_LIMIT_EXCEEDED
const AUTH_APACHE_FAILED
static handleForcedAuthentication()
static _getAllAuthModes()
const AUTH_SOAP_NO_ILIAS_USER
const AUTH_MODE_INACTIVE
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)
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...
$server
global $DIC
Definition: feed.php:28
global $ilSetting
Definition: privfeed.php:17
$lng