1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* Copyright 2018 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\StepupGateway\GatewayBundle\Service\Gateway; |
20
|
|
|
|
21
|
|
|
use SAML2\Response as SAMLResponse; |
22
|
|
|
use Surfnet\SamlBundle\Monolog\SamlAuthenticationLogger; |
23
|
|
|
use Surfnet\StepupBundle\Service\LoaResolutionService; |
24
|
|
|
use Surfnet\StepupBundle\Service\SecondFactorTypeService; |
25
|
|
|
use Surfnet\StepupGateway\GatewayBundle\Saml\ResponseContext; |
26
|
|
|
use Surfnet\StepupGateway\GatewayBundle\Service\ProxyResponseService; |
27
|
|
|
use Surfnet\StepupGateway\GatewayBundle\Service\SecondFactorService; |
28
|
|
|
|
29
|
|
|
class RespondService |
30
|
|
|
{ |
31
|
|
|
/** @var SamlAuthenticationLogger */ |
32
|
|
|
private $samlLogger; |
33
|
|
|
|
34
|
|
|
/** @var LoaResolutionService */ |
35
|
|
|
private $loaResolutionService; |
36
|
|
|
|
37
|
|
|
/** @var ProxyResponseService */ |
38
|
|
|
private $responseProxy; |
39
|
|
|
|
40
|
|
|
/** @var SecondFactorService */ |
41
|
|
|
private $secondFactorService; |
42
|
|
|
|
43
|
|
|
/** @var SecondFactorTypeService */ |
44
|
|
|
private $secondFactorTypeService; |
45
|
|
|
|
46
|
|
|
/** |
47
|
|
|
* GatewayServiceProviderService constructor. |
48
|
|
|
* @param SamlAuthenticationLogger $samlLogger |
49
|
|
|
* @param LoaResolutionService $loaResolutionService |
50
|
|
|
* @param ProxyResponseService $responseProxy |
51
|
|
|
* @param SecondFactorService $secondFactorService |
52
|
|
|
* @param SecondFactorTypeService $secondFactorTypeService |
53
|
|
|
*/ |
54
|
|
View Code Duplication |
public function __construct( |
|
|
|
|
55
|
|
|
SamlAuthenticationLogger $samlLogger, |
56
|
|
|
LoaResolutionService $loaResolutionService, |
57
|
|
|
ProxyResponseService $responseProxy, |
58
|
|
|
SecondFactorService $secondFactorService, |
59
|
|
|
SecondFactorTypeService $secondFactorTypeService |
60
|
|
|
) { |
61
|
|
|
$this->samlLogger = $samlLogger; |
62
|
|
|
$this->loaResolutionService = $loaResolutionService; |
63
|
|
|
$this->responseProxy = $responseProxy; |
64
|
|
|
$this->secondFactorService = $secondFactorService; |
65
|
|
|
$this->secondFactorTypeService = $secondFactorTypeService; |
66
|
|
|
} |
67
|
|
|
|
68
|
|
|
/** |
69
|
|
|
* Send a SAML response back to the service provider. |
70
|
|
|
* |
71
|
|
|
* Second factor verification handled by the LoginService is |
72
|
|
|
* finished. This method sends a AuthnResponse back to the service |
73
|
|
|
* provider in response to the AuthnRequest received in the LoginService. |
74
|
|
|
* @param ResponseContext $responseContext |
75
|
|
|
* @return SAMLResponse |
76
|
|
|
*/ |
77
|
|
|
public function respond(ResponseContext $responseContext) |
78
|
|
|
{ |
79
|
|
|
$originalRequestId = $responseContext->getInResponseTo(); |
80
|
|
|
|
81
|
|
|
$logger = $this->samlLogger->forAuthentication($originalRequestId); |
82
|
|
|
$logger->notice('Creating Response'); |
83
|
|
|
|
84
|
|
|
$grantedLoa = null; |
85
|
|
|
if ($responseContext->isSecondFactorVerified()) { |
86
|
|
|
$secondFactor = $this->secondFactorService->findByUuid( |
87
|
|
|
$responseContext->getSelectedSecondFactor() |
88
|
|
|
); |
89
|
|
|
|
90
|
|
|
$secondFactorTypeService = $this->secondFactorTypeService; |
91
|
|
|
$grantedLoa = $this->loaResolutionService->getLoaByLevel( |
92
|
|
|
$secondFactor->getLoaLevel($secondFactorTypeService) |
93
|
|
|
); |
94
|
|
|
} |
95
|
|
|
|
96
|
|
|
$response = $this->responseProxy->createProxyResponse( |
97
|
|
|
$responseContext->reconstituteAssertion(), |
98
|
|
|
$responseContext->getDestination(), |
99
|
|
|
(string)$grantedLoa |
100
|
|
|
); |
101
|
|
|
|
102
|
|
|
$logger->notice(sprintf( |
103
|
|
|
'Responding to request "%s" with response based on response from the remote IdP with response "%s"', |
104
|
|
|
$responseContext->getInResponseTo(), |
105
|
|
|
$response->getId() |
106
|
|
|
)); |
107
|
|
|
|
108
|
|
|
return $response; |
109
|
|
|
} |
110
|
|
|
|
111
|
|
|
/** |
112
|
|
|
* Reset the state of the response |
113
|
|
|
* |
114
|
|
|
* @param ResponseContext $responseContext |
115
|
|
|
*/ |
116
|
|
|
public function resetRespondState(ResponseContext $responseContext) |
117
|
|
|
{ |
118
|
|
|
$responseContext->responseSent(); |
119
|
|
|
} |
120
|
|
|
} |
121
|
|
|
|
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.