| 1 |  |  | <?php | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 3 |  |  | /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 4 |  |  |  * Copyright 2014 SURFnet bv | 
            
                                                                                                            
                            
            
                                    
            
            
                | 5 |  |  |  * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 6 |  |  |  * Licensed under the Apache License, Version 2.0 (the "License"); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 7 |  |  |  * you may not use this file except in compliance with the License. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 8 |  |  |  * You may obtain a copy of the License at | 
            
                                                                                                            
                            
            
                                    
            
            
                | 9 |  |  |  * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 10 |  |  |  *     http://www.apache.org/licenses/LICENSE-2.0 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 11 |  |  |  * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 12 |  |  |  * Unless required by applicable law or agreed to in writing, software | 
            
                                                                                                            
                            
            
                                    
            
            
                | 13 |  |  |  * distributed under the License is distributed on an "AS IS" BASIS, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 14 |  |  |  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 15 |  |  |  * See the License for the specific language governing permissions and | 
            
                                                                                                            
                            
            
                                    
            
            
                | 16 |  |  |  * limitations under the License. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 17 |  |  |  */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 18 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 19 |  |  | namespace Surfnet\StepupSelfService\SelfServiceBundle\Controller\Registration; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 20 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 21 |  |  | use Sensio\Bundle\FrameworkExtraBundle\Configuration\Template; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 22 |  |  | use Surfnet\StepupSelfService\SelfServiceBundle\Command\SendSmsChallengeCommand; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 23 |  |  | use Surfnet\StepupSelfService\SelfServiceBundle\Command\VerifySmsChallengeCommand; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 24 |  |  | use Surfnet\StepupSelfService\SelfServiceBundle\Controller\Controller; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 25 |  |  | use Surfnet\StepupSelfService\SelfServiceBundle\Service\SmsSecondFactorService; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 26 |  |  | use Symfony\Component\Form\FormError; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 27 |  |  | use Symfony\Component\HttpFoundation\Request; | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 28 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 29 |  |  | class SmsController extends Controller | 
            
                                                                        
                            
            
                                    
            
            
                | 30 |  |  | { | 
            
                                                                        
                            
            
                                    
            
            
                | 31 |  |  |     /** | 
            
                                                                        
                            
            
                                    
            
            
                | 32 |  |  |      * @Template | 
            
                                                                        
                            
            
                                    
            
            
                | 33 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 34 |  |  |     public function sendChallengeAction(Request $request) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 35 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 36 |  |  |         $this->assertSecondFactorEnabled('sms'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 37 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 38 |  |  |         $identity = $this->getIdentity(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 39 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 40 |  |  |         $command = new SendSmsChallengeCommand(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 41 |  |  |         $form = $this->createForm('ss_send_sms_challenge', $command)->handleRequest($request); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 42 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 43 |  |  |         /** @var SmsSecondFactorService $service */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 44 |  |  |         $service = $this->get('surfnet_stepup_self_service_self_service.service.sms_second_factor'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 45 |  |  |         $otpRequestsRemaining = $service->getOtpRequestsRemainingCount(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 46 |  |  |         $maximumOtpRequests = $service->getMaximumOtpRequestsCount(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 47 |  |  |         $viewVariables = ['otpRequestsRemaining' => $otpRequestsRemaining, 'maximumOtpRequests' => $maximumOtpRequests]; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 48 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 49 |  |  |         if ($form->isValid()) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 50 |  |  |             $command->identity = $identity->id; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 51 |  |  |             $command->institution = $identity->institution; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 52 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 53 |  |  |             if ($otpRequestsRemaining === 0) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 54 |  |  |                 $form->addError(new FormError('ss.prove_phone_possession.challenge_request_limit_reached')); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 55 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 56 |  |  |                 return array_merge(['form' => $form->createView()], $viewVariables); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 57 |  |  |             } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 58 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 59 |  |  |             if ($service->sendChallenge($command)) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 60 |  |  |                 return $this->redirect($this->generateUrl('ss_registration_sms_prove_possession')); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 61 |  |  |             } else { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 62 |  |  |                 $form->addError(new FormError('ss.prove_phone_possession.send_sms_challenge_failed')); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 63 |  |  |             } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 64 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 65 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 66 |  |  |         return array_merge(['form' => $form->createView()], $viewVariables); | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 67 |  |  |     } | 
            
                                                                        
                            
            
                                    
            
            
                | 68 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 69 |  |  |     /** | 
            
                                                                        
                            
            
                                    
            
            
                | 70 |  |  |      * @Template | 
            
                                                                        
                            
            
                                    
            
            
                | 71 |  |  |      * @param Request $request | 
            
                                                                        
                            
            
                                    
            
            
                | 72 |  |  |      * @return array|\Symfony\Component\HttpFoundation\RedirectResponse | 
            
                                                                        
                            
            
                                    
            
            
                | 73 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 74 |  |  |     public function provePossessionAction(Request $request) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 75 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 76 |  |  |         $this->assertSecondFactorEnabled('sms'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 77 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 78 |  |  |         /** @var SmsSecondFactorService $service */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 79 |  |  |         $service = $this->get('surfnet_stepup_self_service_self_service.service.sms_second_factor'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 80 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 81 |  |  |         if (!$service->hasSmsVerificationState()) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 82 |  |  |             $this->get('session')->getFlashBag()->add('notice', 'ss.registration.sms.alert.no_verification_state'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 83 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 84 |  |  |             return $this->redirectToRoute('ss_registration_sms_send_challenge'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 85 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 86 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 87 |  |  |         $identity = $this->getIdentity(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 88 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 89 |  |  |         $command = new VerifySmsChallengeCommand(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 90 |  |  |         $command->identity = $identity->id; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 91 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 92 |  |  |         $form = $this->createForm('ss_verify_sms_challenge', $command)->handleRequest($request); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 93 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 94 |  |  |         if ($form->isValid()) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 95 |  |  |             $result = $service->provePossession($command); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 96 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 97 |  |  |             if ($result->isSuccessful()) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 98 |  |  |                 $service->clearSmsVerificationState(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 99 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 100 |  |  |                 return $this->redirectToRoute( | 
            
                                                                                                            
                            
            
                                    
            
            
                | 101 |  |  |                     'ss_registration_email_verification_email_sent', | 
            
                                                                                                            
                            
            
                                    
            
            
                | 102 |  |  |                     ['secondFactorId' => $result->getSecondFactorId()] | 
            
                                                                                                            
                            
            
                                    
            
            
                | 103 |  |  |                 ); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 104 |  |  |             } elseif ($result->wasIncorrectChallengeResponseGiven()) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 105 |  |  |                 $form->addError(new FormError('ss.prove_phone_possession.incorrect_challenge_response')); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 106 |  |  |             } elseif ($result->hasChallengeExpired()) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 107 |  |  |                 $form->addError(new FormError('ss.prove_phone_possession.challenge_expired')); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 108 |  |  |             } elseif ($result->wereTooManyAttemptsMade()) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 109 |  |  |                 $form->addError(new FormError('ss.prove_phone_possession.too_many_attempts')); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 110 |  |  |             } else { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 111 |  |  |                 $form->addError(new FormError('ss.prove_phone_possession.proof_of_possession_failed')); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 112 |  |  |             } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 113 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 114 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 115 |  |  |         return ['form' => $form->createView()]; | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 116 |  |  |     } | 
            
                                                        
            
                                    
            
            
                | 117 |  |  | } | 
            
                                                        
            
                                    
            
            
                | 118 |  |  |  |