2323use DOMDocument ;
2424use DOMElement ;
2525use DOMXPath ;
26+ use OpenConext \EngineBlock \Service \Wayf \RememberedIdpCookie ;
2627use OpenConext \EngineBlockBundle \Sbs \Msg ;
2728use OpenConext \EngineBlockFunctionalTestingBundle \Fixtures \DataStore \AbstractDataStore ;
2829use OpenConext \EngineBlockFunctionalTestingBundle \Fixtures \FunctionalTestingAttributeAggregationClient ;
3940use RuntimeException ;
4041use SAML2 \Constants ;
4142use 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 */
0 commit comments