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

Commit dafb137

Browse files
committed
Time Optimalization
1 parent 378c6be commit dafb137

File tree

3 files changed

+48
-4
lines changed

3 files changed

+48
-4
lines changed

lib/Disco.php

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,13 +11,17 @@
1111
* comment them out or in case of automated metadata fetching configure blacklist in config-metarefresh.php
1212
*
1313
* @author Ondrej Velisek <[email protected]>
14+
* @author Pavel Vyskocil <[email protected]>
1415
*/
1516
class sspmod_perun_Disco extends sspmod_discopower_PowerIdPDisco
1617
{
1718
const CONFIG_FILE_NAME = 'module_perun.php';
1819
const PROPNAME_DISABLE_WHITELISTING = 'disco.disableWhitelisting';
1920

2021
private $originalsp;
22+
private $whitelist;
23+
private $greylist;
24+
private $service;
2125

2226
public function __construct(array $metadataSets, $instance)
2327
{
@@ -27,6 +31,9 @@ public function __construct(array $metadataSets, $instance)
2731
$id = explode(":", $query['AuthID'])[0];
2832
$state = SimpleSAML_Auth_State::loadState($id, 'saml:sp:sso');
2933
$this->originalsp = $state['SPMetadata'];
34+
$this->service = new sspmod_perun_IdpListsServiceCsv();
35+
$this->whitelist = $this->service->listToArray("whitelist");
36+
$this->greylist = $this->service->listToArray("greylist");
3037
}
3138

3239

@@ -115,10 +122,10 @@ protected function scoping($list)
115122

116123
protected function whitelisting($list)
117124
{
118-
$service = new sspmod_perun_IdpListsServiceCsv();
119125
foreach ($list as $entityId => $idp) {
120126
$unset = true;
121-
if ($service->isWhitelisted($entityId)) {
127+
128+
if (in_array($entityId, $this->whitelist)){
122129
$unset = false;
123130
}
124131
if (isset($idp['EntityAttributes']['http://macedir.org/entity-category-support'])) {
@@ -149,9 +156,8 @@ protected function whitelisting($list)
149156

150157
protected function greylisting($list)
151158
{
152-
$service = new sspmod_perun_IdpListsServiceCsv();
153159
foreach ($list as $entityId => $idp) {
154-
if ($service->isGreylisted($entityId)) {
160+
if (in_array($entityId, $this->greylist)) {
155161
unset($list[$entityId]);
156162
}
157163
}

lib/IdpListsService.php

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
* Note that implementation should be thread/concurrency safe.
1313
*
1414
* @author Ondrej Velisek <[email protected]>
15+
* @author Pavel Vyskocil <[email protected]>
1516
*/
1617
interface sspmod_perun_IdpListsService
1718
{
@@ -49,4 +50,10 @@ function isGreylisted($entityID);
4950
* @param null|string $reason
5051
*/
5152
function whitelistIdp($entityID, $reason = null);
53+
54+
/**
55+
* @param string $listName "whitelist" or "greylist"
56+
* @return array of entityIdPs
57+
*/
58+
function listToArray($listName);
5259
}

lib/IdpListsServiceCsv.php

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
* first column is timestamp, second entityid and third reason
66
*
77
* @author Ondrej Velisek <[email protected]>
8+
* @author Pavel Vyskocil <[email protected]>
89
*/
910
class sspmod_perun_IdpListsServiceCsv implements sspmod_perun_IdpListsService
1011
{
@@ -174,6 +175,36 @@ function whitelistIdp($entityID, $reason = null)
174175

175176
}
176177

178+
function listToArray($listName){
179+
if ($listName === "whitelist"){
180+
$list = $this->whitelistFile;
181+
} else{
182+
$list = $this->greylistFile;
183+
}
184+
185+
$resultList = array();
186+
187+
if (!file_exists($list)) {
188+
return $resultList;
189+
}
190+
191+
$f = fopen($list, 'r');
192+
if (flock($f, LOCK_SH)) {
193+
194+
while (($idp = $this->arrayToIdp(fgetcsv($f))) !== false) {
195+
array_push($resultList, $idp['entityid']);
196+
}
197+
198+
fflush($f);
199+
flock($f, LOCK_UN);
200+
} else {
201+
throw new SimpleSAML_Error_Exception("IdpListsServiceCsv - unable to get file lock. Hint: Try to create folder config/idplists and add write rights.");
202+
}
203+
fclose($f);
204+
205+
return $resultList;
206+
}
207+
177208
private function arrayToIdp($csv) {
178209
if (!is_array($csv)) return false;
179210

0 commit comments

Comments
 (0)