Skip to content

Commit 4d5ca98

Browse files
committed
Add Behat coverage for the per-SP WAYF remember-choice
Prior to this change, the per-SP remember-choice flow had no acceptance tests and the CI config did not enable the feature. This change enables the per-SP mode for CI, adds fixture and step support for selecting an IdP with remember-choice and asserting the rememberedidps cookie, and adds the RememberWayfChoice.feature scenarios. It also initialises the EmptyMduiElement properties that the mock WAYF read before they were set.
1 parent 552692e commit 4d5ca98

6 files changed

Lines changed: 246 additions & 4 deletions

File tree

‎config/packages/ci/parameters.yml‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@ parameters:
33
api.users.nameidlookup.password: secret
44
feature_api_users_nameid_lookup: true
55
feature_hide_bookmarkable_url: true
6+
wayf.remember_choice: false
7+
feature_enable_wayf_remember_choice_per_idp: true
68
auth.log.attributes:
79
uid: 'urn:mace:dir:attribute-def:uid'
810
encryption_keys:

‎src/OpenConext/EngineBlock/Metadata/EmptyMduiElement.php‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,10 @@ class EmptyMduiElement implements MultilingualElement, JsonSerializable
3131
{
3232
private $name;
3333

34+
public $height = null;
35+
public $width = null;
36+
public $url = null;
37+
3438
public function __construct(string $name)
3539
{
3640
$this->name = $name;

‎src/OpenConext/EngineBlockFunctionalTestingBundle/Features/Context/EngineBlockContext.php‎

Lines changed: 129 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
use DOMDocument;
2424
use DOMElement;
2525
use DOMXPath;
26+
use OpenConext\EngineBlock\Service\Wayf\RememberedIdpCookie;
2627
use OpenConext\EngineBlockBundle\Sbs\Msg;
2728
use OpenConext\EngineBlockFunctionalTestingBundle\Fixtures\DataStore\AbstractDataStore;
2829
use OpenConext\EngineBlockFunctionalTestingBundle\Fixtures\FunctionalTestingAttributeAggregationClient;
@@ -39,6 +40,7 @@
3940
use RuntimeException;
4041
use SAML2\Constants;
4142
use SAML2\DOMDocumentFactory;
43+
use Symfony\Component\BrowserKit\Cookie;
4244

4345
/**
4446
* @SuppressWarnings(PHPMD.TooManyPublicMethods) Both set up and tasks can be a lot...
@@ -370,6 +372,19 @@ public function anIdpInitiatedSingleSignOnForSpIsInvalidParameterByIdP($spName,
370372
* @Given /^I select "([^"]*)" on the WAYF$/
371373
*/
372374
public function iSelectOnTheWAYF($idpName)
375+
{
376+
$this->selectIdpOnTheWayf($idpName, false);
377+
}
378+
379+
/**
380+
* @Given /^I select "([^"]*)" on the WAYF and remember my choice$/
381+
*/
382+
public function iSelectOnTheWAYFAndRememberMyChoice($idpName)
383+
{
384+
$this->selectIdpOnTheWayf($idpName, true);
385+
}
386+
387+
private function selectIdpOnTheWayf(string $idpName, bool $rememberChoice): void
373388
{
374389
/** @var MockIdentityProvider $mockIdp */
375390
$mockIdp = $this->mockIdpRegistry->get($idpName);
@@ -380,13 +395,29 @@ public function iSelectOnTheWAYF($idpName)
380395
);
381396
}
382397

383-
$selector = '[data-entityid="' . $mockIdp->entityId() . '"] button.idp__submit';
398+
$selector = '[data-entityid="' . $mockIdp->entityId() . '"]';
399+
$idpContainer = $this->getMinkContext()->getSession()->getPage()->find('css', $selector);
384400

385-
$mink = $this->getMinkContext()->getSession()->getPage();
386-
$button = $mink->find('css', $selector);
401+
if (!$idpContainer) {
402+
throw new RuntimeException(sprintf('Unable to find idp container with selector "%s"', $selector));
403+
}
404+
405+
if ($rememberChoice) {
406+
$rememberChoiceField = $idpContainer->find('css', 'input[name="rememberChoice"]');
407+
408+
if (!$rememberChoiceField) {
409+
throw new RuntimeException(
410+
sprintf('Unable to find hidden rememberChoice field within selector "%s"', $selector)
411+
);
412+
}
413+
414+
$rememberChoiceField->setValue('1');
415+
}
416+
417+
$button = $idpContainer->find('css', 'button.idp__submit');
387418

388419
if (!$button) {
389-
throw new RuntimeException(sprintf('Unable to find button with selector "%s"', $selector));
420+
throw new RuntimeException(sprintf('Unable to find button with selector "%s button.idp__submit"', $selector));
390421
}
391422

392423
$button->click();
@@ -510,6 +541,15 @@ public function iLoseMySession()
510541
// set unknown session id to prevent session not found exception
511542
$session->setCookie(session_name(), '000000');
512543
}
544+
545+
/**
546+
* @Given /^I start a new browser session$/
547+
*/
548+
public function iStartANewBrowserSession()
549+
{
550+
$this->getMinkContext()->getSession()->setCookie(session_name(), null);
551+
}
552+
513553
/**
514554
* @Given /^I lose my session and reload$/
515555
*/
@@ -726,6 +766,91 @@ public function aLangCookieShouldBeSetWithValue($locale)
726766
}
727767
}
728768

769+
/**
770+
* @Then /^the "rememberedidps" cookie should be set$/
771+
*/
772+
public function theRememberedIdpsCookieShouldBeSet()
773+
{
774+
$cookie = $this->getMinkContext()->getSession()->getCookie(RememberedIdpCookie::NAME);
775+
776+
if ($cookie === null) {
777+
throw new ExpectationException(
778+
'The rememberedidps cookie has not been set',
779+
$this->getMinkContext()->getSession()->getDriver()
780+
);
781+
}
782+
}
783+
784+
/**
785+
* @Then /^the "rememberedidps" cookie should not be set$/
786+
*/
787+
public function theRememberedIdpsCookieShouldNotBeSet()
788+
{
789+
$cookie = $this->getMinkContext()->getSession()->getCookie(RememberedIdpCookie::NAME);
790+
791+
if ($cookie !== null) {
792+
throw new ExpectationException(
793+
'The rememberedidps cookie should not be set, but it is',
794+
$this->getMinkContext()->getSession()->getDriver()
795+
);
796+
}
797+
}
798+
799+
/**
800+
* @Then /^the "rememberedidps" cookie should remember "([^"]*)" for SP "([^"]*)"$/
801+
*/
802+
public function theRememberedIdpsCookieShouldRemember($idpName, $spName)
803+
{
804+
$cookie = $this->getMinkContext()->getSession()->getCookie(RememberedIdpCookie::NAME);
805+
$entries = $cookie === null ? null : json_decode((string) gzinflate((string) base64_decode($cookie, true)), true);
806+
$spEntityId = $this->mockSpRegistry->get($spName)->entityId();
807+
$idpEntityId = $this->mockIdpRegistry->get($idpName)->entityId();
808+
809+
if (!is_array($entries) || ($entries[$spEntityId]['idp'] ?? null) !== $idpEntityId) {
810+
throw new ExpectationException(
811+
sprintf('The rememberedidps cookie does not remember "%s" for SP "%s"', $idpName, $spName),
812+
$this->getMinkContext()->getSession()->getDriver()
813+
);
814+
}
815+
}
816+
817+
/**
818+
* @Given /^my browser has remembered IdP "([^"]*)" for SP "([^"]*)"$/
819+
*/
820+
public function myBrowserHasRememberedIdpForSp($idpName, $spName)
821+
{
822+
$this->setRememberedIdpsCookie($idpName, $spName, time() + 3600);
823+
}
824+
825+
/**
826+
* @Given /^my browser has remembered IdP "([^"]*)" for SP "([^"]*)" but that choice has expired$/
827+
*/
828+
public function myBrowserHasRememberedIdpForSpButItHasExpired($idpName, $spName)
829+
{
830+
$this->setRememberedIdpsCookie($idpName, $spName, time() - 3600);
831+
}
832+
833+
private function setRememberedIdpsCookie(string $idpName, string $spName, int $expires): void
834+
{
835+
$entries = [
836+
$this->mockSpRegistry->get($spName)->entityId() => [
837+
'idp' => $this->mockIdpRegistry->get($idpName)->entityId(),
838+
'expires' => $expires,
839+
],
840+
];
841+
842+
$cookie = new Cookie(
843+
RememberedIdpCookie::NAME,
844+
base64_encode(gzdeflate(json_encode($entries))),
845+
null,
846+
'/',
847+
parse_url($this->engineBlockDomain, PHP_URL_HOST),
848+
true
849+
);
850+
851+
$this->getMinkContext()->getSession()->getDriver()->getClient()->getCookieJar()->set($cookie);
852+
}
853+
729854
/**
730855
* @Given /^I have a locale cookie containing "([^"]*)"$/
731856
*/

‎src/OpenConext/EngineBlockFunctionalTestingBundle/Features/Context/MockSpContext.php‎

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -697,6 +697,19 @@ public function spIsConfiguredToDisplayOnlyConnectedIdps($spName)
697697
->save();
698698
}
699699

700+
/**
701+
* @Given /^SP "([^"]*)" allows remembering the WAYF choice$/
702+
* @param string $spName
703+
*/
704+
public function spAllowsRememberingTheWayfChoice($spName)
705+
{
706+
$sp = $this->anUnregisteredServiceProviderNamed($spName);
707+
708+
$this->serviceRegistryFixture
709+
->allowWayfRememberChoiceForSp($sp->entityId())
710+
->save();
711+
}
712+
700713
/**
701714
* @Given /^SP "([^"]*)" scopes its request to IDP "([^"]*)"$/
702715
*/
Lines changed: 91 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,91 @@
1+
Feature: Per-SP remembered WAYF choice
2+
In order to not have to pick my IdP every time I log in to the same SP
3+
As a user
4+
I want EngineBlock to remember my previous IdP choice for that SP
5+
6+
# This is the per-SP remember-choice feature (feature_enable_wayf_remember_choice_per_idp),
7+
# not the older, session-based "wayf.remember_choice" feature: the two are mutually
8+
# exclusive and cannot both be enabled.
9+
#
10+
# The choice is stored in the "rememberedidps" cookie, so it must survive the loss of the
11+
# EngineBlock session. The second login in every scenario therefore starts without an
12+
# existing EB session: "I start a new browser session" clears only the PHP session
13+
# cookie and leaves "rememberedidps" in place. Without that step the legacy session
14+
# based mechanism could skip the WAYF and the scenario would prove nothing.
15+
16+
Background:
17+
Given an EngineBlock instance on "dev.openconext.local"
18+
And no registered SPs
19+
And no registered Idps
20+
And an Identity Provider named "Dummy-IdP"
21+
And an Identity Provider named "Second-IdP"
22+
And a Service Provider named "Remembering-SP"
23+
And SP "Remembering-SP" allows remembering the WAYF choice
24+
And a Service Provider named "Non-Remembering-SP"
25+
26+
Scenario: Remembering a choice skips the WAYF on a subsequent login
27+
When I log in at "Remembering-SP"
28+
And I select "Dummy-IdP" on the WAYF and remember my choice
29+
And I pass through EngineBlock
30+
And I pass through the IdP
31+
Then the "rememberedidps" cookie should remember "Dummy-IdP" for SP "Remembering-SP"
32+
When I give my consent
33+
And I pass through EngineBlock
34+
Then the url should match "functional-testing/Remembering-SP/acs"
35+
When I start a new browser session
36+
And I log in at "Remembering-SP"
37+
And I pass through EngineBlock
38+
Then the url should not match "authentication/proxy/wayf"
39+
And the url should match "Dummy-IdP/sso"
40+
When I pass through the IdP
41+
And I pass through EngineBlock
42+
Then the url should match "functional-testing/Remembering-SP/acs"
43+
44+
Scenario: The remembered choice is scoped per SP
45+
Given a Service Provider named "Other-Remembering-SP"
46+
And SP "Other-Remembering-SP" allows remembering the WAYF choice
47+
And my browser has remembered IdP "Dummy-IdP" for SP "Remembering-SP"
48+
When I log in at "Other-Remembering-SP"
49+
Then I should see "Select an account to login to"
50+
When I log in at "Remembering-SP"
51+
And I pass through EngineBlock
52+
Then the url should match "Dummy-IdP/sso"
53+
54+
Scenario: An SP without the coin never gets the choice remembered
55+
When I log in at "Non-Remembering-SP"
56+
And I select "Dummy-IdP" on the WAYF and remember my choice
57+
And I pass through EngineBlock
58+
And I pass through the IdP
59+
Then the "rememberedidps" cookie should not be set
60+
When I give my consent
61+
And I pass through EngineBlock
62+
Then the url should match "functional-testing/Non-Remembering-SP/acs"
63+
When I start a new browser session
64+
And I log in at "Non-Remembering-SP"
65+
Then I should see "Select an account to login to"
66+
67+
Scenario: A remembered IdP that is no longer connected to the SP is ignored
68+
Given an Identity Provider named "Third-IdP"
69+
And my browser has remembered IdP "Dummy-IdP" for SP "Remembering-SP"
70+
And SP "Remembering-SP" is not connected to IdP "Dummy-IdP"
71+
When I log in at "Remembering-SP"
72+
Then I should see "Select an account to login to"
73+
74+
Scenario: An expired remembered choice is ignored
75+
Given my browser has remembered IdP "Dummy-IdP" for SP "Remembering-SP" but that choice has expired
76+
When I log in at "Remembering-SP"
77+
Then I should see "Select an account to login to"
78+
79+
Scenario: The reset endpoint clears a remembered choice
80+
Given my browser has remembered IdP "Dummy-IdP" for SP "Remembering-SP"
81+
When I go to Engineblock URL "/reset-remember-wayf"
82+
Then the "rememberedidps" cookie should not be set
83+
When I log in at "Remembering-SP"
84+
Then I should see "Select an account to login to"
85+
86+
Scenario: The cookie removal page lists and removes the remembered choice
87+
Given my browser has remembered IdP "Dummy-IdP" for SP "Remembering-SP"
88+
When I go to Engineblock URL "/authentication/idp/remove-cookies"
89+
Then the response should contain 'rememberedidps'
90+
When I press "remove_rememberedidps"
91+
Then the "rememberedidps" cookie should not be set

‎src/OpenConext/EngineBlockFunctionalTestingBundle/Fixtures/ServiceRegistryFixture.php‎

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -405,6 +405,13 @@ public function displayUnconnectedIdpsForSp($entityId, $displayUnconnected = tru
405405
return $this;
406406
}
407407

408+
public function allowWayfRememberChoiceForSp($entityId)
409+
{
410+
$this->setCoin($this->getServiceProvider($entityId), 'wayfRememberChoice', true);
411+
412+
return $this;
413+
}
414+
408415
public function disconnectSp($spEntityId, $idpEntityId)
409416
{
410417
$sp = $this->getServiceProvider($spEntityId);

0 commit comments

Comments
 (0)