ws-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From cohei...@apache.org
Subject svn commit: r1667818 - in /webservices/wss4j/trunk: ws-security-common/ ws-security-common/src/main/java/org/apache/wss4j/common/saml/ ws-security-dom/src/test/java/org/apache/wss4j/dom/saml/ ws-security-stax/src/main/java/org/apache/wss4j/stax/impl/pr...
Date Thu, 19 Mar 2015 17:11:17 GMT
Author: coheigea
Date: Thu Mar 19 17:11:17 2015
New Revision: 1667818

URL: http://svn.apache.org/r1667818
Log:
Fixing loading OpenSAML 3.x in an OSGi container

Added:
    webservices/wss4j/trunk/ws-security-common/src/main/java/org/apache/wss4j/common/saml/OpenSAMLBootstrap.java
Modified:
    webservices/wss4j/trunk/ws-security-common/pom.xml
    webservices/wss4j/trunk/ws-security-common/src/main/java/org/apache/wss4j/common/saml/OpenSAMLUtil.java
    webservices/wss4j/trunk/ws-security-dom/src/test/java/org/apache/wss4j/dom/saml/SamlTokenTest.java
    webservices/wss4j/trunk/ws-security-stax/src/main/java/org/apache/wss4j/stax/impl/processor/output/SAMLTokenOutputProcessor.java

Modified: webservices/wss4j/trunk/ws-security-common/pom.xml
URL: http://svn.apache.org/viewvc/webservices/wss4j/trunk/ws-security-common/pom.xml?rev=1667818&r1=1667817&r2=1667818&view=diff
==============================================================================
--- webservices/wss4j/trunk/ws-security-common/pom.xml (original)
+++ webservices/wss4j/trunk/ws-security-common/pom.xml Thu Mar 19 17:11:17 2015
@@ -132,6 +132,18 @@
             </exclusions>
         </dependency>
         <dependency>
+            <groupId>org.opensaml</groupId>
+            <artifactId>opensaml-xacml-impl</artifactId>
+            <version>${opensaml.version}</version>
+            <scope>compile</scope>
+        </dependency>
+        <dependency>
+            <groupId>org.opensaml</groupId>
+            <artifactId>opensaml-xacml-saml-impl</artifactId>
+            <version>${opensaml.version}</version>
+            <scope>compile</scope>
+        </dependency>
+        <dependency>
             <groupId>org.apache.directory.server</groupId>
             <artifactId>apacheds-kerberos-codec</artifactId>
             <optional>true</optional>

Added: webservices/wss4j/trunk/ws-security-common/src/main/java/org/apache/wss4j/common/saml/OpenSAMLBootstrap.java
URL: http://svn.apache.org/viewvc/webservices/wss4j/trunk/ws-security-common/src/main/java/org/apache/wss4j/common/saml/OpenSAMLBootstrap.java?rev=1667818&view=auto
==============================================================================
--- webservices/wss4j/trunk/ws-security-common/src/main/java/org/apache/wss4j/common/saml/OpenSAMLBootstrap.java
(added)
+++ webservices/wss4j/trunk/ws-security-common/src/main/java/org/apache/wss4j/common/saml/OpenSAMLBootstrap.java
Thu Mar 19 17:11:17 2015
@@ -0,0 +1,118 @@
+/**
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you 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 org.apache.wss4j.common.saml;
+
+import java.io.InputStream;
+
+import javax.naming.ConfigurationException;
+
+import org.opensaml.core.config.Configuration;
+import org.opensaml.core.xml.config.XMLConfigurationException;
+import org.opensaml.core.xml.config.XMLConfigurator;
+import org.opensaml.core.xml.config.XMLObjectProviderRegistrySupport;
+
+/**
+ * This class intializes the Opensaml library.
+ */
+public class OpenSAMLBootstrap {
+    
+    /** List of default configuration files. */
+    private static final String[] xmlConfigs = { 
+        "/default-config.xml",
+        "/schema-config.xml",
+        "/saml1-assertion-config.xml", 
+        "/saml1-metadata-config.xml", 
+        "/saml1-protocol-config.xml",
+        "/saml2-assertion-config.xml", 
+        "/saml2-assertion-delegation-restriction-config.xml",    
+        "/saml2-ecp-config.xml",
+        "/saml2-metadata-algorithm-config.xml",
+        "/saml2-metadata-attr-config.xml",
+        "/saml2-metadata-config.xml",
+        "/saml2-metadata-idp-discovery-config.xml",
+        "/saml2-metadata-query-config.xml", 
+        "/saml2-metadata-reqinit-config.xml", 
+        "/saml2-metadata-ui-config.xml",
+        "/saml2-metadata-rpi-config.xml",
+        "/saml2-protocol-config.xml",
+        "/saml2-protocol-thirdparty-config.xml",
+        "/saml2-protocol-aslo-config.xml",
+        "/saml2-channel-binding-config.xml",
+        "/saml-ec-gss-config.xml",
+        "/signature-config.xml",
+        "/encryption-config.xml", 
+        "/xacml20-context-config.xml",
+        "/xacml20-policy-config.xml",
+        "/xacml10-saml2-profile-config.xml",
+        "/xacml11-saml2-profile-config.xml",
+        "/xacml2-saml2-profile-config.xml",
+        "/xacml3-saml2-profile-config.xml",
+        "/saml2-xacml2-profile.xml",
+    };
+    
+    /**
+     * Initializes the OpenSAML library, loading default configurations.
+     * 
+     * @throws ConfigurationException thrown if there is a problem initializing the OpenSAML
library
+     */
+    public static synchronized void bootstrap() throws XMLConfigurationException {
+        ClassLoader loader = Thread.currentThread().getContextClassLoader();
+        try {
+            XMLConfigurator configurator = new XMLConfigurator();
+            
+            Thread.currentThread().setContextClassLoader(XMLObjectProviderRegistrySupport.class.getClassLoader());
+            
+            for (String config : xmlConfigs) {
+                //most are found in the Configuration.class classloader
+                InputStream ins = Configuration.class.getResourceAsStream(config);
+                if (ins == null) {
+                    //some are from us
+                    ins = OpenSAMLBootstrap.class.getResourceAsStream(config);
+                }
+                if (ins != null) {
+                    configurator.load(ins);
+                }
+            }
+        } finally {
+            Thread.currentThread().setContextClassLoader(loader);
+        }
+    }
+    
+    
+    /*
+    protected static void initializeParserPool() throws ConfigurationException {
+        StaticBasicParserPool pp = new StaticBasicParserPool();
+        pp.setMaxPoolSize(50);
+        
+        Map<String, Boolean> features = new HashMap<String, Boolean>();
+        features.put(XMLConstants.FEATURE_SECURE_PROCESSING, true);
+        features.put("http://apache.org/xml/features/disallow-doctype-decl", true);
+        pp.setBuilderFeatures(features);
+        pp.setExpandEntityReferences(false);
+        
+        try {
+            pp.initialize();
+        } catch (XMLParserException e) {
+            throw new ConfigurationException("Error initializing parser pool", e);
+        }
+        Configuration.setParserPool(pp);
+    }
+    */
+}
\ No newline at end of file

Modified: webservices/wss4j/trunk/ws-security-common/src/main/java/org/apache/wss4j/common/saml/OpenSAMLUtil.java
URL: http://svn.apache.org/viewvc/webservices/wss4j/trunk/ws-security-common/src/main/java/org/apache/wss4j/common/saml/OpenSAMLUtil.java?rev=1667818&r1=1667817&r2=1667818&view=diff
==============================================================================
--- webservices/wss4j/trunk/ws-security-common/src/main/java/org/apache/wss4j/common/saml/OpenSAMLUtil.java
(original)
+++ webservices/wss4j/trunk/ws-security-common/src/main/java/org/apache/wss4j/common/saml/OpenSAMLUtil.java
Thu Mar 19 17:11:17 2015
@@ -23,11 +23,14 @@ import javax.xml.namespace.QName;
 
 import org.apache.wss4j.common.crypto.WSProviderConfig;
 import org.apache.wss4j.common.ext.WSSecurityException;
-import org.opensaml.core.config.InitializationException;
-import org.opensaml.core.config.InitializationService;
+import org.opensaml.core.config.Configuration;
+import org.opensaml.core.config.ConfigurationService;
+import org.opensaml.core.config.provider.MapBasedConfiguration;
 import org.opensaml.core.xml.XMLObject;
 import org.opensaml.core.xml.XMLObjectBuilder;
 import org.opensaml.core.xml.XMLObjectBuilderFactory;
+import org.opensaml.core.xml.config.XMLConfigurationException;
+import org.opensaml.core.xml.config.XMLObjectProviderRegistry;
 import org.opensaml.core.xml.config.XMLObjectProviderRegistrySupport;
 import org.opensaml.core.xml.io.Marshaller;
 import org.opensaml.core.xml.io.MarshallerFactory;
@@ -35,6 +38,7 @@ import org.opensaml.core.xml.io.Marshall
 import org.opensaml.core.xml.io.Unmarshaller;
 import org.opensaml.core.xml.io.UnmarshallerFactory;
 import org.opensaml.core.xml.io.UnmarshallingException;
+import org.opensaml.saml.config.SAMLConfiguration;
 import org.opensaml.xmlsec.signature.Signature;
 import org.opensaml.xmlsec.signature.support.SignatureException;
 import org.opensaml.xmlsec.signature.support.Signer;
@@ -66,39 +70,31 @@ public final class OpenSAMLUtil {
             if (LOG.isDebugEnabled()) {
                 LOG.debug("Initializing the opensaml2 library...");
             }
+            WSProviderConfig.init();
+
+            Configuration configuration = new MapBasedConfiguration();
+            ConfigurationService.setConfiguration(configuration);
+
+            XMLObjectProviderRegistry providerRegistry = new XMLObjectProviderRegistry();
+            configuration.register(XMLObjectProviderRegistry.class, providerRegistry, ConfigurationService.DEFAULT_PARTITION_NAME);
+
             try {
-                WSProviderConfig.init();
+                OpenSAMLBootstrap.bootstrap();
                 
-                try {
-                    tryInitWithClassLoader(Thread.currentThread().getContextClassLoader());
-                } catch (Throwable t) {
-                    tryInitWithClassLoader(InitializationService.class.getClassLoader());
-                }
+                SAMLConfiguration samlConfiguration = new SAMLConfiguration();
+                configuration.register(SAMLConfiguration.class, samlConfiguration, ConfigurationService.DEFAULT_PARTITION_NAME);
+
+                builderFactory = XMLObjectProviderRegistrySupport.getBuilderFactory();
+                marshallerFactory = XMLObjectProviderRegistrySupport.getMarshallerFactory();
+                unmarshallerFactory = XMLObjectProviderRegistrySupport.getUnmarshallerFactory();
                 
                 samlEngineInitialized = true;
                 if (LOG.isDebugEnabled()) {
                     LOG.debug("opensaml3 library bootstrap complete");
                 }
-            } catch (InitializationException e) {
-                LOG.error(
-                    "Unable to bootstrap the opensaml3 library - all SAML operations will
fail", 
-                    e
-                );
-             }
-        }
-    }
-    
-    private static void tryInitWithClassLoader(ClassLoader l) throws InitializationException
{
-        ClassLoader loader = Thread.currentThread().getContextClassLoader();
-        try {
-            Thread.currentThread().setContextClassLoader(l);
-            InitializationService.initialize();
-            // OpenSAMLBootstrap.bootstrap();
-            builderFactory = XMLObjectProviderRegistrySupport.getBuilderFactory();
-            marshallerFactory = XMLObjectProviderRegistrySupport.getMarshallerFactory();
-            unmarshallerFactory = XMLObjectProviderRegistrySupport.getUnmarshallerFactory();
-        } finally {
-            Thread.currentThread().setContextClassLoader(loader);
+            } catch (XMLConfigurationException ex) {
+                LOG.error("Unable to bootstrap the opensaml3 library - all SAML operations
will fail", ex);
+            }
         }
     }
 

Modified: webservices/wss4j/trunk/ws-security-dom/src/test/java/org/apache/wss4j/dom/saml/SamlTokenTest.java
URL: http://svn.apache.org/viewvc/webservices/wss4j/trunk/ws-security-dom/src/test/java/org/apache/wss4j/dom/saml/SamlTokenTest.java?rev=1667818&r1=1667817&r2=1667818&view=diff
==============================================================================
--- webservices/wss4j/trunk/ws-security-dom/src/test/java/org/apache/wss4j/dom/saml/SamlTokenTest.java
(original)
+++ webservices/wss4j/trunk/ws-security-dom/src/test/java/org/apache/wss4j/dom/saml/SamlTokenTest.java
Thu Mar 19 17:11:17 2015
@@ -43,6 +43,7 @@ import org.apache.wss4j.common.saml.Saml
 import org.apache.wss4j.common.saml.bean.SubjectConfirmationDataBean;
 import org.apache.wss4j.common.saml.builder.SAML1Constants;
 import org.apache.wss4j.common.saml.builder.SAML2Constants;
+import org.apache.wss4j.common.util.DOM2Writer;
 import org.apache.wss4j.common.util.XMLUtils;
 import org.apache.wss4j.dom.WSConstants;
 import org.apache.wss4j.dom.WSSConfig;
@@ -840,12 +841,13 @@ public class SamlTokenTest extends org.j
         SAMLCallback samlCallback = new SAMLCallback();
         SAMLUtil.doSAMLCallback(callbackHandler, samlCallback);
         SamlAssertionWrapper samlAssertion = new SamlAssertionWrapper(samlCallback);
-        String assertionString = samlAssertion.assertionToString();
         
-        // Convert String to DOM + into an assertionWrapper
         DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
         dbf.setNamespaceAware(true);
+        Document doc = dbf.newDocumentBuilder().newDocument();
+        String assertionString = DOM2Writer.nodeToString(samlAssertion.toDOM(doc));
         
+        // Convert String to DOM + into an assertionWrapper
         InputStream in = new ByteArrayInputStream(assertionString.getBytes());
         Document newDoc = dbf.newDocumentBuilder().parse(in);
         

Modified: webservices/wss4j/trunk/ws-security-stax/src/main/java/org/apache/wss4j/stax/impl/processor/output/SAMLTokenOutputProcessor.java
URL: http://svn.apache.org/viewvc/webservices/wss4j/trunk/ws-security-stax/src/main/java/org/apache/wss4j/stax/impl/processor/output/SAMLTokenOutputProcessor.java?rev=1667818&r1=1667817&r2=1667818&view=diff
==============================================================================
--- webservices/wss4j/trunk/ws-security-stax/src/main/java/org/apache/wss4j/stax/impl/processor/output/SAMLTokenOutputProcessor.java
(original)
+++ webservices/wss4j/trunk/ws-security-stax/src/main/java/org/apache/wss4j/stax/impl/processor/output/SAMLTokenOutputProcessor.java
Thu Mar 19 17:11:17 2015
@@ -28,6 +28,8 @@ import java.util.List;
 
 import javax.crypto.spec.SecretKeySpec;
 import javax.xml.namespace.QName;
+import javax.xml.parsers.DocumentBuilder;
+import javax.xml.parsers.ParserConfigurationException;
 import javax.xml.stream.XMLStreamException;
 
 import org.apache.wss4j.common.crypto.CryptoType;
@@ -58,6 +60,7 @@ import org.apache.xml.security.stax.impl
 import org.apache.xml.security.stax.securityEvent.TokenSecurityEvent;
 import org.apache.xml.security.stax.securityToken.OutboundSecurityToken;
 import org.apache.xml.security.stax.securityToken.SecurityTokenProvider;
+import org.apache.xml.security.utils.XMLUtils;
 import org.opensaml.saml.common.SAMLVersion;
 import org.w3c.dom.Element;
 
@@ -403,7 +406,13 @@ public class SAMLTokenOutputProcessor ex
                 }
                 WSSUtils.updateSecurityHeaderOrder(outputProcessorChain, headerElementName,
getAction(), false);
 
-                outputDOMElement(samlAssertionWrapper.toDOM(null), subOutputProcessorChain);
+                try {
+                    DocumentBuilder db = XMLUtils.createDocumentBuilder(false);
+                    outputDOMElement(samlAssertionWrapper.toDOM(db.newDocument()), subOutputProcessorChain);
+                } catch (ParserConfigurationException ex) {
+                    LOG.debug("Error writing out SAML Assertion", ex);
+                    throw new XMLSecurityException(ex);
+                }
                 if (includeSTR) {
                     WSSUtils.updateSecurityHeaderOrder(
                             outputProcessorChain, WSSConstants.TAG_wsse_SecurityTokenReference,
getAction(), false);                    



Mime
View raw message