| 1 |  |  | <?php | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 3 |  |  | /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 4 |  |  |  * Copyright 2014 SURFnet bv | 
            
                                                                                                            
                            
            
                                    
            
            
                | 5 |  |  |  * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 6 |  |  |  * Licensed under the Apache License, Version 2.0 (the "License"); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 7 |  |  |  * you may not use this file except in compliance with the License. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 8 |  |  |  * You may obtain a copy of the License at | 
            
                                                                                                            
                            
            
                                    
            
            
                | 9 |  |  |  * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 10 |  |  |  *     http://www.apache.org/licenses/LICENSE-2.0 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 11 |  |  |  * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 12 |  |  |  * Unless required by applicable law or agreed to in writing, software | 
            
                                                                                                            
                            
            
                                    
            
            
                | 13 |  |  |  * distributed under the License is distributed on an "AS IS" BASIS, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 14 |  |  |  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 15 |  |  |  * See the License for the specific language governing permissions and | 
            
                                                                                                            
                            
            
                                    
            
            
                | 16 |  |  |  * limitations under the License. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 17 |  |  |  */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 18 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 19 |  |  | namespace Surfnet\StepupMiddleware\ManagementBundle\Validator; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 20 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 21 |  |  | use Assert\Assertion; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 22 |  |  | use Surfnet\StepupMiddleware\ManagementBundle\Validator\Assert as StepupAssert; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 23 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 24 |  |  | class ServiceProviderConfigurationValidator implements ConfigurationValidatorInterface | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 25 |  |  | { | 
            
                                                                        
                            
            
                                    
            
            
                | 26 |  |  |     public function validate(array $configuration, $propertyPath) | 
            
                                                                        
                            
            
                                    
            
            
                | 27 |  |  |     { | 
            
                                                                        
                            
            
                                    
            
            
                | 28 |  |  |         Assertion::isArray($configuration, 'invalid configuration format, must be an object', $propertyPath); | 
            
                                                                        
                            
            
                                    
            
            
                | 29 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 30 |  |  |         $requiredProperties = [ | 
            
                                                                        
                            
            
                                    
            
            
                | 31 |  |  |             'entity_id', | 
            
                                                                        
                            
            
                                    
            
            
                | 32 |  |  |             'public_key', | 
            
                                                                        
                            
            
                                    
            
            
                | 33 |  |  |             'acs', | 
            
                                                                        
                            
            
                                    
            
            
                | 34 |  |  |             'loa', | 
            
                                                                        
                            
            
                                    
            
            
                | 35 |  |  |             'assertion_encryption_enabled', | 
            
                                                                        
                            
            
                                    
            
            
                | 36 |  |  |             'second_factor_only', | 
            
                                                                        
                            
            
                                    
            
            
                | 37 |  |  |             'second_factor_only_nameid_patterns', | 
            
                                                                        
                            
            
                                    
            
            
                | 38 |  |  |             'blacklisted_encryption_algorithms', | 
            
                                                                        
                            
            
                                    
            
            
                | 39 |  |  |             'use_pdp' | 
            
                                                                        
                            
            
                                    
            
            
                | 40 |  |  |         ]; | 
            
                                                                        
                            
            
                                    
            
            
                | 41 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 42 |  |  |         if (empty($configuration['use_pdp'])) { | 
            
                                                                        
                            
            
                                    
            
            
                | 43 |  |  |             $configuration['use_pdp'] = false; | 
            
                                                                        
                            
            
                                    
            
            
                | 44 |  |  |         } | 
            
                                                                        
                            
            
                                    
            
            
                | 45 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 46 |  |  |         StepupAssert::keysMatch( | 
            
                                                                        
                            
            
                                    
            
            
                | 47 |  |  |             $configuration, | 
            
                                                                        
                            
            
                                    
            
            
                | 48 |  |  |             $requiredProperties, | 
            
                                                                        
                            
            
                                    
            
            
                | 49 |  |  |             sprintf( | 
            
                                                                        
                            
            
                                    
            
            
                | 50 |  |  |                 "The following properties must be present: '%s'; other properties are not supported", | 
            
                                                                        
                            
            
                                    
            
            
                | 51 |  |  |                 join("', '", $requiredProperties) | 
            
                                                                        
                            
            
                                    
            
            
                | 52 |  |  |             ), | 
            
                                                                        
                            
            
                                    
            
            
                | 53 |  |  |             $propertyPath | 
            
                                                                        
                            
            
                                    
            
            
                | 54 |  |  |         ); | 
            
                                                                        
                            
            
                                    
            
            
                | 55 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 56 |  |  |         $this->validateStringValue($configuration, 'entity_id', $propertyPath); | 
            
                                                                        
                            
            
                                    
            
            
                | 57 |  |  |         $this->validateStringValue($configuration, 'public_key', $propertyPath); | 
            
                                                                        
                            
            
                                    
            
            
                | 58 |  |  |         $this->validateAssertionConsumerUrls($configuration, $propertyPath); | 
            
                                                                        
                            
            
                                    
            
            
                | 59 |  |  |         $this->validateLoaDefinition($configuration, $propertyPath); | 
            
                                                                        
                            
            
                                    
            
            
                | 60 |  |  |         $this->validateBooleanValue( | 
            
                                                                        
                            
            
                                    
            
            
                | 61 |  |  |             $configuration, | 
            
                                                                        
                            
            
                                    
            
            
                | 62 |  |  |             'assertion_encryption_enabled', | 
            
                                                                        
                            
            
                                    
            
            
                | 63 |  |  |             $propertyPath | 
            
                                                                        
                            
            
                                    
            
            
                | 64 |  |  |         ); | 
            
                                                                        
                            
            
                                    
            
            
                | 65 |  |  |         $this->validateBooleanValue( | 
            
                                                                        
                            
            
                                    
            
            
                | 66 |  |  |             $configuration, | 
            
                                                                        
                            
            
                                    
            
            
                | 67 |  |  |             'second_factor_only', | 
            
                                                                        
                            
            
                                    
            
            
                | 68 |  |  |             $propertyPath | 
            
                                                                        
                            
            
                                    
            
            
                | 69 |  |  |         ); | 
            
                                                                        
                            
            
                                    
            
            
                | 70 |  |  |         $this->validateListOfNameIdPatterns( | 
            
                                                                        
                            
            
                                    
            
            
                | 71 |  |  |             $configuration, | 
            
                                                                        
                            
            
                                    
            
            
                | 72 |  |  |             'second_factor_only_nameid_patterns', | 
            
                                                                        
                            
            
                                    
            
            
                | 73 |  |  |             $propertyPath | 
            
                                                                        
                            
            
                                    
            
            
                | 74 |  |  |         ); | 
            
                                                                        
                            
            
                                    
            
            
                | 75 |  |  |         $this->validateStringValues( | 
            
                                                                        
                            
            
                                    
            
            
                | 76 |  |  |             $configuration, | 
            
                                                                        
                            
            
                                    
            
            
                | 77 |  |  |             'blacklisted_encryption_algorithms', | 
            
                                                                        
                            
            
                                    
            
            
                | 78 |  |  |             $propertyPath | 
            
                                                                        
                            
            
                                    
            
            
                | 79 |  |  |         ); | 
            
                                                                        
                            
            
                                    
            
            
                | 80 |  |  |         $this->validateBooleanValue($configuration, 'use_pdp', $propertyPath); | 
            
                                                                        
                            
            
                                    
            
            
                | 81 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 82 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 83 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 84 |  |  |      * @param array  $configuration | 
            
                                                                                                            
                            
            
                                    
            
            
                | 85 |  |  |      * @param string $name | 
            
                                                                                                            
                            
            
                                    
            
            
                | 86 |  |  |      * @param string $propertyPath | 
            
                                                                                                            
                            
            
                                    
            
            
                | 87 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 88 |  |  |     private function validateStringValue($configuration, $name, $propertyPath) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 89 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 90 |  |  |         Assertion::string($configuration[$name], 'value must be a string', $propertyPath . '.' . $name); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 91 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 92 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 93 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 94 |  |  |      * @param array  $configuration | 
            
                                                                                                            
                            
            
                                    
            
            
                | 95 |  |  |      * @param string $name | 
            
                                                                                                            
                            
            
                                    
            
            
                | 96 |  |  |      * @param string $propertyPath | 
            
                                                                                                            
                            
            
                                    
            
            
                | 97 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 98 |  |  |     private function validateStringValues($configuration, $name, $propertyPath) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 99 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 100 |  |  |         Assertion::isArray($configuration[$name], 'value must be an array', $propertyPath . '.' . $name); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 101 |  |  |         Assertion::allString($configuration[$name], 'value must be an array of strings', $propertyPath . '.' . $name); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 102 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 103 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 104 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 105 |  |  |      * @param array  $configuration | 
            
                                                                                                            
                            
            
                                    
            
            
                | 106 |  |  |      * @param string $name | 
            
                                                                                                            
                            
            
                                    
            
            
                | 107 |  |  |      * @param string $propertyPath | 
            
                                                                                                            
                            
            
                                    
            
            
                | 108 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 109 |  |  |     private function validateBooleanValue($configuration, $name, $propertyPath) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 110 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 111 |  |  |         Assertion::boolean($configuration[$name], 'value must be a boolean', $propertyPath . '.' . $name); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 112 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 113 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 114 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 115 |  |  |      * @param array  $configuration | 
            
                                                                                                            
                            
            
                                    
            
            
                | 116 |  |  |      * @param string $propertyPath | 
            
                                                                                                            
                            
            
                                    
            
            
                | 117 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 118 |  |  |     private function validateAssertionConsumerUrls($configuration, $propertyPath) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 119 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 120 |  |  |         $value = $configuration['acs']; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 121 |  |  |         $propertyPath = $propertyPath . '.acs'; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 122 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 123 |  |  |         Assertion::isArray($value, 'must contain a non-empty array of strings', $propertyPath); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 124 |  |  |         Assertion::true(count($value) >= 1, 'array must contain at least one value', $propertyPath); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 125 |  |  |         Assertion::allString($value, 'must be an array of strings', $propertyPath); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 126 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 127 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 128 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 129 |  |  |      * @param array  $configuration | 
            
                                                                                                            
                            
            
                                    
            
            
                | 130 |  |  |      * @param string $propertyPath | 
            
                                                                                                            
                            
            
                                    
            
            
                | 131 |  |  |      */ | 
            
                                                                                                            
                            
            
                                                                    
                                                                                                        
            
            
                | 132 |  | View Code Duplication |     private function validateLoaDefinition($configuration, $propertyPath) | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 133 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 134 |  |  |         $value = $configuration['loa']; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 135 |  |  |         $path  = $propertyPath . '.loa'; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 136 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 137 |  |  |         Assertion::isArray($value, 'must be an object', $path); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 138 |  |  |         Assertion::keyExists($value, '__default__', "must have the default loa set on the '__default__' property", $path); | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 139 |  |  |         Assertion::allString($value, 'all properties must contain strings as values', $path); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 140 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 141 |  |  |         // Test if all SP specific LoA configuration entries are lower case. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 142 |  |  |         $this->assertValidInstitutionIdentifiers( | 
            
                                                                                                            
                            
            
                                    
            
            
                | 143 |  |  |             $value, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 144 |  |  |             'The shacHomeOrganisation names in SP LoA configuration must all be lower case', | 
            
                                                                                                            
                            
            
                                    
            
            
                | 145 |  |  |             $path | 
            
                                                                                                            
                            
            
                                    
            
            
                | 146 |  |  |         ); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 147 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 148 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 149 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 150 |  |  |      * @param array $configuration | 
            
                                                                                                            
                            
            
                                    
            
            
                | 151 |  |  |      * @param string $name | 
            
                                                                                                            
                            
            
                                    
            
            
                | 152 |  |  |      * @param string $propertyPath | 
            
                                                                                                            
                            
            
                                    
            
            
                | 153 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 154 |  |  |     private function validateListOfNameIdPatterns($configuration, $name, $propertyPath) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 155 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 156 |  |  |         $value = $configuration[$name]; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 157 |  |  |         $propertyPath = $propertyPath . '.' . $name; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 158 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 159 |  |  |         Assertion::isArray($value, 'must contain an array', $propertyPath); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 160 |  |  |         Assertion::allString($value, 'must be an array of strings', $propertyPath); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 161 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 162 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 163 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 164 |  |  |      * All institution names (SHO values) should be lowercase. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 165 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 166 |  |  |      * For example: | 
            
                                                                                                            
                            
            
                                    
            
            
                | 167 |  |  |      *  [ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 168 |  |  |      *     '__default__'      => 'loa1', // valid | 
            
                                                                                                            
                            
            
                                    
            
            
                | 169 |  |  |      *     'institution-1.nl' => 'loa1', // valid | 
            
                                                                                                            
                            
            
                                    
            
            
                | 170 |  |  |      *     'My.Institution'   => 'loa2', // invalid | 
            
                                                                                                            
                            
            
                                    
            
            
                | 171 |  |  |      *  ] | 
            
                                                                                                            
                            
            
                                    
            
            
                | 172 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 173 |  |  |      * @param array $spLoaConfiguration | 
            
                                                                                                            
                            
            
                                    
            
            
                | 174 |  |  |      * @param string $message | 
            
                                                                                                            
                            
            
                                    
            
            
                | 175 |  |  |      * @param $propertyPath | 
            
                                                                                                            
                            
            
                                    
            
            
                | 176 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 177 |  |  |     private function assertValidInstitutionIdentifiers(array $spLoaConfiguration, $message, $propertyPath) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 178 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 179 |  |  |         $assertLowerCase = function ($sho) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 180 |  |  |             return ($sho === strtolower($sho)); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 181 |  |  |         }; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 182 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 183 |  |  |         // The array keys match the institution name / SHO. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 184 |  |  |         $lowerCaseTestResults = array_map($assertLowerCase, array_keys($spLoaConfiguration)); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 185 |  |  |         Assertion::allTrue($lowerCaseTestResults, $message, $propertyPath); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 186 |  |  |     } | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 187 |  |  | } | 
            
                                                        
            
                                    
            
            
                | 188 |  |  |  | 
            
                        
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.