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

Commit c9067a5

Browse files
Merge pull request #39 from pajavyskocil/getSpMetadata
Generate metadata from Perun
2 parents 766386d + 8b53299 commit c9067a5

File tree

3 files changed

+148
-0
lines changed

3 files changed

+148
-0
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ All notable changes to this project will be documented in this file.
88
- Added new model Member
99
- Added new model Resource
1010
- New methods for getting data from Perun LDAP and Perun RPC
11+
- Added function for generating metadata for SimpleSAMLphp Proxy AAI from Perun
1112

1213
[Changed]
1314
- Connectors methods are not static for now.
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
<?php
2+
3+
$config = array(
4+
/**
5+
* Identifier of Proxy
6+
*/
7+
'proxyIdentifier' => '',
8+
9+
/**
10+
* Name of facility attribute Proxy Identifiers
11+
*/
12+
'perunProxyIdentifierAttr' => '',
13+
14+
/**
15+
* Name of facility attribute EntityID
16+
*/
17+
'perunProxyEntityIDAttr' => '',
18+
19+
/**
20+
* Absolute path, where the metadata will be stored
21+
*/
22+
'absoluteFileName' => '',
23+
24+
/**
25+
* List of attributes definitions
26+
*/
27+
'attributesDefinitions' => array(
28+
// Name of attribute from perun => key which will be used in generated metadata
29+
'perunAttrName' => 'metadataName',
30+
),
31+
);

www/getSpMetadata.php

Lines changed: 116 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,116 @@
1+
<?php
2+
3+
const CONFIG_FILE_NAME = 'module_perun_getMetadata.php';
4+
5+
const PERUN_PROXY_IDENTIFIER_ATTR_NAME = 'perunProxyIdentifierAttr';
6+
const PERUN_PROXY_ENTITY_ID_ATTR_NAME = 'perunProxyEntityIDAttr';
7+
8+
const PROXY_IDENTIFIER = 'proxyIdentifier';
9+
const ABSOLUTE_FILE_NAME = 'absoluteFileName';
10+
const ATTRIBUTES_DEFINITIONS = 'attributesDefinitions';
11+
const FACILITY_ATTRIBUTES = 'facilityAttributes';
12+
13+
const TYPE_INTEGER = 'java.lang.Integer';
14+
const TYPE_BOOLEAN = 'java.lang.Boolean';
15+
const TYPE_STRING = 'java.lang.String';
16+
const TYPE_ARRAY = 'java.util.ArrayList';
17+
const TYPE_MAP = 'java.util.LinkedHashMap';
18+
19+
$conf = SimpleSAML_Configuration::getConfig(CONFIG_FILE_NAME);
20+
21+
$proxyIdentifier = $conf->getString(PROXY_IDENTIFIER);
22+
assert(is_null($proxyIdentifier) || empty($proxyIdentifier));
23+
24+
$attributesDefinitions = $conf->getArray(ATTRIBUTES_DEFINITIONS);
25+
assert(is_null($attributesDefinitions) || is_array($attributesDefinitions));
26+
27+
$perunProxyIdentifierAttr = $conf->getString(PERUN_PROXY_IDENTIFIER_ATTR_NAME);
28+
$perunProxyEntityIDAttr = $conf->getString(PERUN_PROXY_ENTITY_ID_ATTR_NAME);
29+
assert(is_null($perunProxyEntityIDAttr) || empty($perunProxyEntityIDAttr) || is_null($perunProxyIdentifierAttr) || empty($perunProxyIdentifierAttr));
30+
31+
$absoluteFileName = $conf->getString(ABSOLUTE_FILE_NAME);
32+
assert(is_null($absoluteFileName) || empty($absoluteFileName));
33+
34+
35+
$rpcAdapter = new sspmod_perun_AdapterRpc();
36+
37+
// Get list of all attribute names
38+
$attrNames = array();
39+
$allAttrNames = array();
40+
array_push($allAttrNames, $perunProxyEntityIDAttr);
41+
foreach ($attributesDefinitions as $key => $value) {
42+
array_push($attrNames, $key);
43+
array_push($allAttrNames, $key);
44+
}
45+
46+
// Get all facilities with proxyIdentifiers
47+
$attributeDefinition = array();
48+
$attributeDefinition[$perunProxyIdentifierAttr] = $proxyIdentifier;
49+
$facilities = $rpcAdapter->searchFacilitiesByAttributeValue($attributeDefinition);
50+
51+
// Get facilities with attributes
52+
$facilitiesWithAttributes = array();
53+
foreach ($facilities as $facility) {
54+
$attributes = $rpcAdapter->getFacilityAttributes($facility, $allAttrNames);
55+
$facilityAttributes = array();
56+
foreach ($attributes as $attribute) {
57+
$facilityAttributes[$attribute['name']] = $attribute;
58+
}
59+
$facilitiesWithAttributes[$facility->getId()] = array(
60+
'facility' => $facility,
61+
FACILITY_ATTRIBUTES=> $facilityAttributes,
62+
);
63+
}
64+
65+
// Generate array with metadata
66+
$content = '<?php' . PHP_EOL;
67+
foreach ($facilitiesWithAttributes as $facilityWithAttributes) {
68+
$metadataContent = '';
69+
if (isset($facilityWithAttributes[FACILITY_ATTRIBUTES][$perunProxyEntityIDAttr]) && !empty($facilityWithAttributes[FACILITY_ATTRIBUTES][$perunProxyEntityIDAttr]['value'])) {
70+
$metadataContent .= '$metadata[\'' . $facilityWithAttributes[FACILITY_ATTRIBUTES][$perunProxyEntityIDAttr]['value'] . '\'] = array(' . PHP_EOL;
71+
foreach ($attributesDefinitions as $perunAttrName => $metadataAttrName) {
72+
$attribute = $facilityWithAttributes[FACILITY_ATTRIBUTES][$perunAttrName];
73+
if (($attribute['type'] === TYPE_INTEGER ) && is_numeric($attribute['value']) && !is_null($attribute['value'])) {
74+
$metadataContent .= "\t '" . $metadataAttrName . "' => " . $attribute['value'] . "," . PHP_EOL ;
75+
} elseif (($attribute['type'] === TYPE_STRING) && !is_null($attribute['value'])) {
76+
$metadataContent .= "\t '" . $metadataAttrName . "' => '" . $attribute['value'] . "'," . PHP_EOL ;
77+
} elseif ($attribute['type'] === TYPE_BOOLEAN) {
78+
$metadataContent .= "\t '" . $metadataAttrName . "' => " ;
79+
if (is_null($attribute['value']) || $attribute['value'] === 'false') {
80+
$metadataContent .= "false," . PHP_EOL;
81+
} else {
82+
$metadataContent .= "true," . PHP_EOL;
83+
}
84+
} elseif ($attribute['type'] === TYPE_ARRAY && !is_null($attribute['value'])) {
85+
$metadataContent .= "\t '" . $metadataAttrName . "' => array(" . PHP_EOL;
86+
foreach ($attribute['value'] as $value) {
87+
$metadataContent .= "\t\t'" . $value . "'," . PHP_EOL;
88+
}
89+
$metadataContent .= "\t)," . PHP_EOL;
90+
} elseif ($attribute['type'] === TYPE_MAP && !is_null($attribute['value'])) {
91+
$metadataContent .= "\t '" . $metadataAttrName . "' => array(" . PHP_EOL;
92+
foreach ($attribute['value'] as $key => $value) {
93+
$metadataContent .= "\t\t'" . $key . "' => '" . $value . "'," . PHP_EOL;
94+
}
95+
$metadataContent .= "\t)," . PHP_EOL;
96+
}
97+
}
98+
$metadataContent .= ");" . PHP_EOL . "\n";
99+
}
100+
$content .= $metadataContent;
101+
102+
}
103+
104+
file_put_contents($absoluteFileName, $content,LOCK_EX);
105+
106+
if (file_exists($absoluteFileName)) {
107+
header('Content-Description: File Transfer');
108+
header('Content-Type: application/octet-stream');
109+
header('Content-Disposition: attachment; filename="' . basename($absoluteFileName) . '"');
110+
header('Expires: 0');
111+
header('Cache-Control: must-revalidate');
112+
header('Pragma: public');
113+
header('Content-Length: ' . filesize($absoluteFileName));
114+
readfile($absoluteFileName);
115+
exit;
116+
}

0 commit comments

Comments
 (0)