You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@cxf.apache.org by co...@apache.org on 2013/02/26 22:27:37 UTC
svn commit: r1450433 - in
/cxf/trunk/systests/ws-security/src/test/java/org/apache/cxf/systest/ws/saml:
SamlTokenTest.java client/SamlElementCallbackHandler.java
Author: coheigea
Date: Tue Feb 26 21:27:36 2013
New Revision: 1450433
URL: http://svn.apache.org/r1450433
Log:
Adding a test that uses a SAML CallbackHandler to set a DOM Element directly on a SAMLCallback
Added:
cxf/trunk/systests/ws-security/src/test/java/org/apache/cxf/systest/ws/saml/client/SamlElementCallbackHandler.java
Modified:
cxf/trunk/systests/ws-security/src/test/java/org/apache/cxf/systest/ws/saml/SamlTokenTest.java
Modified: cxf/trunk/systests/ws-security/src/test/java/org/apache/cxf/systest/ws/saml/SamlTokenTest.java
URL: http://svn.apache.org/viewvc/cxf/trunk/systests/ws-security/src/test/java/org/apache/cxf/systest/ws/saml/SamlTokenTest.java?rev=1450433&r1=1450432&r2=1450433&view=diff
==============================================================================
--- cxf/trunk/systests/ws-security/src/test/java/org/apache/cxf/systest/ws/saml/SamlTokenTest.java (original)
+++ cxf/trunk/systests/ws-security/src/test/java/org/apache/cxf/systest/ws/saml/SamlTokenTest.java Tue Feb 26 21:27:36 2013
@@ -29,6 +29,7 @@ import org.apache.cxf.Bus;
import org.apache.cxf.bus.spring.SpringBusFactory;
import org.apache.cxf.systest.ws.common.SecurityTestUtil;
import org.apache.cxf.systest.ws.saml.client.SamlCallbackHandler;
+import org.apache.cxf.systest.ws.saml.client.SamlElementCallbackHandler;
import org.apache.cxf.systest.ws.saml.client.SamlRoleCallbackHandler;
import org.apache.cxf.systest.ws.saml.server.Server;
import org.apache.cxf.testutil.common.AbstractBusClientServerTestBase;
@@ -108,6 +109,40 @@ public class SamlTokenTest extends Abstr
}
@org.junit.Test
+ public void testSaml1ElementOverTransport() throws Exception {
+
+ SpringBusFactory bf = new SpringBusFactory();
+ URL busFile = SamlTokenTest.class.getResource("client/client.xml");
+
+ Bus bus = bf.createBus(busFile.toString());
+ SpringBusFactory.setDefaultBus(bus);
+ SpringBusFactory.setThreadDefaultBus(bus);
+
+ URL wsdl = SamlTokenTest.class.getResource("DoubleItSaml.wsdl");
+ Service service = Service.create(wsdl, SERVICE_QNAME);
+ QName portQName = new QName(NAMESPACE, "DoubleItSaml1TransportPort");
+ DoubleItPortType saml1Port =
+ service.getPort(portQName, DoubleItPortType.class);
+ updateAddressPort(saml1Port, PORT2);
+
+ try {
+ saml1Port.doubleIt(25);
+ fail("Expected failure on an invocation with no SAML Assertion");
+ } catch (javax.xml.ws.soap.SOAPFaultException ex) {
+ assertTrue(ex.getMessage().contains("No SAML CallbackHandler available"));
+ }
+
+ ((BindingProvider)saml1Port).getRequestContext().put(
+ "ws-security.saml-callback-handler", new SamlElementCallbackHandler(false)
+ );
+ int result = saml1Port.doubleIt(25);
+ assertTrue(result == 50);
+
+ ((java.io.Closeable)saml1Port).close();
+ bus.shutdown(true);
+ }
+
+ @org.junit.Test
public void testSaml2OverSymmetric() throws Exception {
SpringBusFactory bf = new SpringBusFactory();
@@ -611,5 +646,4 @@ public class SamlTokenTest extends Abstr
bus.shutdown(true);
}
-
}
Added: cxf/trunk/systests/ws-security/src/test/java/org/apache/cxf/systest/ws/saml/client/SamlElementCallbackHandler.java
URL: http://svn.apache.org/viewvc/cxf/trunk/systests/ws-security/src/test/java/org/apache/cxf/systest/ws/saml/client/SamlElementCallbackHandler.java?rev=1450433&view=auto
==============================================================================
--- cxf/trunk/systests/ws-security/src/test/java/org/apache/cxf/systest/ws/saml/client/SamlElementCallbackHandler.java (added)
+++ cxf/trunk/systests/ws-security/src/test/java/org/apache/cxf/systest/ws/saml/client/SamlElementCallbackHandler.java Tue Feb 26 21:27:36 2013
@@ -0,0 +1,84 @@
+/**
+ * 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.cxf.systest.ws.saml.client;
+
+import java.io.IOException;
+
+import javax.security.auth.callback.Callback;
+import javax.security.auth.callback.CallbackHandler;
+import javax.security.auth.callback.UnsupportedCallbackException;
+
+import org.w3c.dom.Document;
+import org.w3c.dom.Element;
+
+import org.apache.cxf.helpers.DOMUtils;
+import org.apache.ws.security.saml.ext.AssertionWrapper;
+import org.apache.ws.security.saml.ext.SAMLCallback;
+import org.apache.ws.security.saml.ext.SAMLParms;
+
+/**
+ * A CallbackHandler instance that is used by the STS to mock up a SAML Attribute Assertion. This
+ * particular CallbackHandler creates the SAML Assertion by delegating it to the standard
+ * SamlCallbackHandler, and then just sets it on the SAMLCallback as a DOM Element. Essentially,
+ * this is a test that it's possible to set a DOM Element on the SAMLCallback and have it included
+ * in the request.
+ */
+public class SamlElementCallbackHandler implements CallbackHandler {
+ private boolean saml2 = true;
+
+ public SamlElementCallbackHandler() {
+ //
+ }
+
+ public SamlElementCallbackHandler(boolean saml2) {
+ this.saml2 = saml2;
+ }
+
+ public void handle(Callback[] callbacks) throws IOException, UnsupportedCallbackException {
+ for (int i = 0; i < callbacks.length; i++) {
+ if (callbacks[i] instanceof SAMLCallback) {
+ SAMLCallback callback = (SAMLCallback) callbacks[i];
+ Element assertionElement;
+ try {
+ Document doc = DOMUtils.createDocument();
+ assertionElement = getSAMLAssertion(doc);
+ } catch (Exception e) {
+ throw new IOException(e.getMessage());
+ }
+ callback.setAssertionElement(assertionElement);
+ }
+ }
+ }
+
+ /**
+ * Mock up a SAML Assertion by using another SAMLCallbackHandler
+ * @throws Exception
+ */
+ private Element getSAMLAssertion(Document doc) throws Exception {
+ SAMLParms parms = new SAMLParms();
+ SamlCallbackHandler callbackHandler = new SamlCallbackHandler(saml2);
+ parms.setCallbackHandler(callbackHandler);
+ AssertionWrapper assertionWrapper = new AssertionWrapper(parms);
+
+ return assertionWrapper.toDOM(doc);
+ }
+
+
+}