Fixed an IdP configuration schema bug.
[java-idp.git] / src / edu / internet2 / middleware / shibboleth / idp / IdPResponder.java
index e19494a..b34edbe 100644 (file)
 package edu.internet2.middleware.shibboleth.idp;
 
 import java.io.IOException;
-import java.security.cert.CertificateParsingException;
-import java.security.cert.X509Certificate;
-import java.util.ArrayList;
-import java.util.Collection;
-import java.util.Iterator;
-import java.util.List;
-
-import javax.security.auth.x500.X500Principal;
+import java.net.MalformedURLException;
+import java.net.URI;
+import java.net.URL;
+import java.util.HashMap;
+import java.util.Random;
+
+import javax.servlet.RequestDispatcher;
 import javax.servlet.ServletException;
 import javax.servlet.UnavailableException;
+import javax.servlet.http.HttpServlet;
 import javax.servlet.http.HttpServletRequest;
 import javax.servlet.http.HttpServletResponse;
 
 import org.apache.log4j.Logger;
 import org.apache.log4j.MDC;
-import org.apache.xml.security.exceptions.XMLSecurityException;
-import org.apache.xml.security.keys.KeyInfo;
-import org.opensaml.SAMLAssertion;
-import org.opensaml.SAMLAttributeQuery;
 import org.opensaml.SAMLBinding;
+import org.opensaml.SAMLBindingFactory;
 import org.opensaml.SAMLException;
-import org.opensaml.SAMLIdentifier;
 import org.opensaml.SAMLRequest;
 import org.opensaml.SAMLResponse;
-
-import sun.misc.BASE64Decoder;
-
-import edu.internet2.middleware.shibboleth.common.SAMLBindingFactory;
-import edu.internet2.middleware.shibboleth.common.ServiceProvider;
-import edu.internet2.middleware.shibboleth.common.ShibPOSTProfile;
-import edu.internet2.middleware.shibboleth.common.TargetFederationComponent;
-import edu.internet2.middleware.shibboleth.hs.HSRelyingParty;
-import edu.internet2.middleware.shibboleth.metadata.AttributeConsumerRole;
-import edu.internet2.middleware.shibboleth.metadata.KeyDescriptor;
-import edu.internet2.middleware.shibboleth.metadata.Provider;
-import edu.internet2.middleware.shibboleth.metadata.ProviderRole;
+import org.w3c.dom.Document;
+import org.w3c.dom.Element;
+import org.w3c.dom.NodeList;
+
+import edu.internet2.middleware.shibboleth.aa.arp.ArpEngine;
+import edu.internet2.middleware.shibboleth.aa.arp.ArpException;
+import edu.internet2.middleware.shibboleth.aa.attrresolv.AttributeResolver;
+import edu.internet2.middleware.shibboleth.aa.attrresolv.AttributeResolverException;
+import edu.internet2.middleware.shibboleth.artifact.ArtifactMapper;
+import edu.internet2.middleware.shibboleth.artifact.ArtifactMapperFactory;
+import edu.internet2.middleware.shibboleth.artifact.provider.MemoryArtifactMapper;
+import edu.internet2.middleware.shibboleth.common.Credentials;
+import edu.internet2.middleware.shibboleth.common.NameIdentifierMapping;
+import edu.internet2.middleware.shibboleth.common.NameIdentifierMappingException;
+import edu.internet2.middleware.shibboleth.common.NameMapper;
+import edu.internet2.middleware.shibboleth.common.OriginConfig;
+import edu.internet2.middleware.shibboleth.common.ServiceProviderMapper;
+import edu.internet2.middleware.shibboleth.common.ServiceProviderMapperException;
+import edu.internet2.middleware.shibboleth.common.ShibbolethConfigurationException;
+import edu.internet2.middleware.shibboleth.metadata.Metadata;
+import edu.internet2.middleware.shibboleth.metadata.MetadataException;
 
 /**
- * Primary entrypoint for requests to the SAML IdP. Listens on multiple endpoints, routes requests to the appropriate
+ * Primary entry point for requests to the SAML IdP. Listens on multiple endpoints, routes requests to the appropriate
  * IdP processing components, and delivers proper protocol responses.
  * 
  * @author Walter Hoehn
  */
 
-public class IdPResponder extends TargetFederationComponent {
+public class IdPResponder extends HttpServlet {
 
-       private static Logger           transactionLog  = Logger.getLogger("Shibboleth-TRANSACTION");
-       private static Logger           log                             = Logger.getLogger(IdPResponder.class.getName());
-       private SAMLBinding                     binding;
-       private ArtifactRepository      artifactRepository;
+       private static Logger transactionLog = Logger.getLogger("Shibboleth-TRANSACTION");
+       private static Logger log = Logger.getLogger(IdPResponder.class.getName());
+       private static Random idgen = new Random();
+       private SAMLBinding binding;
 
+       private IdPConfig configuration;
+       private HashMap protocolHandlers = new HashMap();
+       private IdPProtocolSupport protocolSupport;
+
+       /*
+        * @see javax.servlet.GenericServlet#init()
+        */
        public void init() throws ServletException {
 
                super.init();
@@ -84,261 +96,300 @@ public class IdPResponder extends TargetFederationComponent {
                log.info("Initializing Identity Provider.");
 
                try {
-                       binding = SAMLBindingFactory.getInstance(SAMLBinding.SAML_SOAP_HTTPS);
-                       log.info("Identity Provider initialization complete.");
+                       binding = SAMLBindingFactory.getInstance(SAMLBinding.SOAP);
 
-               } catch (SAMLException se) {
-                       log.fatal("SAML SOAP binding could not be loaded: " + se);
-                       throw new UnavailableException("Identity Provider failed to initialize.");
-               }
-       }
+                       Document originConfig = OriginConfig.getOriginConfig(this.getServletContext());
 
-       public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
+                       // Load global configuration properties
+                       configuration = new IdPConfig(originConfig.getDocumentElement());
 
-               MDC.put("serviceId", "[IdP] " + new SAMLIdentifier().toString());
-               MDC.put("remoteAddr", request.getRemoteAddr());
-               log.debug("Recieved a request via POST.");
+                       // Load name mappings
+                       NameMapper nameMapper = new NameMapper();
+                       NodeList itemElements = originConfig.getDocumentElement().getElementsByTagNameNS(
+                                       NameIdentifierMapping.mappingNamespace, "NameMapping");
 
-               // Parse SOAP request and marshall SAML request object
-               try {
-                       SAMLRequest samlRequest = null;
-                       try {
-                               samlRequest = binding.receive(request);
-                       } catch (SAMLException e) {
-                               log.fatal("Unable to parse request: " + e);
-                               throw new SAMLException("Invalid request data.");
+                       for (int i = 0; i < itemElements.getLength(); i++) {
+                               try {
+                                       nameMapper.addNameMapping((Element) itemElements.item(i));
+                               } catch (NameIdentifierMappingException e) {
+                                       log.error("Name Identifier mapping could not be loaded: " + e);
+                               }
                        }
 
-                       // Determine the request type
-                       Iterator artifacts = samlRequest.getArtifacts();
-                       if (artifacts.hasNext()) {
-                               log.info("Recieved a request to dereference an assertion artifact.");
-                               processArtifactDereference(samlRequest, request, response);
-                               return;
+                       // Load signing credentials
+                       itemElements = originConfig.getDocumentElement().getElementsByTagNameNS(Credentials.credentialsNamespace,
+                                       "Credentials");
+                       if (itemElements.getLength() < 1) {
+                               log.error("No credentials specified.");
+                       }
+                       if (itemElements.getLength() > 1) {
+                               log.error("Multiple Credentials specifications found, using first.");
                        }
+                       Credentials credentials = new Credentials((Element) itemElements.item(0));
 
-                       if (samlRequest.getQuery() != null && (samlRequest.getQuery() instanceof SAMLAttributeQuery)) {
-                               log.info("Recieved an attribute query.");
-                               processAttributeQuery(samlRequest, request, response);
-                               return;
+                       // Load relying party config
+                       ServiceProviderMapper spMapper;
+                       try {
+                               spMapper = new ServiceProviderMapper(originConfig.getDocumentElement(), configuration, credentials,
+                                               nameMapper);
+                       } catch (ServiceProviderMapperException e) {
+                               log.error("Could not load Identity Provider configuration: " + e);
+                               throw new ShibbolethConfigurationException("Could not load Identity Provider configuration.");
                        }
 
-                       throw new SAMLException(SAMLException.REQUESTER,
-                                       "Identity Provider unable to respond to this SAML Request type.");
+                       // Startup Attribute Resolver & ARP engine
+                       AttributeResolver resolver = null;
+                       ArpEngine arpEngine = null;
+                       try {
+                               resolver = new AttributeResolver(configuration);
 
-               } catch (SAMLException e) {
-                       // TODO handle properly, like in the AA stuff
-               }
-       }
+                               itemElements = originConfig.getDocumentElement().getElementsByTagNameNS(IdPConfig.configNameSpace,
+                                               "ReleasePolicyEngine");
 
-       private void processAttributeQuery(SAMLRequest samlRequest, HttpServletRequest request, HttpServletResponse response) {
-       //TODO validate that the endpoint is valid for the request type
-       //TODO implement
-       }
+                               if (itemElements.getLength() > 1) {
+                                       log.warn("Encountered multiple <ReleasePolicyEngine/> configuration elements.  Using first...");
+                               }
+                               if (itemElements.getLength() < 1) {
+                                       arpEngine = new ArpEngine();
+                               } else {
+                                       arpEngine = new ArpEngine((Element) itemElements.item(0));
+                               }
 
-       private void processArtifactDereference(SAMLRequest samlRequest, HttpServletRequest request,
-                       HttpServletResponse response) throws SAMLException, IOException {
-               //TODO validate that the endpoint is valid for the request type
+                       } catch (ArpException ae) {
+                               log.fatal("The Identity Provider could not be initialized "
+                                               + "due to a problem with the ARP Engine configuration: " + ae);
+                               throw new ShibbolethConfigurationException("Could not load ARP Engine.");
+                       } catch (AttributeResolverException ne) {
+                               log.fatal("The Identity Provider could not be initialized due "
+                                               + "to a problem with the Attribute Resolver configuration: " + ne);
+                               throw new ShibbolethConfigurationException("Could not load Attribute Resolver.");
+                       }
 
-               // Pull credential from request
-               X509Certificate credential = getCredentialFromProvider(request);
-               if (credential == null || credential.getSubjectX500Principal().getName(X500Principal.RFC2253).equals("")) {
-                       log.info("Request is from an unauthenticated service provider.");
-                       throw new SAMLException(SAMLException.REQUESTER,
-                                       "SAML Artifacts cannot be dereferenced for unauthenticated requesters.");
-               }
+                       // Load artifact mapping implementation
+                       ArtifactMapper artifactMapper = null;
+                       itemElements = originConfig.getDocumentElement().getElementsByTagNameNS(IdPConfig.configNameSpace,
+                                       "ArtifactMapper");
+                       if (itemElements.getLength() > 1) {
+                               log.warn("Encountered multiple <ArtifactMapper/> configuration elements.  Using first...");
+                       }
+                       if (itemElements.getLength() > 0) {
+                               artifactMapper = ArtifactMapperFactory.getInstance((Element) itemElements.item(0));
+                       } else {
+                               log.debug("No Artifact Mapper configuration found.  Defaulting to Memory-based implementation.");
+                               artifactMapper = new MemoryArtifactMapper();
+                       }
 
-               log.info("Request contains credential: (" + credential.getSubjectX500Principal().getName(X500Principal.RFC2253)
-                               + ").");
-
-               ArrayList assertions = new ArrayList();
-               Iterator artifacts = samlRequest.getArtifacts();
-
-               int queriedArtifacts = 0;
-               StringBuffer dereferencedArtifacts = new StringBuffer(); //for transaction log
-               while (artifacts.hasNext()) {
-                       queriedArtifacts++;
-                       String artifact = (String) artifacts.next();
-                       log.debug("Attempting to dereference artifact: (" + artifact + ").");
-                       ArtifactMapping mapping = artifactRepository.recoverAssertion(artifact);
-                       if (mapping != null) {
-                               SAMLAssertion assertion = mapping.getAssertion();
-
-                               //See if we have metadata for this provider
-                               Provider provider = lookup(mapping.getServiceProviderId());
-                               if (provider == null) {
-                                       log.info("No metadata found for provider: (" + mapping.getServiceProviderId() + ").");
-                                       throw new SAMLException(SAMLException.REQUESTER, "Invalid service provider.");
+                       // Load protocol handlers and support library
+                       protocolSupport = new IdPProtocolSupport(configuration, transactionLog, nameMapper, spMapper, arpEngine,
+                                       resolver, artifactMapper);
+                       itemElements = originConfig.getDocumentElement().getElementsByTagNameNS(IdPConfig.configNameSpace,
+                                       "ProtocolHandler");
+
+                       // Default if no handlers are specified
+                       if (itemElements.getLength() < 1) {
+                               // TODO work out defaulting
+
+                               // If handlers were specified, load them and register them against their locations
+                       } else {
+                               EACHHANDLER : for (int i = 0; i < itemElements.getLength(); i++) {
+                                       IdPProtocolHandler handler = ProtocolHandlerFactory.getInstance((Element) itemElements.item(i));
+                                       URI[] locations = handler.getLocations();
+                                       EACHLOCATION : for (int j = 0; j < locations.length; j++) {
+                                               if (protocolHandlers.containsKey(locations[j].toString())) {
+                                                       log.error("Multiple protocol handlers are registered to listen at ("
+                                                                       + locations[j]
+                                                                       + ").  Ignoring all except ("
+                                                                       + ((IdPProtocolHandler) protocolHandlers.get(locations[j].toString()))
+                                                                                       .getHandlerName() + ").");
+                                                       continue EACHLOCATION;
+                                               }
+                                               log.info("Registering handler (" + handler.getHandlerName() + ") to listen at (" + locations[j]
+                                                               + ").");
+                                               protocolHandlers.put(locations[j].toString(), handler);
+                                       }
                                }
+                       }
+
+                       // Load metadata
+                       itemElements = originConfig.getDocumentElement().getElementsByTagNameNS(IdPConfig.configNameSpace,
+                                       "MetadataProvider");
+                       for (int i = 0; i < itemElements.getLength(); i++) {
+                               protocolSupport.addMetadataProvider((Element) itemElements.item(i));
+                       }
+                       if (protocolSupport.providerCount() < 1) {
+                               log.error("No Metadata Provider metadata loaded.");
+                               throw new ShibbolethConfigurationException("Could not load SAML metadata.");
+                       }
+
+                       log.info("Identity Provider initialization complete.");
+
+               } catch (ShibbolethConfigurationException ae) {
+                       log.fatal("The Identity Provider could not be initialized: " + ae);
+                       throw new UnavailableException("Identity Provider failed to initialize.");
+               } catch (SAMLException se) {
+                       log.fatal("SAML SOAP binding could not be loaded: " + se);
+                       throw new UnavailableException("Identity Provider failed to initialize.");
+               }
+       }
+
+       /*
+        * @see javax.servlet.http.HttpServlet#doGet(javax.servlet.http.HttpServletRequest,
+        *      javax.servlet.http.HttpServletResponse)
+        */
+       public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
+
+               MDC.put("serviceId", "[IdP] " + idgen.nextInt());
+               MDC.put("remoteAddr", request.getRemoteAddr());
+               log.debug("Recieved a request via GET for location (" + request.getRequestURL() + ").");
 
-                               //Make sure that the suppplied credential is valid for the provider to which the artifact was issued
-                               if (!isValidCredential(provider, credential)) {
-                                       log.error("Supplied credential ("
-                                                       + credential.getSubjectX500Principal().getName(X500Principal.RFC2253)
-                                                       + ") is NOT valid for provider (" + mapping.getServiceProviderId()
-                                                       + "), to whom this artifact was issued.");
-                                       throw new SAMLException(SAMLException.REQUESTER, "Invalid credential.");
+               try {
+                       // Determine which protocol we are responding to (at this point normally Shibv1 vs. EAuth)
+                       String requestURL = request.getRequestURL().toString();
+                       IdPProtocolHandler activeHandler = (IdPProtocolHandler) protocolHandlers.get(requestURL);
+                       if (activeHandler == null) {
+                               log.debug("No protocol handler registered for location (" + request.getRequestURL()
+                                               + ").  Attempting to match against relative path.");
+                               try {
+                                       activeHandler = (IdPProtocolHandler) protocolHandlers.get(new URL(requestURL).getPath());
+                               } catch (MalformedURLException e) {
+                                       // squelch, we will just fail to find a handler
                                }
+                       }
 
-                               log.debug("Supplied credential validated for the provider to which this artifact was issued.");
+                       if (activeHandler == null) {
+                               log.error("No protocol handler registered for location (" + request.getRequestURL() + ").");
+                               throw new SAMLException("Request submitted to an invalid location.");
+                       }
 
-                               assertions.add(assertion);
-                               dereferencedArtifacts.append("(" + artifact + ")");
+                       // Pass request to the appropriate handler
+                       log.info("Processing " + activeHandler.getHandlerName() + " request.");
+                       if (activeHandler.processRequest(request, response, null, protocolSupport) != null) {
+                               // This shouldn't happen unless somebody configures a protocol handler incorrectly
+                               log.error("Protocol Handler returned a SAML Response, but there is no binding to handle it.");
+                               throw new SAMLException(SAMLException.RESPONDER, "General error processing request.");
                        }
+
+               } catch (SAMLException ex) {
+                       log.error(ex);
+                       displayBrowserError(request, response, ex);
+                       return;
                }
+       }
 
-               //The spec requires that if any artifacts are dereferenced, they must all be dereferenced
-               if (assertions.size() > 0 & assertions.size() != queriedArtifacts) { throw new SAMLException(
-                               SAMLException.REQUESTER, "Unable to successfully dereference all artifacts."); }
+       /*
+        * @see javax.servlet.http.HttpServlet#doPost(javax.servlet.http.HttpServletRequest,
+        *      javax.servlet.http.HttpServletResponse)
+        */
+       public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
 
-               //Create and send response
-               SAMLResponse samlResponse = new SAMLResponse(samlRequest.getId(), null, assertions, null);
+               MDC.put("serviceId", "[IdP] " + idgen.nextInt());
+               MDC.put("remoteAddr", request.getRemoteAddr());
+               log.debug("Recieved a request via POST for location (" + request.getRequestURL() + ").");
 
-               if (log.isDebugEnabled()) {
+               // Parse SOAP request and marshall SAML request object
+               SAMLRequest samlRequest = null;
+               try {
                        try {
-                               log.debug("Dumping generated SAML Response:"
-                                               + System.getProperty("line.separator")
-                                               + new String(new BASE64Decoder().decodeBuffer(new String(samlResponse.toBase64(), "ASCII")),
-                                                               "UTF8"));
+                               samlRequest = binding.receive(request);
                        } catch (SAMLException e) {
-                               log.error("Encountered an error while decoding SAMLReponse for logging purposes.");
-                       } catch (IOException e) {
-                               log.error("Encountered an error while decoding SAMLReponse for logging purposes.");
+                               log.fatal("Unable to parse request: " + e);
+                               throw new SAMLException("Invalid request data.");
                        }
-               }
 
-               binding.respond(response, samlResponse, null);
+                       // If we have DEBUG logging turned on, dump out the request to the log
+                       // This takes some processing, so only do it if we need to
+                       if (log.isDebugEnabled()) {
+                               log.debug("Dumping generated SAML Request:" + System.getProperty("line.separator")
+                                               + samlRequest.toString());
+                       }
 
-               transactionLog.info("Succesfully dereferenced the following artifacts: " + dereferencedArtifacts.toString());
-               /*
-                * } catch (Exception e) { log.error("Error while processing request: " + e); try { sendFailure(res,
-                * samlRequest, new SAMLException(SAMLException.RESPONDER, "General error processing request.")); return; }
-                * catch (Exception ee) { log.fatal("Could not construct a SAML error response: " + ee); throw new
-                * ServletException("Handle Service response failure."); } }
-                */
-       }
+                       // Determine which protocol handler is active for this endpoint
+                       String requestURL = request.getRequestURL().toString();
+                       IdPProtocolHandler activeHandler = (IdPProtocolHandler) protocolHandlers.get(requestURL);
+                       if (activeHandler == null) {
+                               log.debug("No protocol handler registered for location (" + request.getRequestURL()
+                                               + ").  Attempting to match against relative path.");
+                               try {
+                                       activeHandler = (IdPProtocolHandler) protocolHandlers.get(new URL(requestURL).getPath());
+                               } catch (MalformedURLException e) {
+                                       // squelch, we will just fail to find a handler
+                               }
+                       }
 
-       public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
+                       // Pass request to the appropriate handler and respond
+                       log.info("Processing " + activeHandler.getHandlerName() + " request.");
 
-               MDC.put("serviceId", "[IdP] " + new SAMLIdentifier().toString());
-               MDC.put("remoteAddr", request.getRemoteAddr());
-               log.debug("Recieved a request via GET.");
-       }
+                       SAMLResponse samlResponse = activeHandler.processRequest(request, response, samlRequest, protocolSupport);
+                       binding.respond(response, samlResponse, null);
 
-       private X509Certificate getCredentialFromProvider(HttpServletRequest req) {
-               X509Certificate[] certArray = (X509Certificate[]) req.getAttribute("javax.servlet.request.X509Certificate");
-               if (certArray != null && certArray.length > 0) { return certArray[0]; }
-               return null;
+               } catch (SAMLException e) {
+                       sendFailureToSAMLBinding(response, samlRequest, e);
+               }
        }
 
-       private boolean isValidCredential(Provider provider, X509Certificate certificate) {
+       private void sendFailureToSAMLBinding(HttpServletResponse httpResponse, SAMLRequest samlRequest,
+                       SAMLException exception) throws ServletException {
 
-               ProviderRole[] roles = provider.getRoles();
-               if (roles.length == 0) {
-                       log.info("Inappropriate metadata for provider.");
-                       return false;
-               }
-               //TODO figure out what to do about this role business here
-               for (int i = 0; roles.length > i; i++) {
-                       if (roles[i] instanceof AttributeConsumerRole) {
-                               KeyDescriptor[] descriptors = roles[i].getKeyDescriptors();
-                               for (int j = 0; descriptors.length > j; j++) {
-                                       KeyInfo[] keyInfo = descriptors[j].getKeyInfo();
-                                       for (int k = 0; keyInfo.length > k; k++) {
-                                               for (int l = 0; keyInfo[k].lengthKeyName() > l; l++) {
-                                                       try {
-
-                                                               //First, try to match DN against metadata
-                                                               try {
-                                                                       if (certificate.getSubjectX500Principal().getName(X500Principal.RFC2253).equals(
-                                                                                       new X500Principal(keyInfo[k].itemKeyName(l).getKeyName())
-                                                                                                       .getName(X500Principal.RFC2253))) {
-                                                                               log.debug("Matched against DN.");
-                                                                               return true;
-                                                                       }
-                                                               } catch (IllegalArgumentException iae) {
-                                                                       //squelch this runtime exception, since this might be a valid case
-                                                               }
-
-                                                               //If that doesn't work, we try matching against some Subject Alt Names
-                                                               try {
-                                                                       Collection altNames = certificate.getSubjectAlternativeNames();
-                                                                       if (altNames != null) {
-                                                                               for (Iterator nameIterator = altNames.iterator(); nameIterator.hasNext();) {
-                                                                                       List altName = (List) nameIterator.next();
-                                                                                       if (altName.get(0).equals(new Integer(2))
-                                                                                                       || altName.get(0).equals(new Integer(6))) { //2 is DNS, 6 is URI
-                                                                                               if (altName.get(1).equals(keyInfo[k].itemKeyName(l).getKeyName())) {
-                                                                                                       log.debug("Matched against SubjectAltName.");
-                                                                                                       return true;
-                                                                                               }
-                                                                                       }
-                                                                               }
-                                                                       }
-                                                               } catch (CertificateParsingException e1) {
-                                                                       log
-                                                                                       .error("Encountered an problem trying to extract Subject Alternate Name from supplied certificate: "
-                                                                                                       + e1);
-                                                               }
-
-                                                               //If that doesn't work, try to match using SSL-style hostname matching
-                                                               if (ShibPOSTProfile.getHostNameFromDN(certificate.getSubjectX500Principal()).equals(
-                                                                               keyInfo[k].itemKeyName(l).getKeyName())) {
-                                                                       log.debug("Matched against hostname.");
-                                                                       return true;
-                                                               }
-
-                                                       } catch (XMLSecurityException e) {
-                                                               log.error("Encountered an error reading federation metadata: " + e);
-                                                       }
-                                               }
-                                       }
+               log.error("Error while processing request: " + exception);
+               try {
+                       SAMLResponse samlResponse = new SAMLResponse((samlRequest != null) ? samlRequest.getId() : null, null,
+                                       null, exception);
+                       if (log.isDebugEnabled()) {
+                               log.debug("Dumping generated SAML Error Response:" + System.getProperty("line.separator")
+                                               + samlResponse.toString());
+                       }
+                       binding.respond(httpResponse, samlResponse, null);
+                       log.debug("Returning SAML Error Response.");
+               } catch (SAMLException se) {
+                       try {
+                               binding.respond(httpResponse, null, exception);
+                       } catch (SAMLException e) {
+                               log.error("Caught exception while responding to requester: " + e.getMessage());
+                               try {
+                                       httpResponse.sendError(HttpServletResponse.SC_INTERNAL_SERVER_ERROR, "Error while responding.");
+                               } catch (IOException ee) {
+                                       log.fatal("Could not construct a SAML error response: " + ee);
+                                       throw new ServletException("Identity Provider response failure.");
                                }
                        }
+                       log.error("Identity Provider failed to make an error message: " + se);
                }
-               log.info("Supplied credential not found in metadata.");
-               return false;
        }
 
-       abstract class ArtifactRepository {
-
-               // TODO figure out what to do about this interface long term
-               abstract String addAssertion(SAMLAssertion assertion, HSRelyingParty relyingParty);
+       private static void displayBrowserError(HttpServletRequest req, HttpServletResponse res, Exception e)
+                       throws ServletException, IOException {
 
-               abstract ArtifactMapping recoverAssertion(String artifact);
+               req.setAttribute("errorText", e.toString());
+               req.setAttribute("requestURL", req.getRequestURI().toString());
+               RequestDispatcher rd = req.getRequestDispatcher("/IdPError.jsp");
+               rd.forward(req, res);
        }
 
-       class ArtifactMapping {
-
-               //TODO figure out what to do about this interface long term
-               private String                  assertionHandle;
-               private long                    expirationTime;
-               private SAMLAssertion   assertion;
-               private String                  serviceProviderId;
+}
 
-               ArtifactMapping(String assertionHandle, SAMLAssertion assertion, ServiceProvider sp) {
-                       this.assertionHandle = assertionHandle;
-                       this.assertion = assertion;
-                       expirationTime = System.currentTimeMillis() + (1000 * 60 * 5); //in 5 minutes
-                       serviceProviderId = sp.getProviderId();
-               }
+class MetadataProviderFactory {
 
-               boolean isExpired() {
-                       if (System.currentTimeMillis() > expirationTime) { return true; }
-                       return false;
-               }
+       private static Logger log = Logger.getLogger(MetadataProviderFactory.class.getName());
 
-               boolean isCorrectProvider(ServiceProvider sp) {
-                       if (sp.getProviderId().equals(serviceProviderId)) { return true; }
-                       return false;
-               }
+       public static Metadata loadProvider(Element e) throws MetadataException {
 
-               SAMLAssertion getAssertion() {
-                       return assertion;
-               }
-
-               String getServiceProviderId() {
-                       return serviceProviderId;
+               String className = e.getAttribute("type");
+               if (className == null || className.equals("")) {
+                       log.error("Metadata Provider requires specification of the attribute \"type\".");
+                       throw new MetadataException("Failed to initialize Metadata Provider.");
+               } else {
+                       try {
+                               Class[] params = {Class.forName("org.w3c.dom.Element"),};
+                               return (Metadata) Class.forName(className).getConstructor(params).newInstance(new Object[]{e});
+                       } catch (Exception loaderException) {
+                               log.error("Failed to load Metadata Provider implementation class: " + loaderException);
+                               Throwable cause = loaderException.getCause();
+                               while (cause != null) {
+                                       log.error("caused by: " + cause);
+                                       cause = cause.getCause();
+                               }
+                               throw new MetadataException("Failed to initialize Metadata Provider.");
+                       }
                }
        }
 }
\ No newline at end of file