Search in sources :

Example 11 with WSS4JStaxOutInterceptor

use of org.apache.cxf.ws.security.wss4j.WSS4JStaxOutInterceptor in project cxf by apache.

the class CryptoCoverageCheckerTest method testEncryptedUsernameToken.

@org.junit.Test
public void testEncryptedUsernameToken() throws Exception {
    if (!unrestrictedPoliciesInstalled) {
        return;
    }
    SpringBusFactory bf = new SpringBusFactory();
    URL busFile = CryptoCoverageCheckerTest.class.getResource("client.xml");
    Bus bus = bf.createBus(busFile.toString());
    BusFactory.setDefaultBus(bus);
    BusFactory.setThreadDefaultBus(bus);
    URL wsdl = CryptoCoverageCheckerTest.class.getResource("DoubleItCoverageChecker.wsdl");
    Service service = Service.create(wsdl, SERVICE_QNAME);
    QName portQName = new QName(NAMESPACE, "DoubleItEncryptedUsernameTokenPort");
    DoubleItPortType port = service.getPort(portQName, DoubleItPortType.class);
    updateAddressPort(port, test.getPort());
    Map<String, Object> outProps = new HashMap<>();
    outProps.put("action", "UsernameToken Encrypt");
    outProps.put("encryptionPropFile", "bob.properties");
    outProps.put("user", "alice");
    outProps.put("encryptionUser", "bob");
    outProps.put("passwordCallbackClass", "org.apache.cxf.systest.ws.common.KeystorePasswordCallback");
    outProps.put("encryptionParts", "{}{http://schemas.xmlsoap.org/soap/envelope/}Body;" + "{Element}{" + WSS4JConstants.WSSE_NS + "}UsernameToken;");
    if (test.isStreaming()) {
        SecurityTestUtil.enableStreaming(port);
    }
    if (test.isStreaming()) {
        WSS4JStaxOutInterceptor staxOutInterceptor = new WSS4JStaxOutInterceptor(outProps);
        bus.getOutInterceptors().add(staxOutInterceptor);
    } else {
        WSS4JOutInterceptor outInterceptor = new WSS4JOutInterceptor(outProps);
        bus.getOutInterceptors().add(outInterceptor);
    }
    assertEquals(50, port.doubleIt(25));
    ((java.io.Closeable) port).close();
    bus.shutdown(true);
}
Also used : WSS4JStaxOutInterceptor(org.apache.cxf.ws.security.wss4j.WSS4JStaxOutInterceptor) Bus(org.apache.cxf.Bus) HashMap(java.util.HashMap) QName(javax.xml.namespace.QName) Service(javax.xml.ws.Service) URL(java.net.URL) SpringBusFactory(org.apache.cxf.bus.spring.SpringBusFactory) DoubleItPortType(org.example.contract.doubleit.DoubleItPortType) WSS4JOutInterceptor(org.apache.cxf.ws.security.wss4j.WSS4JOutInterceptor)

Example 12 with WSS4JStaxOutInterceptor

use of org.apache.cxf.ws.security.wss4j.WSS4JStaxOutInterceptor in project cxf by apache.

the class ActionTest method testSignatureProgrammaticStAX.

@org.junit.Test
public void testSignatureProgrammaticStAX() throws Exception {
    SpringBusFactory bf = new SpringBusFactory();
    URL busFile = ActionTest.class.getResource("client.xml");
    Bus bus = bf.createBus(busFile.toString());
    BusFactory.setDefaultBus(bus);
    BusFactory.setThreadDefaultBus(bus);
    URL wsdl = ActionTest.class.getResource("DoubleItAction.wsdl");
    Service service = Service.create(wsdl, SERVICE_QNAME);
    QName portQName = new QName(NAMESPACE, "DoubleItSignatureConfigPort");
    DoubleItPortType port = service.getPort(portQName, DoubleItPortType.class);
    updateAddressPort(port, PORT);
    // Programmatic interceptor
    WSSSecurityProperties properties = new WSSSecurityProperties();
    properties.setActions(Collections.singletonList(WSSConstants.SIGNATURE));
    properties.setSignatureUser("alice");
    properties.setCallbackHandler(new KeystorePasswordCallback());
    properties.setSignatureKeyIdentifier(WSSecurityTokenConstants.KEYIDENTIFIER_SECURITY_TOKEN_DIRECT_REFERENCE);
    Properties sigProperties = CryptoFactory.getProperties("alice.properties", this.getClass().getClassLoader());
    properties.setSignatureCryptoProperties(sigProperties);
    WSS4JStaxOutInterceptor outInterceptor = new WSS4JStaxOutInterceptor(properties);
    Client client = ClientProxy.getClient(port);
    client.getOutInterceptors().add(outInterceptor);
    assertEquals(50, port.doubleIt(25));
    ((java.io.Closeable) port).close();
    bus.shutdown(true);
}
Also used : WSS4JStaxOutInterceptor(org.apache.cxf.ws.security.wss4j.WSS4JStaxOutInterceptor) Bus(org.apache.cxf.Bus) WSSSecurityProperties(org.apache.wss4j.stax.ext.WSSSecurityProperties) QName(javax.xml.namespace.QName) Service(javax.xml.ws.Service) Properties(java.util.Properties) WSSSecurityProperties(org.apache.wss4j.stax.ext.WSSSecurityProperties) URL(java.net.URL) SpringBusFactory(org.apache.cxf.bus.spring.SpringBusFactory) DoubleItPortType(org.example.contract.doubleit.DoubleItPortType) Client(org.apache.cxf.endpoint.Client) KeystorePasswordCallback(org.apache.cxf.systest.ws.common.KeystorePasswordCallback)

Example 13 with WSS4JStaxOutInterceptor

use of org.apache.cxf.ws.security.wss4j.WSS4JStaxOutInterceptor in project cxf by apache.

the class WSSecurityClientTest method testUsernameTokenStreaming.

@Test
public void testUsernameTokenStreaming() throws Exception {
    final javax.xml.ws.Service svc = javax.xml.ws.Service.create(WSDL_LOC, GREETER_SERVICE_QNAME);
    final Greeter greeter = svc.getPort(USERNAME_TOKEN_PORT_QNAME, Greeter.class);
    updateAddressPort(greeter, test.getPort());
    Client client = ClientProxy.getClient(greeter);
    Map<String, Object> props = new HashMap<>();
    props.put("action", "UsernameToken");
    props.put("user", "alice");
    props.put("passwordType", "PasswordText");
    WSS4JStaxOutInterceptor wss4jOut = new WSS4JStaxOutInterceptor(props);
    client.getOutInterceptors().add(wss4jOut);
    ((BindingProvider) greeter).getRequestContext().put("password", "password");
    try {
        greeter.greetMe("CXF");
        fail("should fail because of password text instead of digest");
    } catch (Exception ex) {
    // expected
    }
    client.getOutInterceptors().remove(wss4jOut);
    props.put("passwordType", "PasswordDigest");
    wss4jOut = new WSS4JStaxOutInterceptor(props);
    client.getOutInterceptors().add(wss4jOut);
    String s = greeter.greetMe("CXF");
    assertEquals("Hello CXF", s);
    client.getOutInterceptors().remove(wss4jOut);
    try {
        ((BindingProvider) greeter).getRequestContext().put("password", "foo");
        wss4jOut = new WSS4JStaxOutInterceptor(props);
        client.getOutInterceptors().add(wss4jOut);
        greeter.greetMe("CXF");
        fail("should fail");
    } catch (Exception ex) {
    // expected
    }
    client.getOutInterceptors().remove(wss4jOut);
    try {
        props.put("passwordType", "PasswordText");
        wss4jOut = new WSS4JStaxOutInterceptor(props);
        client.getOutInterceptors().add(wss4jOut);
        ((BindingProvider) greeter).getRequestContext().put("password", "password");
        greeter.greetMe("CXF");
        fail("should fail");
    } catch (Exception ex) {
    // expected
    }
    client.getOutInterceptors().remove(wss4jOut);
    ((java.io.Closeable) greeter).close();
}
Also used : WSS4JStaxOutInterceptor(org.apache.cxf.ws.security.wss4j.WSS4JStaxOutInterceptor) HashMap(java.util.HashMap) Service(javax.xml.ws.Service) WSSecurityException(org.apache.wss4j.common.ext.WSSecurityException) SOAPFaultException(javax.xml.ws.soap.SOAPFaultException) Greeter(org.apache.hello_world_soap_http.Greeter) Client(org.apache.cxf.endpoint.Client) Test(org.junit.Test)

Example 14 with WSS4JStaxOutInterceptor

use of org.apache.cxf.ws.security.wss4j.WSS4JStaxOutInterceptor in project cxf by apache.

the class CryptoCoverageCheckerTest method testSignedBodyTimestampSoap12.

@org.junit.Test
public void testSignedBodyTimestampSoap12() throws Exception {
    SpringBusFactory bf = new SpringBusFactory();
    URL busFile = CryptoCoverageCheckerTest.class.getResource("client.xml");
    Bus bus = bf.createBus(busFile.toString());
    BusFactory.setDefaultBus(bus);
    BusFactory.setThreadDefaultBus(bus);
    URL wsdl = CryptoCoverageCheckerTest.class.getResource("DoubleItCoverageChecker.wsdl");
    Service service = Service.create(wsdl, SERVICE_QNAME);
    QName portQName = new QName(NAMESPACE, "DoubleItBodyTimestampSoap12Port");
    DoubleItPortType port = service.getPort(portQName, DoubleItPortType.class);
    updateAddressPort(port, test.getPort());
    Map<String, Object> outProps = new HashMap<>();
    outProps.put("action", "Timestamp Signature");
    outProps.put("signaturePropFile", "alice.properties");
    outProps.put("user", "alice");
    outProps.put("passwordCallbackClass", "org.apache.cxf.systest.ws.common.KeystorePasswordCallback");
    outProps.put("signatureParts", "{}{http://www.w3.org/2003/05/soap-envelope}Body;" + "{}{http://docs.oasis-open.org/wss/2004/01/oasis-" + "200401-wss-wssecurity-utility-1.0.xsd}Timestamp;");
    if (test.isStreaming()) {
        SecurityTestUtil.enableStreaming(port);
    }
    if (test.isStreaming()) {
        WSS4JStaxOutInterceptor staxOutInterceptor = new WSS4JStaxOutInterceptor(outProps);
        bus.getOutInterceptors().add(staxOutInterceptor);
    } else {
        WSS4JOutInterceptor outInterceptor = new WSS4JOutInterceptor(outProps);
        bus.getOutInterceptors().add(outInterceptor);
    }
    assertEquals(50, port.doubleIt(25));
    ((java.io.Closeable) port).close();
    bus.shutdown(true);
}
Also used : WSS4JStaxOutInterceptor(org.apache.cxf.ws.security.wss4j.WSS4JStaxOutInterceptor) Bus(org.apache.cxf.Bus) HashMap(java.util.HashMap) QName(javax.xml.namespace.QName) Service(javax.xml.ws.Service) URL(java.net.URL) SpringBusFactory(org.apache.cxf.bus.spring.SpringBusFactory) DoubleItPortType(org.example.contract.doubleit.DoubleItPortType) WSS4JOutInterceptor(org.apache.cxf.ws.security.wss4j.WSS4JOutInterceptor)

Example 15 with WSS4JStaxOutInterceptor

use of org.apache.cxf.ws.security.wss4j.WSS4JStaxOutInterceptor in project cxf by apache.

the class StaxClient method main.

public static void main(String[] args) throws Exception {
    try {
        SpringBusFactory bf = new SpringBusFactory();
        URL busFile = StaxClient.class.getResource("/wssec.xml");
        Bus bus = bf.createBus(busFile.toString());
        BusFactory.setDefaultBus(bus);
        WSSSecurityProperties properties = new WSSSecurityProperties();
        properties.addAction(WSSConstants.USERNAMETOKEN);
        properties.addAction(WSSConstants.TIMESTAMP);
        properties.setUsernameTokenPasswordType(WSSConstants.UsernameTokenPasswordType.PASSWORD_DIGEST);
        properties.setTokenUser("abcd");
        properties.setCallbackHandler(new UTPasswordCallback());
        WSSSecurityProperties inProperties = new WSSSecurityProperties();
        inProperties.addAction(WSSConstants.USERNAMETOKEN);
        inProperties.addAction(WSSConstants.TIMESTAMP);
        inProperties.setUsernameTokenPasswordType(WSSConstants.UsernameTokenPasswordType.PASSWORD_TEXT);
        inProperties.setCallbackHandler(new UTPasswordCallback());
        GreeterService service = new GreeterService();
        Greeter port = service.getGreeterPort();
        org.apache.cxf.endpoint.Client client = ClientProxy.getClient(port);
        client.getInInterceptors().add(new WSS4JStaxInInterceptor(inProperties));
        client.getOutInterceptors().add(new WSS4JStaxOutInterceptor(properties));
        String[] names = new String[] { "Anne", "Bill", "Chris", "Scott" };
        // make a sequence of 4 invocations
        for (int i = 0; i < 4; i++) {
            System.out.println("Invoking greetMe...");
            String response = port.greetMe(names[i]);
            System.out.println("response: " + response + "\n");
        }
        if (port instanceof Closeable) {
            ((Closeable) port).close();
        }
        bus.shutdown(true);
    } catch (UndeclaredThrowableException ex) {
        ex.getUndeclaredThrowable().printStackTrace();
    } catch (Exception ex) {
        ex.printStackTrace();
    } finally {
        System.exit(0);
    }
}
Also used : WSS4JStaxOutInterceptor(org.apache.cxf.ws.security.wss4j.WSS4JStaxOutInterceptor) Bus(org.apache.cxf.Bus) WSSSecurityProperties(org.apache.wss4j.stax.ext.WSSSecurityProperties) WSS4JStaxInInterceptor(org.apache.cxf.ws.security.wss4j.WSS4JStaxInInterceptor) Closeable(java.io.Closeable) GreeterService(org.apache.cxf.hello_world_soap_http.GreeterService) URL(java.net.URL) UndeclaredThrowableException(java.lang.reflect.UndeclaredThrowableException) SpringBusFactory(org.apache.cxf.bus.spring.SpringBusFactory) Greeter(org.apache.cxf.hello_world_soap_http.Greeter) UndeclaredThrowableException(java.lang.reflect.UndeclaredThrowableException) UTPasswordCallback(demo.wssec.server.UTPasswordCallback)

Aggregations

WSS4JStaxOutInterceptor (org.apache.cxf.ws.security.wss4j.WSS4JStaxOutInterceptor)27 HashMap (java.util.HashMap)25 QName (javax.xml.namespace.QName)25 URL (java.net.URL)14 Service (javax.xml.ws.Service)14 Bus (org.apache.cxf.Bus)14 SpringBusFactory (org.apache.cxf.bus.spring.SpringBusFactory)14 Client (org.apache.cxf.endpoint.Client)14 DoubleItPortType (org.example.contract.doubleit.DoubleItPortType)13 Test (org.junit.Test)13 LoggingInInterceptor (org.apache.cxf.ext.logging.LoggingInInterceptor)12 LoggingOutInterceptor (org.apache.cxf.ext.logging.LoggingOutInterceptor)12 Service (org.apache.cxf.service.Service)12 AbstractSecurityTest (org.apache.cxf.ws.security.wss4j.AbstractSecurityTest)12 Echo (org.apache.cxf.ws.security.wss4j.Echo)12 WSS4JInInterceptor (org.apache.cxf.ws.security.wss4j.WSS4JInInterceptor)12 WSS4JOutInterceptor (org.apache.cxf.ws.security.wss4j.WSS4JOutInterceptor)12 WSSSecurityProperties (org.apache.wss4j.stax.ext.WSSSecurityProperties)8 ArrayList (java.util.ArrayList)6 WSSConstants (org.apache.wss4j.stax.ext.WSSConstants)6