1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* Copyright 2020 SURFnet B.V. |
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\Behat; |
20
|
|
|
|
21
|
|
|
use Behat\Behat\Context\Context; |
22
|
|
|
use Behat\Behat\Hook\Scope\BeforeScenarioScope; |
23
|
|
|
use Behat\Mink\Driver\Selenium2Driver; |
24
|
|
|
use Behat\Symfony2Extension\Context\KernelAwareContext; |
25
|
|
|
use RuntimeException; |
26
|
|
|
use RobRichards\XMLSecLibs\XMLSecurityKey; |
27
|
|
|
use SAML2\AuthnRequest; |
28
|
|
|
use SAML2\Certificate\Key; |
29
|
|
|
use SAML2\Certificate\KeyLoader; |
30
|
|
|
use SAML2\Certificate\PrivateKeyLoader; |
31
|
|
|
use SAML2\Configuration\PrivateKey; |
32
|
|
|
use SAML2\Constants; |
33
|
|
|
use SAML2\XML\saml\Issuer; |
34
|
|
|
use SAML2\XML\saml\NameID; |
35
|
|
|
use Surfnet\SamlBundle\Entity\IdentityProvider; |
36
|
|
|
use Surfnet\SamlBundle\SAML2\AuthnRequest as Saml2AuthnRequest; |
37
|
|
|
use Surfnet\StepupGateway\Behat\Repository\SamlEntityRepository; |
38
|
|
|
use Surfnet\StepupGateway\Behat\Service\FixtureService; |
39
|
|
|
use Symfony\Component\HttpFoundation\Request; |
40
|
|
|
use Symfony\Component\HttpFoundation\RequestStack; |
41
|
|
|
use Symfony\Component\HttpKernel\KernelInterface; |
42
|
|
|
|
43
|
|
|
class ServiceProviderContext implements Context, KernelAwareContext |
44
|
|
|
{ |
45
|
|
|
const SSP_URL = 'https://ssp.stepup.example.com/sp.php'; |
46
|
|
|
const SSO_ENDPOINT_URL = 'https://gateway.stepup.example.com/authentication/single-sign-on'; |
47
|
|
|
const SFO_ENDPOINT_URL = 'https://gateway.stepup.example.com/second-factor-only/single-sign-on'; |
48
|
|
|
|
49
|
|
|
/** |
50
|
|
|
* @var array |
51
|
|
|
*/ |
52
|
|
|
private $currentSp; |
53
|
|
|
|
54
|
|
|
/** |
55
|
|
|
* @var array |
56
|
|
|
*/ |
57
|
|
|
private $currentSfoSp; |
58
|
|
|
|
59
|
|
|
/** |
60
|
|
|
* @var array |
61
|
|
|
*/ |
62
|
|
|
private $currentIdP; |
63
|
|
|
|
64
|
|
|
/** |
65
|
|
|
* @var FixtureService |
66
|
|
|
*/ |
67
|
|
|
private $fixtureService; |
68
|
|
|
|
69
|
|
|
/** |
70
|
|
|
* @var KernelInterface |
71
|
|
|
*/ |
72
|
|
|
private $kernel; |
73
|
|
|
|
74
|
|
|
/** |
75
|
|
|
* @var MinkContext |
76
|
|
|
*/ |
77
|
|
|
private $minkContext; |
78
|
|
|
|
79
|
|
|
public function __construct(FixtureService $fixtureService) |
80
|
|
|
{ |
81
|
|
|
$this->fixtureService = $fixtureService; |
82
|
|
|
} |
83
|
|
|
|
84
|
|
|
public function setKernel(KernelInterface $kernel) |
85
|
|
|
{ |
86
|
|
|
$this->kernel = $kernel; |
87
|
|
|
} |
88
|
|
|
|
89
|
|
|
/** |
90
|
|
|
* @BeforeScenario |
91
|
|
|
*/ |
92
|
|
|
public function gatherContexts(BeforeScenarioScope $scope) |
93
|
|
|
{ |
94
|
|
|
$environment = $scope->getEnvironment(); |
95
|
|
|
$this->minkContext = $environment->getContext(MinkContext::class); |
|
|
|
|
96
|
|
|
} |
97
|
|
|
|
98
|
|
|
/** |
99
|
|
|
* @Given /^an SFO enabled SP with EntityID ([^\']*)$/ |
100
|
|
|
*/ |
101
|
|
|
public function anSFOEnabledSPWithEntityID($entityId) |
102
|
|
|
{ |
103
|
|
|
$this->registerSp($entityId, true); |
104
|
|
|
} |
105
|
|
|
|
106
|
|
|
/** |
107
|
|
|
* @Given /^an SP with EntityID ([^\']*)$/ |
108
|
|
|
*/ |
109
|
|
|
public function anSPWithEntityID($entityId) |
110
|
|
|
{ |
111
|
|
|
$this->registerSp($entityId, false); |
112
|
|
|
} |
113
|
|
|
/** |
114
|
|
|
* @Given /^an IdP with EntityID ([^\']*)$/ |
115
|
|
|
*/ |
116
|
|
|
public function anIdPWithEntityID($entityId) |
117
|
|
|
{ |
118
|
|
|
$this->registerIdp($entityId, false); |
|
|
|
|
119
|
|
|
} |
120
|
|
|
|
121
|
|
|
private function registerSp($entityId, $sfoEnabled) |
122
|
|
|
{ |
123
|
|
|
$publicKeyLoader = new KeyLoader(); |
124
|
|
|
// todo: use from services_test.yml |
125
|
|
|
$publicKeyLoader->loadCertificateFile('/var/www/ci/certificates/sp.crt'); |
126
|
|
|
$keys = $publicKeyLoader->getKeys(); |
127
|
|
|
/** @var Key $cert */ |
128
|
|
|
$cert = $keys->first(); |
129
|
|
|
|
130
|
|
|
$spEntity = $this->fixtureService->registerSP($entityId, $cert['X509Certificate'], $sfoEnabled); |
131
|
|
|
|
132
|
|
|
$spEntity['configuration'] = json_decode($spEntity['configuration'], true); |
133
|
|
|
if ($sfoEnabled) { |
134
|
|
|
$this->currentSfoSp = $spEntity; |
135
|
|
|
} else { |
136
|
|
|
$this->currentSp = $spEntity; |
137
|
|
|
} |
138
|
|
|
} |
139
|
|
|
|
140
|
|
|
private function registerIdP($entityId) |
141
|
|
|
{ |
142
|
|
|
$publicKeyLoader = new KeyLoader(); |
143
|
|
|
// todo: use from services_test.yml |
144
|
|
|
$publicKeyLoader->loadCertificateFile('/var/www/ci/certificates/idp.crt'); |
145
|
|
|
$keys = $publicKeyLoader->getKeys(); |
146
|
|
|
/** @var Key $cert */ |
147
|
|
|
$cert = $keys->first(); |
148
|
|
|
|
149
|
|
|
$idpEntity = $this->fixtureService->registerIdP($entityId, $cert['X509Certificate']); |
150
|
|
|
|
151
|
|
|
$idpEntity['configuration'] = json_decode($idpEntity['configuration'], true); |
152
|
|
|
$this->currentIdP = $idpEntity; |
153
|
|
|
} |
154
|
|
|
|
155
|
|
|
/** |
156
|
|
|
* @When /^([^\']*) starts an SFO authentication$/ |
157
|
|
|
*/ |
158
|
|
|
public function iStartAnSFOAuthentication($nameId) |
159
|
|
|
{ |
160
|
|
|
$this->iStartAnSFOAuthenticationWithLoa($nameId, "self-asserted"); |
161
|
|
|
} |
162
|
|
|
|
163
|
|
|
/** |
164
|
|
|
* @When /^([^\']*) starts an SFO authentication with LoA ([^\']*)$/ |
165
|
|
|
*/ |
166
|
|
|
public function iStartAnSFOAuthenticationWithLoa($nameId, string $loa, bool $forceAuthN = false) |
167
|
|
|
{ |
168
|
|
|
$authnRequest = new AuthnRequest(); |
169
|
|
|
// In order to later assert if the response succeeded or failed, set our own dummy ACS location |
170
|
|
|
$authnRequest->setAssertionConsumerServiceURL(SamlEntityRepository::SP_ACS_LOCATION); |
171
|
|
|
$issuerVo = new Issuer(); |
172
|
|
|
$issuerVo->setValue($this->currentSfoSp['entityId']); |
173
|
|
|
$authnRequest->setIssuer($issuerVo); |
174
|
|
|
$authnRequest->setDestination(self::SFO_ENDPOINT_URL); |
175
|
|
|
$authnRequest->setProtocolBinding(Constants::BINDING_HTTP_REDIRECT); |
176
|
|
|
$authnRequest->setNameId($this->buildNameId($nameId)); |
177
|
|
|
if ($forceAuthN) { |
178
|
|
|
$authnRequest->setForceAuthn(true); |
179
|
|
|
} |
180
|
|
|
// Sign with random key, does not mather for now. |
181
|
|
|
$authnRequest->setSignatureKey( |
182
|
|
|
$this->loadPrivateKey(new PrivateKey('/var/www/ci/certificates/sp.key', 'default')) |
183
|
|
|
); |
184
|
|
|
switch ($loa) { |
185
|
|
|
case "1": |
186
|
|
|
case "2": |
187
|
|
|
case "3": |
188
|
|
|
$authnRequest->setRequestedAuthnContext( |
189
|
|
|
['AuthnContextClassRef' => ['http://stepup.example.com/assurance/sfo-level' . $loa]] |
190
|
|
|
); |
191
|
|
|
break; |
192
|
|
|
case "self-asserted": |
193
|
|
|
$authnRequest->setRequestedAuthnContext( |
194
|
|
|
['AuthnContextClassRef' => ['http://stepup.example.com/assurance/loa-self-asserted']] |
195
|
|
|
); |
196
|
|
|
break; |
197
|
|
|
default: |
198
|
|
|
throw new RuntimeException(sprintf('The specified LoA-%s is not supported', $loa)); |
199
|
|
|
} |
200
|
|
|
$request = Saml2AuthnRequest::createNew($authnRequest); |
201
|
|
|
$query = $request->buildRequestQuery(); |
202
|
|
|
|
203
|
|
|
$this->getSession()->visit($request->getDestination().'?'.$query); |
204
|
|
|
} |
205
|
|
|
|
206
|
|
|
/** |
207
|
|
|
* @When /^([^\']*) starts an SFO authentication requiring LoA ([^\']*)$/ |
208
|
|
|
*/ |
209
|
|
|
public function iStartAnSFOAuthenticationWithLoaRequirement($nameId, $loa) |
210
|
|
|
{ |
211
|
|
|
$this->iStartAnSFOAuthenticationWithLoa($nameId, $loa); |
212
|
|
|
} |
213
|
|
|
/** |
214
|
|
|
* @When /^([^\']*) starts a forced SFO authentication requiring LoA ([^\']*)$/ |
215
|
|
|
*/ |
216
|
|
|
public function iStartAForcedSFOAuthenticationWithLoaRequirement($nameId, $loa) |
217
|
|
|
{ |
218
|
|
|
$this->iStartAnSFOAuthenticationWithLoa($nameId, $loa, true); |
219
|
|
|
} |
220
|
|
|
|
221
|
|
|
/** |
222
|
|
|
* @When /^([^\']*) starts an ADFS authentication requiring ([^\']*)$/ |
223
|
|
|
*/ |
224
|
|
|
public function iStartAnADFSAuthenticationWithLoaRequirement($nameId, $loa) |
225
|
|
|
{ |
226
|
|
|
$requestParams = [ |
227
|
|
|
'loa' => $loa, |
228
|
|
|
'nameId' => $nameId, |
229
|
|
|
'entityId' => $this->currentSfoSp['entityId'] |
230
|
|
|
]; |
231
|
|
|
$this->getSession()->visit(SamlEntityRepository::SP_ADFS_SSO_LOCATION . '?' . http_build_query($requestParams)); |
232
|
|
|
$this->minkContext->pressButton('Submit'); |
233
|
|
|
} |
234
|
|
|
|
235
|
|
|
/** |
236
|
|
|
* @When /^([^\']*) starts an authentication at Default SP$/ |
237
|
|
|
*/ |
238
|
|
|
public function iStartAnAuthenticationAtDefaultSP($nameId) |
239
|
|
|
{ |
240
|
|
|
$authnRequest = new AuthnRequest(); |
241
|
|
|
// In order to later assert if the response succeeded or failed, set our own dummy ACS location |
242
|
|
|
$authnRequest->setAssertionConsumerServiceURL(SamlEntityRepository::SP_ACS_LOCATION); |
243
|
|
|
$issuerVo = new Issuer(); |
244
|
|
|
$issuerVo->setValue('https://ssp.stepup.example.com/module.php/saml/sp/metadata.php/default-sp'); |
245
|
|
|
$authnRequest->setIssuer($issuerVo); |
246
|
|
|
$authnRequest->setDestination(self::SSO_ENDPOINT_URL); |
247
|
|
|
$authnRequest->setProtocolBinding(Constants::BINDING_HTTP_REDIRECT); |
248
|
|
|
$authnRequest->setNameId($this->buildNameId($nameId)); |
249
|
|
|
// Sign with random key, does not mather for now. |
250
|
|
|
// todo: use from services_test.yml |
251
|
|
|
$authnRequest->setSignatureKey( |
252
|
|
|
$this->loadPrivateKey(new PrivateKey('/var/www/ci/certificates/sp.key', 'default')) |
253
|
|
|
); |
254
|
|
|
$authnRequest->setRequestedAuthnContext( |
255
|
|
|
['AuthnContextClassRef' => ['http://stepup.example.com/assurance/level2']] |
256
|
|
|
); |
257
|
|
|
$request = Saml2AuthnRequest::createNew($authnRequest); |
258
|
|
|
$query = $request->buildRequestQuery(); |
259
|
|
|
$this->getSession()->visit($authnRequest->getDestination().'?'.$query); |
260
|
|
|
} |
261
|
|
|
|
262
|
|
|
/** |
263
|
|
|
* @When /^([^\']*) starts an authentication requiring LoA ([^\']*)$/ |
264
|
|
|
*/ |
265
|
|
|
public function iStartAnSsoAuthenticationWithLoaRequirement($nameId, $loa) |
266
|
|
|
{ |
267
|
|
|
$authnRequest = new AuthnRequest(); |
268
|
|
|
// In order to later assert if the response succeeded or failed, set our own dummy ACS location |
269
|
|
|
$authnRequest->setAssertionConsumerServiceURL(SamlEntityRepository::SP_ACS_LOCATION); |
270
|
|
|
$issuerVo = new Issuer(); |
271
|
|
|
$issuerVo->setValue($this->currentSp['entityId']); |
272
|
|
|
$authnRequest->setIssuer($issuerVo); |
273
|
|
|
$authnRequest->setDestination(self::SSO_ENDPOINT_URL); |
274
|
|
|
$authnRequest->setProtocolBinding(Constants::BINDING_HTTP_REDIRECT); |
275
|
|
|
$authnRequest->setNameId($this->buildNameId($nameId)); |
276
|
|
|
// Sign with random key, does not mather for now. |
277
|
|
|
// todo: use from services_test.yml |
278
|
|
|
$authnRequest->setSignatureKey( |
279
|
|
|
$this->loadPrivateKey(new PrivateKey('/var/www/ci/certificates/sp.key', 'default')) |
280
|
|
|
); |
281
|
|
|
|
282
|
|
|
switch ($loa) { |
283
|
|
|
case "1": |
284
|
|
|
case "2": |
285
|
|
|
case "3": |
286
|
|
|
$authnRequest->setRequestedAuthnContext( |
287
|
|
|
['AuthnContextClassRef' => ['http://stepup.example.com/assurance/level' . $loa]] |
288
|
|
|
); |
289
|
|
|
break; |
290
|
|
|
case "self-asserted": |
291
|
|
|
$authnRequest->setRequestedAuthnContext( |
|
|
|
|
292
|
|
|
['AuthnContextClassRef' => ['http://stepup.example.com/assurance/loa-self-asserted']] |
293
|
|
|
); |
294
|
|
|
default: |
295
|
|
|
throw new RuntimeException(sprintf('The specified LoA-%s is not supported', $loa)); |
296
|
|
|
} |
297
|
|
|
|
298
|
|
|
$request = Saml2AuthnRequest::createNew($authnRequest); |
299
|
|
|
$query = $request->buildRequestQuery(); |
300
|
|
|
$this->getSession()->visit($request->getDestination().'?'.$query); |
301
|
|
|
} |
302
|
|
|
|
303
|
|
|
/** |
304
|
|
|
* @When /^I authenticate at the IdP as ([^\']*)$/ |
305
|
|
|
*/ |
306
|
|
|
public function iAuthenticateAtTheIdp($username) |
307
|
|
|
{ |
308
|
|
|
$this->minkContext->fillField('username', $username); |
309
|
|
|
$this->minkContext->fillField('password', $username); |
310
|
|
|
// Submit the form |
311
|
|
|
$this->minkContext->pressButton('Login'); |
312
|
|
|
if (!$this->getSession()->getDriver() instanceof Selenium2Driver) { |
313
|
|
|
// Submit the SAML Response |
314
|
|
|
$this->minkContext->pressButton('Submit'); |
315
|
|
|
} |
316
|
|
|
} |
317
|
|
|
|
318
|
|
|
/** |
319
|
|
|
* @return IdentityProvider |
320
|
|
|
*/ |
321
|
|
|
public function getIdentityProvider() |
322
|
|
|
{ |
323
|
|
|
/** @var RequestStack $stack */ |
324
|
|
|
|
325
|
|
|
$stack = $this->kernel->getContainer()->get('request_stack'); |
326
|
|
|
$stack->push(Request::create('https://gateway.stepup.example.com')); |
327
|
|
|
$ip = $this->kernel->getContainer()->get('surfnet_saml.hosted.identity_provider'); |
328
|
|
|
$stack->pop(); |
329
|
|
|
|
330
|
|
|
return $ip; |
331
|
|
|
} |
332
|
|
|
|
333
|
|
|
private static function loadPrivateKey(PrivateKey $key) |
334
|
|
|
{ |
335
|
|
|
$keyLoader = new PrivateKeyLoader(); |
336
|
|
|
$privateKey = $keyLoader->loadPrivateKey($key); |
337
|
|
|
|
338
|
|
|
$key = new XMLSecurityKey(XMLSecurityKey::RSA_SHA256, ['type' => 'private']); |
339
|
|
|
$key->loadKey($privateKey->getKeyAsString()); |
340
|
|
|
|
341
|
|
|
return $key; |
342
|
|
|
} |
343
|
|
|
|
344
|
|
|
/** |
345
|
|
|
* @Given /^I log out at the IdP$/ |
346
|
|
|
*/ |
347
|
|
|
public function iLogOutAtTheIdP() |
348
|
|
|
{ |
349
|
|
|
$this->minkContext->visit(self::SSP_URL); |
350
|
|
|
$this->minkContext->pressButton('Logout'); |
351
|
|
|
} |
352
|
|
|
|
353
|
|
|
private function getSession() |
354
|
|
|
{ |
355
|
|
|
return $this->minkContext->getSession(); |
356
|
|
|
} |
357
|
|
|
|
358
|
|
|
private function buildNameId(string $nameId): NameID |
359
|
|
|
{ |
360
|
|
|
$nameIdVo = new NameID(); |
361
|
|
|
$nameIdVo->setValue($nameId); |
362
|
|
|
$nameIdVo->setFormat(Constants::NAMEFORMAT_UNSPECIFIED); |
363
|
|
|
return $nameIdVo; |
364
|
|
|
} |
365
|
|
|
} |
366
|
|
|
|