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

Commit 66ae43f

Browse files
authored
feat: optionally keep indexes of endpoints in metadata (#149)
1 parent ede9601 commit 66ae43f

File tree

3 files changed

+103
-2
lines changed

3 files changed

+103
-2
lines changed

lib/MetadataToPerun.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -99,7 +99,7 @@ public function metadataToFacility(array $metadata)
9999
$this->addXmlAttributes($metadata, $facility);
100100

101101
foreach ($this->transformers as $transformer) {
102-
$attrs = array_filter(array_intersect_key($facility, array_flip($transformer['attributes'])));
102+
$attrs = array_intersect_key($facility, array_flip($transformer['attributes']));
103103
if (!empty($attrs)) {
104104
$newAttrs = $transformer['instance']->transform($attrs);
105105
$facility = array_merge($facility, $newAttrs);
Lines changed: 101 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,101 @@
1+
<?php
2+
3+
/**
4+
* @author Pavel Brousek <[email protected]>
5+
*/
6+
7+
namespace SimpleSAML\Module\perun\transformers;
8+
9+
use SimpleSAML\Module\perun\AttributeTransformer;
10+
11+
/**
12+
* Get map of binding to comma separated list of endpoints with the option to keep original indexes.
13+
*/
14+
class EndpointIndexMap extends AttributeTransformer
15+
{
16+
const MAPLIST_SEPARATOR = ',';
17+
18+
const BINDING_PREFIX = 'urn:oasis:names:tc:SAML:2.0:bindings:';
19+
20+
private $defaultBinding;
21+
22+
private $fullNames;
23+
24+
/**
25+
* @override
26+
*/
27+
public function __construct(\SimpleSAML\Configuration $config)
28+
{
29+
$this->defaultBinding = $config->getString('defaultBinding');
30+
$this->fullNames = !$config->getBoolean('shortNames', false);
31+
}
32+
33+
/**
34+
* @override
35+
*/
36+
public function transform(array $attributes)
37+
{
38+
if (count($attributes) !== 2) {
39+
throw new \Exception(
40+
'Invalid configuration of EndpointIndexMap transformer, exactly 2 input attributes exptected'
41+
);
42+
}
43+
list($endpoints, $keepIndexes) = array_values($attributes);
44+
$names = array_keys($attributes);
45+
$attributeName = $names[0];
46+
if (is_bool($endpoints)) {
47+
$tmp = $keepIndexes;
48+
$keepIndexes = $endpoints;
49+
$endpoints = $tmp;
50+
$attributeName = $names[1];
51+
}
52+
if (empty($endpoints)) {
53+
$result = null;
54+
} else {
55+
if (!is_array($endpoints)) {
56+
$endpoints = [['Location' => $endpoints]];
57+
}
58+
$result = [];
59+
foreach ($endpoints as $endpoint) {
60+
$binding = $endpoint['Binding'] ?: $this->defaultBinding;
61+
if (!$this->fullNames) {
62+
$binding = str_replace(self::BINDING_PREFIX, '', $binding);
63+
}
64+
if (!isset($result[$binding])) {
65+
$result[$binding] = [];
66+
}
67+
68+
$result[$binding][$endpoint['index'] - 1] = $endpoint['Location'];
69+
}
70+
if ($keepIndexes) {
71+
$result = array_map(function ($endpoints) {
72+
$e = [];
73+
for ($i = 0; $i <= max(array_keys($endpoints)); $i++) {
74+
$e[] = isset($endpoints[$i]) ? $endpoints[$i] : '';
75+
}
76+
return implode(self::MAPLIST_SEPARATOR, $e);
77+
}, $result);
78+
} else {
79+
$result = array_map(function ($endpoints) {
80+
return implode(self::MAPLIST_SEPARATOR, $endpoints);
81+
}, $result);
82+
}
83+
}
84+
return [
85+
$attributeName => $result,
86+
];
87+
}
88+
89+
/**
90+
* @override
91+
*/
92+
public function getDescription(array $attributes)
93+
{
94+
$descriptions = array_values($attributes);
95+
$names = array_keys($attributes);
96+
return [
97+
$names[0] => sprintf('comma-separated lists of Locations per Bindings from (%s)', $descriptions[0]),
98+
$names[1] => '',
99+
];
100+
}
101+
}

lib/transformers/EndpointMapToArray.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ class EndpointMapToArray extends AttributeTransformer
1717

1818
const BINDING_PREFIX = 'urn:oasis:names:tc:SAML:2.0:bindings:';
1919

20-
const INDEX_MIN = 0;
20+
const INDEX_MIN = 1;
2121

2222
private $defaultBinding;
2323

0 commit comments

Comments
 (0)