ImportSAML2MetaData.java revision 4a2f0f0be43dfd4c1b490cbf3cc48b6ba6084b1c
4a2f0f0be43dfd4c1b490cbf3cc48b6ba6084b1cAllan Foster * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
4a2f0f0be43dfd4c1b490cbf3cc48b6ba6084b1cAllan Foster * Copyright (c) 2008 Sun Microsystems Inc. All Rights Reserved
4a2f0f0be43dfd4c1b490cbf3cc48b6ba6084b1cAllan Foster * The contents of this file are subject to the terms
4a2f0f0be43dfd4c1b490cbf3cc48b6ba6084b1cAllan Foster * of the Common Development and Distribution License
4a2f0f0be43dfd4c1b490cbf3cc48b6ba6084b1cAllan Foster * (the License). You may not use this file except in
4a2f0f0be43dfd4c1b490cbf3cc48b6ba6084b1cAllan Foster * compliance with the License.
4a2f0f0be43dfd4c1b490cbf3cc48b6ba6084b1cAllan Foster * You can obtain a copy of the License at
4a2f0f0be43dfd4c1b490cbf3cc48b6ba6084b1cAllan Foster * https://opensso.dev.java.net/public/CDDLv1.0.html or
4a2f0f0be43dfd4c1b490cbf3cc48b6ba6084b1cAllan Foster * See the License for the specific language governing
4a2f0f0be43dfd4c1b490cbf3cc48b6ba6084b1cAllan Foster * permission and limitations under the License.
4a2f0f0be43dfd4c1b490cbf3cc48b6ba6084b1cAllan Foster * When distributing Covered Code, include this CDDL
4a2f0f0be43dfd4c1b490cbf3cc48b6ba6084b1cAllan Foster * Header Notice in each file and include the License file
4a2f0f0be43dfd4c1b490cbf3cc48b6ba6084b1cAllan Foster * If applicable, add the following below the CDDL Header,
4a2f0f0be43dfd4c1b490cbf3cc48b6ba6084b1cAllan Foster * with the fields enclosed by brackets [] replaced by
4a2f0f0be43dfd4c1b490cbf3cc48b6ba6084b1cAllan Foster * your own identifying information:
4a2f0f0be43dfd4c1b490cbf3cc48b6ba6084b1cAllan Foster * "Portions Copyrighted [year] [name of copyright owner]"
4a2f0f0be43dfd4c1b490cbf3cc48b6ba6084b1cAllan Foster * $Id: ImportSAML2MetaData.java,v 1.5 2008/07/08 01:12:01 exu Exp $
4a2f0f0be43dfd4c1b490cbf3cc48b6ba6084b1cAllan Foster * Portions Copyrighted 2011 ForgeRock AS
4a2f0f0be43dfd4c1b490cbf3cc48b6ba6084b1cAllan Fosterimport com.sun.identity.saml2.common.SAML2Constants;
4a2f0f0be43dfd4c1b490cbf3cc48b6ba6084b1cAllan Fosterimport com.sun.identity.saml2.meta.SAML2MetaConstants;
4a2f0f0be43dfd4c1b490cbf3cc48b6ba6084b1cAllan Fosterimport com.sun.identity.saml2.meta.SAML2MetaException;
4a2f0f0be43dfd4c1b490cbf3cc48b6ba6084b1cAllan Fosterimport com.sun.identity.saml2.meta.SAML2MetaManager;
4a2f0f0be43dfd4c1b490cbf3cc48b6ba6084b1cAllan Fosterimport com.sun.identity.saml2.meta.SAML2MetaSecurityUtils;
4a2f0f0be43dfd4c1b490cbf3cc48b6ba6084b1cAllan Fosterimport com.sun.identity.saml2.meta.SAML2MetaUtils;
4a2f0f0be43dfd4c1b490cbf3cc48b6ba6084b1cAllan Fosterimport com.sun.identity.saml2.jaxb.entityconfig.BaseConfigType;
4a2f0f0be43dfd4c1b490cbf3cc48b6ba6084b1cAllan Fosterimport com.sun.identity.saml2.jaxb.entityconfig.EntityConfigElement;
4a2f0f0be43dfd4c1b490cbf3cc48b6ba6084b1cAllan Fosterimport com.sun.identity.saml2.jaxb.metadata.EntityDescriptorElement;
4a2f0f0be43dfd4c1b490cbf3cc48b6ba6084b1cAllan Foster * Import SAML2 Metadata.
4a2f0f0be43dfd4c1b490cbf3cc48b6ba6084b1cAllan Foster * Imports meta and extended metadata.
4a2f0f0be43dfd4c1b490cbf3cc48b6ba6084b1cAllan Foster * @param realm Realm of the entity.
4a2f0f0be43dfd4c1b490cbf3cc48b6ba6084b1cAllan Foster * @param metadata Meta data.
4a2f0f0be43dfd4c1b490cbf3cc48b6ba6084b1cAllan Foster * @param extended extended data.
4a2f0f0be43dfd4c1b490cbf3cc48b6ba6084b1cAllan Foster * @return realm and entity ID.
4a2f0f0be43dfd4c1b490cbf3cc48b6ba6084b1cAllan Foster SAML2MetaManager metaManager = new SAML2MetaManager();
4a2f0f0be43dfd4c1b490cbf3cc48b6ba6084b1cAllan Foster Object obj = SAML2MetaUtils.convertStringToJAXB(extended);
4a2f0f0be43dfd4c1b490cbf3cc48b6ba6084b1cAllan Foster configElt = (obj instanceof EntityConfigElement) ?
4a2f0f0be43dfd4c1b490cbf3cc48b6ba6084b1cAllan Foster if (configElt != null && configElt.isHosted()) {
4a2f0f0be43dfd4c1b490cbf3cc48b6ba6084b1cAllan Foster configElt.getIDPSSOConfigOrSPSSOConfigOrAuthnAuthorityConfig();
4a2f0f0be43dfd4c1b490cbf3cc48b6ba6084b1cAllan Foster descriptor = getEntityDescriptorElement(metadata);
4a2f0f0be43dfd4c1b490cbf3cc48b6ba6084b1cAllan Foster metaManager.createEntity(realm, descriptor, configElt);
4a2f0f0be43dfd4c1b490cbf3cc48b6ba6084b1cAllan Foster static EntityDescriptorElement getEntityDescriptorElement(String metadata)
4a2f0f0be43dfd4c1b490cbf3cc48b6ba6084b1cAllan Foster throws SAML2MetaException, JAXBException, WorkflowException {
4a2f0f0be43dfd4c1b490cbf3cc48b6ba6084b1cAllan Foster Document doc = XMLUtils.toDOMDocument(metadata, debug);
4a2f0f0be43dfd4c1b490cbf3cc48b6ba6084b1cAllan Foster "import-entity-exception-invalid-descriptor", null);
4a2f0f0be43dfd4c1b490cbf3cc48b6ba6084b1cAllan Foster if ((!SAML2MetaConstants.ENTITY_DESCRIPTOR.equals(
4a2f0f0be43dfd4c1b490cbf3cc48b6ba6084b1cAllan Foster "import-entity-exception-invalid-descriptor", null);
4a2f0f0be43dfd4c1b490cbf3cc48b6ba6084b1cAllan Foster Object obj = SAML2MetaUtils.convertNodeToJAXB(doc);
4a2f0f0be43dfd4c1b490cbf3cc48b6ba6084b1cAllan Foster return (obj instanceof EntityDescriptorElement) ?
4a2f0f0be43dfd4c1b490cbf3cc48b6ba6084b1cAllan Foster private static void workaroundAbstractRoleDescriptor(Document doc) {
4a2f0f0be43dfd4c1b490cbf3cc48b6ba6084b1cAllan Foster NodeList nl = doc.getDocumentElement().getElementsByTagNameNS(
4a2f0f0be43dfd4c1b490cbf3cc48b6ba6084b1cAllan Foster SAML2MetaConstants.NS_METADATA,SAML2MetaConstants.ROLE_DESCRIPTOR);
4a2f0f0be43dfd4c1b490cbf3cc48b6ba6084b1cAllan Foster String type = child.getAttributeNS(SAML2Constants.NS_XSI, "type");
4a2f0f0be43dfd4c1b490cbf3cc48b6ba6084b1cAllan Foster SAML2MetaConstants.ATTRIBUTE_QUERY_DESCRIPTOR_TYPE)) ||
4a2f0f0be43dfd4c1b490cbf3cc48b6ba6084b1cAllan Foster SAML2MetaConstants.ATTRIBUTE_QUERY_DESCRIPTOR_TYPE))) {
4a2f0f0be43dfd4c1b490cbf3cc48b6ba6084b1cAllan Foster String newTag = type.substring(0, type.length() - 4);
4a2f0f0be43dfd4c1b490cbf3cc48b6ba6084b1cAllan Foster xmlstr = "<" + newTag + xmlstr.substring(index +
4a2f0f0be43dfd4c1b490cbf3cc48b6ba6084b1cAllan Foster xmlstr = xmlstr.substring(0, index) + "</" + newTag +
4a2f0f0be43dfd4c1b490cbf3cc48b6ba6084b1cAllan Foster Document tmpDoc = XMLUtils.toDOMDocument(xmlstr, debug);
4a2f0f0be43dfd4c1b490cbf3cc48b6ba6084b1cAllan Foster doc.importNode(tmpDoc.getDocumentElement(), true);
4a2f0f0be43dfd4c1b490cbf3cc48b6ba6084b1cAllan Foster child.getParentNode().replaceChild(newChild, child);
4a2f0f0be43dfd4c1b490cbf3cc48b6ba6084b1cAllan Foster private static Object workaroundJAXBBug(Object obj) throws JAXBException {
4a2f0f0be43dfd4c1b490cbf3cc48b6ba6084b1cAllan Foster String metadata = SAML2MetaUtils.convertJAXBToString(obj);
4a2f0f0be43dfd4c1b490cbf3cc48b6ba6084b1cAllan Foster String replaced = metadata.replaceAll("<(.*:)?Extensions/>", "");