Search in sources :

Example 36 with WSS4JOutInterceptor

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

the class WSConnectionImpl method configureWSSecurity.

private <T> void configureWSSecurity(Dispatch<T> dispatch) {
    if (this.mcf.getAsSecurityType() == WSManagedConnectionFactory.SecurityType.WSSecurity) {
        Bus bus = BusFactory.getThreadDefaultBus();
        BusFactory.setThreadDefaultBus(this.mcf.getBus());
        try {
            Client client = ((DispatchImpl) dispatch).getClient();
            Endpoint ep = client.getEndpoint();
            // spring configuration file
            if (this.mcf.getOutInterceptors() != null) {
                for (Interceptor i : this.mcf.getOutInterceptors()) {
                    ep.getOutInterceptors().add(i);
                }
            }
            // ws-security pass-thru from custom jaas domain
            Subject subject = ConnectionContext.getSubject();
            if (subject != null) {
                WSSecurityCredential credential = ConnectionContext.getSecurityCredential(subject, WSSecurityCredential.class);
                if (credential != null) {
                    if (credential.useSts()) {
                        dispatch.getRequestContext().put(SecurityConstants.STS_CLIENT, credential.buildStsClient(bus));
                    }
                    if (credential.getSecurityHandler() == WSSecurityCredential.SecurityHandler.WSS4J) {
                        ep.getOutInterceptors().add(new WSS4JOutInterceptor(credential.getRequestPropterties()));
                        ep.getInInterceptors().add(new WSS4JInInterceptor(credential.getResponsePropterties()));
                    } else if (credential.getSecurityHandler() == WSSecurityCredential.SecurityHandler.WSPOLICY) {
                        dispatch.getRequestContext().putAll(credential.getRequestPropterties());
                        dispatch.getResponseContext().putAll(credential.getResponsePropterties());
                    }
                }
                // When properties are set on subject treat them as they can configure WS-Security
                HashMap<String, String> properties = ConnectionContext.getSecurityCredential(subject, HashMap.class);
                for (String key : properties.keySet()) {
                    if (key.startsWith("ws-security.")) {
                        // $NON-NLS-1$
                        ep.put(key, properties.get(key));
                    }
                }
            }
        } finally {
            BusFactory.setThreadDefaultBus(bus);
        }
    }
}
Also used : Bus(org.apache.cxf.Bus) Endpoint(org.apache.cxf.endpoint.Endpoint) DispatchImpl(org.apache.cxf.jaxws.DispatchImpl) WSS4JInInterceptor(org.apache.cxf.ws.security.wss4j.WSS4JInInterceptor) WSS4JOutInterceptor(org.apache.cxf.ws.security.wss4j.WSS4JOutInterceptor) Client(org.apache.cxf.endpoint.Client) WebClient(org.apache.cxf.jaxrs.client.WebClient) WSS4JInInterceptor(org.apache.cxf.ws.security.wss4j.WSS4JInInterceptor) WSS4JOutInterceptor(org.apache.cxf.ws.security.wss4j.WSS4JOutInterceptor) Interceptor(org.apache.cxf.interceptor.Interceptor) Subject(javax.security.auth.Subject)

Example 37 with WSS4JOutInterceptor

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

the class CalculatorTest method testCalculatorViaWsInterfaceWithTimestamp2ways.

public void testCalculatorViaWsInterfaceWithTimestamp2ways() throws Exception {
    final Service calcService = Service.create(new URL("http://localhost:" + port + "/webservice-ws-security/CalculatorImplTimestamp2ways?wsdl"), new QName("http://superbiz.org/wsdl", "CalculatorWsService"));
    assertNotNull(calcService);
    // for debugging (ie. TCPMon)
    calcService.addPort(new QName("http://superbiz.org/wsdl", "CalculatorWsService2"), SOAPBinding.SOAP12HTTP_BINDING, "http://127.0.0.1:8204/CalculatorImplTimestamp2ways");
    // CalculatorWs calc = calcService.getPort(
    // new QName("http://superbiz.org/wsdl", "CalculatorWsService2"),
    // CalculatorWs.class);
    final CalculatorWs calc = calcService.getPort(CalculatorWs.class);
    final Client client = ClientProxy.getClient(calc);
    final Endpoint endpoint = client.getEndpoint();
    endpoint.getOutInterceptors().add(new SAAJOutInterceptor());
    endpoint.getInInterceptors().add(new SAAJInInterceptor());
    final Map<String, Object> outProps = new HashMap<String, Object>();
    outProps.put(WSHandlerConstants.ACTION, WSHandlerConstants.TIMESTAMP);
    final WSS4JOutInterceptor wssOut = new WSS4JOutInterceptor(outProps);
    endpoint.getOutInterceptors().add(wssOut);
    final Map<String, Object> inProps = new HashMap<String, Object>();
    inProps.put(WSHandlerConstants.ACTION, WSHandlerConstants.TIMESTAMP);
    final WSS4JInInterceptor wssIn = new WSS4JInInterceptor(inProps);
    endpoint.getInInterceptors().add(wssIn);
    assertEquals(12, calc.multiply(3, 4));
}
Also used : SAAJOutInterceptor(org.apache.cxf.binding.soap.saaj.SAAJOutInterceptor) HashMap(java.util.HashMap) QName(javax.xml.namespace.QName) Service(javax.xml.ws.Service) URL(java.net.URL) SAAJInInterceptor(org.apache.cxf.binding.soap.saaj.SAAJInInterceptor) Endpoint(org.apache.cxf.endpoint.Endpoint) WSS4JInInterceptor(org.apache.cxf.ws.security.wss4j.WSS4JInInterceptor) WSS4JOutInterceptor(org.apache.cxf.ws.security.wss4j.WSS4JOutInterceptor) Client(org.apache.cxf.endpoint.Client)

Example 38 with WSS4JOutInterceptor

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

the class CalculatorTest method testCalculatorViaWsInterfaceWithUsernameTokenPlainPasswordEncrypt.

public void testCalculatorViaWsInterfaceWithUsernameTokenPlainPasswordEncrypt() throws Exception {
    final Service calcService = Service.create(new URL("http://localhost:" + port + "/webservice-ws-security/CalculatorImplUsernameTokenPlainPasswordEncrypt?wsdl"), new QName("http://superbiz.org/wsdl", "CalculatorWsService"));
    assertNotNull(calcService);
    // for debugging (ie. TCPMon)
    calcService.addPort(new QName("http://superbiz.org/wsdl", "CalculatorWsService2"), SOAPBinding.SOAP12HTTP_BINDING, "http://127.0.0.1:8204/CalculatorImplUsernameTokenPlainPasswordEncrypt");
    // CalculatorWs calc = calcService.getPort(
    // new QName("http://superbiz.org/wsdl", "CalculatorWsService2"),
    // CalculatorWs.class);
    final CalculatorWs calc = calcService.getPort(CalculatorWs.class);
    final Client client = ClientProxy.getClient(calc);
    final Endpoint endpoint = client.getEndpoint();
    endpoint.getOutInterceptors().add(new SAAJOutInterceptor());
    final Map<String, Object> outProps = new HashMap<String, Object>();
    outProps.put(WSHandlerConstants.ACTION, WSHandlerConstants.USERNAME_TOKEN + " " + WSHandlerConstants.ENCRYPT);
    outProps.put(WSHandlerConstants.USER, "jane");
    outProps.put(WSHandlerConstants.PASSWORD_TYPE, WSConstants.PW_TEXT);
    outProps.put(WSHandlerConstants.PW_CALLBACK_REF, new CallbackHandler() {

        @Override
        public void handle(final Callback[] callbacks) throws IOException, UnsupportedCallbackException {
            final WSPasswordCallback pc = (WSPasswordCallback) callbacks[0];
            pc.setPassword("waterfall");
        }
    });
    outProps.put(WSHandlerConstants.ENC_PROP_FILE, "META-INF/CalculatorImplUsernameTokenPlainPasswordEncrypt-client.properties");
    outProps.put(WSHandlerConstants.ENCRYPTION_USER, "serveralias");
    final WSS4JOutInterceptor wssOut = new WSS4JOutInterceptor(outProps);
    endpoint.getOutInterceptors().add(wssOut);
    assertEquals(10, calc.sum(4, 6));
}
Also used : SAAJOutInterceptor(org.apache.cxf.binding.soap.saaj.SAAJOutInterceptor) CallbackHandler(javax.security.auth.callback.CallbackHandler) HashMap(java.util.HashMap) QName(javax.xml.namespace.QName) Service(javax.xml.ws.Service) IOException(java.io.IOException) URL(java.net.URL) WSPasswordCallback(org.apache.wss4j.common.ext.WSPasswordCallback) Callback(javax.security.auth.callback.Callback) Endpoint(org.apache.cxf.endpoint.Endpoint) UnsupportedCallbackException(javax.security.auth.callback.UnsupportedCallbackException) WSS4JOutInterceptor(org.apache.cxf.ws.security.wss4j.WSS4JOutInterceptor) Client(org.apache.cxf.endpoint.Client) WSPasswordCallback(org.apache.wss4j.common.ext.WSPasswordCallback)

Example 39 with WSS4JOutInterceptor

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

the class CalculatorTest method testCalculatorViaWsInterfaceFactoryBean.

public void testCalculatorViaWsInterfaceFactoryBean() throws Exception {
    final JaxWsProxyFactoryBean factory = new JaxWsProxyFactoryBean();
    factory.setServiceClass(CalculatorWs.class);
    factory.setAddress("http://localhost:" + port + "/webservice-ws-security/CalculatorImpl");
    final CalculatorWs calc = (CalculatorWs) factory.create();
    final Client client = ClientProxy.getClient(calc);
    final Endpoint endpoint = client.getEndpoint();
    endpoint.getOutInterceptors().add(new SAAJOutInterceptor());
    final Map<String, Object> outProps = new HashMap<>();
    outProps.put(WSHandlerConstants.ACTION, WSHandlerConstants.USERNAME_TOKEN);
    outProps.put(WSHandlerConstants.USER, "jane");
    outProps.put(WSHandlerConstants.PASSWORD_TYPE, WSConstants.PW_TEXT);
    outProps.put(WSHandlerConstants.PW_CALLBACK_REF, new CallbackHandler() {

        @Override
        public void handle(final Callback[] callbacks) throws IOException, UnsupportedCallbackException {
            final WSPasswordCallback pc = (WSPasswordCallback) callbacks[0];
            pc.setPassword("waterfall");
        }
    });
    final WSS4JOutInterceptor wssOut = new WSS4JOutInterceptor(outProps);
    endpoint.getOutInterceptors().add(wssOut);
    assertEquals(10, calc.sum(4, 6));
}
Also used : SAAJOutInterceptor(org.apache.cxf.binding.soap.saaj.SAAJOutInterceptor) CallbackHandler(javax.security.auth.callback.CallbackHandler) HashMap(java.util.HashMap) JaxWsProxyFactoryBean(org.apache.cxf.jaxws.JaxWsProxyFactoryBean) IOException(java.io.IOException) WSPasswordCallback(org.apache.wss4j.common.ext.WSPasswordCallback) Callback(javax.security.auth.callback.Callback) Endpoint(org.apache.cxf.endpoint.Endpoint) UnsupportedCallbackException(javax.security.auth.callback.UnsupportedCallbackException) WSS4JOutInterceptor(org.apache.cxf.ws.security.wss4j.WSS4JOutInterceptor) Client(org.apache.cxf.endpoint.Client) WSPasswordCallback(org.apache.wss4j.common.ext.WSPasswordCallback)

Example 40 with WSS4JOutInterceptor

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

the class CalculatorTest method call.

@Test
public void call() throws MalformedURLException {
    final EJBContainer container = EJBContainer.createEJBContainer(new Properties() {

        {
            setProperty(OpenEjbContainer.OPENEJB_EMBEDDED_REMOTABLE, "true");
            // random port to avoid issue on CI, default is 4204
            setProperty("httpejbd.port", "0");
        }
    });
    // get back the random port
    final int port = Integer.parseInt(SystemInstance.get().getProperty("httpejbd.port"));
    // normal call
    final Service service = Service.create(new URL("http://127.0.0.1:" + port + "/webservice-ws-with-resources-config/CalculatorBean?wsdl"), new QName("http://security.ws.superbiz.org/", "CalculatorBeanService"));
    final Calculator calculator = service.getPort(Calculator.class);
    ClientProxy.getClient(calculator).getOutInterceptors().add(new WSS4JOutInterceptor(new HashMap<String, Object>() {

        {
            put("action", "UsernameToken");
            put("user", "openejb");
            put("passwordType", "PasswordText");
            put("passwordCallbackRef", new CallbackHandler() {

                @Override
                public void handle(Callback[] callbacks) throws IOException, UnsupportedCallbackException {
                    final WSPasswordCallback pc = (WSPasswordCallback) callbacks[0];
                    pc.setPassword("tomee");
                }
            });
        }
    }));
    assertEquals(5, calculator.add(2, 3));
    // bad auth
    final Calculator calculator2 = service.getPort(Calculator.class);
    ClientProxy.getClient(calculator2).getOutInterceptors().add(new WSS4JOutInterceptor(new HashMap<String, Object>() {

        {
            put("action", "UsernameToken");
            put("user", "openejb");
            put("passwordType", "PasswordText");
            put("passwordCallbackRef", new CallbackHandler() {

                @Override
                public void handle(Callback[] callbacks) throws IOException, UnsupportedCallbackException {
                    final WSPasswordCallback pc = (WSPasswordCallback) callbacks[0];
                    pc.setPassword("wrong");
                }
            });
        }
    }));
    try {
        assertEquals(5, calculator2.add(2, 3));
    } catch (SOAPFaultException sfe) {
        assertThat(sfe.getMessage(), CoreMatchers.containsString("A security error was encountered when verifying the message"));
    }
    container.close();
    // valid it passed because all was fine and not because the server config was not here
    assertTrue(PasswordCallbackHandler.wasCalled());
}
Also used : CallbackHandler(javax.security.auth.callback.CallbackHandler) HashMap(java.util.HashMap) QName(javax.xml.namespace.QName) Service(javax.xml.ws.Service) SOAPFaultException(javax.xml.ws.soap.SOAPFaultException) IOException(java.io.IOException) Properties(java.util.Properties) URL(java.net.URL) WSPasswordCallback(org.apache.wss4j.common.ext.WSPasswordCallback) Callback(javax.security.auth.callback.Callback) EJBContainer(javax.ejb.embeddable.EJBContainer) WSS4JOutInterceptor(org.apache.cxf.ws.security.wss4j.WSS4JOutInterceptor) UnsupportedCallbackException(javax.security.auth.callback.UnsupportedCallbackException) WSPasswordCallback(org.apache.wss4j.common.ext.WSPasswordCallback) Test(org.junit.Test)

Aggregations

WSS4JOutInterceptor (org.apache.cxf.ws.security.wss4j.WSS4JOutInterceptor)65 HashMap (java.util.HashMap)50 QName (javax.xml.namespace.QName)32 URL (java.net.URL)31 Client (org.apache.cxf.endpoint.Client)29 Service (javax.xml.ws.Service)27 Test (org.junit.Test)23 Bus (org.apache.cxf.Bus)20 SpringBusFactory (org.apache.cxf.bus.spring.SpringBusFactory)18 DoubleItPortType (org.example.contract.doubleit.DoubleItPortType)16 SOAPFaultException (javax.xml.ws.soap.SOAPFaultException)12 WSS4JStaxOutInterceptor (org.apache.cxf.ws.security.wss4j.WSS4JStaxOutInterceptor)12 Endpoint (org.apache.cxf.endpoint.Endpoint)10 SAAJOutInterceptor (org.apache.cxf.binding.soap.saaj.SAAJOutInterceptor)9 CallbackHandler (javax.security.auth.callback.CallbackHandler)8 WSPasswordCallback (org.apache.wss4j.common.ext.WSPasswordCallback)8 ClassPathXmlApplicationContext (org.springframework.context.support.ClassPathXmlApplicationContext)8 IOException (java.io.IOException)7 Callback (javax.security.auth.callback.Callback)7 UnsupportedCallbackException (javax.security.auth.callback.UnsupportedCallbackException)7