Total Complexity | 75 |
Total Lines | 449 |
Duplicated Lines | 0 % |
Changes | 13 | ||
Bugs | 1 | Features | 0 |
Complex classes like PowerIdPDisco often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
While breaking up the class, it is a good idea to analyze how other classes use PowerIdPDisco, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
27 | class PowerIdPDisco extends IdPDisco |
||
28 | { |
||
29 | /** |
||
30 | * The configuration for this instance. |
||
31 | * |
||
32 | * @var \SimpleSAML\Configuration |
||
33 | */ |
||
34 | private Configuration $discoconfig; |
||
35 | |||
36 | /** |
||
37 | * The domain to use when saving common domain cookies. This is null if support for common domain cookies is |
||
38 | * disabled. |
||
39 | * |
||
40 | * @var string|null |
||
41 | */ |
||
42 | private ?string $cdcDomain; |
||
43 | |||
44 | /** |
||
45 | * The lifetime of the CDC cookie, in seconds. If set to null, it will only be valid until the browser is closed. |
||
46 | * |
||
47 | * @var int|null |
||
48 | */ |
||
49 | private ?int $cdcLifetime; |
||
50 | |||
51 | |||
52 | /** |
||
53 | * The default sort weight for entries without 'discopower.weight'. |
||
54 | * |
||
55 | * @var int|null |
||
56 | */ |
||
57 | private static ?int $defaultWeight = 100; |
||
58 | |||
59 | /** |
||
60 | * Initializes this discovery service. |
||
61 | * |
||
62 | * The constructor does the parsing of the request. If this is an invalid request, it will throw an exception. |
||
63 | * |
||
64 | * @param array $metadataSets Array with metadata sets we find remote entities in. |
||
65 | * @param string $instance The name of this instance of the discovery service. |
||
66 | */ |
||
67 | public function __construct(array $metadataSets, string $instance) |
||
68 | { |
||
69 | parent::__construct($metadataSets, $instance); |
||
70 | |||
71 | $this->discoconfig = Configuration::getConfig('module_discopower.php'); |
||
72 | |||
73 | $this->cdcDomain = $this->discoconfig->getString('cdc.domain', null); |
||
74 | if ($this->cdcDomain !== null && $this->cdcDomain[0] !== '.') { |
||
75 | // ensure that the CDC domain starts with a dot ('.') as required by the spec |
||
76 | $this->cdcDomain = '.' . $this->cdcDomain; |
||
77 | } |
||
78 | |||
79 | $this->cdcLifetime = $this->discoconfig->getInteger('cdc.lifetime', null); |
||
80 | |||
81 | self::$defaultWeight = $this->discoconfig->getInteger('defaultweight', 100); |
||
82 | } |
||
83 | |||
84 | |||
85 | /** |
||
86 | * Log a message. |
||
87 | * |
||
88 | * This is an helper function for logging messages. It will prefix the messages with our discovery service type. |
||
89 | * |
||
90 | * @param string $message The message which should be logged. |
||
91 | */ |
||
92 | protected function log(string $message): void |
||
93 | { |
||
94 | Logger::info('PowerIdPDisco.' . $this->instance . ': ' . $message); |
||
95 | } |
||
96 | |||
97 | |||
98 | /** |
||
99 | * Compare two entities. |
||
100 | * |
||
101 | * This function is used to sort the entity list. It sorts based on weights, |
||
102 | * and where those aren't available, English name. It puts larger weights |
||
103 | * higher, and will always put IdP's with names configured before those with |
||
104 | * only an entityID. |
||
105 | * |
||
106 | * @param array $a The metadata of the first entity. |
||
107 | * @param array $b The metadata of the second entity. |
||
108 | * |
||
109 | * @return int How $a compares to $b. |
||
110 | */ |
||
111 | public static function mcmp(array $a, array $b): int |
||
112 | { |
||
113 | // default weights |
||
114 | if (!isset($a['discopower.weight']) || !is_int($a['discopower.weight'])) { |
||
115 | $a['discopower.weight'] = self::$defaultWeight; |
||
116 | } |
||
117 | if (!isset($b['discopower.weight']) || !is_int($b['discopower.weight'])) { |
||
118 | $b['discopower.weight'] = self::$defaultWeight; |
||
119 | } |
||
120 | if ($a['discopower.weight'] > $b['discopower.weight']) { |
||
121 | return -1; // higher weights further up |
||
122 | } elseif ($b['discopower.weight'] > $a['discopower.weight']) { |
||
123 | return 1; // lower weights further down |
||
124 | } elseif (isset($a['name']['en']) && isset($b['name']['en'])) { |
||
125 | return strcasecmp($a['name']['en'], $b['name']['en']); |
||
126 | } elseif (isset($a['name']['en'])) { |
||
127 | return -1; // place name before entity ID |
||
128 | } elseif (isset($b['name']['en'])) { |
||
129 | return 1; // Place entity ID after name |
||
130 | } else { |
||
131 | return strcasecmp($a['entityid'], $b['entityid']); |
||
132 | } |
||
133 | } |
||
134 | |||
135 | |||
136 | /** |
||
137 | * Structure the list of IdPs in a hierarchy based upon the tags. |
||
138 | * |
||
139 | * @param array $list A list of IdPs. |
||
140 | * |
||
141 | * @return array The list of IdPs structured accordingly. |
||
142 | */ |
||
143 | protected function idplistStructured(array $list): array |
||
144 | { |
||
145 | $slist = []; |
||
146 | |||
147 | $order = $this->discoconfig->getArray('taborder', []); |
||
148 | foreach ($order as $oe) { |
||
149 | $slist[$oe] = []; |
||
150 | } |
||
151 | |||
152 | $enableTabs = $this->discoconfig->getValue('tabs', null); |
||
153 | |||
154 | foreach ($list as $key => $val) { |
||
155 | $tags = ['misc']; |
||
156 | if (array_key_exists('tags', $val)) { |
||
157 | $tags = $val['tags']; |
||
158 | } |
||
159 | |||
160 | |||
161 | foreach ($tags as $tag) { |
||
162 | if (!empty($enableTabs) && !in_array($tag, $enableTabs)) { |
||
163 | continue; |
||
164 | } |
||
165 | $slist[$tag][$key] = $val; |
||
166 | } |
||
167 | } |
||
168 | |||
169 | foreach ($slist as $tab => $tbslist) { |
||
170 | uasort($slist[$tab], [self::class, 'mcmp']); |
||
171 | // reorder with a hook if one exists |
||
172 | Module::callHooks('discosort', $slist[$tab]); |
||
173 | } |
||
174 | |||
175 | return $slist; |
||
176 | } |
||
177 | |||
178 | |||
179 | /** |
||
180 | * Do the actual filtering according the rules defined. |
||
181 | * |
||
182 | * @param array $filter A set of rules regarding filtering. |
||
183 | * @param array $entry An entry to be evaluated by the filters. |
||
184 | * @param boolean $default What to do in case the entity does not match any rules. Defaults to true. |
||
185 | * |
||
186 | * @return boolean True if the entity should be kept, false if it should be discarded according to the filters. |
||
187 | */ |
||
188 | private function processFilter(array $filter, array $entry, bool $default = true): bool |
||
210 | } |
||
211 | |||
212 | |||
213 | /** |
||
214 | * Filter a list of entities according to any filters defined in the parent class, plus discopower configuration |
||
215 | * options regarding filtering. |
||
216 | * |
||
217 | * @param array $list A list of entities to filter. |
||
218 | * |
||
219 | * @return array The list in $list after filtering entities. |
||
220 | */ |
||
221 | protected function filterList(array $list): array |
||
287 | } |
||
288 | |||
289 | |||
290 | /** |
||
291 | * Handles a request to this discovery service. |
||
292 | * |
||
293 | * The IdP disco parameters should be set before calling this function. |
||
294 | */ |
||
295 | public function handleRequest(): void |
||
350 | } |
||
351 | |||
352 | |||
353 | /** |
||
354 | * @param \SimpleSAML\XHTML\Template $t |
||
355 | * @param array $metadata |
||
356 | * @return array |
||
357 | */ |
||
358 | private function processMetadata(Template $t, array $metadata): array |
||
359 | { |
||
360 | $basequerystring = '?' . |
||
361 | 'entityID=' . urlencode($t->data['entityID']) . '&' . |
||
362 | 'return=' . urlencode($t->data['return']) . '&' . |
||
363 | 'returnIDParam=' . urlencode($t->data['returnIDParam']) . '&idpentityid='; |
||
364 | |||
365 | $httpUtils = new Utils\HTTP(); |
||
366 | foreach ($metadata as $tab => $idps) { |
||
367 | foreach ($idps as $entityid => $entity) { |
||
368 | $entity['actionUrl'] = $basequerystring . urlencode($entity['entityid']); |
||
369 | if (array_key_exists('icon', $entity) && $entity['icon'] !== null) { |
||
370 | $entity['iconUrl'] = $httpUtils->resolveURL($entity['icon']); |
||
371 | } |
||
372 | $metadata[$tab][$entityid] = $entity; |
||
373 | } |
||
374 | } |
||
375 | return $metadata; |
||
376 | } |
||
377 | |||
378 | |||
379 | /** |
||
380 | * Get the IdP entities saved in the common domain cookie. |
||
381 | * |
||
382 | * @return array List of IdP entities. |
||
383 | */ |
||
384 | private function getCDC(): array |
||
385 | { |
||
386 | if (!isset($_COOKIE['_saml_idp'])) { |
||
387 | return []; |
||
388 | } |
||
389 | |||
390 | $ret = (string) $_COOKIE['_saml_idp']; |
||
391 | $ret = explode(' ', $ret); |
||
392 | foreach ($ret as &$idp) { |
||
393 | $idp = base64_decode($idp); |
||
394 | if ($idp === false) { |
||
395 | // not properly base64 encoded |
||
396 | return []; |
||
397 | } |
||
398 | } |
||
399 | |||
400 | return $ret; |
||
401 | } |
||
402 | |||
403 | |||
404 | /** |
||
405 | * Save the current IdP choice to a cookie. |
||
406 | * |
||
407 | * This function overrides the corresponding function in the parent class, to add support for common domain cookie. |
||
408 | * |
||
409 | * @param string $idp The entityID of the IdP. |
||
410 | */ |
||
411 | protected function setPreviousIdP(string $idp): void |
||
450 | } |
||
451 | |||
452 | |||
453 | /** |
||
454 | * Retrieve the previous IdP the user used. |
||
455 | * |
||
456 | * This function overrides the corresponding function in the parent class, to add support for common domain cookie. |
||
457 | * |
||
458 | * @return string|null The entity id of the previous IdP the user used, or null if this is the first time. |
||
459 | */ |
||
460 | protected function getPreviousIdP(): ?string |
||
476 | } |
||
477 | } |
||
478 |