|
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\StepupGateway\GatewayBundle\Saml; |
|
20
|
|
|
|
|
21
|
|
|
use DateTime; |
|
22
|
|
|
use DateTimeZone; |
|
23
|
|
|
use DOMDocument; |
|
24
|
|
|
use SAML2_Assertion; |
|
25
|
|
|
use Surfnet\SamlBundle\Entity\IdentityProvider; |
|
26
|
|
|
use Surfnet\SamlBundle\Entity\ServiceProvider; |
|
27
|
|
|
use Surfnet\StepupGateway\GatewayBundle\Saml\Proxy\ProxyStateHandler; |
|
28
|
|
|
use Surfnet\StepupGateway\GatewayBundle\Service\SamlEntityService; |
|
29
|
|
|
|
|
30
|
|
|
class ResponseContext |
|
31
|
|
|
{ |
|
32
|
|
|
/** |
|
33
|
|
|
* @var IdentityProvider |
|
34
|
|
|
*/ |
|
35
|
|
|
private $hostedIdentityProvider; |
|
36
|
|
|
|
|
37
|
|
|
/** |
|
38
|
|
|
* @var \Surfnet\StepupGateway\GatewayBundle\Service\SamlEntityService |
|
39
|
|
|
*/ |
|
40
|
|
|
private $samlEntityService; |
|
41
|
|
|
|
|
42
|
|
|
/** |
|
43
|
|
|
* @var ProxyStateHandler |
|
44
|
|
|
*/ |
|
45
|
|
|
private $stateHandler; |
|
46
|
|
|
|
|
47
|
|
|
/** |
|
48
|
|
|
* @var DateTime |
|
49
|
|
|
*/ |
|
50
|
|
|
private $generationTime; |
|
51
|
|
|
|
|
52
|
|
|
/** |
|
53
|
|
|
* @var IdentityProvider|null |
|
54
|
|
|
*/ |
|
55
|
|
|
private $authenticatingIdp; |
|
56
|
|
|
|
|
57
|
|
|
/** |
|
58
|
|
|
* @var ServiceProvider |
|
59
|
|
|
*/ |
|
60
|
|
|
private $targetServiceProvider; |
|
61
|
|
|
|
|
62
|
|
|
public function __construct( |
|
63
|
|
|
IdentityProvider $identityProvider, |
|
64
|
|
|
SamlEntityService $samlEntityService, |
|
65
|
|
|
ProxyStateHandler $stateHandler |
|
66
|
|
|
) { |
|
67
|
|
|
$this->hostedIdentityProvider = $identityProvider; |
|
68
|
|
|
$this->samlEntityService = $samlEntityService; |
|
69
|
|
|
$this->stateHandler = $stateHandler; |
|
70
|
|
|
$this->generationTime = new DateTime('now', new DateTimeZone('UTC')); |
|
71
|
|
|
} |
|
72
|
|
|
|
|
73
|
|
|
/** |
|
74
|
|
|
* @return string |
|
75
|
|
|
*/ |
|
76
|
|
|
public function getDestination() |
|
77
|
|
|
{ |
|
78
|
|
|
$serviceProvider = $this->getServiceProvider(); |
|
79
|
|
|
|
|
80
|
|
|
if (!$serviceProvider) { |
|
81
|
|
|
return null; |
|
82
|
|
|
} |
|
83
|
|
|
|
|
84
|
|
|
return $serviceProvider->getAssertionConsumerUrl(); |
|
85
|
|
|
} |
|
86
|
|
|
|
|
87
|
|
|
/** |
|
88
|
|
|
* @return null|string |
|
89
|
|
|
*/ |
|
90
|
|
|
public function getIssuer() |
|
91
|
|
|
{ |
|
92
|
|
|
return $this->hostedIdentityProvider->getEntityId(); |
|
93
|
|
|
} |
|
94
|
|
|
|
|
95
|
|
|
/** |
|
96
|
|
|
* @return int |
|
97
|
|
|
*/ |
|
98
|
|
|
public function getIssueInstant() |
|
99
|
|
|
{ |
|
100
|
|
|
return $this->generationTime->getTimestamp(); |
|
101
|
|
|
} |
|
102
|
|
|
|
|
103
|
|
|
/** |
|
104
|
|
|
* @return null|string |
|
105
|
|
|
*/ |
|
106
|
|
|
public function getInResponseTo() |
|
107
|
|
|
{ |
|
108
|
|
|
return $this->stateHandler->getRequestId(); |
|
109
|
|
|
} |
|
110
|
|
|
|
|
111
|
|
|
/** |
|
112
|
|
|
* @return null|string |
|
113
|
|
|
*/ |
|
114
|
|
|
public function getExpectedInResponseTo() |
|
115
|
|
|
{ |
|
116
|
|
|
return $this->stateHandler->getGatewayRequestId(); |
|
117
|
|
|
} |
|
118
|
|
|
|
|
119
|
|
|
/** |
|
120
|
|
|
* @return null|string |
|
121
|
|
|
*/ |
|
122
|
|
|
public function getRequiredLoa() |
|
123
|
|
|
{ |
|
124
|
|
|
return $this->stateHandler->getRequiredLoaIdentifier(); |
|
125
|
|
|
} |
|
126
|
|
|
|
|
127
|
|
|
/** |
|
128
|
|
|
* @return IdentityProvider |
|
129
|
|
|
*/ |
|
130
|
|
|
public function getIdentityProvider() |
|
131
|
|
|
{ |
|
132
|
|
|
return $this->hostedIdentityProvider; |
|
133
|
|
|
} |
|
134
|
|
|
|
|
135
|
|
|
/** |
|
136
|
|
|
* @return null|ServiceProvider |
|
137
|
|
|
*/ |
|
138
|
|
|
public function getServiceProvider() |
|
139
|
|
|
{ |
|
140
|
|
|
if (isset($this->targetServiceProvider)) { |
|
141
|
|
|
return $this->targetServiceProvider; |
|
142
|
|
|
} |
|
143
|
|
|
|
|
144
|
|
|
$serviceProviderId = $this->stateHandler->getRequestServiceProvider(); |
|
145
|
|
|
|
|
146
|
|
|
return $this->targetServiceProvider = $this->samlEntityService->getServiceProvider($serviceProviderId); |
|
147
|
|
|
} |
|
148
|
|
|
|
|
149
|
|
|
/** |
|
150
|
|
|
* @return null|string |
|
151
|
|
|
*/ |
|
152
|
|
|
public function getRelayState() |
|
153
|
|
|
{ |
|
154
|
|
|
return $this->stateHandler->getRelayState(); |
|
155
|
|
|
} |
|
156
|
|
|
|
|
157
|
|
|
/** |
|
158
|
|
|
* @param SAML2_Assertion $assertion |
|
159
|
|
|
*/ |
|
160
|
|
|
public function saveAssertion(SAML2_Assertion $assertion) |
|
161
|
|
|
{ |
|
162
|
|
|
// we pluck the NameId to make it easier to access it without having to reconstitute the assertion |
|
163
|
|
|
$nameId = $assertion->getNameId(); |
|
164
|
|
|
if (!empty($nameId['Value'])) { |
|
165
|
|
|
$this->stateHandler->saveIdentityNameId($nameId['Value']); |
|
166
|
|
|
} |
|
167
|
|
|
|
|
168
|
|
|
// same for the entityId of the authenticating Authority |
|
169
|
|
|
$authenticatingAuthorities = $assertion->getAuthenticatingAuthority(); |
|
170
|
|
|
if (!empty($authenticatingAuthorities)) { |
|
171
|
|
|
$this->stateHandler->setAuthenticatingIdp(reset($authenticatingAuthorities)); |
|
172
|
|
|
} |
|
173
|
|
|
|
|
174
|
|
|
$this->stateHandler->saveAssertion($assertion->toXML()->ownerDocument->saveXML()); |
|
175
|
|
|
} |
|
176
|
|
|
|
|
177
|
|
|
/** |
|
178
|
|
|
* @return SAML2_Assertion |
|
179
|
|
|
*/ |
|
180
|
|
|
public function reconstituteAssertion() |
|
181
|
|
|
{ |
|
182
|
|
|
$assertionAsXML = $this->stateHandler->getAssertion(); |
|
183
|
|
|
$assertionDocument = new DOMDocument(); |
|
184
|
|
|
$assertionDocument->loadXML($assertionAsXML); |
|
185
|
|
|
|
|
186
|
|
|
return new SAML2_Assertion($assertionDocument->documentElement); |
|
187
|
|
|
} |
|
188
|
|
|
|
|
189
|
|
|
/** |
|
190
|
|
|
* @return null|string |
|
191
|
|
|
*/ |
|
192
|
|
|
public function getIdentityNameId() |
|
193
|
|
|
{ |
|
194
|
|
|
return $this->stateHandler->getIdentityNameId(); |
|
195
|
|
|
} |
|
196
|
|
|
|
|
197
|
|
|
/** |
|
198
|
|
|
* @return null|IdentityProvider |
|
199
|
|
|
*/ |
|
200
|
|
|
public function getAuthenticatingIdp() |
|
201
|
|
|
{ |
|
202
|
|
|
$entityId = $this->stateHandler->getAuthenticatingIdp(); |
|
203
|
|
|
|
|
204
|
|
|
if (!$entityId) { |
|
205
|
|
|
return null; |
|
206
|
|
|
} |
|
207
|
|
|
|
|
208
|
|
|
if (isset($this->authenticatingIdp)) { |
|
209
|
|
|
return $this->authenticatingIdp; |
|
210
|
|
|
} |
|
211
|
|
|
|
|
212
|
|
|
$this->authenticatingIdp = $this->samlEntityService->hasIdentityProvider($entityId) |
|
213
|
|
|
? $this->samlEntityService->getIdentityProvider($entityId) |
|
214
|
|
|
: null; |
|
215
|
|
|
|
|
216
|
|
|
return $this->authenticatingIdp; |
|
217
|
|
|
} |
|
218
|
|
|
|
|
219
|
|
|
/** |
|
220
|
|
|
* @param string|null $secondFactorId |
|
221
|
|
|
*/ |
|
222
|
|
|
public function saveSelectedSecondFactor($secondFactorId) |
|
223
|
|
|
{ |
|
224
|
|
|
$this->stateHandler->setSelectedSecondFactorId($secondFactorId); |
|
225
|
|
|
$this->stateHandler->setSecondFactorVerified(false); |
|
226
|
|
|
} |
|
227
|
|
|
|
|
228
|
|
|
/** |
|
229
|
|
|
* @return null|string |
|
230
|
|
|
*/ |
|
231
|
|
|
public function getSelectedSecondFactor() |
|
232
|
|
|
{ |
|
233
|
|
|
return $this->stateHandler->getSelectedSecondFactorId(); |
|
234
|
|
|
} |
|
235
|
|
|
|
|
236
|
|
|
public function markSecondFactorVerified() |
|
237
|
|
|
{ |
|
238
|
|
|
$this->stateHandler->setSecondFactorVerified(true); |
|
239
|
|
|
} |
|
240
|
|
|
|
|
241
|
|
|
/** |
|
242
|
|
|
* @return bool |
|
243
|
|
|
*/ |
|
244
|
|
|
public function isSecondFactorVerified() |
|
245
|
|
|
{ |
|
246
|
|
|
return $this->stateHandler->getSelectedSecondFactorId() && $this->stateHandler->isSecondFactorVerified(); |
|
247
|
|
|
} |
|
248
|
|
|
|
|
249
|
|
|
public function getResponseAction() |
|
250
|
|
|
{ |
|
251
|
|
|
return $this->stateHandler->getResponseAction(); |
|
252
|
|
|
} |
|
253
|
|
|
|
|
254
|
|
|
/** |
|
255
|
|
|
* Resets some state after the response is sent (e.g. resets which second factor was selected and whether it was |
|
256
|
|
|
* verified). |
|
257
|
|
|
*/ |
|
258
|
|
|
public function responseSent() |
|
259
|
|
|
{ |
|
260
|
|
|
$this->saveSelectedSecondFactor(null); |
|
261
|
|
|
} |
|
262
|
|
|
} |
|
263
|
|
|
|