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\SendSmsChallengeCommand; |
24
|
|
|
use Surfnet\StepupSelfService\SelfServiceBundle\Form\Type\SendSmsChallengeType; |
25
|
|
|
use Surfnet\StepupSelfService\SelfServiceBundle\Service\ControllerCheckerService; |
26
|
|
|
use Surfnet\StepupSelfService\SelfServiceBundle\Service\SmsSecondFactorService; |
27
|
|
|
use Surfnet\StepupSelfService\SelfServiceBundle\Service\SmsSecondFactorServiceInterface; |
28
|
|
|
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController; |
29
|
|
|
use Symfony\Component\HttpFoundation\Request; |
30
|
|
|
use Symfony\Component\HttpFoundation\Response; |
31
|
|
|
use Symfony\Component\Routing\Attribute\Route; |
32
|
|
|
|
33
|
|
|
class SmsSendChallengeController extends AbstractController |
|
|
|
|
34
|
|
|
{ |
35
|
|
|
public function __construct( |
|
|
|
|
36
|
|
|
private readonly SmsSecondFactorService $smsSecondFactorService, |
37
|
|
|
private readonly ControllerCheckerService $checkerService, |
38
|
|
|
) { |
39
|
|
|
} |
40
|
|
|
#[Route( |
41
|
|
|
path: '/registration/sms/send-challenge', |
42
|
|
|
name: 'ss_registration_sms_send_challenge', |
43
|
|
|
methods: ['GET','POST'], |
44
|
|
|
)] |
45
|
|
|
public function __invoke(Request $request): Response |
|
|
|
|
46
|
|
|
{ |
47
|
|
|
$this->checkerService->assertSecondFactorEnabled('sms'); |
48
|
|
|
|
49
|
|
|
$command = new SendSmsChallengeCommand(); |
50
|
|
|
$form = $this->createForm(SendSmsChallengeType::class, $command)->handleRequest($request); |
51
|
|
|
|
52
|
|
|
$otpRequestsRemaining = $this->smsSecondFactorService->getOtpRequestsRemainingCount( |
53
|
|
|
SmsSecondFactorServiceInterface::REGISTRATION_SECOND_FACTOR_ID |
54
|
|
|
); |
55
|
|
|
$maximumOtpRequests = $this->smsSecondFactorService->getMaximumOtpRequestsCount(); |
56
|
|
|
$viewVariables = [ |
57
|
|
|
'otpRequestsRemaining' => $otpRequestsRemaining, |
58
|
|
|
'maximumOtpRequests' => $maximumOtpRequests, |
59
|
|
|
'verifyEmail' => $this->checkerService->emailVerificationIsRequired(), |
60
|
|
|
]; |
61
|
|
|
|
62
|
|
|
if ($form->isSubmitted() && $form->isValid()) { |
63
|
|
|
$command->identity = $this->getUser()->getIdentity()->id; |
|
|
|
|
64
|
|
|
$command->institution = $this->getUser()->getIdentity()->institution; |
65
|
|
|
|
66
|
|
|
if ($otpRequestsRemaining === 0) { |
67
|
|
|
$this->addFlash('error', 'ss.prove_phone_possession.challenge_request_limit_reached'); |
68
|
|
|
return $this->render( |
69
|
|
|
'registration/sms/send_challenge.html.twig', |
70
|
|
|
['form' => $form->createView(), ...$viewVariables] |
71
|
|
|
); |
72
|
|
|
} |
73
|
|
|
|
74
|
|
|
if ($this->smsSecondFactorService->sendChallenge($command)) { |
75
|
|
|
return $this->redirect($this->generateUrl('ss_registration_sms_prove_possession')); |
76
|
|
|
} else { |
77
|
|
|
$this->addFlash('error', 'ss.prove_phone_possession.send_sms_challenge_failed'); |
78
|
|
|
} |
79
|
|
|
} |
80
|
|
|
|
81
|
|
|
return $this->render( |
82
|
|
|
'registration/sms/send_challenge.html.twig', |
83
|
|
|
['form' => $form->createView(), ...$viewVariables] |
84
|
|
|
); |
85
|
|
|
} |
86
|
|
|
} |
87
|
|
|
|