1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types = 1); |
4
|
|
|
|
5
|
|
|
/** |
6
|
|
|
* Copyright 2023 SURFnet bv |
7
|
|
|
* |
8
|
|
|
* Licensed under the Apache License, Version 2.0 (the "License"); |
9
|
|
|
* you may not use this file except in compliance with the License. |
10
|
|
|
* You may obtain a copy of the License at |
11
|
|
|
* |
12
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0 |
13
|
|
|
* |
14
|
|
|
* Unless required by applicable law or agreed to in writing, software |
15
|
|
|
* distributed under the License is distributed on an "AS IS" BASIS, |
16
|
|
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
17
|
|
|
* See the License for the specific language governing permissions and |
18
|
|
|
* limitations under the License. |
19
|
|
|
*/ |
|
|
|
|
20
|
|
|
|
21
|
|
|
namespace Surfnet\StepupSelfService\SelfServiceBundle\Controller\Registration\Sms; |
22
|
|
|
|
23
|
|
|
use Surfnet\StepupSelfService\SelfServiceBundle\Command\VerifySmsChallengeCommand; |
24
|
|
|
use Surfnet\StepupSelfService\SelfServiceBundle\Form\Type\VerifySmsChallengeType; |
25
|
|
|
use Surfnet\StepupSelfService\SelfServiceBundle\Service\ControllerCheckerService; |
26
|
|
|
use Surfnet\StepupSelfService\SelfServiceBundle\Service\SmsSecondFactorServiceInterface; |
27
|
|
|
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController; |
28
|
|
|
use Symfony\Component\HttpFoundation\Request; |
29
|
|
|
use Symfony\Component\HttpFoundation\Response; |
30
|
|
|
use Symfony\Component\Routing\Attribute\Route; |
31
|
|
|
|
32
|
|
|
class SmsProofPossessionController extends AbstractController |
|
|
|
|
33
|
|
|
{ |
34
|
|
|
public function __construct( |
|
|
|
|
35
|
|
|
private readonly SmsSecondFactorServiceInterface $smsSecondFactorService, |
36
|
|
|
private readonly ControllerCheckerService $checkerService, |
37
|
|
|
) { |
38
|
|
|
} |
39
|
|
|
|
40
|
|
|
#[Route( |
41
|
|
|
path: '/registration/sms/prove-possession', |
42
|
|
|
name: 'ss_registration_sms_prove_possession', |
43
|
|
|
methods: ['GET','POST'], |
44
|
|
|
)] |
45
|
|
|
public function __invoke(Request $request): Response |
|
|
|
|
46
|
|
|
{ |
47
|
|
|
$this->checkerService->assertSecondFactorEnabled('sms'); |
48
|
|
|
|
49
|
|
|
if (!$this->smsSecondFactorService->hasSmsVerificationState(SmsSecondFactorServiceInterface::REGISTRATION_SECOND_FACTOR_ID)) { |
50
|
|
|
$this->addFlash('notice', 'ss.registration.sms.alert.no_verification_state'); |
51
|
|
|
|
52
|
|
|
return $this->redirectToRoute('ss_registration_sms_send_challenge'); |
53
|
|
|
} |
54
|
|
|
|
55
|
|
|
$command = new VerifySmsChallengeCommand(); |
56
|
|
|
|
57
|
|
|
$command->identity = $this->getUser()->getIdentity()->id; |
|
|
|
|
58
|
|
|
|
59
|
|
|
$form = $this->createForm(VerifySmsChallengeType::class, $command)->handleRequest($request); |
60
|
|
|
|
61
|
|
|
if ($form->isSubmitted() && $form->isValid()) { |
62
|
|
|
$result = $this->smsSecondFactorService->provePossession($command); |
63
|
|
|
|
64
|
|
|
if ($result->isSuccessful()) { |
65
|
|
|
$this->smsSecondFactorService->clearSmsVerificationState(SmsSecondFactorServiceInterface::REGISTRATION_SECOND_FACTOR_ID); |
66
|
|
|
$route = $this->checkerService->emailVerificationIsRequired() |
67
|
|
|
? 'ss_registration_email_verification_email_sent' |
68
|
|
|
: 'ss_second_factor_vetting_types'; |
69
|
|
|
|
70
|
|
|
return $this->redirectToRoute($route, ['secondFactorId' => $result->getSecondFactorId()]); |
71
|
|
|
} |
72
|
|
|
|
73
|
|
|
match (true) { |
74
|
|
|
$result->wasIncorrectChallengeResponseGiven() => $this->addFlash('error', 'ss.prove_phone_possession.incorrect_challenge_response'), |
|
|
|
|
75
|
|
|
$result->hasChallengeExpired() => $this->addFlash('error', 'ss.prove_phone_possession.challenge_expired'), |
|
|
|
|
76
|
|
|
$result->wereTooManyAttemptsMade() => $this->addFlash('error', 'ss.prove_phone_possession.too_many_attempts'), |
|
|
|
|
77
|
|
|
default => $this->addFlash('error', 'ss.prove_phone_possession.proof_of_possession_failed'), |
|
|
|
|
78
|
|
|
}; |
79
|
|
|
} |
80
|
|
|
|
81
|
|
|
return $this->render( |
82
|
|
|
'registration/sms/prove_possession.html.twig', |
83
|
|
|
[ |
84
|
|
|
'form' => $form->createView(), |
85
|
|
|
'verifyEmail' => $this->checkerService->emailVerificationIsRequired(), |
86
|
|
|
] |
87
|
|
|
); |
88
|
|
|
} |
89
|
|
|
} |
90
|
|
|
|