| 1 |  |  | <?php | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 3 |  |  | /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 4 |  |  |  * Copyright 2017 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\StepupBundle\Service; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 20 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 21 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 22 |  |  | use Surfnet\StepupBundle\Exception\DomainException; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 23 |  |  | use Surfnet\StepupBundle\Value\GssfConfig; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 24 |  |  | use Surfnet\StepupBundle\Value\Loa; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 25 |  |  | use Surfnet\StepupBundle\Value\SecondFactorType; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 26 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 27 |  |  | class SecondFactorTypeService | 
            
                                                                                                            
                            
            
                                    
            
            
                | 28 |  |  | { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 29 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 30 |  |  |      * @var array | 
            
                                                                                                            
                            
            
                                    
            
            
                | 31 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 32 |  |  |     private $loaLevelTypeMap = [ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 33 |  |  |         'sms' => Loa::LOA_2, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 34 |  |  |         'yubikey' => Loa::LOA_3, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 35 |  |  |         'u2f' => Loa::LOA_3, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 36 |  |  |     ]; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 37 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 38 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 39 |  |  |      * @var GssfConfig | 
            
                                                                                                            
                            
            
                                    
            
            
                | 40 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 41 |  |  |     private $gssfConfig; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 42 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 43 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 44 |  |  |      * @param array $gssfConfig | 
            
                                                                                                            
                            
            
                                    
            
            
                | 45 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 46 |  |  |     public function __construct(array $gssfConfig) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 47 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 48 |  |  |         $this->gssfConfig = new GssfConfig($gssfConfig); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 49 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 50 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 51 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 52 |  |  |      * @return string[] | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 53 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 54 |  |  |     public function getAvailableSecondFactorTypes() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 55 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 56 |  |  |         return array_merge( | 
            
                                                                                                            
                            
            
                                    
            
            
                | 57 |  |  |             $this->getAvailableGssfSecondFactorTypes(), | 
            
                                                                                                            
                            
            
                                    
            
            
                | 58 |  |  |             array_keys($this->loaLevelTypeMap) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 59 |  |  |         ); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 60 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 61 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 62 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 63 |  |  |      * @return string[] | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 64 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 65 |  |  |     private function getAvailableGssfSecondFactorTypes() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 66 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 67 |  |  |         return $this->gssfConfig->getSecondFactorTypes(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 68 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 69 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 70 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 71 |  |  |      * @param SecondFactorType $secondFactorType | 
            
                                                                                                            
                            
            
                                    
            
            
                | 72 |  |  |      * @param Loa $loa | 
            
                                                                                                            
                            
            
                                    
            
            
                | 73 |  |  |      * @return bool | 
            
                                                                                                            
                            
            
                                    
            
            
                | 74 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 75 |  |  |     public function canSatisfy(SecondFactorType $secondFactorType, Loa $loa) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 76 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 77 |  |  |         return $loa->levelIsLowerOrEqualTo($this->getLevel($secondFactorType)); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 78 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 79 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 80 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 81 |  |  |      * @param SecondFactorType $secondFactorType | 
            
                                                                                                            
                            
            
                                    
            
            
                | 82 |  |  |      * @param Loa $loa | 
            
                                                                                                            
                            
            
                                    
            
            
                | 83 |  |  |      * @return bool | 
            
                                                                                                            
                            
            
                                    
            
            
                | 84 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 85 |  |  |     public function isSatisfiedBy(SecondFactorType $secondFactorType, Loa $loa) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 86 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 87 |  |  |         return $loa->levelIsHigherOrEqualTo($this->getLevel($secondFactorType)); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 88 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 89 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 90 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 91 |  |  |      * @param SecondFactorType $secondFactorType | 
            
                                                                                                            
                            
            
                                    
            
            
                | 92 |  |  |      * @param SecondFactorTypeService|SecondFactorType $other | 
            
                                                                                                            
                            
            
                                    
            
            
                | 93 |  |  |      * @return bool | 
            
                                                                                                            
                            
            
                                    
            
            
                | 94 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 95 |  |  |     public function hasEqualOrHigherLoaComparedTo(SecondFactorType $secondFactorType, SecondFactorType $other) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 96 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 97 |  |  |         return $this->getLevel($secondFactorType) >= $this->getLevel($other); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 98 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 99 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 100 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 101 |  |  |      * @param SecondFactorType $secondFactorType | 
            
                                                                                                            
                            
            
                                    
            
            
                | 102 |  |  |      * @param SecondFactorTypeService|SecondFactorType $other | 
            
                                                                                                            
                            
            
                                    
            
            
                | 103 |  |  |      * @return bool | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 104 |  |  |      */ | 
            
                                                                        
                            
            
                                    
            
            
                | 105 |  |  |     public function hasEqualOrLowerLoaComparedTo(SecondFactorType $secondFactorType, SecondFactorType $other) | 
            
                                                                        
                            
            
                                    
            
            
                | 106 |  |  |     { | 
            
                                                                        
                            
            
                                    
            
            
                | 107 |  |  |         return $this->getLevel($secondFactorType) <= $this->getLevel($other); | 
            
                                                                        
                            
            
                                    
            
            
                | 108 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 109 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 110 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 111 |  |  |      * @param SecondFactorType $secondFactorType | 
            
                                                                                                            
                            
            
                                    
            
            
                | 112 |  |  |      * @return int | 
            
                                                                                                            
                            
            
                                    
            
            
                | 113 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 114 |  |  |     public function getLevel(SecondFactorType $secondFactorType) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 115 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 116 |  |  |         $loaMap = array_merge( | 
            
                                                                                                            
                            
            
                                    
            
            
                | 117 |  |  |             $this->loaLevelTypeMap, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 118 |  |  |             $this->gssfConfig->getLoaMap() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 119 |  |  |         ); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 120 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 121 |  |  |         if (array_key_exists($secondFactorType->getSecondFactorType(), $loaMap)) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 122 |  |  |             return $loaMap[$secondFactorType->getSecondFactorType()]; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 123 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 124 |  |  |         throw new DomainException( | 
            
                                                                                                            
                            
            
                                    
            
            
                | 125 |  |  |             sprintf( | 
            
                                                                                                            
                            
            
                                    
            
            
                | 126 |  |  |                 'The Loa level of this type: %s can\'t be retrieved.', | 
            
                                                                                                            
                            
            
                                    
            
            
                | 127 |  |  |                 $secondFactorType->getSecondFactorType() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 128 |  |  |             ) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 129 |  |  |         ); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 130 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 131 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 132 |  |  |     public function isGssf(SecondFactorType $secondFactorType) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 133 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 134 |  |  |         return in_array($secondFactorType->__toString(), $this->getAvailableGssfSecondFactorTypes()); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 135 |  |  |     } | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 136 |  |  | } | 
            
                                                        
            
                                    
            
            
                | 137 |  |  |  | 
            
                        
This check compares the return type specified in the
@returnannotation of a function or method doc comment with the types returned by the function and raises an issue if they mismatch.