1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types = 1); |
4
|
|
|
|
5
|
|
|
/** |
6
|
|
|
* Copyright 2021 SURFnet B.V. |
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\SelfVet; |
22
|
|
|
|
23
|
|
|
use Exception; |
24
|
|
|
use Psr\Log\LoggerInterface; |
25
|
|
|
use Surfnet\SamlBundle\Entity\IdentityProvider; |
26
|
|
|
use Surfnet\SamlBundle\Entity\ServiceProvider; |
27
|
|
|
use Surfnet\SamlBundle\Http\PostBinding; |
28
|
|
|
use Surfnet\SamlBundle\Monolog\SamlAuthenticationLogger; |
29
|
|
|
use Surfnet\SamlBundle\SAML2\Response\Assertion\InResponseTo; |
30
|
|
|
use Surfnet\StepupSelfService\SelfServiceBundle\Command\SelfVetCommand; |
31
|
|
|
use Surfnet\StepupSelfService\SelfServiceBundle\Service\SecondFactorService; |
32
|
|
|
use Surfnet\StepupSelfService\SelfServiceBundle\Service\SelfVetMarshaller; |
33
|
|
|
use Surfnet\StepupSelfService\SelfServiceBundle\Value\SelfVetRequestId; |
34
|
|
|
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController; |
35
|
|
|
use Symfony\Component\HttpFoundation\RedirectResponse; |
36
|
|
|
use Symfony\Component\HttpFoundation\Request; |
37
|
|
|
use Symfony\Component\HttpFoundation\RequestStack; |
38
|
|
|
use Symfony\Component\HttpKernel\Exception\AccessDeniedHttpException; |
39
|
|
|
use Symfony\Component\Routing\Attribute\Route; |
40
|
|
|
use Symfony\Component\Security\Core\Exception\AuthenticationException; |
41
|
|
|
|
42
|
|
|
/** |
|
|
|
|
43
|
|
|
* @SuppressWarnings(PHPMD.CouplingBetweenObjects) - Controllers are prone to higher coupling. This one is no exception |
44
|
|
|
* TODO: Split up into smaller controllers |
45
|
|
|
*/ |
|
|
|
|
46
|
|
|
class SelfVetConsumeController extends AbstractController |
47
|
|
|
{ |
48
|
|
|
final public const SELF_VET_SESSION_ID = 'second_factor_self_vet_request_id'; |
49
|
|
|
|
50
|
|
|
public function __construct( |
|
|
|
|
51
|
|
|
private readonly LoggerInterface $logger, |
52
|
|
|
private readonly SecondFactorService $secondFactorService, |
53
|
|
|
private readonly SelfVetMarshaller $selfVetMarshaller, |
54
|
|
|
private readonly ServiceProvider $serviceProvider, |
55
|
|
|
private readonly IdentityProvider $identityProvider, |
56
|
|
|
private readonly PostBinding $postBinding, |
57
|
|
|
private readonly SamlAuthenticationLogger $samlAuthenticationLogger, |
58
|
|
|
private readonly RequestStack $requestStack, |
59
|
|
|
) { |
60
|
|
|
} |
61
|
|
|
|
62
|
|
|
#[Route( |
63
|
|
|
path: '/second-factor/{secondFactorId}/self-vet-consume-assertion', |
64
|
|
|
name: 'ss_second_factor_self_vet_consume_assertion', |
65
|
|
|
methods: ['POST'], |
66
|
|
|
)] |
67
|
|
|
public function consumeSelfVetAssertion(Request $httpRequest, string $secondFactorId): RedirectResponse |
|
|
|
|
68
|
|
|
{ |
69
|
|
|
$identity = $this->getUser()->getIdentity(); |
|
|
|
|
70
|
|
|
if (!$this->selfVetMarshaller->isAllowed($identity, $secondFactorId)) { |
71
|
|
|
throw $this->createNotFoundException(); |
72
|
|
|
} |
73
|
|
|
|
74
|
|
|
if (!$this->requestStack->getSession()->has(self::SELF_VET_SESSION_ID)) { |
75
|
|
|
$this->logger->error( |
76
|
|
|
'Received an authentication response for self vetting a second factor, but no response was expected' |
77
|
|
|
); |
78
|
|
|
throw new AccessDeniedHttpException('Did not expect an authentication response'); |
79
|
|
|
} |
80
|
|
|
|
81
|
|
|
$this->logger->notice('Received an authentication response for self vetting a second factor'); |
82
|
|
|
|
83
|
|
|
/** @var SelfVetRequestId $initiatedRequestId */ |
|
|
|
|
84
|
|
|
$initiatedRequestId = $this->requestStack->getSession()->get(self::SELF_VET_SESSION_ID); |
85
|
|
|
|
86
|
|
|
$samlLogger = $this->samlAuthenticationLogger->forAuthentication($initiatedRequestId->requestId()); |
87
|
|
|
|
88
|
|
|
$this->requestStack->getSession()->remove(self::SELF_VET_SESSION_ID); |
89
|
|
|
|
90
|
|
|
try { |
91
|
|
|
$assertion = $this->postBinding->processResponse( |
92
|
|
|
$httpRequest, |
93
|
|
|
$this->identityProvider, |
94
|
|
|
$this->serviceProvider |
95
|
|
|
); |
96
|
|
|
|
97
|
|
|
if (!InResponseTo::assertEquals($assertion, $initiatedRequestId->requestId())) { |
98
|
|
|
$samlLogger->error( |
99
|
|
|
sprintf( |
100
|
|
|
'Expected a response to the request with ID "%s", but the SAMLResponse was a response to a different request', |
101
|
|
|
$initiatedRequestId |
|
|
|
|
102
|
|
|
) |
103
|
|
|
); |
104
|
|
|
throw new AuthenticationException('Unexpected InResponseTo in SAMLResponse'); |
105
|
|
|
} |
106
|
|
|
$candidateSecondFactor = $this->secondFactorService->findOneVerified($secondFactorId); |
107
|
|
|
// Proof of possession of higher/equal LoA was successful, now apply the self vet command on Middleware |
108
|
|
|
$command = new SelfVetCommand(); |
109
|
|
|
$command->identity = $this->getUser()->getIdentity(); |
110
|
|
|
$command->secondFactor = $candidateSecondFactor; |
111
|
|
|
$command->authoringLoa = $assertion->getAuthnContextClassRef(); |
112
|
|
|
|
113
|
|
|
if ($this->secondFactorService->selfVet($command)) { |
114
|
|
|
$this->addFlash('success', 'ss.self_vet.second_factor.alert.successful'); |
115
|
|
|
} else { |
116
|
|
|
$this->addFlash('error', 'ss.self_vet.second_factor.alert.failed'); |
117
|
|
|
} |
118
|
|
|
} catch (Exception) { |
119
|
|
|
$this->addFlash('error', 'ss.self_vet.second_factor.verification_failed'); |
120
|
|
|
} |
121
|
|
|
return $this->redirectToRoute('ss_second_factor_list'); |
122
|
|
|
} |
123
|
|
|
} |
124
|
|
|
|