Clean up role descriptor discovery code some more, better error handling
authorlajoie <lajoie@ab3bd59b-922f-494d-bb5f-6f0a3c29deca>
Thu, 21 Jun 2007 11:44:24 +0000 (11:44 +0000)
committerlajoie <lajoie@ab3bd59b-922f-494d-bb5f-6f0a3c29deca>
Thu, 21 Jun 2007 11:44:24 +0000 (11:44 +0000)
git-svn-id: https://subversion.switch.ch/svn/shibboleth/java-idp/trunk@2271 ab3bd59b-922f-494d-bb5f-6f0a3c29deca

src/edu/internet2/middleware/shibboleth/idp/ShibbolethConstants.java [new file with mode: 0644]
src/edu/internet2/middleware/shibboleth/idp/profile/saml1/AttributeQueryProfileHandler.java
src/edu/internet2/middleware/shibboleth/idp/profile/saml1/ShibbolethSSOProfileHandler.java

diff --git a/src/edu/internet2/middleware/shibboleth/idp/ShibbolethConstants.java b/src/edu/internet2/middleware/shibboleth/idp/ShibbolethConstants.java
new file mode 100644 (file)
index 0000000..07865c4
--- /dev/null
@@ -0,0 +1,29 @@
+/*
+ * Copyright [2007] [University Corporation for Advanced Internet Development, Inc.]
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package edu.internet2.middleware.shibboleth.idp;
+
+import org.opensaml.common.xml.SAMLConstants;
+
+/**
+ * Constants related used in the SAML specifications.
+ */
+public class ShibbolethConstants extends SAMLConstants {
+
+    /** URI for the Shibboleth SSO profile. */
+    public static final String SHIB_SSO_PROFILE_URI = "urn:mace:shibboleth:1.0";
+    
+}
\ No newline at end of file
index 4873913..3f68042 100644 (file)
@@ -22,6 +22,7 @@ import javax.servlet.ServletRequest;
 import javax.servlet.ServletResponse;
 
 import org.apache.log4j.Logger;
+import org.opensaml.common.SAMLVersion;
 import org.opensaml.common.binding.BindingException;
 import org.opensaml.common.binding.decoding.MessageDecoder;
 import org.opensaml.common.binding.encoding.MessageEncoder;
@@ -33,6 +34,7 @@ import org.opensaml.saml1.core.AttributeQuery;
 import org.opensaml.saml1.core.Response;
 import org.opensaml.saml1.core.Statement;
 import org.opensaml.saml1.core.StatusCode;
+import org.opensaml.saml2.metadata.RoleDescriptor;
 import org.opensaml.saml2.metadata.provider.MetadataProviderException;
 import org.opensaml.ws.security.SecurityPolicyException;
 
@@ -41,6 +43,9 @@ import edu.internet2.middleware.shibboleth.common.profile.ProfileRequest;
 import edu.internet2.middleware.shibboleth.common.profile.ProfileResponse;
 import edu.internet2.middleware.shibboleth.common.relyingparty.RelyingPartyConfiguration;
 import edu.internet2.middleware.shibboleth.common.relyingparty.provider.saml1.AttributeQueryConfiguration;
+import edu.internet2.middleware.shibboleth.common.relyingparty.provider.saml1.ShibbolethSSOConfiguration;
+import edu.internet2.middleware.shibboleth.idp.ShibbolethConstants;
+import edu.internet2.middleware.shibboleth.idp.profile.saml1.ShibbolethSSOProfileHandler.ShibbolethSSORequestContext;
 
 /**
  * SAML 1 Attribute Query profile handler.
@@ -83,7 +88,7 @@ public class AttributeQueryProfileHandler extends AbstractSAML1ProfileHandler {
         } catch (ProfileException e) {
             samlResponse = buildErrorResponse(requestContext);
         }
-        
+
         requestContext.setSamlResponse(samlResponse);
         encodeResponse(requestContext);
     }
@@ -130,35 +135,87 @@ public class AttributeQueryProfileHandler extends AbstractSAML1ProfileHandler {
             SAMLSecurityPolicy securityPolicy = requestContext.getMessageDecoder().getSecurityPolicy();
             requestContext.setRelyingPartyId(securityPolicy.getIssuer());
 
-            try {
-                requestContext.setRelyingPartyMetadata(getMetadataProvider().getEntityDescriptor(
-                        requestContext.getRelyingPartyId()));
+            AttributeQuery attributeQuery = (AttributeQuery) requestContext.getMessageDecoder().getSAMLMessage();
+            requestContext.setSamlRequest(attributeQuery);
+
+            populateRelyingPartyData(requestContext);
+
+            populateAssertingPartyData(requestContext);
+        }
+    }
+
+    /**
+     * Populates the relying party entity and role metadata and relying party configuration data.
+     * 
+     * @param requestContext current request context with relying party ID populated
+     * 
+     * @throws ProfileException thrown if metadata can not be located for the relying party
+     */
+    protected void populateRelyingPartyData(AttributeQueryContext requestContext) throws ProfileException {
+        try {
+            requestContext.setRelyingPartyMetadata(getMetadataProvider().getEntityDescriptor(
+                    requestContext.getRelyingPartyId()));
+
+            RoleDescriptor relyingPartyRole = requestContext.getRelyingPartyMetadata().getSPSSODescriptor(
+                    SAMLConstants.SAML10P_NS);
+
+            if (relyingPartyRole == null) {
+                relyingPartyRole = requestContext.getRelyingPartyMetadata()
+                        .getSPSSODescriptor(SAMLConstants.SAML10P_NS);
+                if (relyingPartyRole == null) {
+                    throw new MetadataProviderException("Unable to locate SPSSO role descriptor for entity "
+                            + requestContext.getRelyingPartyId());
+                }
+            }
+            requestContext.setRelyingPartyRoleMetadata(relyingPartyRole);
 
-                //TODO determine protocol by message version
-                requestContext.setRelyingPartyRoleMetadata(requestContext.getRelyingPartyMetadata().getSPSSODescriptor(
-                        SAMLConstants.SAML10P_NS));
+            RelyingPartyConfiguration rpConfig = getRelyingPartyConfiguration(requestContext.getRelyingPartyId());
+            requestContext.setRelyingPartyConfiguration(rpConfig);
 
-                RelyingPartyConfiguration rpConfig = getRelyingPartyConfiguration(requestContext.getRelyingPartyId());
-                requestContext.setRelyingPartyConfiguration(rpConfig);
+            requestContext.setProfileConfiguration((AttributeQueryConfiguration) rpConfig
+                    .getProfileConfiguration(AttributeQueryConfiguration.PROFILE_ID));
 
-                requestContext.setAssertingPartyId(requestContext.getRelyingPartyConfiguration().getProviderId());
+        } catch (MetadataProviderException e) {
+            log.error("Unable to locate metadata for relying party " + requestContext.getRelyingPartyId());
+            requestContext.setFailureStatus(buildStatus(StatusCode.RESPONDER, null,
+                    "Unable to locate metadata for relying party " + requestContext.getRelyingPartyId()));
+            throw new ProfileException("Unable to locate metadata for relying party "
+                    + requestContext.getRelyingPartyId());
+        }
+    }
 
-                requestContext.setAssertingPartyMetadata(getMetadataProvider().getEntityDescriptor(
-                        requestContext.getAssertingPartyId()));
+    /**
+     * Populates the asserting party entity and role metadata.
+     * 
+     * @param requestContext current request context with relying party configuration populated
+     * 
+     * @throws ProfileException thrown if metadata can not be located for the asserting party
+     */
+    protected void populateAssertingPartyData(AttributeQueryContext requestContext) throws ProfileException {
+        String assertingPartyId = requestContext.getRelyingPartyConfiguration().getProviderId();
+
+        try {
+            requestContext.setAssertingPartyId(assertingPartyId);
 
-                requestContext.setAssertingPartyRoleMetadata(requestContext.getAssertingPartyMetadata()
-                        .getAttributeAuthorityDescriptor(SAMLConstants.SAML10P_NS));
+            requestContext.setAssertingPartyMetadata(getMetadataProvider().getEntityDescriptor(assertingPartyId));
 
-                requestContext.setProfileConfiguration((AttributeQueryConfiguration) rpConfig
-                        .getProfileConfiguration(AttributeQueryConfiguration.PROFILE_ID));
+            RoleDescriptor assertingPartyRole = requestContext.getAssertingPartyMetadata()
+                    .getAttributeAuthorityDescriptor(SAMLConstants.SAML11P_NS);
 
-                requestContext.setSamlRequest((AttributeQuery) requestContext.getMessageDecoder().getSAMLMessage());
-            } catch (MetadataProviderException e) {
-                log.error("Unable to locate metadata for asserting or relying party");
-                requestContext
-                        .setFailureStatus(buildStatus(StatusCode.RESPONDER, null, "Error locating party metadata"));
-                throw new ProfileException("Error locating party metadata");
+            if (assertingPartyRole == null) {
+                assertingPartyRole = requestContext.getAssertingPartyMetadata().getAttributeAuthorityDescriptor(
+                        SAMLConstants.SAML10P_NS);
+                if (assertingPartyRole == null) {
+                    throw new MetadataProviderException("Unable to locate IDPSSO role descriptor for entity "
+                            + assertingPartyId);
+                }
             }
+            requestContext.setAssertingPartyRoleMetadata(assertingPartyRole);
+        } catch (MetadataProviderException e) {
+            log.error("Unable to locate metadata for asserting party " + assertingPartyId);
+            requestContext.setFailureStatus(buildStatus(StatusCode.RESPONDER, null,
+                    "Unable to locate metadata for relying party " + assertingPartyId));
+            throw new ProfileException("Unable to locate metadata for relying party " + assertingPartyId);
         }
     }
 
index bae9288..e9b7321 100644 (file)
@@ -53,6 +53,7 @@ import edu.internet2.middleware.shibboleth.common.profile.ProfileResponse;
 import edu.internet2.middleware.shibboleth.common.relyingparty.RelyingPartyConfiguration;
 import edu.internet2.middleware.shibboleth.common.relyingparty.provider.saml1.ShibbolethSSOConfiguration;
 import edu.internet2.middleware.shibboleth.common.util.HttpHelper;
+import edu.internet2.middleware.shibboleth.idp.ShibbolethConstants;
 import edu.internet2.middleware.shibboleth.idp.authn.LoginContext;
 import edu.internet2.middleware.shibboleth.idp.authn.ShibbolethSSOLoginContext;
 
@@ -283,44 +284,80 @@ public class ShibbolethSSOProfileHandler extends AbstractSAML1ProfileHandler {
 
         requestContext.setRelyingPartyId(relyingPartyId);
 
-        try {
+        populateRelyingPartyData(requestContext);
+        
+        populateAssertingPartyData(requestContext);
+        
+        return requestContext;
+    }
 
+    /**
+     * Populates the relying party entity and role metadata and relying party configuration data.
+     * 
+     * @param requestContext current request context with relying party ID populated
+     * 
+     * @throws ProfileException thrown if metadata can not be located for the relying party
+     */
+    protected void populateRelyingPartyData(ShibbolethSSORequestContext requestContext) throws ProfileException {
+        try {
             requestContext.setRelyingPartyMetadata(getMetadataProvider().getEntityDescriptor(
                     requestContext.getRelyingPartyId()));
 
-            RoleDescriptor assertingPartyRole;
             RoleDescriptor relyingPartyRole = requestContext.getRelyingPartyMetadata().getSPSSODescriptor(
-                    "urn:oasis:names:tc:SAML:1.1:protocol");
+                    SAMLConstants.SAML10P_NS);
 
             if (relyingPartyRole == null) {
                 relyingPartyRole = requestContext.getRelyingPartyMetadata()
-                        .getSPSSODescriptor(SAMLConstants.SAML11P_NS);
-                assertingPartyRole = requestContext.getAssertingPartyMetadata().getIDPSSODescriptor(
-                        SAMLConstants.SAML10P_NS);
-            } else {
-                assertingPartyRole = requestContext.getAssertingPartyMetadata().getIDPSSODescriptor(
-                        SAMLConstants.SAML11P_NS);
+                        .getSPSSODescriptor(SAMLConstants.SAML10P_NS);
+                if (relyingPartyRole == null) {
+                    throw new MetadataProviderException("Unable to locate SPSSO role descriptor for entity "
+                            + requestContext.getRelyingPartyId());
+                }
             }
             requestContext.setRelyingPartyRoleMetadata(relyingPartyRole);
 
-            RelyingPartyConfiguration rpConfig = getRelyingPartyConfiguration(relyingPartyId);
+            RelyingPartyConfiguration rpConfig = getRelyingPartyConfiguration(requestContext.getRelyingPartyId());
             requestContext.setRelyingPartyConfiguration(rpConfig);
 
-            requestContext.setAssertingPartyId(requestContext.getRelyingPartyConfiguration().getProviderId());
+            requestContext.setProfileConfiguration((ShibbolethSSOConfiguration) rpConfig
+                    .getProfileConfiguration(ShibbolethSSOConfiguration.PROFILE_ID));
 
-            requestContext.setAssertingPartyMetadata(getMetadataProvider().getEntityDescriptor(
-                    requestContext.getAssertingPartyId()));
+        } catch (MetadataProviderException e) {
+            log.error("Unable to locate metadata for relying party " + requestContext.getRelyingPartyId());
+            requestContext.setFailureStatus(buildStatus(StatusCode.RESPONDER, null,
+                    "Unable to locate metadata for relying party " + requestContext.getRelyingPartyId()));
+            throw new ProfileException("Unable to locate metadata for relying party "
+                    + requestContext.getRelyingPartyId());
+        }
+    }
 
-            requestContext.setAssertingPartyRoleMetadata(assertingPartyRole);
+    /**
+     * Populates the asserting party entity and role metadata.
+     * 
+     * @param requestContext current request context with relying party configuration populated
+     * 
+     * @throws ProfileException thrown if metadata can not be located for the asserting party
+     */
+    protected void populateAssertingPartyData(ShibbolethSSORequestContext requestContext) throws ProfileException {
+        String assertingPartyId = requestContext.getRelyingPartyConfiguration().getProviderId();
 
-            requestContext.setProfileConfiguration((ShibbolethSSOConfiguration) rpConfig
-                    .getProfileConfiguration(ShibbolethSSOConfiguration.PROFILE_ID));
+        try {
+            requestContext.setAssertingPartyId(assertingPartyId);
+
+            requestContext.setAssertingPartyMetadata(getMetadataProvider().getEntityDescriptor(assertingPartyId));
 
-            return requestContext;
+            RoleDescriptor assertingPartyRole = requestContext.getAssertingPartyMetadata().getIDPSSODescriptor(
+                    ShibbolethConstants.SHIB_SSO_PROFILE_URI);
+            if (assertingPartyRole == null) {
+                throw new MetadataProviderException("Unable to locate IDPSSO role descriptor for entity "
+                        + assertingPartyId);
+            }
+            requestContext.setAssertingPartyRoleMetadata(assertingPartyRole);
         } catch (MetadataProviderException e) {
-            log.error("Unable to locate metadata for asserting or relying party");
-            requestContext.setFailureStatus(buildStatus(StatusCode.RESPONDER, null, "Error locating party metadata"));
-            throw new ProfileException("Error locating party metadata");
+            log.error("Unable to locate metadata for asserting party " + assertingPartyId);
+            requestContext.setFailureStatus(buildStatus(StatusCode.RESPONDER, null,
+                    "Unable to locate metadata for relying party " + assertingPartyId));
+            throw new ProfileException("Unable to locate metadata for relying party " + assertingPartyId);
         }
     }