@@ -188,11 +188,15 @@ public function onKernelResponse(FilterResponseEvent $event)
188188 if ($ this ->stateHandler ->can (SamlStateHandler::TRANSITION_SSO_RESPOND )) {
189189 $ event ->setResponse ($ this ->continueSingleSignOn ());
190190 return ;
191+ }else {
192+ $ this ->logger ->debug ("Cannot TRANSITION_SSO_RESPOND, state is " . $ this ->stateHandler ->get ()->getState ());
191193 }
192194
193- if ($ this ->stateHandler ->can (SamlStateHandler::TRANSITION_SLS_RESPOND )) {
195+ if ($ this ->stateHandler ->can (SamlStateHandler::TRANSITION_SLS_RESPOND , false )) {
194196 $ event ->setResponse ($ this ->continueSingleLogoutService ());
195197 return ;
198+ }else {
199+ $ this ->logger ->debug ("Cannot TRANSITION_SLS_RESPOND, state is " . $ this ->stateHandler ->get ()->getState ());
196200 }
197201 }
198202
@@ -206,6 +210,10 @@ public function onAuthenticationSuccess(CoreAuthenticationEvent $event)
206210 return ;
207211 }
208212
213+ if ($ this ->stateHandler ->has ()){
214+ $ this ->stateHandler ->get ()->resetLoginRetryCount ();
215+ }
216+
209217 $ user = $ event ->getAuthenticationToken ()->getUser ();
210218 if ($ this ->stateHandler ->has ()
211219 && $ user instanceof UserInterface && $ this ->stateHandler ->has ()) {
@@ -233,6 +241,19 @@ public function onAuthenticationFailure(CoreAuthenticationFailureEvent $event)
233241 return ;
234242 }
235243
244+ if ($ this ->stateHandler ->has ()){
245+ /** @var \SAML2_AuthnRequest $authRequest */
246+ $ authRequest = $ this ->stateHandler ->get ()->getRequest ();
247+
248+ $ sp = $ this ->getServiceProvider ($ authRequest ->getIssuer ());
249+
250+ if ($ this ->stateHandler ->get ()->getLoginRetryCount () < $ sp ->getMaxRetryLogin ()){
251+ $ this ->stateHandler ->get ()->incrementLoginRetryCount ();
252+ $ this ->logger ->debug ("Login failed, retrying " );
253+ return ;
254+ }
255+ }
256+
236257 $ this ->logger ->notice ("Authentication failed " );
237258 $ this ->stateHandler ->apply (SamlStateHandler::TRANSITION_SSO_AUTHENTICATE_FAIL );
238259 }
@@ -247,10 +268,7 @@ public function onLogoutSuccess(LogoutEvent $event)
247268 $ this ->stateHandler ->resume (true );
248269 $ this ->stateHandler ->get ()->setOriginalLogoutResponse ($ event ->getResponse ());
249270
250- $ this ->stateHandler
251- ->apply (SamlStateHandler::TRANSITION_SLS_START )
252- ->apply (SamlStateHandler::TRANSITION_SLS_START_DISPATCH )
253- ->apply (SamlStateHandler::TRANSITION_SLS_END_DISPATCH );
271+ $ this ->stateHandler ->apply (SamlStateHandler::TRANSITION_SLS_START_BY_IDP );
254272
255273 return ;
256274 }
@@ -291,7 +309,7 @@ public function processSingleSignOn(Request $httpRequest)
291309 $ authRequest = $ inputBinding ->receiveSignedAuthnRequest ($ httpRequest );
292310 }
293311
294- $ this ->validateRequest ($ authRequest );
312+ $ this ->validateMessage ($ authRequest );
295313
296314 $ event = new ReceiveAuthnRequestEvent ($ authRequest , $ this ->identityProvider , $ this ->stateHandler );
297315 $ this ->eventDispatcher ->dispatch (Saml2Events::SSO_AUTHN_RECEIVE_REQUEST , $ event );
@@ -386,12 +404,7 @@ public function processSingleLogoutService(Request $httpRequest)
386404 $ inputBinding = $ this ->bindingContainer ->get ($ this ->identityProvider ->getSlsBinding ());
387405
388406 try {
389- $ logoutMessage = $ inputBinding ->receiveUnsignedLogoutRequest ($ httpRequest );
390- $ sp = $ this ->getServiceProvider ($ logoutMessage ->getIssuer ());
391- if ($ sp ->wantSignedLogoutRequest ()) {
392- $ logoutMessage = $ inputBinding ->receiveSignedLogoutRequest ($ httpRequest );
393- }
394- $ this ->validateRequest ($ logoutMessage );
407+ $ logoutMessage = $ inputBinding ->receiveUnsignedMessage ($ httpRequest );
395408 } catch (\Throwable $ e ) {
396409 // handle error, apparently the request cannot be processed :(
397410 $ msg = sprintf ('Could not process Request, error: "%s" ' , $ e ->getMessage ());
@@ -401,6 +414,12 @@ public function processSingleLogoutService(Request $httpRequest)
401414 }
402415
403416 if ($ logoutMessage instanceof \SAML2_LogoutRequest) {
417+ $ sp = $ this ->getServiceProvider ($ logoutMessage ->getIssuer ());
418+ if ($ sp ->wantSignedLogoutRequest ()) {
419+ $ logoutMessage = $ inputBinding ->receiveSignedLogoutRequest ($ httpRequest );
420+ }
421+ $ this ->validateMessage ($ logoutMessage );
422+
404423 $ this ->logger ->notice ('Received LogoutRequest, started processing ' );
405424
406425 $ this ->stateHandler ->resume (true )->apply (SamlStateHandler::TRANSITION_SLS_START );
@@ -414,6 +433,12 @@ public function processSingleLogoutService(Request $httpRequest)
414433 }
415434
416435 if ($ logoutMessage instanceof \SAML2_LogoutResponse) {
436+ $ sp = $ this ->getServiceProvider ($ logoutMessage ->getIssuer ());
437+ if ($ sp ->wantSignedLogoutResponse ()) {
438+ $ logoutMessage = $ inputBinding ->receiveSignedLogoutResponse ($ httpRequest );
439+ }
440+ $ this ->validateMessage ($ logoutMessage );
441+
417442 $ this ->logger ->notice ('Received LogoutResponse, continue processing ' );
418443 $ this ->stateHandler ->apply (SamlStateHandler::TRANSITION_SLS_END_PROPAGATE );
419444
@@ -455,6 +480,8 @@ public function continueSingleLogoutService()
455480
456481 $ outBinding = $ this ->bindingContainer ->get ($ sp ->getSingleLogoutBinding ());
457482
483+ $ this ->logger ->notice (sprintf ('Propagate logout to sp %s ' ,$ sp ->getSingleLogoutUrl ()));
484+
458485 if ($ sp ->wantSignedLogoutRequest ()) {
459486 $ response = $ outBinding ->getSignedRequest ($ logoutRequest );
460487 } else {
@@ -474,6 +501,8 @@ public function continueSingleLogoutService()
474501 $ sp = $ this ->getServiceProvider ($ logoutRequest ->getIssuer ());
475502 $ outBinding = $ this ->bindingContainer ->get ($ sp ->getSingleLogoutBinding ());
476503
504+ $ this ->logger ->notice (sprintf ('Logout: Respond to sp initiator %s ' ,$ sp ->getEntityId ()));
505+
477506 if ($ sp ->wantSignedLogoutResponse ()) {
478507 $ response = $ outBinding ->getSignedResponse ($ logoutResponse );
479508 } else {
@@ -498,6 +527,8 @@ public function continueSingleLogoutService()
498527
499528 $ this ->stateHandler ->resume ();
500529
530+ $ this ->logger ->notice ('Saml: Logout terminated ' );
531+
501532 return $ response ;
502533 }
503534
@@ -682,19 +713,15 @@ protected function getIdentityProviderXmlPublicKey()
682713 }
683714
684715 /**
685- * @param \SAML2_Request $request
716+ * @param \SAML2_Message $message
686717 */
687- protected function validateRequest ( \ SAML2_Request $ request )
718+ protected function validateMessage ( \ SAML2_Message $ message )
688719 {
689- if (!$ this ->serviceProviderRepository ->hasServiceProvider ($ request ->getIssuer ())) {
690- throw new UnknownServiceProviderException ($ request ->getIssuer ());
691- }
692-
693- if (!$ this ->identityProvider ->wantSignedAuthnRequest ()) {
694- return ;
720+ if (!$ this ->serviceProviderRepository ->hasServiceProvider ($ message ->getIssuer ())) {
721+ throw new UnknownServiceProviderException ($ message ->getIssuer ());
695722 }
696723
697- $ serviceProvider = $ this ->getServiceProvider ($ request ->getIssuer ());
724+ $ serviceProvider = $ this ->getServiceProvider ($ message ->getIssuer ());
698725
699726 $ this ->logger ->debug (sprintf ('Extracting public keys for ServiceProvider "%s" ' , $ serviceProvider ->getEntityId ()));
700727
@@ -715,7 +742,7 @@ protected function validateRequest(\SAML2_Request $request)
715742 $ key = new \XMLSecurityKey (\XMLSecurityKey::RSA_SHA256 , array ('type ' => 'public ' ));
716743 $ key ->loadKey ($ x509Key ->getCertificate ());
717744
718- $ request ->validate ($ key );
745+ $ message ->validate ($ key );
719746 }
720747 }
721748}
0 commit comments