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

Commit d150af7

Browse files
Merge pull request #20 from BaranekD/isCesnetEligible
Possibility to read isCesnetEligible attribute from LDAP
2 parents 84633a3 + 455555b commit d150af7

File tree

3 files changed

+84
-31
lines changed

3 files changed

+84
-31
lines changed

CHANGELOG.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,9 @@
22
All notable changes to this project will be documented in this file.
33

44
## [Unreleased]
5+
#### Added
6+
- Possibility to read isCesnetEligible attribute from LDAP
7+
58
#### Changed
69
- Using of short array syntax ([] instead of array())
710

config-templates/processFilterConfigurations-example.md

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,12 +13,16 @@ Example how to configure ComputeLoA filter:
1313
## IsCesnetEligible
1414
Example how to configure IsCesnetEligible filter:
1515

16+
* Interface says if attribute will be read from RPC or LDAP
17+
* If interface is LDAP, LDAP.attributeName has to be filled
18+
* RPC.attributeName has to be filled
1619
* Put something like this into saml20-idp-hosted.php:
1720

1821
```php
1922
25 => [
20-
'class' => 'cesnet:IsCesnetEligible',
21-
'cesnetEligibleLastSeenAttr' => 'urn:perun:user:attribute-def:def:isCesnetEligibleLastSeen',
22-
'listOfPerunEntityIds' => ['entityId1', 'entityId2'],
23+
'class' => 'cesnet:IsCesnetEligible',
24+
'interface' => 'RPC/LDAP',
25+
'RPC.attributeName' => 'urn:perun:user:attribute-def:def:isCesnetEligibleLastSeen',
26+
'LDAP.attributeName' => 'isCesnetEligible',
2327
],
2428
```

lib/Auth/Process/IsCesnetEligible.php

Lines changed: 74 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
namespace SimpleSAML\Module\cesnet\Auth\Process;
44

5+
use SimpleSAML\Auth\ProcessingFilter;
56
use SimpleSAML\Module\perun\LdapConnector;
67
use SimpleSAML\Module\perun\RpcConnector;
78
use SimpleSAML\Module\perun\AdapterLdap;
@@ -17,19 +18,27 @@
1718
*
1819
* @author Pavel Vyskocil <vyskocilpavel@muni.cz>
1920
*/
20-
class IsCesnetEligible extends \SimpleSAML\Auth\ProcessingFilter
21+
class IsCesnetEligible extends ProcessingFilter
2122
{
2223
const CONFIG_FILE_NAME = 'module_cesnet_IsCesnetEligible.php';
2324
const ORGANIZATION_LDAP_BASE = 'ou=Organizations,o=eduID.cz,o=apps,dc=cesnet,dc=cz';
2425

2526
const HOSTEL_ENTITY_ID = "https://idp.hostel.eduid.cz/idp/shibboleth";
2627

2728
const INTERFACE_PROPNAME = "interface";
28-
const CESNET_ELIGIBLE_LAST_SEEN_ATTR = "cesnetEligibleLastSeenAttr";
29+
const ATTR_NAME = "attrName";
30+
const RPC_ATTRIBUTE_NAME = "RPC.attributeName";
31+
const LDAP_ATTRIBUTE_NAME = 'LDAP.attributeName';
2932
const DEFAULT_ATTR_NAME = 'isCesnetEligibleLastSeen';
33+
const LDAP = 'LDAP';
34+
const RPC = 'RPC';
3035

31-
private $cesnetEligibleLastSeen;
32-
private $cesnetEligibleLastSeenAttr;
36+
private $cesnetEligibleLastSeenValue;
37+
private $cesnetEligibleLastSeenAttribute;
38+
private $interface = self::RPC;
39+
private $rpcAttrName;
40+
private $ldapAttrName;
41+
private $returnAttrName = self::DEFAULT_ATTR_NAME;
3342

3443
private $spEntityId;
3544
private $idpEntityId;
@@ -40,6 +49,11 @@ class IsCesnetEligible extends \SimpleSAML\Auth\ProcessingFilter
4049
*/
4150
private $cesnetLdapConnector;
4251

52+
/**
53+
* @var AdapterLdap
54+
*/
55+
private $ldapAdapter;
56+
4357
/**
4458
* @var RpcConnector
4559
*/
@@ -49,23 +63,33 @@ public function __construct($config, $reserved)
4963
{
5064
parent::__construct($config, $reserved);
5165

52-
if (!isset($config[self::CESNET_ELIGIBLE_LAST_SEEN_ATTR])) {
66+
if (!isset($config[self::RPC_ATTRIBUTE_NAME]) || empty($config[self::RPC_ATTRIBUTE_NAME])) {
5367
throw new Exception(
5468
"cesnet:IsCesnetEligible - missing mandatory configuration option '" .
55-
self::CESNET_ELIGIBLE_LAST_SEEN_ATTR . "'."
69+
self::RPC_ATTRIBUTE_NAME . "'."
5670
);
5771
}
5872

59-
if (isset($config['attrName'])) {
60-
$this->attrName = $config['attrName'];
61-
} else {
62-
$this->attrName = self::DEFAULT_ATTR_NAME;
63-
}
64-
65-
$this->cesnetEligibleLastSeenAttr = $config[self::CESNET_ELIGIBLE_LAST_SEEN_ATTR];
73+
$this->rpcAttrName = $config[self::RPC_ATTRIBUTE_NAME];
6674

67-
$this->cesnetLdapConnector = (new AdapterLdap(self::CONFIG_FILE_NAME))->getConnector();
6875
$this->rpcConnector = (new AdapterRpc())->getConnector();
76+
$this->cesnetLdapConnector = (new AdapterLdap(self::CONFIG_FILE_NAME))->getConnector();
77+
78+
if (isset($config[self::ATTR_NAME]) && !empty($config[self::ATTR_NAME])) {
79+
$this->returnAttrName = $config['attrName'];
80+
}
81+
82+
if (isset($config[self::INTERFACE_PROPNAME], $config[self::LDAP_ATTRIBUTE_NAME]) &&
83+
$config[self::INTERFACE_PROPNAME] === self::LDAP && !empty($config[self::LDAP_ATTRIBUTE_NAME])) {
84+
$this->interface = $config[self::INTERFACE_PROPNAME];
85+
$this->ldapAttrName = $config[self::LDAP_ATTRIBUTE_NAME];
86+
$this->ldapAdapter = new AdapterLdap();
87+
} else {
88+
Logger::warning(
89+
"cesnet:IsCesnetEligible - One of " . self::INTERFACE_PROPNAME . self::LDAP_ATTRIBUTE_NAME .
90+
" is missing or empty. RPC interface will be used"
91+
);
92+
}
6993
}
7094

7195
public function process(&$request)
@@ -104,32 +128,54 @@ public function process(&$request)
104128

105129
try {
106130
if (!empty($user)) {
107-
$this->cesnetEligibleLastSeen = $this->rpcConnector->get(
108-
'attributesManager',
109-
'getAttribute',
110-
['user' => $user->getId(), 'attributeName' => $this->cesnetEligibleLastSeenAttr,]
111-
);
112-
}
131+
if ($this->interface === self::LDAP) {
132+
$attrs = $this->ldapAdapter->getUserAttributes($user, [$this->ldapAttrName]);
133+
if (isset($attrs[$this->ldapAttrName][0])) {
134+
$this->cesnetEligibleLastSeenValue = $attrs[$this->ldapAttrName][0];
135+
}
136+
} else {
137+
$this->cesnetEligibleLastSeenAttribute = $this->rpcConnector->get(
138+
'attributesManager',
139+
'getAttribute',
140+
['user' => $user->getId(), 'attributeName' => $this->rpcAttrName]
141+
);
142+
$this->cesnetEligibleLastSeenValue = $this->cesnetEligibleLastSeenAttribute['value'];
143+
}
113144

114-
if ((!empty($this->eduPersonScopedAffiliation) && $this->isCesnetEligible())
115-
|| $isHostelVerified
116-
) {
117-
$this->cesnetEligibleLastSeen['value'] = date("Y-m-d H:i:s");
145+
if ($isHostelVerified || (!empty($this->eduPersonScopedAffiliation) && $this->isCesnetEligible())) {
146+
$this->cesnetEligibleLastSeenValue = date("Y-m-d H:i:s");
147+
148+
if ($this->cesnetEligibleLastSeenAttribute === null) {
149+
$this->cesnetEligibleLastSeenAttribute = $this->rpcConnector->get(
150+
'attributesManager',
151+
'getAttribute',
152+
['user' => $user->getId(), 'attributeName' => $this->rpcAttrName,]
153+
);
154+
}
155+
$this->cesnetEligibleLastSeenAttribute['value'] = $this->cesnetEligibleLastSeenValue;
118156

119-
if (!empty($user)) {
120157
$this->rpcConnector->post(
121158
'attributesManager',
122159
'setAttribute',
123-
['user' => $user->getId(), 'attribute' => $this->cesnetEligibleLastSeen,]
160+
['user' => $user->getId(), 'attribute' => $this->cesnetEligibleLastSeenAttribute,]
161+
);
162+
163+
Logger::debug(
164+
"cesnet:IsCesnetEligible - Value of attribute isCesnetEligibleLastSeen was updated to " .
165+
$this->cesnetEligibleLastSeenValue . "in Perun system."
124166
);
125167
}
126168
}
127169
} catch (Exception $ex) {
128170
Logger::warning("cesnet:IsCesnetEligible - " . $ex->getMessage());
129171
}
130172

131-
if ($this->cesnetEligibleLastSeen['value'] != null) {
132-
$request['Attributes'][$this->attrName] = [$this->cesnetEligibleLastSeen['value']];
173+
if ($this->cesnetEligibleLastSeenValue !== null) {
174+
$request['Attributes'][$this->returnAttrName] = [$this->cesnetEligibleLastSeenValue];
175+
Logger::debug(
176+
"cesnet:IsCesnetEligible - Attribute " . $this->returnAttrName . " was set to value " .
177+
$this->cesnetEligibleLastSeenValue
178+
);
133179
}
134180
}
135181

0 commit comments

Comments
 (0)