|
| 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 | +} |
0 commit comments