ILIAS  release_9 Revision v9.13-25-g2c18ec4c24f
class.ilAuthProviderApache.php
Go to the documentation of this file.
1 <?php
2 
3 declare(strict_types=1);
4 
5 /******************************************************************************
6  *
7  * This file is part of ILIAS, a powerful learning management system.
8  *
9  * ILIAS is licensed with the GPL-3.0, you should have received a copy
10  * of said license along with the source code.
11  *
12  * If this is not the case or you just want to try ILIAS, you'll find
13  * us at:
14  * https://www.ilias.de
15  * https://github.com/ILIAS-eLearning
16  *
17  *****************************************************************************/
18 
25 {
28  public const APACHE_AUTH_TYPE_BY_FUNCTION = 3;
29 
30  private const ENV_APACHE_AUTH_INDICATOR_NAME = 'apache_auth_indicator_name';
31 
32  private const ERR_WRONG_LOGIN = 'err_wrong_login';
33 
34  private const APACHE_ENABLE_LDAP = 'apache_enable_ldap';
35  private const APACHE_LDAP_SID = 'apache_ldap_sid';
36 
38  private string $migration_account = '';
39  private bool $force_new_account = false;
40 
42  {
43  parent::__construct($credentials);
44  $this->settings = new ilSetting('apache_auth');
45  }
46 
47  public function doAuthentication(ilAuthStatus $status): bool
48  {
49  if (!$this->settings->get('apache_enable_auth', '0')) {
50  $this->getLogger()->info('Apache auth disabled.');
51  $this->handleAuthenticationFail($status, 'apache_auth_err_disabled');
52  return false;
53  }
54 
55  if (
56  !$this->settings->get(self::ENV_APACHE_AUTH_INDICATOR_NAME, '') ||
57  !$this->settings->get('apache_auth_indicator_value', '')
58  ) {
59  $this->getLogger()->warning('Apache auth indicator match failure.');
60  $this->handleAuthenticationFail($status, 'apache_auth_err_indicator_match_failure');
61  return false;
62  }
63 
64  $validIndicatorValues = array_filter(array_map(
65  'trim',
66  str_getcsv($this->settings->get('apache_auth_indicator_value', ''))
67  ));
68  //TODO PHP8-REVIEW: $DIC->http()->request()->getServerParams()['apache_auth_indicator_name']
69  if (
70  !isset($_SERVER[$this->settings->get(self::ENV_APACHE_AUTH_INDICATOR_NAME, '')]) ||
71  !in_array($_SERVER[$this->settings->get(self::ENV_APACHE_AUTH_INDICATOR_NAME, '')], $validIndicatorValues, true)
72  ) {
73  $this->getLogger()->warning('Apache authentication failed (indicator name <-> value');
74  $this->handleAuthenticationFail($status, self::ERR_WRONG_LOGIN);
75  return false;
76  }
77 
78  if (!ilUtil::isLogin($this->getCredentials()->getUsername())) {
79  $this->getLogger()->warning('Invalid login name given: ' . $this->getCredentials()->getUsername());
80  $this->handleAuthenticationFail($status, 'apache_auth_err_invalid_login');
81  return false;
82  }
83 
84  if ($this->getCredentials()->getUsername() === '') {
85  $this->getLogger()->info('No username given');
86  $this->handleAuthenticationFail($status, self::ERR_WRONG_LOGIN);
87  return false;
88  }
89 
90  // Apache with ldap as data source
91  if ($this->settings->get(self::APACHE_ENABLE_LDAP, '0')) {
92  return $this->handleLDAPDataSource($status);
93  }
94 
95  $login = ilObjUser::_checkExternalAuthAccount('apache', $this->getCredentials()->getUsername());
96  $usr_id = ilObjUser::_lookupId($login);
97  if (!$usr_id) {
98  $this->getLogger()->info('Cannot find user id for external account: ' . $this->getCredentials()->getUsername());
99  $this->handleAuthenticationFail($status, self::ERR_WRONG_LOGIN);
100  return false;
101  }
102 
104  $status->setAuthenticatedUserId($usr_id);
105  return true;
106  }
107 
108  public function migrateAccount(ilAuthStatus $status): void
109  {
110  $this->force_new_account = true;
111  if ($this->settings->get(self::APACHE_ENABLE_LDAP, '0')) {
112  $this->handleLDAPDataSource($status);
113  }
114  }
115 
116  public function createNewAccount(ilAuthStatus $status): void
117  {
118  $this->force_new_account = true;
119  if ($this->settings->get(self::APACHE_ENABLE_LDAP, '0')) {
120  $this->handleLDAPDataSource($status);
121  }
122  }
123 
124  public function getExternalAccountName(): string
125  {
127  }
128 
129  public function setExternalAccountName(string $name): void
130  {
131  $this->migration_account = $name;
132  }
133 
134  public function getTriggerAuthMode(): string
135  {
136  return (string) ilAuthUtils::AUTH_APACHE;
137  }
138 
139  public function getUserAuthModeName(): string
140  {
141  if ($this->settings->get(self::APACHE_LDAP_SID, '0')) {
142  return 'ldap_' . $this->settings->get(self::APACHE_LDAP_SID, '');
143  }
144 
145  return 'apache';
146  }
147 
148  private function handleLDAPDataSource(ilAuthStatus $status): bool
149  {
151  (int) $this->settings->get(self::APACHE_LDAP_SID, '0')
152  );
153 
154  $this->getLogger()->debug('Using ldap data source with server configuration: ' . $server->getName());
155 
156  $sync = new ilLDAPUserSynchronisation('ldap_' . $server->getServerId(), $server->getServerId());
157  $sync->setExternalAccount($this->getCredentials()->getUsername());
158  $sync->setUserData([]);
159  $sync->forceCreation($this->force_new_account);
160  $sync->forceReadLdapData(true);
161 
162  try {
163  $internal_account = $sync->sync();
164  $this->getLogger()->debug('Internal account: ' . $internal_account);
165  } catch (UnexpectedValueException $e) {
166  $this->getLogger()->info('Login failed with message: ' . $e->getMessage());
167  $this->handleAuthenticationFail($status, self::ERR_WRONG_LOGIN);
168  return false;
170  $this->handleAuthenticationFail($status, 'err_auth_ldap_failed');
171  return false;
173  $this->getLogger()->info('Login failed with message: ' . $e->getMessage());
174  $this->handleAuthenticationFail($status, 'err_auth_ldap_no_ilias_user');
175  return false;
177  $this->setExternalAccountName($this->getCredentials()->getUsername());
178  $this->getLogger()->info('Authentication failed: account migration required for external account: ' . $this->getCredentials()->getUsername());
180  return false;
181  }
182 
184  $status->setAuthenticatedUserId(ilObjUser::_lookupId($internal_account));
185  return true;
186  }
187 }
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...
migrateAccount(ilAuthStatus $status)
Create new account.
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...
static getInstanceByServerId(int $a_server_id)
Get instance by server id.
doAuthentication(ilAuthStatus $status)
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...
handleLDAPDataSource(ilAuthStatus $status)
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...
static _lookupId($a_user_str)
createNewAccount(ilAuthStatus $status)
Create new ILIAS account for external_account.
static _checkExternalAuthAccount(string $a_auth, string $a_account, bool $tryFallback=true)
check whether external account and authentication method matches with a user
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...
handleAuthenticationFail(ilAuthStatus $status, string $a_reason)
Handle failed authentication.
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...
__construct(VocabulariesInterface $vocabularies)
setExternalAccount(string $a_ext)
Set external account (unique for each auth mode)
getTriggerAuthMode()
Get auth mode which triggered the account migration 2_1 for ldap account migration with server id 1 1...
setStatus(int $a_status)
Set auth status.
$_SERVER['HTTP_HOST']
Definition: raiseError.php:10
static isLogin(string $a_login)
ilAuthCredentials $credentials
getExternalAccountName()
Get external account name.
__construct(ilAuthCredentials $credentials)
getLogger()
Get logger.
getUserAuthModeName()
Get user auth mode name ldap_1 for ldap account migration with server id 1 apache for apache auth...
$server
setAuthenticatedUserId(int $a_id)
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...
const STATUS_ACCOUNT_MIGRATION_REQUIRED