@@ -490,8 +490,7 @@ public function authenticateWithIdentityProvider()
490490 {
491491 $ this ->minkContext ->assertPageAddress ('https://ssp.dev.openconext.local/simplesaml/module.php/core/loginuserpass ' );
492492
493- $ this ->minkContext ->fillField ('username ' , 'user-a1 ' );
494- $ this ->minkContext ->fillField ('password ' , 'user-a1 ' );
493+ $ this ->fillIdentityProviderLoginForm ('user-a1 ' );
495494
496495 $ this ->minkContext ->pressButton ('Login ' );
497496 $ this ->minkContext ->pressButton ('Yes, continue ' );
@@ -535,13 +534,28 @@ public function authenticateWithIdentityProviderFor($userName)
535534 {
536535 $ this ->minkContext ->assertPageAddress ('https://ssp.dev.openconext.local/simplesaml/module.php/core/loginuserpass ' );
537536
538- $ this ->minkContext ->fillField ('username ' , $ userName );
539- $ this ->minkContext ->fillField ('password ' , $ userName );
537+ $ this ->fillIdentityProviderLoginForm ($ userName );
540538
541539 $ this ->minkContext ->pressButton ('Login ' );
542540 $ this ->pressConsentIfShown ();
543541 }
544542
543+ /**
544+ * Fill the login form of the devssp IdP.
545+ *
546+ * Newer devssp images only ask for a username, older images also require a password that is equal to the username.
547+ * This function works with both.
548+ */
549+ private function fillIdentityProviderLoginForm (string $ userName ): void
550+ {
551+ $ this ->minkContext ->fillField ('username ' , $ userName );
552+
553+ // Only fill the password when that field is present
554+ if ($ this ->minkContext ->getSession ()->getPage ()->findField ('password ' ) !== null ) {
555+ $ this ->minkContext ->fillField ('password ' , $ userName );
556+ }
557+ }
558+
545559 /**
546560 * SimpleSAMLphp's consent module remembers a given SP+attribute-set combination for the
547561 * browser session, so a consent screen may or may not appear depending on what earlier
@@ -559,8 +573,7 @@ public function authenticateWithIdentityProviderForWithStepup($userName)
559573 {
560574 $ this ->minkContext ->assertPageAddress ('https://ssp.dev.openconext.local/simplesaml/module.php/core/loginuserpass ' );
561575
562- $ this ->minkContext ->fillField ('username ' , $ userName );
563- $ this ->minkContext ->fillField ('password ' , $ userName );
576+ $ this ->fillIdentityProviderLoginForm ($ userName );
564577
565578 $ this ->minkContext ->pressButton ('Login ' );
566579 $ this ->minkContext ->pressButton ('Yes, continue ' );
@@ -583,22 +596,16 @@ public function assertErrorAtServiceProvider()
583596 {
584597 switch ($ this ->activeSp ) {
585598 case 'second-sp ' :
586- $ this -> minkContext -> assertPageAddress ( '/simplesaml/module.php/debugsp/acs/second-sp ' ) ;
599+ $ oldAcsPath = '/simplesaml/module.php/debugsp/acs/second-sp ' ;
587600 break ;
588601 case 'default-sp ' :
589- $ this -> minkContext -> assertPageAddress ( '/simplesaml/module.php/saml/sp/saml2-acs.php/default-sp ' ) ;
602+ $ oldAcsPath = '/simplesaml/module.php/saml/sp/saml2-acs.php/default-sp ' ;
590603 break ;
591604 default :
592605 throw new Exception ($ this ->activeSp . ' is not supported ' );
593606 }
594607
595- $ this ->minkContext ->assertPageContainsText (
596- sprintf ('Unhandled exception ' )
597- );
598-
599- $ this ->minkContext ->assertPageNotContainsText (
600- sprintf ('You are logged in to SP ' )
601- );
608+ $ this ->assertSamlErrorResponseAtServiceProvider ($ oldAcsPath );
602609 }
603610
604611 /**
@@ -608,36 +615,85 @@ public function assertAdfsErrorAtServiceProvider()
608615 {
609616 switch ($ this ->activeSp ) {
610617 case 'second-sp ' :
611- $ this ->minkContext ->assertPageAddress ('/simplesaml/module.php/debugsp/acs/second-sp ' );
612- break ;
613618 case 'default-sp ' :
614- $ this -> minkContext -> assertPageAddress ( '/simplesaml/module.php/debugsp/acs/default-sp ' ) ;
619+ $ acsPath = '/simplesaml/module.php/debugsp/acs/ ' . $ this -> activeSp ;
615620 break ;
616621 default :
617622 throw new Exception ($ this ->activeSp . ' is not supported ' );
618623 }
619624
620- $ this ->minkContext ->assertPageContainsText (
621- sprintf ('Unhandled exception ' )
622- );
625+ $ this ->assertSamlErrorResponseAtServiceProvider ($ acsPath );
623626
624- $ currentUrl = $ this ->minkContext ->getSession ()->getCurrentUrl ();
625627 // With behat we can not verify if the Context and AuthMethod are present in the POST request data, but we can
626- // check if Gateway added back the original query string
628+ // check if Gateway added back the original query string to the URL of the ACS.
629+ // The newer devssp redirects from the ACS to the Test SP (sp.php), so look up the ACS request in the history.
627630 $ expectedAdfsParams = '?SAMLRequest=request_that_must_be_kept&Context=context_value_that_must_be_kept ' ;
631+ $ acsUrl = $ this ->findVisitedUrlContaining ($ acsPath );
628632 // The original parameters sent by the ADFS client should still be on the Url
629- if (strpos ($ currentUrl , $ expectedAdfsParams ) === false ) {
633+ if ($ acsUrl === null || strpos ($ acsUrl , $ expectedAdfsParams ) === false ) {
630634 throw new Exception (
631635 sprintf (
632- 'The original Adfs parameters are not on the error page (expected: "%s") ' ,
633- $ expectedAdfsParams
636+ 'The original Adfs parameters are not on the ACS URL (expected: "%s", ACS URL: "%s") ' ,
637+ $ expectedAdfsParams ,
638+ $ acsUrl ?? 'not visited '
634639 )
635640 );
636641 }
642+ }
643+
644+ /**
645+ * Assert that the SP received a SAML error response.
646+ *
647+ * Older devssp images show an "Unhandled exception" page at the ACS location ($oldAcsPath).
648+ * Newer devssp images redirect from the ACS to the Test SP (sp.php), which shows the SAML error status.
649+ * Both are accepted so the tests work with both the old and the new devssp image.
650+ */
651+ private function assertSamlErrorResponseAtServiceProvider (string $ oldAcsPath ): void
652+ {
653+ $ currentPath = parse_url ($ this ->minkContext ->getSession ()->getCurrentUrl (), PHP_URL_PATH );
654+
655+ if ($ currentPath === '/simplesaml/sp.php ' ) {
656+ $ this ->minkContext ->assertPageContainsText ('Last login failed ' );
657+ $ this ->minkContext ->assertPageContainsText ('SAML Error Status ' );
658+ return ;
659+ }
660+
661+ $ this ->minkContext ->assertPageAddress ($ oldAcsPath );
662+ $ this ->minkContext ->assertPageContainsText ('Unhandled exception ' );
663+ $ this ->minkContext ->assertPageNotContainsText ('You are logged in to SP ' );
664+ }
665+
666+ /**
667+ * Find the most recent URL in the browser history (including followed redirects) that contains $needle.
668+ * The history position is restored afterwards.
669+ */
670+ private function findVisitedUrlContaining (string $ needle ): ?string
671+ {
672+ $ history = $ this ->minkContext ->getSession ()->getDriver ()->getClient ()->getHistory ();
673+ if ($ history ->isEmpty ()) {
674+ return null ;
675+ }
676+
677+ $ found = null ;
678+ $ request = $ history ->current ();
679+ $ steps = 0 ;
680+ while (true ) {
681+ if (strpos ($ request ->getUri (), $ needle ) !== false ) {
682+ $ found = $ request ->getUri ();
683+ break ;
684+ }
685+ try {
686+ $ request = $ history ->back ();
687+ $ steps ++;
688+ } catch (\LogicException $ e ) {
689+ break ;
690+ }
691+ }
692+ for (; $ steps > 0 ; $ steps --) {
693+ $ history ->forward ();
694+ }
637695
638- $ this ->minkContext ->assertPageNotContainsText (
639- sprintf ('You are logged in to SP ' )
640- );
696+ return $ found ;
641697 }
642698
643699 /**
0 commit comments