Search in sources :

Example 1 with XmlEncInInterceptor

use of org.apache.cxf.rs.security.xml.XmlEncInInterceptor in project cxf by apache.

the class JAXRSXmlSecTest method testEncryptionNoSignature.

@Test
public void testEncryptionNoSignature() throws Exception {
    if (test.streaming) {
        // Only testing the endpoints, not the clients here
        return;
    }
    String address = "https://localhost:" + test.port + "/xmlsec-validate";
    JAXRSClientFactoryBean bean = new JAXRSClientFactoryBean();
    bean.setAddress(address);
    SpringBusFactory bf = new SpringBusFactory();
    URL busFile = JAXRSXmlSecTest.class.getResource("client.xml");
    Bus springBus = bf.createBus(busFile.toString());
    bean.setBus(springBus);
    Map<String, Object> properties = new HashMap<>();
    properties.put(SecurityConstants.CALLBACK_HANDLER, "org.apache.cxf.systest.jaxrs.security.saml.KeystorePasswordCallback");
    properties.put(SecurityConstants.ENCRYPT_USERNAME, "bob");
    properties.put(SecurityConstants.ENCRYPT_PROPERTIES, "org/apache/cxf/systest/jaxrs/security/bob.properties");
    properties.put(SecurityConstants.SIGNATURE_PROPERTIES, "org/apache/cxf/systest/jaxrs/security/alice.properties");
    bean.setProperties(properties);
    XmlEncOutInterceptor encInterceptor = new XmlEncOutInterceptor();
    encInterceptor.setKeyIdentifierType(RSSecurityUtils.X509_CERT);
    encInterceptor.setSymmetricEncAlgorithm(XMLCipher.AES_128);
    bean.getOutInterceptors().add(encInterceptor);
    bean.getInInterceptors().add(new XmlEncInInterceptor());
    bean.getInInterceptors().add(new XmlSigInInterceptor());
    bean.setServiceClass(BookStore.class);
    BookStore store = bean.create(BookStore.class);
    try {
        store.addBook(new Book("CXF", 126L));
        fail("Failure expected on no Signature");
    } catch (WebApplicationException ex) {
    // expected
    }
}
Also used : Bus(org.apache.cxf.Bus) BookStore(org.apache.cxf.systest.jaxrs.security.BookStore) JAXRSClientFactoryBean(org.apache.cxf.jaxrs.client.JAXRSClientFactoryBean) WebApplicationException(javax.ws.rs.WebApplicationException) HashMap(java.util.HashMap) XmlEncOutInterceptor(org.apache.cxf.rs.security.xml.XmlEncOutInterceptor) URL(java.net.URL) XmlSigInInterceptor(org.apache.cxf.rs.security.xml.XmlSigInInterceptor) SpringBusFactory(org.apache.cxf.bus.spring.SpringBusFactory) Book(org.apache.cxf.systest.jaxrs.security.Book) XmlEncInInterceptor(org.apache.cxf.rs.security.xml.XmlEncInInterceptor) Test(org.junit.Test)

Example 2 with XmlEncInInterceptor

use of org.apache.cxf.rs.security.xml.XmlEncInInterceptor in project cxf by apache.

the class JAXRSXmlSecTest method testSignatureNoEncryption.

@Test
public void testSignatureNoEncryption() throws Exception {
    if (test.streaming) {
        // Only testing the endpoints, not the clients here
        return;
    }
    String address = "https://localhost:" + test.port + "/xmlsec-validate";
    JAXRSClientFactoryBean bean = new JAXRSClientFactoryBean();
    bean.setAddress(address);
    SpringBusFactory bf = new SpringBusFactory();
    URL busFile = JAXRSXmlSecTest.class.getResource("client.xml");
    Bus springBus = bf.createBus(busFile.toString());
    bean.setBus(springBus);
    Map<String, Object> properties = new HashMap<>();
    properties.put(SecurityConstants.CALLBACK_HANDLER, "org.apache.cxf.systest.jaxrs.security.saml.KeystorePasswordCallback");
    properties.put(SecurityConstants.ENCRYPT_USERNAME, "bob");
    properties.put(SecurityConstants.ENCRYPT_PROPERTIES, "org/apache/cxf/systest/jaxrs/security/bob.properties");
    properties.put(SecurityConstants.SIGNATURE_PROPERTIES, "org/apache/cxf/systest/jaxrs/security/alice.properties");
    bean.setProperties(properties);
    XmlSigOutInterceptor sigInterceptor = new XmlSigOutInterceptor();
    bean.getOutInterceptors().add(sigInterceptor);
    bean.getInInterceptors().add(new XmlEncInInterceptor());
    bean.getInInterceptors().add(new XmlSigInInterceptor());
    bean.setServiceClass(BookStore.class);
    BookStore store = bean.create(BookStore.class);
    try {
        store.addBook(new Book("CXF", 126L));
        fail("Failure expected on no Encryption");
    } catch (WebApplicationException ex) {
    // expected
    }
}
Also used : Bus(org.apache.cxf.Bus) BookStore(org.apache.cxf.systest.jaxrs.security.BookStore) XmlSigOutInterceptor(org.apache.cxf.rs.security.xml.XmlSigOutInterceptor) JAXRSClientFactoryBean(org.apache.cxf.jaxrs.client.JAXRSClientFactoryBean) WebApplicationException(javax.ws.rs.WebApplicationException) HashMap(java.util.HashMap) URL(java.net.URL) XmlSigInInterceptor(org.apache.cxf.rs.security.xml.XmlSigInInterceptor) SpringBusFactory(org.apache.cxf.bus.spring.SpringBusFactory) Book(org.apache.cxf.systest.jaxrs.security.Book) XmlEncInInterceptor(org.apache.cxf.rs.security.xml.XmlEncInInterceptor) Test(org.junit.Test)

Example 3 with XmlEncInInterceptor

use of org.apache.cxf.rs.security.xml.XmlEncInInterceptor in project cxf by apache.

the class JAXRSXmlSecTest method doTestPostEncryptedBook.

public void doTestPostEncryptedBook(String address, boolean sign, Map<String, Object> properties, EncryptionProperties encryptionProperties, boolean propagateException, boolean streaming) throws Exception {
    JAXRSClientFactoryBean bean = new JAXRSClientFactoryBean();
    bean.setAddress(address);
    SpringBusFactory bf = new SpringBusFactory();
    URL busFile = JAXRSXmlSecTest.class.getResource("client.xml");
    Bus springBus = bf.createBus(busFile.toString());
    bean.setBus(springBus);
    bean.setProperties(properties);
    if (streaming) {
        XmlSecOutInterceptor encInterceptor = new XmlSecOutInterceptor();
        encInterceptor.setEncryptionKeyIdentifierType(encryptionProperties.getEncryptionKeyIdType());
        encInterceptor.setSymmetricEncAlgorithm(encryptionProperties.getEncryptionSymmetricKeyAlgo());
        encInterceptor.setEncryptionDigestAlgorithm(encryptionProperties.getEncryptionDigestAlgo());
        encInterceptor.setEncryptRequest(true);
        if (sign) {
            encInterceptor.setSignRequest(true);
        }
        bean.getOutInterceptors().add(encInterceptor);
        XmlSecInInterceptor encInInterceptor = new XmlSecInInterceptor();
        encInInterceptor.setRequireEncryption(true);
        bean.setProvider(encInInterceptor);
    } else {
        if (sign) {
            bean.getOutInterceptors().add(new XmlSigOutInterceptor());
        }
        XmlEncOutInterceptor encInterceptor = new XmlEncOutInterceptor();
        encInterceptor.setKeyIdentifierType(encryptionProperties.getEncryptionKeyIdType());
        encInterceptor.setSymmetricEncAlgorithm(encryptionProperties.getEncryptionSymmetricKeyAlgo());
        encInterceptor.setDigestAlgorithm(encryptionProperties.getEncryptionDigestAlgo());
        bean.getOutInterceptors().add(encInterceptor);
        bean.getInInterceptors().add(new XmlEncInInterceptor());
        if (sign) {
            bean.getInInterceptors().add(new XmlSigInInterceptor());
        }
    }
    WebClient wc = bean.createWebClient();
    WebClient.getConfig(wc).getHttpConduit().getClient().setReceiveTimeout(10000000L);
    try {
        Book book = wc.type("application/xml").post(new Book("CXF", 126L), Book.class);
        assertEquals(126L, book.getId());
    } catch (WebApplicationException ex) {
        if (propagateException) {
            throw ex;
        }
        fail(ex.getMessage());
    } catch (ProcessingException ex) {
        assertTrue(ex.getCause() instanceof BadRequestException);
    }
}
Also used : Bus(org.apache.cxf.Bus) XmlSigOutInterceptor(org.apache.cxf.rs.security.xml.XmlSigOutInterceptor) JAXRSClientFactoryBean(org.apache.cxf.jaxrs.client.JAXRSClientFactoryBean) WebApplicationException(javax.ws.rs.WebApplicationException) XmlSecInInterceptor(org.apache.cxf.rs.security.xml.XmlSecInInterceptor) XmlEncOutInterceptor(org.apache.cxf.rs.security.xml.XmlEncOutInterceptor) XmlSecOutInterceptor(org.apache.cxf.rs.security.xml.XmlSecOutInterceptor) WebClient(org.apache.cxf.jaxrs.client.WebClient) URL(java.net.URL) XmlSigInInterceptor(org.apache.cxf.rs.security.xml.XmlSigInInterceptor) SpringBusFactory(org.apache.cxf.bus.spring.SpringBusFactory) Book(org.apache.cxf.systest.jaxrs.security.Book) BadRequestException(javax.ws.rs.BadRequestException) XmlEncInInterceptor(org.apache.cxf.rs.security.xml.XmlEncInInterceptor) ProcessingException(javax.ws.rs.ProcessingException)

Aggregations

URL (java.net.URL)3 WebApplicationException (javax.ws.rs.WebApplicationException)3 Bus (org.apache.cxf.Bus)3 SpringBusFactory (org.apache.cxf.bus.spring.SpringBusFactory)3 JAXRSClientFactoryBean (org.apache.cxf.jaxrs.client.JAXRSClientFactoryBean)3 XmlEncInInterceptor (org.apache.cxf.rs.security.xml.XmlEncInInterceptor)3 XmlSigInInterceptor (org.apache.cxf.rs.security.xml.XmlSigInInterceptor)3 Book (org.apache.cxf.systest.jaxrs.security.Book)3 HashMap (java.util.HashMap)2 XmlEncOutInterceptor (org.apache.cxf.rs.security.xml.XmlEncOutInterceptor)2 XmlSigOutInterceptor (org.apache.cxf.rs.security.xml.XmlSigOutInterceptor)2 BookStore (org.apache.cxf.systest.jaxrs.security.BookStore)2 Test (org.junit.Test)2 BadRequestException (javax.ws.rs.BadRequestException)1 ProcessingException (javax.ws.rs.ProcessingException)1 WebClient (org.apache.cxf.jaxrs.client.WebClient)1 XmlSecInInterceptor (org.apache.cxf.rs.security.xml.XmlSecInInterceptor)1 XmlSecOutInterceptor (org.apache.cxf.rs.security.xml.XmlSecOutInterceptor)1