Use the correct method to get the request endpoint from the servlet engine.
[java-idp.git] / src / edu / internet2 / middleware / shibboleth / idp / IdPResponder.java
1 /*
2  * The Shibboleth License, Version 1. Copyright (c) 2002 University Corporation for Advanced Internet Development, Inc.
3  * All rights reserved Redistribution and use in source and binary forms, with or without modification, are permitted
4  * provided that the following conditions are met: Redistributions of source code must retain the above copyright
5  * notice, this list of conditions and the following disclaimer. Redistributions in binary form must reproduce the above
6  * copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials
7  * provided with the distribution, if any, must include the following acknowledgment: "This product includes software
8  * developed by the University Corporation for Advanced Internet Development <http://www.ucaid.edu> Internet2 Project.
9  * Alternately, this acknowledegement may appear in the software itself, if and wherever such third-party
10  * acknowledgments normally appear. Neither the name of Shibboleth nor the names of its contributors, nor Internet2, nor
11  * the University Corporation for Advanced Internet Development, Inc., nor UCAID may be used to endorse or promote
12  * products derived from this software without specific prior written permission. For written permission, please contact
13  * shibboleth@shibboleth.org Products derived from this software may not be called Shibboleth, Internet2, UCAID, or the
14  * University Corporation for Advanced Internet Development, nor may Shibboleth appear in their name, without prior
15  * written permission of the University Corporation for Advanced Internet Development. THIS SOFTWARE IS PROVIDED BY THE
16  * COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND WITH ALL FAULTS. ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
17  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE, AND NON-INFRINGEMENT ARE
18  * DISCLAIMED AND THE ENTIRE RISK OF SATISFACTORY QUALITY, PERFORMANCE, ACCURACY, AND EFFORT IS WITH LICENSEE. IN NO
19  * EVENT SHALL THE COPYRIGHT OWNER, CONTRIBUTORS OR THE UNIVERSITY CORPORATION FOR ADVANCED INTERNET DEVELOPMENT, INC.
20  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
21  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
22  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE
23  * OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
24  */
25
26 package edu.internet2.middleware.shibboleth.idp;
27
28 import java.io.IOException;
29 import java.net.URI;
30 import java.net.URISyntaxException;
31 import java.util.HashMap;
32 import java.util.Random;
33
34 import javax.servlet.RequestDispatcher;
35 import javax.servlet.ServletException;
36 import javax.servlet.UnavailableException;
37 import javax.servlet.http.HttpServlet;
38 import javax.servlet.http.HttpServletRequest;
39 import javax.servlet.http.HttpServletResponse;
40
41 import org.apache.log4j.Logger;
42 import org.apache.log4j.MDC;
43 import org.opensaml.SAMLBinding;
44 import org.opensaml.SAMLBindingFactory;
45 import org.opensaml.SAMLException;
46 import org.opensaml.SAMLRequest;
47 import org.opensaml.SAMLResponse;
48 import org.w3c.dom.Document;
49 import org.w3c.dom.Element;
50 import org.w3c.dom.NodeList;
51
52 import sun.misc.BASE64Decoder;
53 import edu.internet2.middleware.shibboleth.aa.arp.ArpEngine;
54 import edu.internet2.middleware.shibboleth.aa.arp.ArpException;
55 import edu.internet2.middleware.shibboleth.aa.attrresolv.AttributeResolver;
56 import edu.internet2.middleware.shibboleth.aa.attrresolv.AttributeResolverException;
57 import edu.internet2.middleware.shibboleth.common.Credentials;
58 import edu.internet2.middleware.shibboleth.common.NameIdentifierMapping;
59 import edu.internet2.middleware.shibboleth.common.NameIdentifierMappingException;
60 import edu.internet2.middleware.shibboleth.common.NameMapper;
61 import edu.internet2.middleware.shibboleth.common.OriginConfig;
62 import edu.internet2.middleware.shibboleth.common.ServiceProviderMapper;
63 import edu.internet2.middleware.shibboleth.common.ServiceProviderMapperException;
64 import edu.internet2.middleware.shibboleth.common.ShibbolethConfigurationException;
65 import edu.internet2.middleware.shibboleth.idp.provider.ShibbolethV1SSOHandler;
66 import edu.internet2.middleware.shibboleth.metadata.Metadata;
67 import edu.internet2.middleware.shibboleth.metadata.MetadataException;
68
69 /**
70  * Primary entry point for requests to the SAML IdP. Listens on multiple endpoints, routes requests to the appropriate
71  * IdP processing components, and delivers proper protocol responses.
72  * 
73  * @author Walter Hoehn
74  */
75
76 public class IdPResponder extends HttpServlet {
77
78         private static Logger transactionLog = Logger.getLogger("Shibboleth-TRANSACTION");
79         private static Logger log = Logger.getLogger(IdPResponder.class.getName());
80         private static Random idgen = new Random();
81         private SAMLBinding binding;
82         private Semaphore throttle;
83         private IdPConfig configuration;
84         private HashMap protocolHandlers = new HashMap();
85         private IdPProtocolSupport protocolSupport;
86
87         /*
88          * @see javax.servlet.GenericServlet#init()
89          */
90         public void init() throws ServletException {
91
92                 super.init();
93                 MDC.put("serviceId", "[IdP] Core");
94                 log.info("Initializing Identity Provider.");
95
96                 try {
97                         binding = SAMLBindingFactory.getInstance(SAMLBinding.SOAP);
98
99                         Document originConfig = OriginConfig.getOriginConfig(this.getServletContext());
100
101                         // Load global configuration properties
102                         configuration = new IdPConfig(originConfig.getDocumentElement());
103
104                         // Load a semaphore that throttles how many requests the IdP will handle at once
105                         throttle = new Semaphore(configuration.getMaxThreads());
106
107                         // Load name mappings
108                         NameMapper nameMapper = new NameMapper();
109                         NodeList itemElements = originConfig.getDocumentElement().getElementsByTagNameNS(
110                                         NameIdentifierMapping.mappingNamespace, "NameMapping");
111
112                         for (int i = 0; i < itemElements.getLength(); i++) {
113                                 try {
114                                         nameMapper.addNameMapping((Element) itemElements.item(i));
115                                 } catch (NameIdentifierMappingException e) {
116                                         log.error("Name Identifier mapping could not be loaded: " + e);
117                                 }
118                         }
119
120                         // Load signing credentials
121                         itemElements = originConfig.getDocumentElement().getElementsByTagNameNS(Credentials.credentialsNamespace,
122                                         "Credentials");
123                         if (itemElements.getLength() < 1) {
124                                 log.error("No credentials specified.");
125                         }
126                         if (itemElements.getLength() > 1) {
127                                 log.error("Multiple Credentials specifications found, using first.");
128                         }
129                         Credentials credentials = new Credentials((Element) itemElements.item(0));
130
131                         // Load relying party config
132                         ServiceProviderMapper spMapper;
133                         try {
134                                 spMapper = new ServiceProviderMapper(originConfig.getDocumentElement(), configuration, credentials,
135                                                 nameMapper);
136                         } catch (ServiceProviderMapperException e) {
137                                 log.error("Could not load Identity Provider configuration: " + e);
138                                 throw new ShibbolethConfigurationException("Could not load Identity Provider configuration.");
139                         }
140
141                         // Startup Attribute Resolver & ARP engine
142                         AttributeResolver resolver = null;
143                         ArpEngine arpEngine = null;
144                         try {
145                                 resolver = new AttributeResolver(configuration);
146
147                                 itemElements = originConfig.getDocumentElement().getElementsByTagNameNS(
148                                                 IdPConfig.originConfigNamespace, "ReleasePolicyEngine");
149
150                                 if (itemElements.getLength() > 1) {
151                                         log.warn("Encountered multiple <ReleasePolicyEngine> configuration elements.  Using first...");
152                                 }
153                                 if (itemElements.getLength() < 1) {
154                                         arpEngine = new ArpEngine();
155                                 } else {
156                                         arpEngine = new ArpEngine((Element) itemElements.item(0));
157                                 }
158
159                         } catch (ArpException ae) {
160                                 log.fatal("The Identity Provider could not be initialized "
161                                                 + "due to a problem with the ARP Engine configuration: " + ae);
162                                 throw new ShibbolethConfigurationException("Could not load ARP Engine.");
163                         } catch (AttributeResolverException ne) {
164                                 log.fatal("The Identity Provider could not be initialized due "
165                                                 + "to a problem with the Attribute Resolver configuration: " + ne);
166                                 throw new ShibbolethConfigurationException("Could not load Attribute Resolver.");
167                         }
168
169                         // Load protocol handlers and support library
170                         protocolSupport = new IdPProtocolSupport(configuration, transactionLog, nameMapper, spMapper, arpEngine,
171                                         resolver);
172                         log.debug("Starting with Shibboleth v1 protocol handling enabled.");
173
174                         protocolHandlers.put("https://wraith.memphis.edu/shibboleth/SSO", new ShibbolethV1SSOHandler());
175
176                         // Load metadata
177                         itemElements = originConfig.getDocumentElement().getElementsByTagNameNS(IdPConfig.originConfigNamespace,
178                                         "FederationProvider");
179                         for (int i = 0; i < itemElements.getLength(); i++) {
180                                 protocolSupport.addFederationProvider((Element) itemElements.item(i));
181                         }
182                         if (protocolSupport.providerCount() < 1) {
183                                 log.error("No Federation Provider metadata loaded.");
184                                 throw new ShibbolethConfigurationException("Could not load federation metadata.");
185                         }
186
187                         log.info("Identity Provider initialization complete.");
188
189                 } catch (ShibbolethConfigurationException ae) {
190                         log.fatal("The Identity Provider could not be initialized: " + ae);
191                         throw new UnavailableException("Identity Provider failed to initialize.");
192                 } catch (SAMLException se) {
193                         log.fatal("SAML SOAP binding could not be loaded: " + se);
194                         throw new UnavailableException("Identity Provider failed to initialize.");
195                 }
196         }
197
198         /*
199          * @see javax.servlet.http.HttpServlet#doGet(javax.servlet.http.HttpServletRequest,
200          *      javax.servlet.http.HttpServletResponse)
201          */
202         public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
203
204                 MDC.put("serviceId", "[IdP] " + idgen.nextInt());
205                 MDC.put("remoteAddr", request.getRemoteAddr());
206                 log.debug("Recieved a request via GET for endpoint (" + request.getRequestURL() + ").");
207
208                 try {
209                         // TODO this throttle should probably just wrap signing operations...
210                         throttle.enter();
211
212                         // Determine which protocol we are responding to (at this point normally Shibv1 vs. EAuth)
213                         IdPProtocolHandler activeHandler = (IdPProtocolHandler) protocolHandlers.get(request.getRequestURL()
214                                         .toString());
215                         if (activeHandler == null) {
216                                 log.error("No protocol handler registered for endpoint (" + request.getRequestURL() + ").");
217                                 throw new SAMLException("Request submitted to an invalid endpoint.");
218                         }
219
220                         // Pass request to the appropriate handler
221                         log.info("Processing " + activeHandler.getHandlerName() + " request.");
222                         if (activeHandler.processRequest(request, response, null, protocolSupport) != null) {
223                                 // This shouldn't happen unless somebody configures a protocol handler incorrectly
224                                 log.error("Protocol Handler returned a SAML Response, but there is no binding to handle it.");
225                                 throw new SAMLException(SAMLException.RESPONDER, "General error processing request.");
226                         }
227
228                 } catch (SAMLException ex) {
229                         log.error(ex);
230                         displayBrowserError(request, response, ex);
231                         return;
232                 } finally {
233                         throttle.exit();
234                 }
235         }
236
237         /*
238          * @see javax.servlet.http.HttpServlet#doPost(javax.servlet.http.HttpServletRequest,
239          *      javax.servlet.http.HttpServletResponse)
240          */
241         public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
242
243                 MDC.put("serviceId", "[IdP] " + idgen.nextInt());
244                 MDC.put("remoteAddr", request.getRemoteAddr());
245                 log.debug("Recieved a request via POST for endpoint (" + request.getRequestURI() + ").");
246
247                 // Parse SOAP request and marshall SAML request object
248                 SAMLRequest samlRequest = null;
249                 try {
250                         try {
251                                 samlRequest = binding.receive(request);
252                         } catch (SAMLException e) {
253                                 log.fatal("Unable to parse request: " + e);
254                                 throw new SAMLException("Invalid request data.");
255                         }
256
257                         // If we have DEBUG logging turned on, dump out the request to the log
258                         // This takes some processing, so only do it if we need to
259                         if (log.isDebugEnabled()) {
260                                 try {
261                                         log.debug("Dumping generated SAML Request:"
262                                                         + System.getProperty("line.separator")
263                                                         + new String(new BASE64Decoder().decodeBuffer(new String(samlRequest.toBase64(), "ASCII")),
264                                                                         "UTF8"));
265                                 } catch (SAMLException e) {
266                                         log.error("Encountered an error while decoding SAMLRequest for logging purposes.");
267                                 } catch (IOException e) {
268                                         log.error("Encountered an error while decoding SAMLRequest for logging purposes.");
269                                 }
270                         }
271
272                         // Determine which protocol handler is active for this endpoint
273                         IdPProtocolHandler activeHandler = (IdPProtocolHandler) protocolHandlers.get(request.getRequestURI());
274                         if (activeHandler == null) {
275                                 log.error("No protocol handler registered for endpoint (" + request.getRequestURI() + ").");
276                                 throw new SAMLException("Request submitted to an invalid endpoint.");
277                         }
278
279                         // Pass request to the appropriate handler and respond
280                         log.info("Processing " + activeHandler.getHandlerName() + " request.");
281
282                         SAMLResponse samlResponse = activeHandler.processRequest(request, response, samlRequest, protocolSupport);
283                         binding.respond(response, samlResponse, null);
284
285                 } catch (SAMLException e) {
286                         sendFailureToSAMLBinding(response, samlRequest, e);
287                 }
288         }
289
290         private void sendFailureToSAMLBinding(HttpServletResponse httpResponse, SAMLRequest samlRequest,
291                         SAMLException exception) throws ServletException {
292
293                 log.error("Error while processing request: " + exception);
294                 try {
295                         SAMLResponse samlResponse = new SAMLResponse((samlRequest != null) ? samlRequest.getId() : null, null,
296                                         null, exception);
297                         if (log.isDebugEnabled()) {
298                                 try {
299                                         log.debug("Dumping generated SAML Error Response:"
300                                                         + System.getProperty("line.separator")
301                                                         + new String(
302                                                                         new BASE64Decoder().decodeBuffer(new String(samlResponse.toBase64(), "ASCII")),
303                                                                         "UTF8"));
304                                 } catch (IOException e) {
305                                         log.error("Encountered an error while decoding SAMLReponse for logging purposes.");
306                                 }
307                         }
308                         binding.respond(httpResponse, samlResponse, null);
309                         log.debug("Returning SAML Error Response.");
310                 } catch (SAMLException se) {
311                         try {
312                                 binding.respond(httpResponse, null, exception);
313                         } catch (SAMLException e) {
314                                 log.error("Caught exception while responding to requester: " + e.getMessage());
315                                 try {
316                                         httpResponse.sendError(HttpServletResponse.SC_INTERNAL_SERVER_ERROR, "Error while responding.");
317                                 } catch (IOException ee) {
318                                         log.fatal("Could not construct a SAML error response: " + ee);
319                                         throw new ServletException("Identity Provider response failure.");
320                                 }
321                         }
322                         log.error("Identity Provider failed to make an error message: " + se);
323                 }
324         }
325
326         private static void displayBrowserError(HttpServletRequest req, HttpServletResponse res, Exception e)
327                         throws ServletException, IOException {
328
329                 req.setAttribute("errorText", e.toString());
330                 req.setAttribute("requestURL", req.getRequestURI().toString());
331                 RequestDispatcher rd = req.getRequestDispatcher("/IdPError.jsp");
332                 rd.forward(req, res);
333         }
334
335         private class Semaphore {
336
337                 private int value;
338
339                 public Semaphore(int value) {
340
341                         this.value = value;
342                 }
343
344                 public synchronized void enter() {
345
346                         --value;
347                         if (value < 0) {
348                                 try {
349                                         wait();
350                                 } catch (InterruptedException e) {
351                                         // squelch and continue
352                                 }
353                         }
354                 }
355
356                 public synchronized void exit() {
357
358                         ++value;
359                         notify();
360                 }
361         }
362
363 }
364
365 class FederationProviderFactory {
366
367         private static Logger log = Logger.getLogger(FederationProviderFactory.class.getName());
368
369         public static Metadata loadProvider(Element e) throws MetadataException {
370
371                 String className = e.getAttribute("type");
372                 if (className == null || className.equals("")) {
373                         log.error("Federation Provider requires specification of the attribute \"type\".");
374                         throw new MetadataException("Failed to initialize Federation Provider.");
375                 } else {
376                         try {
377                                 Class[] params = {Class.forName("org.w3c.dom.Element"),};
378                                 return (Metadata) Class.forName(className).getConstructor(params).newInstance(new Object[]{e});
379                         } catch (Exception loaderException) {
380                                 log.error("Failed to load Federation Provider implementation class: " + loaderException);
381                                 Throwable cause = loaderException.getCause();
382                                 while (cause != null) {
383                                         log.error("caused by: " + cause);
384                                         cause = cause.getCause();
385                                 }
386                                 throw new MetadataException("Failed to initialize Federation Provider.");
387                         }
388                 }
389         }
390 }