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 Psr\Log\LoggerInterface; |
22
|
|
|
use Surfnet\StepupSelfService\SelfServiceBundle\Service\InstitutionConfigurationOptionsService; |
23
|
|
|
use Symfony\Bridge\Twig\Attribute\Template; |
24
|
|
|
use Surfnet\StepupSelfService\SelfServiceBundle\Command\SendSmsChallengeCommand; |
25
|
|
|
use Surfnet\StepupSelfService\SelfServiceBundle\Command\VerifySmsChallengeCommand; |
26
|
|
|
use Surfnet\StepupSelfService\SelfServiceBundle\Controller\Controller; |
27
|
|
|
use Surfnet\StepupSelfService\SelfServiceBundle\Form\Type\SendSmsChallengeType; |
28
|
|
|
use Surfnet\StepupSelfService\SelfServiceBundle\Form\Type\VerifySmsChallengeType; |
29
|
|
|
use Surfnet\StepupSelfService\SelfServiceBundle\Service\SmsSecondFactorService; |
30
|
|
|
use Surfnet\StepupSelfService\SelfServiceBundle\Service\SmsSecondFactorServiceInterface; |
31
|
|
|
use Symfony\Component\HttpFoundation\RedirectResponse; |
32
|
|
|
use Symfony\Component\HttpFoundation\Request; |
33
|
|
|
use Symfony\Component\Routing\Annotation\Route; |
34
|
|
|
|
35
|
|
|
class SmsController extends Controller |
|
|
|
|
36
|
|
|
{ |
37
|
|
|
public function __construct( |
|
|
|
|
38
|
|
|
LoggerInterface $logger, |
39
|
|
|
InstitutionConfigurationOptionsService $configurationOptionsService, |
40
|
|
|
private readonly SmsSecondFactorService $smsSecondFactorService, |
41
|
|
|
) { |
42
|
|
|
parent::__construct($logger, $configurationOptionsService); |
43
|
|
|
} |
44
|
|
|
|
45
|
|
|
#[Template('registration/sms/send_challenge.html.twig')] |
46
|
|
|
#[Route( |
47
|
|
|
path: '/registration/sms/send-challenge', |
48
|
|
|
name: 'ss_registration_sms_send_challenge', |
49
|
|
|
methods: ['GET','POST'], |
50
|
|
|
)] |
51
|
|
|
public function sendChallenge(Request $request): array|RedirectResponse |
|
|
|
|
52
|
|
|
{ |
53
|
|
|
$this->assertSecondFactorEnabled('sms'); |
54
|
|
|
|
55
|
|
|
$identity = $this->getIdentity(); |
56
|
|
|
|
57
|
|
|
$command = new SendSmsChallengeCommand(); |
58
|
|
|
$form = $this->createForm(SendSmsChallengeType::class, $command)->handleRequest($request); |
59
|
|
|
|
60
|
|
|
$otpRequestsRemaining = $this->smsSecondFactorService->getOtpRequestsRemainingCount( |
61
|
|
|
SmsSecondFactorServiceInterface::REGISTRATION_SECOND_FACTOR_ID |
62
|
|
|
); |
63
|
|
|
$maximumOtpRequests = $this->smsSecondFactorService->getMaximumOtpRequestsCount(); |
64
|
|
|
$viewVariables = [ |
65
|
|
|
'otpRequestsRemaining' => $otpRequestsRemaining, |
66
|
|
|
'maximumOtpRequests' => $maximumOtpRequests, |
67
|
|
|
'verifyEmail' => $this->emailVerificationIsRequired(), |
68
|
|
|
]; |
69
|
|
|
|
70
|
|
|
if ($form->isSubmitted() && $form->isValid()) { |
71
|
|
|
$command->identity = $identity->id; |
72
|
|
|
$command->institution = $identity->institution; |
73
|
|
|
|
74
|
|
|
if ($otpRequestsRemaining === 0) { |
75
|
|
|
$this->addFlash('error', 'ss.prove_phone_possession.challenge_request_limit_reached'); |
76
|
|
|
return ['form' => $form->createView(), ...$viewVariables]; |
77
|
|
|
} |
78
|
|
|
|
79
|
|
|
if ($this->smsSecondFactorService->sendChallenge($command)) { |
80
|
|
|
return $this->redirect($this->generateUrl('ss_registration_sms_prove_possession')); |
81
|
|
|
} else { |
82
|
|
|
$this->addFlash('error', 'ss.prove_phone_possession.send_sms_challenge_failed'); |
83
|
|
|
} |
84
|
|
|
} |
85
|
|
|
|
86
|
|
|
return ['form' => $form->createView(), ...$viewVariables]; |
87
|
|
|
} |
88
|
|
|
|
89
|
|
|
#[Template('registration/sms/prove_possession.html.twig')] |
90
|
|
|
#[Route( |
91
|
|
|
path: '/registration/sms/prove-possession', |
92
|
|
|
name: 'ss_registration_sms_prove_possession', |
93
|
|
|
methods: ['GET','POST'], |
94
|
|
|
)] |
95
|
|
|
public function provePossession(Request $request): RedirectResponse|array |
|
|
|
|
96
|
|
|
{ |
97
|
|
|
$this->assertSecondFactorEnabled('sms'); |
98
|
|
|
|
99
|
|
|
/** @var SmsSecondFactorService $service */ |
|
|
|
|
100
|
|
|
$service = $this->get('surfnet_stepup_self_service_self_service.service.sms_second_factor'); |
|
|
|
|
101
|
|
|
|
102
|
|
|
if (!$service->hasSmsVerificationState(SmsSecondFactorServiceInterface::REGISTRATION_SECOND_FACTOR_ID)) { |
103
|
|
|
$this->get('session')->getFlashBag()->add('notice', 'ss.registration.sms.alert.no_verification_state'); |
104
|
|
|
|
105
|
|
|
return $this->redirectToRoute('ss_registration_sms_send_challenge'); |
106
|
|
|
} |
107
|
|
|
|
108
|
|
|
$identity = $this->getIdentity(); |
109
|
|
|
|
110
|
|
|
$command = new VerifySmsChallengeCommand(); |
111
|
|
|
$command->identity = $identity->id; |
112
|
|
|
|
113
|
|
|
$form = $this->createForm(VerifySmsChallengeType::class, $command)->handleRequest($request); |
114
|
|
|
|
115
|
|
|
if ($form->isSubmitted() && $form->isValid()) { |
116
|
|
|
$result = $service->provePossession($command); |
117
|
|
|
|
118
|
|
|
if ($result->isSuccessful()) { |
119
|
|
|
$service->clearSmsVerificationState(SmsSecondFactorServiceInterface::REGISTRATION_SECOND_FACTOR_ID); |
120
|
|
|
|
121
|
|
|
if ($this->emailVerificationIsRequired()) { |
122
|
|
|
return $this->redirectToRoute( |
123
|
|
|
'ss_registration_email_verification_email_sent', |
124
|
|
|
['secondFactorId' => $result->getSecondFactorId()] |
125
|
|
|
); |
126
|
|
|
} else { |
127
|
|
|
return $this->redirectToRoute( |
128
|
|
|
'ss_second_factor_vetting_types', |
129
|
|
|
['secondFactorId' => $result->getSecondFactorId()] |
130
|
|
|
); |
131
|
|
|
} |
132
|
|
|
} elseif ($result->wasIncorrectChallengeResponseGiven()) { |
133
|
|
|
$this->addFlash('error', 'ss.prove_phone_possession.incorrect_challenge_response'); |
134
|
|
|
} elseif ($result->hasChallengeExpired()) { |
135
|
|
|
$this->addFlash('error', 'ss.prove_phone_possession.challenge_expired'); |
136
|
|
|
} elseif ($result->wereTooManyAttemptsMade()) { |
137
|
|
|
$this->addFlash('error', 'ss.prove_phone_possession.too_many_attempts'); |
138
|
|
|
} else { |
139
|
|
|
$this->addFlash('error', 'ss.prove_phone_possession.proof_of_possession_failed'); |
140
|
|
|
} |
141
|
|
|
} |
142
|
|
|
|
143
|
|
|
return [ |
144
|
|
|
'form' => $form->createView(), |
145
|
|
|
'verifyEmail' => $this->emailVerificationIsRequired(), |
146
|
|
|
]; |
147
|
|
|
} |
148
|
|
|
} |
149
|
|
|
|