001/* 002 * (C) Copyright 2014 Nuxeo SA (http://nuxeo.com/) and others. 003 * 004 * Licensed under the Apache License, Version 2.0 (the "License"); 005 * you may not use this file except in compliance with the License. 006 * You may obtain a copy of the License at 007 * 008 * http://www.apache.org/licenses/LICENSE-2.0 009 * 010 * Unless required by applicable law or agreed to in writing, software 011 * distributed under the License is distributed on an "AS IS" BASIS, 012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 013 * See the License for the specific language governing permissions and 014 * limitations under the License. 015 * 016 * Contributors: 017 * Nelson Silva <[email protected]> 018 */ 019 020package org.nuxeo.ecm.platform.auth.saml.web; 021 022import java.io.IOException; 023 024import javax.servlet.http.HttpServlet; 025import javax.servlet.http.HttpServletRequest; 026import javax.servlet.http.HttpServletResponse; 027 028import org.apache.commons.logging.Log; 029import org.apache.commons.logging.LogFactory; 030import org.nuxeo.ecm.platform.auth.saml.SAMLConfiguration; 031import org.nuxeo.ecm.platform.ui.web.auth.LoginScreenHelper; 032import org.nuxeo.ecm.platform.web.common.vh.VirtualHostHelper; 033import org.opensaml.saml2.metadata.EntityDescriptor; 034import org.opensaml.xml.Configuration; 035import org.opensaml.xml.io.Marshaller; 036import org.opensaml.xml.io.MarshallingException; 037import org.opensaml.xml.util.XMLHelper; 038import org.w3c.dom.Element; 039 040/** 041 * Servlet that returns local SP metadata for configuring IdPs. 042 * 043 * @since 6.0 044 */ 045public class MetadataServlet extends HttpServlet { 046 047 protected static final Log log = LogFactory.getLog(MetadataServlet.class); 048 049 @Override 050 public void doGet(HttpServletRequest request, HttpServletResponse response) throws IOException { 051 052 String baseURL = VirtualHostHelper.getBaseURL(request); 053 baseURL += (baseURL.endsWith("/") ? "" : "/") + LoginScreenHelper.getStartupPagePath(); 054 055 EntityDescriptor descriptor = SAMLConfiguration.getEntityDescriptor(baseURL); 056 057 try { 058 Marshaller marshaller = Configuration.getMarshallerFactory().getMarshaller(descriptor); 059 if (marshaller == null) { 060 log.error("Unable to marshall message, no marshaller registered for message object: " 061 + descriptor.getElementQName()); 062 return; 063 } 064 Element dom = marshaller.marshall(descriptor); 065 XMLHelper.writeNode(dom, response.getWriter()); 066 } catch (MarshallingException e) { 067 log.error("Unable to write metadata."); 068 } 069 } 070}