Skip to content

Commit 1589b83

Browse files
Auth/OpenIDConnect: Cleanup code and make role mapping more robust
See: https://mantis.ilias.de/view.php?id=39471
1 parent 09d734c commit 1589b83

File tree

2 files changed

+15
-17
lines changed

2 files changed

+15
-17
lines changed

Services/OpenIdConnect/classes/class.ilAuthProviderOpenIdConnect.php

+9-10
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,6 @@ class ilAuthProviderOpenIdConnect extends ilAuthProvider
2929
private const OIDC_AUTH_IDTOKEN = "oidc_auth_idtoken";
3030
private ilOpenIdConnectSettings $settings;
3131
/** @var array $body */
32-
private $body;
3332
private ilLogger $logger;
3433
private ilLanguage $lng;
3534

@@ -40,7 +39,6 @@ public function __construct(ilAuthCredentials $credentials)
4039

4140
$this->logger = $DIC->logger()->auth();
4241
$this->settings = ilOpenIdConnectSettings::getInstance();
43-
$this->body = $DIC->http()->request()->getParsedBody();
4442
$this->lng = $DIC->language();
4543
$this->lng->loadLanguageModule('auth');
4644
}
@@ -97,7 +95,6 @@ public function doAuthentication(ilAuthStatus $status): bool
9795

9896
$oidc->authenticate();
9997
// user is authenticated, otherwise redirected to authorization endpoint or exception
100-
$this->logger->dump($this->body, ilLogLevel::DEBUG);
10198

10299
$claims = $oidc->requestUserInfo();
103100
$this->logger->dump($claims, ilLogLevel::DEBUG);
@@ -136,10 +133,17 @@ private function handleUpdate(ilAuthStatus $status, $user_info): ilAuthStatus
136133
}
137134

138135
$uid_field = $this->settings->getUidField();
139-
$ext_account = $user_info->{$uid_field};
136+
$ext_account = $user_info->{$uid_field} ?? '';
140137

141-
$this->logger->debug('Authenticated external account: ' . $ext_account);
138+
if (!is_string($ext_account) || $ext_account === '') {
139+
$this->logger->error('Could not determine valid external account, value is empty or not a string.');
140+
$this->logger->dump($user_info, ilLogLevel::ERROR);
141+
$status->setStatus(ilAuthStatus::STATUS_AUTHENTICATION_FAILED);
142+
$status->setReason('err_wrong_login');
143+
return $status;
144+
}
142145

146+
$this->logger->debug('Authenticated external account: ' . $ext_account);
143147

144148
$int_account = ilObjUser::_checkExternalAuthAccount(
145149
ilOpenIdConnectUserSync::AUTH_MODE,
@@ -148,11 +152,6 @@ private function handleUpdate(ilAuthStatus $status, $user_info): ilAuthStatus
148152

149153
try {
150154
$sync = new ilOpenIdConnectUserSync($this->settings, $user_info);
151-
if (!is_string($ext_account)) {
152-
$status->setStatus(ilAuthStatus::STATUS_AUTHENTICATION_FAILED);
153-
$status->setReason('err_wrong_login');
154-
return $status;
155-
}
156155
$sync->setExternalAccount($ext_account);
157156
$sync->setInternalAccount((string) $int_account);
158157
$sync->updateUser();

Services/OpenIdConnect/classes/class.ilOpenIdConnectUserSync.php

+6-7
Original file line numberDiff line numberDiff line change
@@ -192,12 +192,9 @@ protected function parseRoleAssignments(): array
192192
continue;
193193
}
194194

195-
[$role_attribute, $role_value] = explode('::', $role_info['value']);
195+
[$role_attribute, $role_value] = array_map(trim(...), explode('::', $role_info['value']));
196196

197-
if (
198-
!$role_attribute ||
199-
!$role_value
200-
) {
197+
if (!$role_attribute || !$role_value) {
201198
$this->logger->debug('No valid role mapping configuration for: ' . $role_id);
202199
continue;
203200
}
@@ -213,14 +210,16 @@ protected function parseRoleAssignments(): array
213210
}
214211

215212
if (is_array($this->user_info->{$role_attribute})) {
216-
if (!in_array($role_value, $this->user_info->{$role_attribute}, true)) {
213+
$roles_claim = array_map(trim(...), $this->user_info->{$role_attribute});
214+
if (!in_array($role_value, $roles_claim, true)) {
217215
$this->logger->debug('User account has no ' . $role_value);
218216
continue;
219217
}
220-
} elseif (strcmp($this->user_info->{$role_attribute}, $role_value) !== 0) {
218+
} elseif (strcmp(trim($this->user_info->{$role_attribute}), $role_value) !== 0) {
221219
$this->logger->debug('User account has no ' . $role_value);
222220
continue;
223221
}
222+
224223
$this->logger->debug('Matching role mapping for role_id: ' . $role_id);
225224

226225
$found_role = true;

0 commit comments

Comments
 (0)