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; |
20
|
|
|
|
21
|
|
|
use Exception; |
22
|
|
|
use Surfnet\SamlBundle\Http\XMLResponse; |
23
|
|
|
use Surfnet\StepupBundle\Value\SecondFactorType; |
24
|
|
|
use Symfony\Component\HttpFoundation\Request; |
25
|
|
|
|
26
|
|
|
class SamlController extends Controller |
27
|
|
|
{ |
28
|
|
|
/** |
29
|
|
|
* @param string $secondFactorId |
30
|
|
|
* |
31
|
|
|
* @return \Symfony\Component\HttpFoundation\RedirectResponse |
32
|
|
|
* @throws \Symfony\Component\HttpKernel\Exception\NotFoundHttpException |
33
|
|
|
* @throws \Symfony\Component\Security\Core\Exception\AccessDeniedException |
34
|
|
|
*/ |
35
|
|
|
public function testSecondFactorAction($secondFactorId) |
36
|
|
|
{ |
37
|
|
|
$this->get('logger')->notice( |
38
|
|
|
'Starting second factor test' |
39
|
|
|
); |
40
|
|
|
|
41
|
|
|
$secondFactorService = $this->get('surfnet_stepup_self_service_self_service.service.second_factor'); |
42
|
|
|
$identity = $this->getIdentity(); |
43
|
|
|
|
44
|
|
View Code Duplication |
if (!$secondFactorService->identityHasSecondFactorOfStateWithId($identity->id, 'vetted', $secondFactorId)) { |
|
|
|
|
45
|
|
|
$this->get('logger')->error( |
46
|
|
|
sprintf( |
47
|
|
|
'Identity "%s" tried to test second factor "%s", but does not own that second factor or it is not vetted', |
|
|
|
|
48
|
|
|
$identity->id, |
49
|
|
|
$secondFactorId |
50
|
|
|
) |
51
|
|
|
); |
52
|
|
|
|
53
|
|
|
throw $this->createNotFoundException(); |
54
|
|
|
} |
55
|
|
|
|
56
|
|
|
$loaResolutionService = $this->get('surfnet_stepup.service.loa_resolution'); |
57
|
|
|
$authenticationRequestFactory = $this->get('self_service.test_second_factor_authentication_request_factory'); |
58
|
|
|
|
59
|
|
|
$secondFactor = $secondFactorService->findOneVetted($secondFactorId); |
60
|
|
|
$secondFactorType = new SecondFactorType($secondFactor->type); |
61
|
|
|
|
62
|
|
|
$authenticationRequest = $authenticationRequestFactory->createSecondFactorTestRequest( |
63
|
|
|
$identity->nameId, |
64
|
|
|
$loaResolutionService->getLoaByLevel($secondFactorType->getLevel()) |
65
|
|
|
); |
66
|
|
|
|
67
|
|
|
$this->get('session')->set('second_factor_test_request_id', $authenticationRequest->getRequestId()); |
68
|
|
|
|
69
|
|
|
$logger = $this->get('surfnet_saml.logger')->forAuthentication($authenticationRequest->getRequestId()); |
70
|
|
|
$logger->notice('Sending authentication request to the second factor test IDP'); |
71
|
|
|
|
72
|
|
|
return $this->get('surfnet_saml.http.redirect_binding')->createRedirectResponseFor($authenticationRequest); |
73
|
|
|
} |
74
|
|
|
|
75
|
|
|
public function consumeAssertionAction(Request $httpRequest) |
76
|
|
|
{ |
77
|
|
|
$this->get('logger')->notice('Received an authentication response for testing a second factor'); |
78
|
|
|
|
79
|
|
|
$session = $this->get('session'); |
80
|
|
|
|
81
|
|
|
if (!$session->has('second_factor_test_request_id')) { |
82
|
|
|
$this->get('logger')->error( |
83
|
|
|
'Received an authentication response for testing a second factor, but no second factor test response was expected' |
|
|
|
|
84
|
|
|
); |
85
|
|
|
|
86
|
|
|
throw $this->createAccessDeniedException('Did not expect an authentication response'); |
87
|
|
|
} |
88
|
|
|
|
89
|
|
|
$session->remove('second_factor_test_request_id'); |
90
|
|
|
|
91
|
|
|
$postBinding = $this->get('surfnet_saml.http.post_binding'); |
92
|
|
|
|
93
|
|
|
try { |
94
|
|
|
$postBinding->processResponse( |
95
|
|
|
$httpRequest, |
96
|
|
|
$this->get('self_service.second_factor_test_idp'), |
97
|
|
|
$this->get('surfnet_saml.hosted.service_provider') |
98
|
|
|
); |
99
|
|
|
|
100
|
|
|
$session->getFlashBag()->add('success', 'ss.test_second_factor.verification_successful'); |
101
|
|
|
} catch (Exception $exception) { |
102
|
|
|
$session->getFlashBag()->add('error', 'ss.test_second_factor.verification_failed'); |
103
|
|
|
} |
104
|
|
|
|
105
|
|
|
return $this->redirectToRoute('ss_second_factor_list'); |
106
|
|
|
} |
107
|
|
|
|
108
|
|
|
public function metadataAction() |
109
|
|
|
{ |
110
|
|
|
/** @var \Surfnet\SamlBundle\Metadata\MetadataFactory $metadataFactory */ |
111
|
|
|
$metadataFactory = $this->get('surfnet_saml.metadata_factory'); |
112
|
|
|
|
113
|
|
|
return new XMLResponse($metadataFactory->generate()); |
114
|
|
|
} |
115
|
|
|
} |
116
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.