1010import static org .junit .Assert .assertEquals ;
1111import static org .junit .Assert .assertFalse ;
1212import static org .junit .Assert .assertNull ;
13+ import static org .junit .Assert .assertNotNull ;
1314import static org .junit .Assert .assertThat ;
1415import static org .junit .Assert .assertTrue ;
1516import static org .mockito .Matchers .matches ;
1819import static org .mockito .Mockito .verify ;
1920import static org .mockito .Mockito .when ;
2021
21- import java .io .File ;
2222import java .io .FileInputStream ;
2323import java .io .FileNotFoundException ;
2424import java .io .IOException ;
25- import java .io .InputStream ;
2625import java .io .UnsupportedEncodingException ;
2726import java .net .URI ;
2827import java .net .URISyntaxException ;
29- import java .net .URLDecoder ;
30- import java .security .Key ;
3128import java .security .KeyStore ;
3229import java .security .KeyStoreException ;
3330import java .security .NoSuchAlgorithmException ;
34- import java .security .PrivateKey ;
35- import java .security .PublicKey ;
3631import java .security .UnrecoverableKeyException ;
37- import java .security .cert .Certificate ;
3832import java .security .cert .CertificateException ;
3933import java .util .ArrayList ;
40- import java .util .Base64 ;
4134import java .util .HashMap ;
4235import java .util .List ;
4336import java .util .Map ;
5245import org .junit .rules .ExpectedException ;
5346
5447import com .onelogin .saml2 .Auth ;
55- import com .onelogin .saml2 .authn .SamlResponse ;
5648import com .onelogin .saml2 .exception .Error ;
5749import com .onelogin .saml2 .exception .ValidationError ;
5850import com .onelogin .saml2 .exception .SettingsException ;
@@ -642,7 +634,7 @@ public void testProcessSLORequestKeepSession() throws Exception {
642634 when (request .getSession ()).thenReturn (session );
643635
644636 String samlRequestEncoded = Util .getFileAsString ("data/logout_requests/logout_request_deflated.xml.base64" );
645- when (request .getParameterMap ()).thenReturn (singletonMap ("SAMLRequest" , new String []{samlRequestEncoded }));
637+ when (request .getParameterMap ()).thenReturn (singletonMap ("SAMLRequest" , new String []{samlRequestEncoded }));
646638
647639 Saml2Settings settings = new SettingsBuilder ().fromFile ("config/config.min.properties" ).build ();
648640 Auth auth = new Auth (settings , request , response );
@@ -671,7 +663,7 @@ public void testProcessSLORequestRemoveSession() throws Exception {
671663 when (request .getSession ()).thenReturn (session );
672664
673665 String samlRequestEncoded = Util .getFileAsString ("data/logout_requests/logout_request_deflated.xml.base64" );
674- when (request .getParameterMap ()).thenReturn (singletonMap ("SAMLRequest" , new String []{samlRequestEncoded }));
666+ when (request .getParameterMap ()).thenReturn (singletonMap ("SAMLRequest" , new String []{samlRequestEncoded }));
675667 Saml2Settings settings = new SettingsBuilder ().fromFile ("config/config.min.properties" ).build ();
676668 Auth auth = new Auth (settings , request , response );
677669 assertFalse (auth .isAuthenticated ());
@@ -682,6 +674,93 @@ public void testProcessSLORequestRemoveSession() throws Exception {
682674 assertTrue (auth .getErrors ().isEmpty ());
683675 }
684676
677+ /**
678+ * Tests the processSLO methods of Auth
679+ * Case: process LogoutRequest, remove session, no stay
680+ *
681+ * @throws Exception
682+ *
683+ * @see com.onelogin.saml2.Auth#processSLO
684+ */
685+ @ Test
686+ public void testProcessSLORequestStay () throws Exception {
687+ HttpServletRequest request = mock (HttpServletRequest .class );
688+ HttpServletResponse response = mock (HttpServletResponse .class );
689+ HttpSession session = mock (HttpSession .class );
690+ when (request .getRequestURL ()).thenReturn (new StringBuffer ("http://stuff.com/endpoints/endpoints/sls.php" ));
691+ when (request .getSession ()).thenReturn (session );
692+
693+ String samlRequestEncoded = Util .getFileAsString ("data/logout_requests/logout_request_deflated.xml.base64" );
694+ when (request .getParameterMap ()).thenReturn (singletonMap ("SAMLRequest" , new String []{samlRequestEncoded }));
695+ Saml2Settings settings = new SettingsBuilder ().fromFile ("config/config.min.properties" ).build ();
696+ Auth auth = new Auth (settings , request , response );
697+ assertFalse (auth .isAuthenticated ());
698+ assertTrue (auth .getErrors ().isEmpty ());
699+ auth .processSLO (false , null );
700+ verify (response ).sendRedirect (matches ("http:\\ /\\ /idp.example.com\\ /simplesaml\\ /saml2\\ /idp\\ /SingleLogoutService.php\\ ?SAMLResponse=(.)*" ));
701+ verify (session , times (1 )).invalidate ();
702+ assertTrue (auth .getErrors ().isEmpty ());
703+ }
704+
705+ /**
706+ * Tests the processSLO methods of Auth
707+ * Case: process LogoutRequest, remove session, stay = false
708+ *
709+ * @throws Exception
710+ *
711+ * @see com.onelogin.saml2.Auth#processSLO
712+ */
713+ @ Test
714+ public void testProcessSLORequestStayFalse () throws Exception {
715+ HttpServletRequest request = mock (HttpServletRequest .class );
716+ HttpServletResponse response = mock (HttpServletResponse .class );
717+ HttpSession session = mock (HttpSession .class );
718+ when (request .getRequestURL ()).thenReturn (new StringBuffer ("http://stuff.com/endpoints/endpoints/sls.php" ));
719+ when (request .getSession ()).thenReturn (session );
720+
721+ String samlRequestEncoded = Util .getFileAsString ("data/logout_requests/logout_request_deflated.xml.base64" );
722+ when (request .getParameterMap ()).thenReturn (singletonMap ("SAMLRequest" , new String []{samlRequestEncoded }));
723+ Saml2Settings settings = new SettingsBuilder ().fromFile ("config/config.min.properties" ).build ();
724+ Auth auth = new Auth (settings , request , response );
725+ assertFalse (auth .isAuthenticated ());
726+ assertTrue (auth .getErrors ().isEmpty ());
727+ String target = auth .processSLO (false , null , false );
728+ verify (response ).sendRedirect (matches ("http:\\ /\\ /idp.example.com\\ /simplesaml\\ /saml2\\ /idp\\ /SingleLogoutService.php\\ ?SAMLResponse=(.)*" ));
729+ verify (response , times (1 )).sendRedirect (matches ("http:\\ /\\ /idp.example.com\\ /simplesaml\\ /saml2\\ /idp\\ /SingleLogoutService.php\\ ?SAMLResponse=(.)*" ));
730+ verify (session , times (1 )).invalidate ();
731+ assertTrue (auth .getErrors ().isEmpty ());
732+ assertThat (target , startsWith ("http://idp.example.com/simplesaml/saml2/idp/SingleLogoutService.php?SAMLResponse=" ));
733+ }
734+
735+ /**
736+ * Tests the processSLO methods of Auth
737+ * Case: process LogoutRequest, remove session, stay = true
738+ *
739+ * @throws Exception
740+ *
741+ * @see com.onelogin.saml2.Auth#processSLO
742+ */
743+ @ Test
744+ public void testProcessSLORequestStayTrue () throws Exception {
745+ HttpServletRequest request = mock (HttpServletRequest .class );
746+ HttpServletResponse response = mock (HttpServletResponse .class );
747+ HttpSession session = mock (HttpSession .class );
748+ when (request .getRequestURL ()).thenReturn (new StringBuffer ("http://stuff.com/endpoints/endpoints/sls.php" ));
749+ when (request .getSession ()).thenReturn (session );
750+
751+ String samlRequestEncoded = Util .getFileAsString ("data/logout_requests/logout_request_deflated.xml.base64" );
752+ when (request .getParameterMap ()).thenReturn (singletonMap ("SAMLRequest" , new String []{samlRequestEncoded }));
753+ Saml2Settings settings = new SettingsBuilder ().fromFile ("config/config.min.properties" ).build ();
754+ Auth auth = new Auth (settings , request , response );
755+ assertFalse (auth .isAuthenticated ());
756+ assertTrue (auth .getErrors ().isEmpty ());
757+ String target = auth .processSLO (false , null , true );
758+ verify (response , times (0 )).sendRedirect (matches ("http:\\ /\\ /idp.example.com\\ /simplesaml\\ /saml2\\ /idp\\ /SingleLogoutService.php\\ ?SAMLResponse=(.)*" ));
759+ verify (session , times (1 )).invalidate ();
760+ assertTrue (auth .getErrors ().isEmpty ());
761+ assertThat (target , startsWith ("http://idp.example.com/simplesaml/saml2/idp/SingleLogoutService.php?SAMLResponse=" ));
762+ }
763+
685764 /**
686765 * Tests the processSLO methods of Auth
687766 * Case: process LogoutRequest, with RelayState and sign response
0 commit comments