| 1 |  |  | <?php declare(strict_types=1); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 3 |  |  | /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 4 |  |  |  * Copyright 2022 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\StepupGateway\GatewayBundle\Sso2fa; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 20 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 21 |  |  | use Doctrine\Common\Collections\Collection; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 22 |  |  | use Exception; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 23 |  |  | use Psr\Log\LoggerInterface; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 24 |  |  | use Surfnet\StepupBundle\Service\SecondFactorTypeService; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 25 |  |  | use Surfnet\StepupGateway\GatewayBundle\Entity\SecondFactor; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 26 |  |  | use Surfnet\StepupGateway\GatewayBundle\Entity\ServiceProvider; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 27 |  |  | use Surfnet\StepupGateway\GatewayBundle\Exception\RuntimeException; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 28 |  |  | use Surfnet\StepupGateway\GatewayBundle\Saml\ResponseContext; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 29 |  |  | use Surfnet\StepupGateway\GatewayBundle\Service\InstitutionConfigurationService; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 30 |  |  | use Surfnet\StepupGateway\GatewayBundle\Service\SecondFactorService; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 31 |  |  | use Surfnet\StepupGateway\GatewayBundle\Sso2fa\DateTime\ExpirationHelperInterface; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 32 |  |  | use Surfnet\StepupGateway\GatewayBundle\Sso2fa\Exception\CookieNotFoundException; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 33 |  |  | use Surfnet\StepupGateway\GatewayBundle\Sso2fa\Exception\DecryptionFailedException; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 34 |  |  | use Surfnet\StepupGateway\GatewayBundle\Sso2fa\Exception\InvalidAuthenticationTimeException; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 35 |  |  | use Surfnet\StepupGateway\GatewayBundle\Sso2fa\Http\CookieHelperInterface; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 36 |  |  | use Surfnet\StepupGateway\GatewayBundle\Sso2fa\ValueObject\CookieValue; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 37 |  |  | use Surfnet\StepupGateway\GatewayBundle\Sso2fa\ValueObject\CookieValueInterface; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 38 |  |  | use Surfnet\StepupGateway\GatewayBundle\Sso2fa\ValueObject\NullCookieValue; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 39 |  |  | use Symfony\Component\HttpFoundation\Request; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 40 |  |  | use Symfony\Component\HttpFoundation\Response; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 41 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 42 |  |  | /** | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 43 |  |  |  * @SuppressWarnings(PHPMD.CouplingBetweenObjects) | Coupling is high as we are integrating logic into the infrastructure | 
            
                                                                                                            
                            
            
                                    
            
            
                | 44 |  |  |  */ | 
                            
                    |  |  |  | 
                                                                                        
                                                                                            
                                                                                            
                                                                                            
                                                                                            
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 45 |  |  | class CookieService implements CookieServiceInterface | 
            
                                                                                                            
                            
            
                                    
            
            
                | 46 |  |  | { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 47 |  |  |     /** | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 48 |  |  |      * @var CookieHelperInterface | 
            
                                                                                                            
                            
            
                                    
            
            
                | 49 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 50 |  |  |     private $cookieHelper; | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 51 |  |  |     /** | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 52 |  |  |      * @var InstitutionConfigurationService | 
            
                                                                                                            
                            
            
                                    
            
            
                | 53 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 54 |  |  |     private $institutionConfigurationService; | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 55 |  |  |     /** | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 56 |  |  |      * @var LoggerInterface | 
            
                                                                                                            
                            
            
                                    
            
            
                | 57 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 58 |  |  |     private $logger; | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 59 |  |  |     /** | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 60 |  |  |      * @var SecondFactorService | 
            
                                                                                                            
                            
            
                                    
            
            
                | 61 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 62 |  |  |     private $secondFactorService; | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 63 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 64 |  |  |     /** | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 65 |  |  |      * @var SecondFactorTypeService | 
            
                                                                                                            
                            
            
                                    
            
            
                | 66 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 67 |  |  |     private $secondFactorTypeService; | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 68 |  |  |     /** | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 69 |  |  |      * @var ExpirationHelperInterface | 
            
                                                                                                            
                            
            
                                    
            
            
                | 70 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 71 |  |  |     private $expirationHelper; | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 72 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 73 |  |  |     public function __construct( | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 74 |  |  |         CookieHelperInterface $cookieHelper, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 75 |  |  |         InstitutionConfigurationService $institutionConfigurationService, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 76 |  |  |         SecondFactorService $secondFactorService, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 77 |  |  |         SecondFactorTypeService $secondFactorTypeService, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 78 |  |  |         ExpirationHelperInterface $expirationHelper, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 79 |  |  |         LoggerInterface $logger | 
            
                                                                                                            
                            
            
                                    
            
            
                | 80 |  |  |     ) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 81 |  |  |         $this->cookieHelper = $cookieHelper; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 82 |  |  |         $this->institutionConfigurationService = $institutionConfigurationService; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 83 |  |  |         $this->secondFactorService = $secondFactorService; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 84 |  |  |         $this->secondFactorTypeService = $secondFactorTypeService; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 85 |  |  |         $this->expirationHelper = $expirationHelper; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 86 |  |  |         $this->logger = $logger; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 87 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 88 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 89 |  |  |     public function handleSsoOn2faCookieStorage( | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 90 |  |  |         ResponseContext $responseContext, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 91 |  |  |         Request $request, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 92 |  |  |         Response $httpResponse | 
            
                                                                                                            
                            
            
                                    
            
            
                | 93 |  |  |     ): Response { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 94 |  |  |         // Check if this specific SP is configured to allow setting of an SSO on 2FA cookie (configured in MW config) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 95 |  |  |         $remoteSp = $this->getRemoteSp($responseContext); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 96 |  |  |         if (!$remoteSp->allowedToSetSsoCookieOn2fa()) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 97 |  |  |             $this->logger->notice( | 
            
                                                                                                            
                            
            
                                    
            
            
                | 98 |  |  |                 sprintf( | 
            
                                                                                                            
                            
            
                                    
            
            
                | 99 |  |  |                     'SP: %s does not allow writing SSO on 2FA cookies', | 
            
                                                                                                            
                            
            
                                    
            
            
                | 100 |  |  |                     $remoteSp->getEntityId() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 101 |  |  |                 ) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 102 |  |  |             ); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 103 |  |  |             return $httpResponse; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 104 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 105 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 106 |  |  |         // The second factor id is read from state. It is the SF Id of the token used during authentication | 
            
                                                                                                            
                            
            
                                    
            
            
                | 107 |  |  |         $secondFactorId = $responseContext->getSelectedSecondFactor(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 108 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 109 |  |  |         // We can only set an SSO on 2FA cookie if a second factor authentication is being handled. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 110 |  |  |         if ($secondFactorId) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 111 |  |  |             $secondFactor = $this->secondFactorService->findByUuid($secondFactorId); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 112 |  |  |             if (!$secondFactor) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 113 |  |  |                 throw new RuntimeException(sprintf('Second Factor token not found with ID: %s', $secondFactorId)); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 114 |  |  |             } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 115 |  |  |             // Test if the institution of the Identity this SF belongs to has SSO on 2FA enabled | 
            
                                                                                                            
                            
            
                                    
            
            
                | 116 |  |  |             $isEnabled = $this->institutionConfigurationService->ssoOn2faEnabled($secondFactor->institution); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 117 |  |  |             $this->logger->notice( | 
            
                                                                                                            
                            
            
                                    
            
            
                | 118 |  |  |                 sprintf( | 
            
                                                                                                            
                            
            
                                    
            
            
                | 119 |  |  |                     'SSO on 2FA is %senabled for %s', | 
            
                                                                                                            
                            
            
                                    
            
            
                | 120 |  |  |                     $isEnabled ? '' : 'not ', | 
            
                                                                                                            
                            
            
                                    
            
            
                | 121 |  |  |                     $secondFactor->institution | 
            
                                                                                                            
                            
            
                                    
            
            
                | 122 |  |  |                 ) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 123 |  |  |             ); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 124 |  |  |             if ($isEnabled) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 125 |  |  |                 // The cookie reader can return a NullCookie if the cookie is not present, was expired or was otherwise | 
            
                                                                                                            
                            
            
                                    
            
            
                | 126 |  |  |                 // deemed invalid. See the CookieHelper::read implementation for details. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 127 |  |  |                 $ssoCookie = $this->read($request); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 128 |  |  |                 $identityId = $responseContext->getIdentityNameId(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 129 |  |  |                 $loa = $secondFactor->getLoaLevel($this->secondFactorTypeService); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 130 |  |  |                 $isValid = $this->isCookieValid($ssoCookie, $loa, $identityId); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 131 |  |  |                 $isVerifiedBySsoOn2faCookie = $responseContext->isVerifiedBySsoOn2faCookie(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 132 |  |  |                 // Did the LoA requirement change? If a higher LoA was requested, or did a new token authentication | 
            
                                                                                                            
                            
            
                                    
            
            
                | 133 |  |  |                 // take place? In that case create a new SSO on 2FA cookie | 
            
                                                                                                            
                            
            
                                    
            
            
                | 134 |  |  |                 if ($this->shouldAddCookie($ssoCookie, $isValid, $loa, $isVerifiedBySsoOn2faCookie)) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 135 |  |  |                     $cookie = CookieValue::from($identityId, $secondFactor->secondFactorId, $loa); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 136 |  |  |                     $this->store($httpResponse, $cookie); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 137 |  |  |                 } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 138 |  |  |             } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 139 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 140 |  |  |         $responseContext->finalizeAuthentication(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 141 |  |  |         return $httpResponse; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 142 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 143 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 144 |  |  |     /** | 
                            
                    |  |  |  | 
                                                                                        
                                                                                            
                                                                                            
                                                                                            
                                                                                            
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 145 |  |  |      * Allow high cyclomatic complexity in favour of keeping this method readable | 
            
                                                                                                            
                            
            
                                    
            
            
                | 146 |  |  |      * @SuppressWarnings(PHPMD.CyclomaticComplexity) | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 147 |  |  |      * @SuppressWarnings(PHPMD.NPathComplexity) | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 148 |  |  |      */ | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                        
                            
            
                                    
            
            
                | 149 |  |  |     public function shouldSkip2faAuthentication( | 
            
                                                                        
                            
            
                                    
            
            
                | 150 |  |  |         ResponseContext $responseContext, | 
            
                                                                        
                            
            
                                    
            
            
                | 151 |  |  |         float $requiredLoa, | 
            
                                                                        
                            
            
                                    
            
            
                | 152 |  |  |         string $identityNameId, | 
            
                                                                        
                            
            
                                    
            
            
                | 153 |  |  |         Collection $secondFactorCollection, | 
            
                                                                        
                            
            
                                    
            
            
                | 154 |  |  |         Request $request | 
            
                                                                        
                            
            
                                    
            
            
                | 155 |  |  |     ): bool { | 
            
                                                                        
                            
            
                                    
            
            
                | 156 |  |  |         if ($responseContext->isForceAuthn()) { | 
            
                                                                        
                            
            
                                    
            
            
                | 157 |  |  |             $this->logger->notice('Ignoring SSO on 2FA cookie when ForceAuthN is specified.'); | 
            
                                                                        
                            
            
                                    
            
            
                | 158 |  |  |             return false; | 
            
                                                                        
                            
            
                                    
            
            
                | 159 |  |  |         } | 
            
                                                                        
                            
            
                                    
            
            
                | 160 |  |  |         $remoteSp = $this->getRemoteSp($responseContext); | 
            
                                                                        
                            
            
                                    
            
            
                | 161 |  |  |         // Test if the SP allows SSO on 2FA to take place (configured in MW config) | 
            
                                                                        
                            
            
                                    
            
            
                | 162 |  |  |         if (!$remoteSp->allowSsoOn2fa()) { | 
            
                                                                        
                            
            
                                    
            
            
                | 163 |  |  |             $this->logger->notice( | 
            
                                                                        
                            
            
                                    
            
            
                | 164 |  |  |                 sprintf( | 
            
                                                                        
                            
            
                                    
            
            
                | 165 |  |  |                     'Ignoring SSO on 2FA for SP: %s', | 
            
                                                                        
                            
            
                                    
            
            
                | 166 |  |  |                     $remoteSp->getEntityId() | 
            
                                                                        
                            
            
                                    
            
            
                | 167 |  |  |                 ) | 
            
                                                                        
                            
            
                                    
            
            
                | 168 |  |  |             ); | 
            
                                                                        
                            
            
                                    
            
            
                | 169 |  |  |             return false; | 
            
                                                                        
                            
            
                                    
            
            
                | 170 |  |  |         } | 
            
                                                                        
                            
            
                                    
            
            
                | 171 |  |  |         $ssoCookie = $this->read($request); | 
            
                                                                        
                            
            
                                    
            
            
                | 172 |  |  |         // Perform validation on the cookie and its contents | 
            
                                                                        
                            
            
                                    
            
            
                | 173 |  |  |         if (!$this->isCookieValid($ssoCookie, $requiredLoa, $identityNameId)) { | 
            
                                                                        
                            
            
                                    
            
            
                | 174 |  |  |             return false; | 
            
                                                                        
                            
            
                                    
            
            
                | 175 |  |  |         } | 
            
                                                                        
                            
            
                                    
            
            
                | 176 |  |  |         if (!$this->secondFactorService->findByUuid($ssoCookie->secondFactorId())) { | 
            
                                                                        
                            
            
                                    
            
            
                | 177 |  |  |             $this->logger->notice( | 
            
                                                                        
                            
            
                                    
            
            
                | 178 |  |  |                 'The second factor stored in the SSO cookie was revoked or has otherwise became unknown to Gateway', | 
            
                                                                        
                            
            
                                    
            
            
                | 179 |  |  |                 [ | 
            
                                                                        
                            
            
                                    
            
            
                | 180 |  |  |                     'secondFactorIdFromCookie' => $ssoCookie->secondFactorId() | 
            
                                                                        
                            
            
                                    
            
            
                | 181 |  |  |                 ] | 
            
                                                                        
                            
            
                                    
            
            
                | 182 |  |  |             ); | 
            
                                                                        
                            
            
                                    
            
            
                | 183 |  |  |             return false; | 
            
                                                                        
                            
            
                                    
            
            
                | 184 |  |  |         } | 
            
                                                                        
                            
            
                                    
            
            
                | 185 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 186 |  |  |         /** @var SecondFactor $secondFactor */ | 
                            
                    |  |  |  | 
                                                                                        
                                                                                            
                                                                                            
                                                                                     | 
            
                                                                        
                            
            
                                    
            
            
                | 187 |  |  |         foreach ($secondFactorCollection as $secondFactor) { | 
            
                                                                        
                            
            
                                    
            
            
                | 188 |  |  |             $loa = $secondFactor->getLoaLevel($this->secondFactorTypeService); | 
            
                                                                        
                            
            
                                    
            
            
                | 189 |  |  |             if ($loa >= $requiredLoa) { | 
            
                                                                        
                            
            
                                    
            
            
                | 190 |  |  |                 $this->logger->notice('Verified the current 2FA authentication can be given with the SSO on 2FA cookie'); | 
            
                                                                        
                            
            
                                    
            
            
                | 191 |  |  |                 $responseContext->saveSelectedSecondFactor($secondFactor); | 
            
                                                                        
                            
            
                                    
            
            
                | 192 |  |  |                 return true; | 
            
                                                                        
                            
            
                                    
            
            
                | 193 |  |  |             } | 
            
                                                                        
                            
            
                                    
            
            
                | 194 |  |  |         } | 
            
                                                                        
                            
            
                                    
            
            
                | 195 |  |  |         return false; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 196 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 197 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 198 |  |  |     public function getCookieFingerprint(Request $request): string | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 199 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 200 |  |  |         return $this->cookieHelper->fingerprint($request); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 201 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 202 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 203 |  |  |     /** | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 204 |  |  |      * This method determines if an SSO on 2FA cookie should be created. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 205 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 206 |  |  |      * The comments in the code block should give a good feel for what business rules | 
            
                                                                                                            
                            
            
                                    
            
            
                | 207 |  |  |      * are applied in this method. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 208 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 209 |  |  |      * @param CookieValueInterface $ssoCookie           The SSO on 2FA cookie as read from the HTTP response | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 210 |  |  |      * @param float $loa                                The LoA that was requested for this authentication, used to | 
                            
                    |  |  |  | 
                                                                                        
                                                                                            
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 211 |  |  |      *                                                  compare to the LoA stored in the SSO cookie | 
            
                                                                                                            
                            
            
                                    
            
            
                | 212 |  |  |      * @param bool $wasAuthenticatedWithSsoOn2faCookie  Indicator if the currently running authentication was performed | 
                            
                    |  |  |  | 
                                                                                        
                                                                                            
                                                                                            
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 213 |  |  |      *                                                  with the SSO on 2FA cookie | 
            
                                                                                                            
                            
            
                                    
            
            
                | 214 |  |  |      */ | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 215 |  |  |     private function shouldAddCookie( | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 216 |  |  |         CookieValueInterface $ssoCookie, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 217 |  |  |         bool $isCookieValid, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 218 |  |  |         float $loa, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 219 |  |  |         bool $wasAuthenticatedWithSsoOn2faCookie | 
            
                                                                                                            
                            
            
                                    
            
            
                | 220 |  |  |     ): bool { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 221 |  |  |         // When the cookie is not yet set, was expired or was otherwise deemed invalid, we get a NullCookieValue | 
            
                                                                                                            
                            
            
                                    
            
            
                | 222 |  |  |         // back from the reader. Indicating there is no valid cookie present. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 223 |  |  |         $cookieNotSet = $ssoCookie instanceof NullCookieValue; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 224 |  |  |         // OR the existing cookie does exist, but the LoA stored in that cookie does not match the required LoA | 
            
                                                                                                            
                            
            
                                    
            
            
                | 225 |  |  |         $cookieDoesNotMeetLoaRequirement = ($ssoCookie instanceof CookieValue && !$ssoCookie->meetsRequiredLoa($loa)); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 226 |  |  |         if (!$ssoCookie instanceof NullCookieValue && $cookieDoesNotMeetLoaRequirement) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 227 |  |  |             $this->logger->notice( | 
            
                                                                                                            
                            
            
                                    
            
            
                | 228 |  |  |                 sprintf( | 
            
                                                                                                            
                            
            
                                    
            
            
                | 229 |  |  |                     'Storing new SSO on 2FA cookie as LoA requirement (%d changed to %d) changed', | 
            
                                                                                                            
                            
            
                                    
            
            
                | 230 |  |  |                     $ssoCookie->getLoa(), | 
            
                                                                                                            
                            
            
                                    
            
            
                | 231 |  |  |                     $loa | 
            
                                                                                                            
                            
            
                                    
            
            
                | 232 |  |  |                 ) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 233 |  |  |             ); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 234 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 235 |  |  |         // OR when a new authentication took place, we replace the existing cookie with a new one | 
            
                                                                                                            
                            
            
                                    
            
            
                | 236 |  |  |         if (!$wasAuthenticatedWithSsoOn2faCookie) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 237 |  |  |             $this->logger->notice('Storing new SSO on 2FA cookie as a new authentication took place'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 238 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 239 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 240 |  |  |         // Or when the cookie is not valid for some reason (see logs for the specific error) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 241 |  |  |         if (!$isCookieValid) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 242 |  |  |             $this->logger->notice('Storing new SSO on 2FA cookie, the current cookie is invalid'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 243 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 244 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 245 |  |  |         return $cookieNotSet || | 
            
                                                                                                            
                            
            
                                    
            
            
                | 246 |  |  |             !$isCookieValid || | 
            
                                                                                                            
                            
            
                                    
            
            
                | 247 |  |  |             $cookieDoesNotMeetLoaRequirement || | 
            
                                                                                                            
                            
            
                                    
            
            
                | 248 |  |  |             !$wasAuthenticatedWithSsoOn2faCookie; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 249 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 250 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 251 |  |  |     private function store(Response $response, CookieValueInterface $cookieValue) | 
                            
                    |  |  |  | 
                                                                                        
                                                                                            
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 252 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 253 |  |  |         $this->cookieHelper->write($response, $cookieValue); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 254 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 255 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 256 |  |  |     private function read(Request $request): CookieValueInterface | 
                            
                    |  |  |  | 
                                                                                        
                                                                                            
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 257 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 258 |  |  |         try { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 259 |  |  |             return $this->cookieHelper->read($request); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 260 |  |  |         } catch (CookieNotFoundException $e) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 261 |  |  |             $this->logger->notice('Attempt to decrypt the cookie failed, the cookie could not be found'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 262 |  |  |             return new NullCookieValue(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 263 |  |  |         } catch (DecryptionFailedException $e) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 264 |  |  |             $this->logger->notice('Decryption of the SSO on 2FA cookie failed'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 265 |  |  |             return new NullCookieValue(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 266 |  |  |         } catch (Exception $e) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 267 |  |  |             $this->logger->notice( | 
            
                                                                                                            
                            
            
                                    
            
            
                | 268 |  |  |                 'Decryption failed, see original message in context', | 
            
                                                                                                            
                            
            
                                    
            
            
                | 269 |  |  |                 ['original-exception-message' => $e->getMessage()] | 
            
                                                                                                            
                            
            
                                    
            
            
                | 270 |  |  |             ); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 271 |  |  |             return new NullCookieValue(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 272 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 273 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 274 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 275 |  |  |     private function getRemoteSp(ResponseContext $responseContext): ServiceProvider | 
                            
                    |  |  |  | 
                                                                                        
                                                                                            
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 276 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 277 |  |  |         $remoteSp = $responseContext->getServiceProvider(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 278 |  |  |         if (!$remoteSp) { | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 279 |  |  |             throw new RuntimeException('SP not found in the response context, unable to continue with SSO on 2FA'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 280 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 281 |  |  |         return $remoteSp; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 282 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 283 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 284 |  |  |     private function isCookieValid(CookieValueInterface $ssoCookie, float $requiredLoa, string $identityNameId): bool | 
                            
                    |  |  |  | 
                                                                                        
                                                                                            
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 285 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 286 |  |  |         if ($ssoCookie instanceof NullCookieValue) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 287 |  |  |             return false; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 288 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 289 |  |  |         if ($ssoCookie instanceof CookieValue && !$ssoCookie->meetsRequiredLoa($requiredLoa)) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 290 |  |  |             $this->logger->notice( | 
            
                                                                                                            
                            
            
                                    
            
            
                | 291 |  |  |                 sprintf( | 
            
                                                                                                            
                            
            
                                    
            
            
                | 292 |  |  |                     'The required LoA %d did not match the LoA of the SSO cookie %d', | 
            
                                                                                                            
                            
            
                                    
            
            
                | 293 |  |  |                     $requiredLoa, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 294 |  |  |                     $ssoCookie->getLoa() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 295 |  |  |                 ) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 296 |  |  |             ); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 297 |  |  |             return false; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 298 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 299 |  |  |         if ($ssoCookie instanceof CookieValue && !$ssoCookie->issuedTo($identityNameId)) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 300 |  |  |             $this->logger->notice( | 
            
                                                                                                            
                            
            
                                    
            
            
                | 301 |  |  |                 sprintf( | 
            
                                                                                                            
                            
            
                                    
            
            
                | 302 |  |  |                     'The SSO on 2FA cookie was not issued to %s, but to %s', | 
            
                                                                                                            
                            
            
                                    
            
            
                | 303 |  |  |                     $identityNameId, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 304 |  |  |                     $ssoCookie->getIdentityId() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 305 |  |  |                 ) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 306 |  |  |             ); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 307 |  |  |             return false; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 308 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 309 |  |  |         try { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 310 |  |  |             $isExpired = $this->expirationHelper->isExpired($ssoCookie); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 311 |  |  |             if ($isExpired) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 312 |  |  |                 $this->logger->notice( | 
            
                                                                                                            
                            
            
                                    
            
            
                | 313 |  |  |                     'The SSO on 2FA cookie has expired. Meaning [authentication time] + [cookie lifetime] is in the past' | 
            
                                                                                                            
                            
            
                                    
            
            
                | 314 |  |  |                 ); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 315 |  |  |                 return false; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 316 |  |  |             } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 317 |  |  |         } catch (InvalidAuthenticationTimeException $e) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 318 |  |  |             $this->logger->notice('The SSO on 2FA cookie contained an invalid authentication time', [$e->getMessage()]); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 319 |  |  |             return false; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 320 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 321 |  |  |         return true; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 322 |  |  |     } | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 323 |  |  | } | 
            
                                                        
            
                                    
            
            
                | 324 |  |  |  |