1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types = 1); |
4
|
|
|
|
5
|
|
|
/** |
6
|
|
|
* Copyright 2014 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\SecondFactor; |
22
|
|
|
|
23
|
|
|
use LogicException; |
24
|
|
|
use Psr\Log\LoggerInterface; |
25
|
|
|
use Surfnet\StepupSelfService\SelfServiceBundle\Command\RevokeCommand; |
26
|
|
|
use Surfnet\StepupSelfService\SelfServiceBundle\Form\Type\RevokeSecondFactorType; |
27
|
|
|
use Surfnet\StepupSelfService\SelfServiceBundle\Service\SecondFactorService; |
28
|
|
|
use Symfony\Bridge\Twig\Attribute\Template; |
29
|
|
|
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController; |
30
|
|
|
use Symfony\Component\HttpFoundation\Request; |
31
|
|
|
use Symfony\Component\HttpFoundation\Response; |
32
|
|
|
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException; |
33
|
|
|
use Symfony\Component\Routing\Attribute\Route; |
34
|
|
|
|
35
|
|
|
class SecondFactorRevokeController extends AbstractController |
|
|
|
|
36
|
|
|
{ |
37
|
|
|
public function __construct( |
|
|
|
|
38
|
|
|
private readonly LoggerInterface $logger, |
39
|
|
|
private readonly SecondFactorService $secondFactorService, |
40
|
|
|
) { |
41
|
|
|
} |
42
|
|
|
|
43
|
|
|
#[Template('second_factor/revoke.html.twig')] |
44
|
|
|
#[Route( |
45
|
|
|
path: '/second-factor/{state}/{secondFactorId}/revoke', |
46
|
|
|
name: 'ss_second_factor_revoke', |
47
|
|
|
requirements: ['state' => '^(unverified|verified|vetted)$'], |
48
|
|
|
methods: ['GET','POST'] |
49
|
|
|
)] |
50
|
|
|
public function __invoke(Request $request, string $state, string $secondFactorId): array|Response |
|
|
|
|
51
|
|
|
{ |
52
|
|
|
$identity = $this->getUser()->getIdentity(); |
|
|
|
|
53
|
|
|
|
54
|
|
|
if (!$this->secondFactorService->identityHasSecondFactorOfStateWithId($identity->id, $state, $secondFactorId)) { |
55
|
|
|
$this->logger->error(sprintf( |
|
|
|
|
56
|
|
|
'Identity "%s" tried to revoke "%s" second factor "%s", but does not own that second factor', |
57
|
|
|
$identity->id, |
58
|
|
|
$state, |
59
|
|
|
$secondFactorId |
60
|
|
|
)); |
|
|
|
|
61
|
|
|
throw new NotFoundHttpException(); |
62
|
|
|
} |
63
|
|
|
|
64
|
|
|
$secondFactor = match ($state) { |
65
|
|
|
'unverified' => $this->secondFactorService->findOneUnverified($secondFactorId), |
66
|
|
|
'verified' => $this->secondFactorService->findOneVerified($secondFactorId), |
67
|
|
|
'vetted' => $this->secondFactorService->findOneVetted($secondFactorId), |
68
|
|
|
default => throw new LogicException('There are no other types of second factor.'), |
69
|
|
|
}; |
70
|
|
|
|
71
|
|
|
if ($secondFactor === null) { |
72
|
|
|
throw new NotFoundHttpException( |
73
|
|
|
sprintf("No %s second factor with id '%s' exists.", $state, $secondFactorId) |
74
|
|
|
); |
75
|
|
|
} |
76
|
|
|
|
77
|
|
|
$command = new RevokeCommand(); |
78
|
|
|
$command->identity = $identity; |
79
|
|
|
$command->secondFactor = $secondFactor; |
80
|
|
|
|
81
|
|
|
$form = $this->createForm(RevokeSecondFactorType::class, $command)->handleRequest($request); |
82
|
|
|
|
83
|
|
|
if ($form->isSubmitted() && $form->isValid()) { |
84
|
|
|
if ($this->secondFactorService->revoke($command)) { |
85
|
|
|
$this->addFlash('success', 'ss.second_factor.revoke.alert.revocation_successful'); |
86
|
|
|
} else { |
87
|
|
|
$this->addFlash('error', 'ss.second_factor.revoke.alert.revocation_failed'); |
88
|
|
|
} |
89
|
|
|
|
90
|
|
|
return $this->redirectToRoute('ss_second_factor_list'); |
91
|
|
|
} |
92
|
|
|
|
93
|
|
|
return [ |
94
|
|
|
'form' => $form->createView(), |
95
|
|
|
'secondFactor' => $secondFactor, |
96
|
|
|
]; |
97
|
|
|
} |
98
|
|
|
} |
99
|
|
|
|