Skip to content
This repository was archived by the owner on Sep 19, 2022. It is now read-only.

Commit 7d5d85e

Browse files
author
Dominik František Bučík
committed
feat: 🎸 Customizable IdPEnityID location (from attribute)
1 parent 9665f18 commit 7d5d85e

File tree

3 files changed

+59
-11
lines changed

3 files changed

+59
-11
lines changed

config-templates/module_proxystatistics.php

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,12 @@
6262
*/
6363
//'userIdAttribute' => 'uid',
6464

65+
/*
66+
* Which attribute should be used for IdP Entity ID
67+
* if left empty, it will be extracted from the request object.
68+
*/
69+
//'sourceIdpEntityIdAttribute' => 'sourceIdpEntityID',
70+
6571
/*
6672
* Database table names. Default is to keep the name (as in `tables.sql`)
6773
*/

lib/Config.php

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,8 @@ class Config
2626

2727
private const USER_ID_ATTRIBUTE = 'userIdAttribute';
2828

29+
private const SOURCE_IDP_ENTITY_ID_ATTRIBUTE = 'sourceIdpEntityIdAttribute';
30+
2931
private const REQUIRE_AUTH_SOURCE = 'requireAuth.source';
3032

3133
private const KEEP_PER_USER = 'keepPerUser';
@@ -36,6 +38,8 @@ class Config
3638

3739
private $mode;
3840

41+
private $sourceIdpEntityIdAttribute;
42+
3943
private static $instance;
4044

4145
private function __construct()
@@ -44,6 +48,7 @@ private function __construct()
4448
$this->store = $this->config->getConfigItem(self::STORE, null);
4549
$this->tables = $this->config->getArray('tables', []);
4650
$this->mode = $this->config->getValueValidate(self::MODE, ['PROXY', 'IDP', 'SP', 'MULTI_IDP'], 'PROXY');
51+
$this->sourceIdpEntityIdAttribute = $this->config->getString(self::SOURCE_IDP_ENTITY_ID_ATTRIBUTE, '');
4752
}
4853

4954
private function __clone()
@@ -79,6 +84,11 @@ public function getIdAttribute()
7984
return $this->config->getString(self::USER_ID_ATTRIBUTE, 'uid');
8085
}
8186

87+
public function getSourceIdpEntityIdAttribute()
88+
{
89+
return $this->sourceIdpEntityIdAttribute;
90+
}
91+
8292
public function getSideInfo($side)
8393
{
8494
assert(in_array($side, [self::SIDES], true));

lib/DatabaseCommand.php

Lines changed: 43 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -71,8 +71,7 @@ public function insertLogin(&$request, &$date)
7171
}
7272
}
7373

74-
$idAttribute = $this->config->getIdAttribute();
75-
$userId = isset($request['Attributes'][$idAttribute]) ? $request['Attributes'][$idAttribute][0] : '';
74+
$userId = $this->getUserId($request);
7675

7776
$ids = [];
7877
foreach (self::TABLE_SIDES as $side => $table) {
@@ -273,23 +272,19 @@ private function writeLogin($date, $ids, $user)
273272
return $this->conn->write($query, $params);
274273
}
275274

276-
private function getEntities($request)
275+
private function getEntities($request): array
277276
{
278277
$entities = [
279278
Config::MODE_IDP => [],
280279
Config::MODE_SP => [],
281280
];
282281
if (Config::MODE_IDP !== $this->mode && Config::MODE_MULTI_IDP !== $this->mode) {
283-
$entities[Config::MODE_IDP]['id'] = $request['saml:sp:IdP'];
284-
$entities[Config::MODE_IDP]['name'] = $request['Attributes']['sourceIdPName'][0];
282+
$entities[Config::MODE_IDP]['id'] = $this->getIdpIdentifier($request);
283+
$entities[Config::MODE_IDP]['name'] = $this->getIdpName($request);
285284
}
286285
if (Config::MODE_SP !== $this->mode) {
287-
$entities[Config::MODE_SP]['id'] = $request['Destination']['entityid'];
288-
if (isset($request['Destination']['UIInfo']['DisplayName']['en'])) {
289-
$entities[Config::MODE_SP]['name'] = $request['Destination']['UIInfo']['DisplayName']['en'];
290-
} else {
291-
$entities[Config::MODE_SP]['name'] = $request['Destination']['name']['en'] ?? '';
292-
}
286+
$entities[Config::MODE_SP]['id'] = $this->getSpIdentifier($request);
287+
$entities[Config::MODE_SP]['name'] = $this->getSpName($request);
293288
}
294289

295290
if (Config::MODE_PROXY !== $this->mode && Config::MODE_MULTI_IDP !== $this->mode) {
@@ -372,4 +367,41 @@ private function getAggregateGroupBy($ids)
372367

373368
return $this->escape_cols($columns);
374369
}
370+
371+
private function getIdpIdentifier($request)
372+
{
373+
$sourceIdpEntityIdAttribute = $this->config->getSourceIdpEntityIdAttribute();
374+
if (!empty($sourceIdpEntityIdAttribute) && !empty($request['Attributes'][$sourceIdpEntityIdAttribute][0])) {
375+
return $request['Attributes'][$sourceIdpEntityIdAttribute][0];
376+
}
377+
378+
return $request['saml:sp:IdP'];
379+
}
380+
381+
private function getUserId($request)
382+
{
383+
$idAttribute = $this->config->getIdAttribute();
384+
385+
return isset($request['Attributes'][$idAttribute]) ? $request['Attributes'][$idAttribute][0] : '';
386+
}
387+
388+
private function getIdpName($request)
389+
{
390+
return $request['Attributes']['sourceIdPName'][0];
391+
}
392+
393+
private function getSpIdentifier($request)
394+
{
395+
return $request['Destination']['entityid'];
396+
}
397+
398+
private function getSpName($request)
399+
{
400+
$displayName = $request['Destination']['UIInfo']['DisplayName']['en'] ?? '';
401+
if (empty($displayName)) {
402+
$displayName = $request['Destination']['name']['en'] ?? '';
403+
}
404+
405+
return$displayName;
406+
}
375407
}

0 commit comments

Comments
 (0)