prevent cast class exception if a user starts a SAML 1 flow, leaves in the middle...
[java-idp.git] / src / main / java / edu / internet2 / middleware / shibboleth / idp / profile / saml1 / ShibbolethSSOProfileHandler.java
index a0799e9..7ceba20 100644 (file)
@@ -59,6 +59,7 @@ import edu.internet2.middleware.shibboleth.common.relyingparty.provider.SAMLMDRe
 import edu.internet2.middleware.shibboleth.common.relyingparty.provider.saml1.ShibbolethSSOConfiguration;
 import edu.internet2.middleware.shibboleth.common.util.HttpHelper;
 import edu.internet2.middleware.shibboleth.idp.authn.ShibbolethSSOLoginContext;
+import edu.internet2.middleware.shibboleth.idp.authn.LoginContext;
 import edu.internet2.middleware.shibboleth.idp.util.HttpServletHelper;
 
 /** Shibboleth SSO request profile handler. */
@@ -117,19 +118,19 @@ public class ShibbolethSSOProfileHandler extends AbstractSAML1ProfileHandler {
         HttpServletResponse httpResponse = ((HttpServletResponseAdapter) outTransport).getWrappedResponse();
         ServletContext servletContext = httpRequest.getSession().getServletContext();
 
-        ShibbolethSSOLoginContext loginContext = (ShibbolethSSOLoginContext) HttpServletHelper.getLoginContext(
+       LoginContext loginContext = HttpServletHelper.getLoginContext(
                 getStorageService(), servletContext, httpRequest);
 
-        if (loginContext == null) {
+        if (loginContext == null || !(loginContext instanceof ShibbolethSSOLoginContext)) {
             log.debug("Incoming request does not contain a login context, processing as first leg of request");
             performAuthentication(inTransport, outTransport);
-        }else if(loginContext.isPrincipalAuthenticated() || loginContext.getAuthenticationFailure() != null){
+        } else if (loginContext.isPrincipalAuthenticated() || loginContext.getAuthenticationFailure() != null) {
             log.debug("Incoming request contains a login context, processing as second leg of request");
             HttpServletHelper.unbindLoginContext(getStorageService(), servletContext, httpRequest, httpResponse);
-            completeAuthenticationRequest(loginContext, inTransport, outTransport);
-        }else {
+            completeAuthenticationRequest((ShibbolethSSOLoginContext)loginContext, inTransport, outTransport);
+        } else {
             log.debug("Incoming request contained a login context but principal was not authenticated, processing as first leg of request");
-            performAuthentication(inTransport, outTransport);            
+            performAuthentication(inTransport, outTransport);
         }
     }
 
@@ -167,9 +168,8 @@ public class ShibbolethSSOProfileHandler extends AbstractSAML1ProfileHandler {
                 .getServletContext(), httpRequest, httpResponse);
 
         try {
-            URLBuilder urlBuilder = HttpServletHelper.getServletContextUrl(httpRequest);
-            urlBuilder.setPath(urlBuilder.getPath() + authenticationManagerPath);
-            String authnEngineUrl = urlBuilder.buildURL();
+            String authnEngineUrl = HttpServletHelper.getContextRelativeUrl(httpRequest, authenticationManagerPath)
+                    .buildURL();
             log.debug("Redirecting user to authentication engine at {}", authnEngineUrl);
             httpResponse.sendRedirect(authnEngineUrl);
         } catch (IOException e) {
@@ -214,8 +214,8 @@ public class ShibbolethSSOProfileHandler extends AbstractSAML1ProfileHandler {
         requestContext.setMessageDecoder(decoder);
         try {
             decoder.decode(requestContext);
-            log.debug("Decoded Shibboleth SSO request from relying party '{}'", requestContext
-                    .getInboundMessageIssuer());
+            log.debug("Decoded Shibboleth SSO request from relying party '{}'",
+                    requestContext.getInboundMessageIssuer());
         } catch (MessageDecodingException e) {
             String msg = "Error decoding Shibboleth SSO request";
             log.warn(msg, e);