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
|
|
|
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($responseContext->getSelectedSecondFactor()); |
87
|
|
|
$grantedLoa = $this->secondFactorService->getLoaLevel($secondFactor); |
88
|
|
|
} |
89
|
|
|
|
90
|
|
|
$response = $this->responseProxy->createProxyResponse( |
91
|
|
|
$responseContext->reconstituteAssertion(), |
92
|
|
|
$responseContext->getDestination(), |
93
|
|
|
(string)$grantedLoa |
94
|
|
|
); |
95
|
|
|
|
96
|
|
|
$logger->notice(sprintf( |
97
|
|
|
'Responding to request "%s" with response based on response from the remote IdP with response "%s"', |
98
|
|
|
$responseContext->getInResponseTo(), |
99
|
|
|
$response->getId() |
100
|
|
|
)); |
101
|
|
|
|
102
|
|
|
return $response; |
103
|
|
|
} |
104
|
|
|
|
105
|
|
|
/** |
106
|
|
|
* Reset the state of the response |
107
|
|
|
* |
108
|
|
|
* @param ResponseContext $responseContext |
109
|
|
|
*/ |
110
|
|
|
public function resetRespondState(ResponseContext $responseContext): void |
111
|
|
|
{ |
112
|
|
|
$responseContext->responseSent(); |
113
|
|
|
} |
114
|
|
|
} |
115
|
|
|
|