Separate test classes + resources
Project: http://git-wip-us.apache.org/repos/asf/cxf/repo Commit: http://git-wip-us.apache.org/repos/asf/cxf/commit/ecf6a384 Tree: http://git-wip-us.apache.org/repos/asf/cxf/tree/ecf6a384 Diff: http://git-wip-us.apache.org/repos/asf/cxf/diff/ecf6a384 Branch: refs/heads/master Commit: ecf6a384aa09062515afd8759fc093cb6117495f Parents: cb686be Author: Colm O hEigeartaigh <[email protected]> Authored: Wed Nov 4 15:28:32 2015 +0000 Committer: Colm O hEigeartaigh <[email protected]> Committed: Wed Nov 4 15:28:32 2015 +0000 ---------------------------------------------------------------------- .../systest/jaxrs/security/jose/BookStore.java | 62 +++ .../jose/jwejws/BookServerAlgorithms.java | 59 +++ .../security/jose/jwejws/BookServerJwsJson.java | 59 +++ .../security/jose/jwejws/BookServerJwt.java | 59 +++ .../jose/jwejws/BookServerReference.java | 59 +++ .../security/jose/jwejws/JAXRSJweJwsTest.java | 449 +++++++++++++++++ .../security/jose/jwejws/JAXRSJwsJsonTest.java | 189 +++++++ .../jose/jwejws/JweJwsAlgorithmTest.java | 504 +++++++++++++++++++ .../jose/jwejws/JweJwsReferenceTest.java | 370 ++++++++++++++ .../jwejws/PrivateKeyPasswordProviderImpl.java | 40 ++ .../security/jwt/BookServerAlgorithms.java | 57 --- .../jaxrs/security/jwt/BookServerJwsJson.java | 57 --- .../jaxrs/security/jwt/BookServerJwt.java | 57 --- .../jaxrs/security/jwt/BookServerReference.java | 57 --- .../systest/jaxrs/security/jwt/BookStore.java | 62 --- .../jaxrs/security/jwt/JAXRSJweJwsTest.java | 448 ----------------- .../jaxrs/security/jwt/JAXRSJwsJsonTest.java | 188 ------- .../jaxrs/security/jwt/JweJwsAlgorithmTest.java | 504 ------------------- .../jaxrs/security/jwt/JweJwsReferenceTest.java | 370 -------------- .../jwt/PrivateKeyPasswordProviderImpl.java | 40 -- .../jaxrs/security/jwt/algorithms-server.xml | 113 ----- .../cxf/systest/jaxrs/security/jwt/client.xml | 38 -- .../jaxrs/security/jwt/reference-server.xml | 101 ---- .../cxf/systest/jaxrs/security/jwt/server.xml | 246 --------- .../jaxrs/security/jwt/serverJwsJson.xml | 90 ---- .../jaxrs/security/oauth2/BookServerOAuth2.java | 6 +- .../systest/jaxrs/security/oauth2/client.xml | 38 -- .../systest/jaxrs/security/oauth2/server.xml | 98 ---- .../jaxrs/security/saml/BookServerSaml.java | 6 +- .../cxf/systest/jaxrs/security/saml/client.xml | 38 -- .../jaxrs/security/saml/secureServer.xml | 113 ----- .../cxf/systest/jaxrs/security/saml/server.xml | 79 --- .../jaxrs/security/samlsso/MetadataServer.java | 6 +- .../systest/jaxrs/security/samlsso/client.xml | 38 -- .../jaxrs/security/samlsso/metadata-server.xml | 62 --- .../jaxrs/security/xml/BookServerXmlSec.java | 6 +- .../cxf/systest/jaxrs/security/xml/client.xml | 38 -- .../cxf/systest/jaxrs/security/xml/server.xml | 201 -------- .../systest/jaxrs/security/xml/stax-server.xml | 216 -------- .../security/jose/jwejws/algorithms-server.xml | 113 +++++ .../jaxrs/security/jose/jwejws/client.xml | 38 ++ .../security/jose/jwejws/reference-server.xml | 101 ++++ .../jaxrs/security/jose/jwejws/server.xml | 246 +++++++++ .../security/jose/jwejws/serverJwsJson.xml | 90 ++++ .../systest/jaxrs/security/oauth2/client.xml | 38 ++ .../systest/jaxrs/security/oauth2/server.xml | 98 ++++ .../cxf/systest/jaxrs/security/saml/client.xml | 38 ++ .../jaxrs/security/saml/secureServer.xml | 113 +++++ .../cxf/systest/jaxrs/security/saml/server.xml | 79 +++ .../systest/jaxrs/security/samlsso/client.xml | 38 ++ .../jaxrs/security/samlsso/metadata-server.xml | 62 +++ .../cxf/systest/jaxrs/security/xml/client.xml | 38 ++ .../cxf/systest/jaxrs/security/xml/server.xml | 201 ++++++++ .../systest/jaxrs/security/xml/stax-server.xml | 216 ++++++++ 54 files changed, 3375 insertions(+), 3357 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/cxf/blob/ecf6a384/systests/rs-security/src/test/java/org/apache/cxf/systest/jaxrs/security/jose/BookStore.java ---------------------------------------------------------------------- diff --git a/systests/rs-security/src/test/java/org/apache/cxf/systest/jaxrs/security/jose/BookStore.java b/systests/rs-security/src/test/java/org/apache/cxf/systest/jaxrs/security/jose/BookStore.java new file mode 100644 index 0000000..9be440e --- /dev/null +++ b/systests/rs-security/src/test/java/org/apache/cxf/systest/jaxrs/security/jose/BookStore.java @@ -0,0 +1,62 @@ +/** + * 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.jaxrs.security.jose; + + +import javax.ws.rs.Consumes; +import javax.ws.rs.POST; +import javax.ws.rs.Path; +import javax.ws.rs.Produces; + +import org.apache.cxf.systest.jaxrs.security.Book; + +@Path("/bookstore") +public class BookStore { + + public BookStore() { + } + + @POST + @Path("/books") + @Produces("text/plain") + @Consumes("text/plain") + public String echoText(String text) { + return text; + } + + @POST + @Path("/books") + @Produces("application/json") + @Consumes("application/json") + public Book echoBook(Book book) { + return book; + } + + @POST + @Path("/books") + @Produces("application/xml") + @Consumes("application/xml") + public Book echoBook2(Book book) { + return book; + } + +} + + http://git-wip-us.apache.org/repos/asf/cxf/blob/ecf6a384/systests/rs-security/src/test/java/org/apache/cxf/systest/jaxrs/security/jose/jwejws/BookServerAlgorithms.java ---------------------------------------------------------------------- diff --git a/systests/rs-security/src/test/java/org/apache/cxf/systest/jaxrs/security/jose/jwejws/BookServerAlgorithms.java b/systests/rs-security/src/test/java/org/apache/cxf/systest/jaxrs/security/jose/jwejws/BookServerAlgorithms.java new file mode 100644 index 0000000..ff35edd --- /dev/null +++ b/systests/rs-security/src/test/java/org/apache/cxf/systest/jaxrs/security/jose/jwejws/BookServerAlgorithms.java @@ -0,0 +1,59 @@ +/** + * 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.jaxrs.security.jose.jwejws; + +import java.net.URL; + +import org.apache.cxf.Bus; +import org.apache.cxf.BusFactory; +import org.apache.cxf.bus.spring.SpringBusFactory; +import org.apache.cxf.testutil.common.AbstractBusTestServerBase; +import org.apache.cxf.testutil.common.TestUtil; + +public class BookServerAlgorithms extends AbstractBusTestServerBase { + public static final String PORT = TestUtil.getPortNumber("jaxrs-jwejws-algorithms"); + private static final URL SERVER_CONFIG_FILE = + BookServerAlgorithms.class.getResource("algorithms-server.xml"); + + protected void run() { + SpringBusFactory bf = new SpringBusFactory(); + Bus springBus = bf.createBus(SERVER_CONFIG_FILE); + BusFactory.setDefaultBus(springBus); + setBus(springBus); + + try { + new BookServerAlgorithms(); + } catch (Exception e) { + throw new RuntimeException(e); + } + } + + public static void main(String[] args) { + try { + BookServerAlgorithms s = new BookServerAlgorithms(); + s.start(); + } catch (Exception ex) { + ex.printStackTrace(); + System.exit(-1); + } finally { + System.out.println("done!"); + } + } +} http://git-wip-us.apache.org/repos/asf/cxf/blob/ecf6a384/systests/rs-security/src/test/java/org/apache/cxf/systest/jaxrs/security/jose/jwejws/BookServerJwsJson.java ---------------------------------------------------------------------- diff --git a/systests/rs-security/src/test/java/org/apache/cxf/systest/jaxrs/security/jose/jwejws/BookServerJwsJson.java b/systests/rs-security/src/test/java/org/apache/cxf/systest/jaxrs/security/jose/jwejws/BookServerJwsJson.java new file mode 100644 index 0000000..7d2e605 --- /dev/null +++ b/systests/rs-security/src/test/java/org/apache/cxf/systest/jaxrs/security/jose/jwejws/BookServerJwsJson.java @@ -0,0 +1,59 @@ +/** + * 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.jaxrs.security.jose.jwejws; + +import java.net.URL; + +import org.apache.cxf.Bus; +import org.apache.cxf.BusFactory; +import org.apache.cxf.bus.spring.SpringBusFactory; +import org.apache.cxf.testutil.common.AbstractBusTestServerBase; +import org.apache.cxf.testutil.common.TestUtil; + +public class BookServerJwsJson extends AbstractBusTestServerBase { + public static final String PORT = TestUtil.getPortNumber("jaxrs-jws-json"); + private static final URL SERVER_CONFIG_FILE = + BookServerJwsJson.class.getResource("serverJwsJson.xml"); + + protected void run() { + SpringBusFactory bf = new SpringBusFactory(); + Bus springBus = bf.createBus(SERVER_CONFIG_FILE); + BusFactory.setDefaultBus(springBus); + setBus(springBus); + + try { + new BookServerJwsJson(); + } catch (Exception e) { + throw new RuntimeException(e); + } + } + + public static void main(String[] args) { + try { + BookServerJwsJson s = new BookServerJwsJson(); + s.start(); + } catch (Exception ex) { + ex.printStackTrace(); + System.exit(-1); + } finally { + System.out.println("done!"); + } + } +} http://git-wip-us.apache.org/repos/asf/cxf/blob/ecf6a384/systests/rs-security/src/test/java/org/apache/cxf/systest/jaxrs/security/jose/jwejws/BookServerJwt.java ---------------------------------------------------------------------- diff --git a/systests/rs-security/src/test/java/org/apache/cxf/systest/jaxrs/security/jose/jwejws/BookServerJwt.java b/systests/rs-security/src/test/java/org/apache/cxf/systest/jaxrs/security/jose/jwejws/BookServerJwt.java new file mode 100644 index 0000000..e4c7117 --- /dev/null +++ b/systests/rs-security/src/test/java/org/apache/cxf/systest/jaxrs/security/jose/jwejws/BookServerJwt.java @@ -0,0 +1,59 @@ +/** + * 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.jaxrs.security.jose.jwejws; + +import java.net.URL; + +import org.apache.cxf.Bus; +import org.apache.cxf.BusFactory; +import org.apache.cxf.bus.spring.SpringBusFactory; +import org.apache.cxf.testutil.common.AbstractBusTestServerBase; +import org.apache.cxf.testutil.common.TestUtil; + +public class BookServerJwt extends AbstractBusTestServerBase { + public static final String PORT = TestUtil.getPortNumber("jaxrs-jwt"); + private static final URL SERVER_CONFIG_FILE = + BookServerJwt.class.getResource("server.xml"); + + protected void run() { + SpringBusFactory bf = new SpringBusFactory(); + Bus springBus = bf.createBus(SERVER_CONFIG_FILE); + BusFactory.setDefaultBus(springBus); + setBus(springBus); + + try { + new BookServerJwt(); + } catch (Exception e) { + throw new RuntimeException(e); + } + } + + public static void main(String[] args) { + try { + BookServerJwt s = new BookServerJwt(); + s.start(); + } catch (Exception ex) { + ex.printStackTrace(); + System.exit(-1); + } finally { + System.out.println("done!"); + } + } +} http://git-wip-us.apache.org/repos/asf/cxf/blob/ecf6a384/systests/rs-security/src/test/java/org/apache/cxf/systest/jaxrs/security/jose/jwejws/BookServerReference.java ---------------------------------------------------------------------- diff --git a/systests/rs-security/src/test/java/org/apache/cxf/systest/jaxrs/security/jose/jwejws/BookServerReference.java b/systests/rs-security/src/test/java/org/apache/cxf/systest/jaxrs/security/jose/jwejws/BookServerReference.java new file mode 100644 index 0000000..0a9b35b --- /dev/null +++ b/systests/rs-security/src/test/java/org/apache/cxf/systest/jaxrs/security/jose/jwejws/BookServerReference.java @@ -0,0 +1,59 @@ +/** + * 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.jaxrs.security.jose.jwejws; + +import java.net.URL; + +import org.apache.cxf.Bus; +import org.apache.cxf.BusFactory; +import org.apache.cxf.bus.spring.SpringBusFactory; +import org.apache.cxf.testutil.common.AbstractBusTestServerBase; +import org.apache.cxf.testutil.common.TestUtil; + +public class BookServerReference extends AbstractBusTestServerBase { + public static final String PORT = TestUtil.getPortNumber("jaxrs-jwejws-reference"); + private static final URL SERVER_CONFIG_FILE = + BookServerReference.class.getResource("reference-server.xml"); + + protected void run() { + SpringBusFactory bf = new SpringBusFactory(); + Bus springBus = bf.createBus(SERVER_CONFIG_FILE); + BusFactory.setDefaultBus(springBus); + setBus(springBus); + + try { + new BookServerReference(); + } catch (Exception e) { + throw new RuntimeException(e); + } + } + + public static void main(String[] args) { + try { + BookServerReference s = new BookServerReference(); + s.start(); + } catch (Exception ex) { + ex.printStackTrace(); + System.exit(-1); + } finally { + System.out.println("done!"); + } + } +} http://git-wip-us.apache.org/repos/asf/cxf/blob/ecf6a384/systests/rs-security/src/test/java/org/apache/cxf/systest/jaxrs/security/jose/jwejws/JAXRSJweJwsTest.java ---------------------------------------------------------------------- diff --git a/systests/rs-security/src/test/java/org/apache/cxf/systest/jaxrs/security/jose/jwejws/JAXRSJweJwsTest.java b/systests/rs-security/src/test/java/org/apache/cxf/systest/jaxrs/security/jose/jwejws/JAXRSJweJwsTest.java new file mode 100644 index 0000000..0d9e0d1 --- /dev/null +++ b/systests/rs-security/src/test/java/org/apache/cxf/systest/jaxrs/security/jose/jwejws/JAXRSJweJwsTest.java @@ -0,0 +1,449 @@ +/** + * 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.jaxrs.security.jose.jwejws; + +import java.net.URL; +import java.security.Security; +import java.util.Collections; +import java.util.LinkedList; +import java.util.List; +import java.util.Properties; + +import com.fasterxml.jackson.jaxrs.json.JacksonJsonProvider; + +import org.apache.cxf.Bus; +import org.apache.cxf.bus.spring.SpringBusFactory; +import org.apache.cxf.jaxrs.client.JAXRSClientFactoryBean; +import org.apache.cxf.jaxrs.client.WebClient; +import org.apache.cxf.rs.security.jose.common.PrivateKeyPasswordProvider; +import org.apache.cxf.rs.security.jose.jaxrs.JweClientResponseFilter; +import org.apache.cxf.rs.security.jose.jaxrs.JweWriterInterceptor; +import org.apache.cxf.rs.security.jose.jaxrs.JwsClientResponseFilter; +import org.apache.cxf.rs.security.jose.jaxrs.JwsWriterInterceptor; +import org.apache.cxf.rs.security.jose.jwa.ContentAlgorithm; +import org.apache.cxf.rs.security.jose.jwa.KeyAlgorithm; +import org.apache.cxf.rs.security.jose.jwa.SignatureAlgorithm; +import org.apache.cxf.rs.security.jose.jwe.AesCbcHmacJweDecryption; +import org.apache.cxf.rs.security.jose.jwe.AesCbcHmacJweEncryption; +import org.apache.cxf.rs.security.jose.jwe.AesWrapKeyDecryptionAlgorithm; +import org.apache.cxf.rs.security.jose.jwe.AesWrapKeyEncryptionAlgorithm; +import org.apache.cxf.rs.security.jose.jws.HmacJwsSignatureProvider; +import org.apache.cxf.rs.security.jose.jws.JwsSignatureProvider; +import org.apache.cxf.systest.jaxrs.security.Book; +import org.apache.cxf.systest.jaxrs.security.jose.BookStore; +import org.apache.cxf.testutil.common.AbstractBusClientServerTestBase; +import org.bouncycastle.jce.provider.BouncyCastleProvider; + +import org.junit.AfterClass; +import org.junit.BeforeClass; +import org.junit.Test; + +public class JAXRSJweJwsTest extends AbstractBusClientServerTestBase { + public static final String PORT = BookServerJwt.PORT; + private static final String CLIENT_JWEJWS_PROPERTIES = + "org/apache/cxf/systest/jaxrs/security/bob.rs.properties"; + private static final String SERVER_JWEJWS_PROPERTIES = + "org/apache/cxf/systest/jaxrs/security/alice.rs.properties"; + private static final String ENCODED_MAC_KEY = "AyM1SysPpbyDfgZld3umj1qzKObwVMkoqQ-EstJQLr_T-1qS0gZH75" + + "aKtMN3Yj0iPS4hcgUuTwjAzZr1Z9CAow"; + @BeforeClass + public static void startServers() throws Exception { + assertTrue("server did not launch correctly", + launchServer(BookServerJwt.class, true)); + registerBouncyCastleIfNeeded(); + } + + private static void registerBouncyCastleIfNeeded() throws Exception { + // Still need it for Oracle Java 7 and Java 8 + Security.addProvider(new BouncyCastleProvider()); + } + @AfterClass + public static void unregisterBouncyCastleIfNeeded() throws Exception { + Security.removeProvider(BouncyCastleProvider.PROVIDER_NAME); + } + @Test + public void testJweJwkPlainTextRSA() throws Exception { + String address = "https://localhost:" + PORT + "/jwejwkrsa"; + BookStore bs = createJweBookStore(address, null); + String text = bs.echoText("book"); + assertEquals("book", text); + } + @Test + public void testJweJwkBookBeanRSA() throws Exception { + String address = "https://localhost:" + PORT + "/jwejwkrsa"; + BookStore bs = createJweBookStore(address, + Collections.singletonList(new JacksonJsonProvider())); + Book book = bs.echoBook(new Book("book", 123L)); + assertEquals("book", book.getName()); + assertEquals(123L, book.getId()); + } + private BookStore createJweBookStore(String address, + List<?> mbProviders) throws Exception { + JAXRSClientFactoryBean bean = new JAXRSClientFactoryBean(); + SpringBusFactory bf = new SpringBusFactory(); + URL busFile = JAXRSJweJwsTest.class.getResource("client.xml"); + Bus springBus = bf.createBus(busFile.toString()); + bean.setBus(springBus); + bean.setServiceClass(BookStore.class); + bean.setAddress(address); + List<Object> providers = new LinkedList<Object>(); + JweWriterInterceptor jweWriter = new JweWriterInterceptor(); + jweWriter.setUseJweOutputStream(true); + providers.add(jweWriter); + providers.add(new JweClientResponseFilter()); + if (mbProviders != null) { + providers.addAll(mbProviders); + } + bean.setProviders(providers); + bean.getProperties(true).put("rs.security.encryption.out.properties", + "org/apache/cxf/systest/jaxrs/security/bob.jwk.properties"); + bean.getProperties(true).put("rs.security.encryption.in.properties", + "org/apache/cxf/systest/jaxrs/security/alice.jwk.properties"); + return bean.create(BookStore.class); + } + + @Test + public void testJweJwkAesWrap() throws Exception { + String address = "https://localhost:" + PORT + "/jwejwkaeswrap"; + JAXRSClientFactoryBean bean = new JAXRSClientFactoryBean(); + SpringBusFactory bf = new SpringBusFactory(); + URL busFile = JAXRSJweJwsTest.class.getResource("client.xml"); + Bus springBus = bf.createBus(busFile.toString()); + bean.setBus(springBus); + bean.setServiceClass(BookStore.class); + bean.setAddress(address); + List<Object> providers = new LinkedList<Object>(); + JweWriterInterceptor jweWriter = new JweWriterInterceptor(); + jweWriter.setUseJweOutputStream(true); + providers.add(jweWriter); + providers.add(new JweClientResponseFilter()); + bean.setProviders(providers); + bean.getProperties(true).put("rs.security.encryption.properties", + "org/apache/cxf/systest/jaxrs/security/secret.jwk.properties"); + bean.getProperties(true).put("jose.debug", true); + BookStore bs = bean.create(BookStore.class); + String text = bs.echoText("book"); + assertEquals("book", text); + } + @Test + public void testJweJwkAesCbcHMacInlineSet() throws Exception { + doTestJweJwkAesCbcHMac("org/apache/cxf/systest/jaxrs/security/secret.aescbchmac.inlineset.properties"); + } + @Test + public void testJweJwkAesCbcHMacInlineSingleKey() throws Exception { + doTestJweJwkAesCbcHMac("org/apache/cxf/systest/jaxrs/security/secret.aescbchmac.inlinejwk.properties"); + } + private void doTestJweJwkAesCbcHMac(String propFile) throws Exception { + String address = "https://localhost:" + PORT + "/jwejwkaescbchmac"; + JAXRSClientFactoryBean bean = new JAXRSClientFactoryBean(); + SpringBusFactory bf = new SpringBusFactory(); + URL busFile = JAXRSJweJwsTest.class.getResource("client.xml"); + Bus springBus = bf.createBus(busFile.toString()); + bean.setBus(springBus); + bean.setServiceClass(BookStore.class); + bean.setAddress(address); + List<Object> providers = new LinkedList<Object>(); + JweWriterInterceptor jweWriter = new JweWriterInterceptor(); + jweWriter.setUseJweOutputStream(true); + providers.add(jweWriter); + providers.add(new JweClientResponseFilter()); + bean.setProviders(providers); + bean.getProperties(true).put("rs.security.encryption.properties", propFile); + PrivateKeyPasswordProvider provider = + new PrivateKeyPasswordProviderImpl("Thus from my lips, by yours, my sin is purged."); + bean.getProperties(true).put("rs.security.key.password.provider", provider); + BookStore bs = bean.create(BookStore.class); + String text = bs.echoText("book"); + assertEquals("book", text); + } + @Test + public void testJweRsaJwsRsa() throws Exception { + String address = "https://localhost:" + PORT + "/jwejwsrsa"; + BookStore bs = createJweJwsBookStore(address, null, null); + String text = bs.echoText("book"); + assertEquals("book", text); + } + @Test + public void testJweRsaJwsRsaCert() throws Exception { + String address = "https://localhost:" + PORT + "/jwejwsrsacert"; + + JAXRSClientFactoryBean bean = new JAXRSClientFactoryBean(); + SpringBusFactory bf = new SpringBusFactory(); + URL busFile = JAXRSJweJwsTest.class.getResource("client.xml"); + Bus springBus = bf.createBus(busFile.toString()); + bean.setBus(springBus); + bean.setServiceClass(BookStore.class); + bean.setAddress(address); + List<Object> providers = new LinkedList<Object>(); + JweWriterInterceptor jweWriter = new JweWriterInterceptor(); + jweWriter.setUseJweOutputStream(true); + providers.add(jweWriter); + providers.add(new JweClientResponseFilter()); + JwsWriterInterceptor jwsWriter = new JwsWriterInterceptor(); + jwsWriter.setUseJwsOutputStream(true); + providers.add(jwsWriter); + providers.add(new JwsClientResponseFilter()); + + bean.setProviders(providers); + bean.getProperties(true).put("rs.security.keystore.file", + "org/apache/cxf/systest/jaxrs/security/certs/jwkPublicSet.txt"); + bean.getProperties(true).put("rs.security.signature.out.properties", CLIENT_JWEJWS_PROPERTIES); + bean.getProperties(true).put("rs.security.encryption.in.properties", CLIENT_JWEJWS_PROPERTIES); + PrivateKeyPasswordProvider provider = new PrivateKeyPasswordProviderImpl(); + bean.getProperties(true).put("rs.security.signature.key.password.provider", provider); + bean.getProperties(true).put("rs.security.decryption.key.password.provider", provider); + BookStore bs = bean.create(BookStore.class); + + WebClient.getConfig(bs).getRequestContext().put("rs.security.keystore.alias.jwe.out", "AliceCert"); + WebClient.getConfig(bs).getRequestContext().put("rs.security.keystore.alias.jws.in", "AliceCert"); + String text = bs.echoText("book"); + assertEquals("book", text); + } + @Test + public void testJweRsaJwsRsaCertInHeaders() throws Exception { + String address = "https://localhost:" + PORT + "/jwejwsrsaCertInHeaders"; + BookStore bs = createJweJwsBookStore(address, null, null); + WebClient.getConfig(bs).getRequestContext().put("rs.security.signature.include.cert", "true"); + WebClient.getConfig(bs).getRequestContext().put("rs.security.encryption.include.cert", "true"); + String text = bs.echoText("book"); + assertEquals("book", text); + } + @Test + public void testJweRsaJwsPlainTextHMac() throws Exception { + String address = "https://localhost:" + PORT + "/jwejwshmac"; + HmacJwsSignatureProvider hmacProvider = + new HmacJwsSignatureProvider(ENCODED_MAC_KEY, SignatureAlgorithm.HS256); + BookStore bs = createJweJwsBookStore(address, hmacProvider, null); + String text = bs.echoText("book"); + assertEquals("book", text); + } + @Test + public void testJweRsaJwsBookHMac() throws Exception { + String address = "https://localhost:" + PORT + "/jwejwshmac"; + HmacJwsSignatureProvider hmacProvider = + new HmacJwsSignatureProvider(ENCODED_MAC_KEY, SignatureAlgorithm.HS256); + BookStore bs = createJweJwsBookStore(address, hmacProvider, + Collections.singletonList(new JacksonJsonProvider())); + Book book = bs.echoBook(new Book("book", 123L)); + assertEquals("book", book.getName()); + assertEquals(123L, book.getId()); + } + + @Test + public void testJwsJwkPlainTextHMac() throws Exception { + String address = "https://localhost:" + PORT + "/jwsjwkhmac"; + BookStore bs = createJwsBookStore(address, null); + String text = bs.echoText("book"); + assertEquals("book", text); + } + @Test + public void testJwsJwkBookHMac() throws Exception { + String address = "https://localhost:" + PORT + "/jwsjwkhmac"; + BookStore bs = createJwsBookStore(address, + Collections.singletonList(new JacksonJsonProvider())); + Book book = bs.echoBook(new Book("book", 123L)); + assertEquals("book", book.getName()); + assertEquals(123L, book.getId()); + } + private BookStore createJwsBookStore(String address, + List<?> mbProviders) throws Exception { + JAXRSClientFactoryBean bean = new JAXRSClientFactoryBean(); + SpringBusFactory bf = new SpringBusFactory(); + URL busFile = JAXRSJweJwsTest.class.getResource("client.xml"); + Bus springBus = bf.createBus(busFile.toString()); + bean.setBus(springBus); + bean.setServiceClass(BookStore.class); + bean.setAddress(address); + List<Object> providers = new LinkedList<Object>(); + JwsWriterInterceptor jwsWriter = new JwsWriterInterceptor(); + jwsWriter.setUseJwsOutputStream(true); + providers.add(jwsWriter); + providers.add(new JwsClientResponseFilter()); + if (mbProviders != null) { + providers.addAll(mbProviders); + } + bean.setProviders(providers); + bean.getProperties(true).put("rs.security.signature.properties", + "org/apache/cxf/systest/jaxrs/security/secret.jwk.properties"); + return bean.create(BookStore.class); + } + @Test + public void testJwsJwkEC() throws Exception { + String address = "https://localhost:" + PORT + "/jwsjwkec"; + JAXRSClientFactoryBean bean = new JAXRSClientFactoryBean(); + SpringBusFactory bf = new SpringBusFactory(); + URL busFile = JAXRSJweJwsTest.class.getResource("client.xml"); + Bus springBus = bf.createBus(busFile.toString()); + bean.setBus(springBus); + bean.setServiceClass(BookStore.class); + bean.setAddress(address); + List<Object> providers = new LinkedList<Object>(); + JwsWriterInterceptor jwsWriter = new JwsWriterInterceptor(); + jwsWriter.setUseJwsOutputStream(true); + providers.add(jwsWriter); + providers.add(new JwsClientResponseFilter()); + bean.setProviders(providers); + bean.getProperties(true).put("rs.security.signature.out.properties", + "org/apache/cxf/systest/jaxrs/security/jws.ec.private.properties"); + bean.getProperties(true).put("rs.security.signature.in.properties", + "org/apache/cxf/systest/jaxrs/security/jws.ec.public.properties"); + BookStore bs = bean.create(BookStore.class); + String text = bs.echoText("book"); + assertEquals("book", text); + } + @Test + public void testJwsJwkRSA() throws Exception { + doTestJwsJwkRSA("https://localhost:" + PORT + "/jwsjwkrsa", false, false); + } + @Test + public void testJwsJwkInHeadersRSA() throws Exception { + doTestJwsJwkRSA("https://localhost:" + PORT + "/jwsjwkrsa", true, true); + } + @Test + public void testJwsJwkKidOnlyInHeadersRSA() throws Exception { + doTestJwsJwkRSA("https://localhost:" + PORT + "/jwsjwkrsa", false, true); + } + private void doTestJwsJwkRSA(String address, + boolean includePublicKey, + boolean includeKeyId) throws Exception { + JAXRSClientFactoryBean bean = new JAXRSClientFactoryBean(); + SpringBusFactory bf = new SpringBusFactory(); + URL busFile = JAXRSJweJwsTest.class.getResource("client.xml"); + Bus springBus = bf.createBus(busFile.toString()); + bean.setBus(springBus); + bean.setServiceClass(BookStore.class); + bean.setAddress(address); + List<Object> providers = new LinkedList<Object>(); + JwsWriterInterceptor jwsWriter = new JwsWriterInterceptor(); + jwsWriter.setUseJwsOutputStream(true); + providers.add(jwsWriter); + providers.add(new JwsClientResponseFilter()); + bean.setProviders(providers); + bean.getProperties(true).put("rs.security.signature.out.properties", + "org/apache/cxf/systest/jaxrs/security/alice.jwk.properties"); + bean.getProperties(true).put("rs.security.signature.in.properties", + "org/apache/cxf/systest/jaxrs/security/bob.jwk.properties"); + if (includePublicKey) { + bean.getProperties(true).put("rs.security.signature.include.public.key", true); + } + if (includeKeyId) { + bean.getProperties(true).put("rs.security.signature.include.key.id", true); + } + BookStore bs = bean.create(BookStore.class); + String text = bs.echoText("book"); + assertEquals("book", text); + } + private BookStore createJweJwsBookStore(String address, + JwsSignatureProvider jwsSigProvider, + List<?> mbProviders) throws Exception { + JAXRSClientFactoryBean bean = new JAXRSClientFactoryBean(); + SpringBusFactory bf = new SpringBusFactory(); + URL busFile = JAXRSJweJwsTest.class.getResource("client.xml"); + Bus springBus = bf.createBus(busFile.toString()); + bean.setBus(springBus); + bean.setServiceClass(BookStore.class); + bean.setAddress(address); + List<Object> providers = new LinkedList<Object>(); + JweWriterInterceptor jweWriter = new JweWriterInterceptor(); + jweWriter.setUseJweOutputStream(true); + providers.add(jweWriter); + providers.add(new JweClientResponseFilter()); + JwsWriterInterceptor jwsWriter = new JwsWriterInterceptor(); + if (jwsSigProvider != null) { + jwsWriter.setSignatureProvider(jwsSigProvider); + } + jwsWriter.setUseJwsOutputStream(true); + providers.add(jwsWriter); + providers.add(new JwsClientResponseFilter()); + if (mbProviders != null) { + providers.addAll(mbProviders); + } + bean.setProviders(providers); + bean.getProperties(true).put("rs.security.encryption.out.properties", SERVER_JWEJWS_PROPERTIES); + bean.getProperties(true).put("rs.security.signature.out.properties", CLIENT_JWEJWS_PROPERTIES); + bean.getProperties(true).put("rs.security.encryption.in.properties", CLIENT_JWEJWS_PROPERTIES); + bean.getProperties(true).put("rs.security.signature.in.properties", SERVER_JWEJWS_PROPERTIES); + PrivateKeyPasswordProvider provider = new PrivateKeyPasswordProviderImpl(); + bean.getProperties(true).put("rs.security.signature.key.password.provider", provider); + bean.getProperties(true).put("rs.security.decryption.key.password.provider", provider); + return bean.create(BookStore.class); + } + + @Test + public void testJweAesCbcHmac() throws Exception { + String address = "https://localhost:" + PORT + "/jweaescbchmac"; + JAXRSClientFactoryBean bean = new JAXRSClientFactoryBean(); + SpringBusFactory bf = new SpringBusFactory(); + URL busFile = JAXRSJweJwsTest.class.getResource("client.xml"); + Bus springBus = bf.createBus(busFile.toString()); + bean.setBus(springBus); + bean.setServiceClass(BookStore.class); + bean.setAddress(address); + List<Object> providers = new LinkedList<Object>(); + // writer + JweWriterInterceptor jweWriter = new JweWriterInterceptor(); + jweWriter.setUseJweOutputStream(true); + + final String cekEncryptionKey = "GawgguFyGrWKav7AX4VKUg"; + AesWrapKeyEncryptionAlgorithm keyEncryption = + new AesWrapKeyEncryptionAlgorithm(cekEncryptionKey, KeyAlgorithm.A128KW); + jweWriter.setEncryptionProvider(new AesCbcHmacJweEncryption(ContentAlgorithm.A128CBC_HS256, + keyEncryption)); + + // reader + JweClientResponseFilter jweReader = new JweClientResponseFilter(); + jweReader.setDecryptionProvider(new AesCbcHmacJweDecryption( + new AesWrapKeyDecryptionAlgorithm(cekEncryptionKey))); + + providers.add(jweWriter); + providers.add(jweReader); + bean.setProviders(providers); + + BookStore bs = bean.create(BookStore.class); + String text = bs.echoText("book"); + assertEquals("book", text); + } + + // Test signing and encrypting an XML payload + @Test + public void testJweRsaJwsRsaXML() throws Exception { + String address = "https://localhost:" + PORT + "/jwejwsrsa"; + BookStore bs = createJweJwsBookStore(address, null, null); + Book book = new Book(); + book.setName("book"); + book = bs.echoBook2(book); + assertEquals("book", book.getName()); + } + + private static class PrivateKeyPasswordProviderImpl implements PrivateKeyPasswordProvider { + private String password = "password"; + PrivateKeyPasswordProviderImpl() { + + } + PrivateKeyPasswordProviderImpl(String password) { + this.password = password; + } + @Override + public char[] getPassword(Properties storeProperties) { + return password.toCharArray(); + } + + } +} http://git-wip-us.apache.org/repos/asf/cxf/blob/ecf6a384/systests/rs-security/src/test/java/org/apache/cxf/systest/jaxrs/security/jose/jwejws/JAXRSJwsJsonTest.java ---------------------------------------------------------------------- diff --git a/systests/rs-security/src/test/java/org/apache/cxf/systest/jaxrs/security/jose/jwejws/JAXRSJwsJsonTest.java b/systests/rs-security/src/test/java/org/apache/cxf/systest/jaxrs/security/jose/jwejws/JAXRSJwsJsonTest.java new file mode 100644 index 0000000..92fc5b2 --- /dev/null +++ b/systests/rs-security/src/test/java/org/apache/cxf/systest/jaxrs/security/jose/jwejws/JAXRSJwsJsonTest.java @@ -0,0 +1,189 @@ +/** + * 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.jaxrs.security.jose.jwejws; + +import java.net.URL; +import java.security.Security; +import java.util.ArrayList; +import java.util.Arrays; +import java.util.Collections; +import java.util.HashMap; +import java.util.LinkedList; +import java.util.List; +import java.util.Map; + +import javax.ws.rs.BadRequestException; + +import com.fasterxml.jackson.jaxrs.json.JacksonJsonProvider; + +import org.apache.cxf.Bus; +import org.apache.cxf.bus.spring.SpringBusFactory; +import org.apache.cxf.jaxrs.client.JAXRSClientFactoryBean; +import org.apache.cxf.rs.security.jose.jaxrs.JweClientResponseFilter; +import org.apache.cxf.rs.security.jose.jaxrs.JweWriterInterceptor; +import org.apache.cxf.rs.security.jose.jaxrs.JwsJsonClientResponseFilter; +import org.apache.cxf.rs.security.jose.jaxrs.JwsJsonWriterInterceptor; +import org.apache.cxf.systest.jaxrs.security.Book; +import org.apache.cxf.systest.jaxrs.security.SecurityTestUtil; +import org.apache.cxf.systest.jaxrs.security.jose.BookStore; +import org.apache.cxf.testutil.common.AbstractBusClientServerTestBase; +import org.bouncycastle.jce.provider.BouncyCastleProvider; + +import org.junit.AfterClass; +import org.junit.BeforeClass; +import org.junit.Test; + +public class JAXRSJwsJsonTest extends AbstractBusClientServerTestBase { + public static final String PORT = BookServerJwsJson.PORT; + + @BeforeClass + public static void startServers() throws Exception { + assertTrue("server did not launch correctly", + launchServer(BookServerJwsJson.class, true)); + registerBouncyCastle(); + } + + private static void registerBouncyCastle() throws Exception { + Security.addProvider(new BouncyCastleProvider()); + } + @AfterClass + public static void unregisterBouncyCastleIfNeeded() throws Exception { + Security.removeProvider(BouncyCastleProvider.PROVIDER_NAME); + } + + @Test + public void testJwsJsonPlainTextHmac() throws Exception { + String address = "https://localhost:" + PORT + "/jwsjsonhmac"; + BookStore bs = createBookStore(address, + "org/apache/cxf/systest/jaxrs/security/secret.jwk.properties", + null); + String text = bs.echoText("book"); + assertEquals("book", text); + } + @Test + public void testJwsJsonBookBeanHmac() throws Exception { + String address = "https://localhost:" + PORT + "/jwsjsonhmac"; + BookStore bs = createBookStore(address, + "org/apache/cxf/systest/jaxrs/security/secret.jwk.properties", + Collections.singletonList(new JacksonJsonProvider())); + Book book = bs.echoBook(new Book("book", 123L)); + assertEquals("book", book.getName()); + assertEquals(123L, book.getId()); + } + @Test + public void testJweCompactJwsJsonBookBeanHmac() throws Exception { + if (!SecurityTestUtil.checkUnrestrictedPoliciesInstalled()) { + return; + } + String address = "https://localhost:" + PORT + "/jwejwsjsonhmac"; + List<?> extraProviders = Arrays.asList(new JacksonJsonProvider(), + new JweWriterInterceptor(), + new JweClientResponseFilter()); + String jwkStoreProperty = "org/apache/cxf/systest/jaxrs/security/secret.jwk.properties"; + Map<String, Object> props = new HashMap<String, Object>(); + props.put("rs.security.signature.list.properties", jwkStoreProperty); + props.put("rs.security.encryption.properties", jwkStoreProperty); + BookStore bs = createBookStore(address, + props, + extraProviders); + Book book = bs.echoBook(new Book("book", 123L)); + assertEquals("book", book.getName()); + assertEquals(123L, book.getId()); + } + + @Test + public void testJwsJsonBookDoubleHmac() throws Exception { + String address = "https://localhost:" + PORT + "/jwsjsonhmac2"; + List<String> properties = new ArrayList<String>(); + properties.add("org/apache/cxf/systest/jaxrs/security/secret.jwk.properties"); + properties.add("org/apache/cxf/systest/jaxrs/security/secret.jwk.hmac.properties"); + BookStore bs = createBookStore(address, properties, null); + Book book = bs.echoBook(new Book("book", 123L)); + assertEquals("book", book.getName()); + assertEquals(123L, book.getId()); + } + + @Test + public void testJwsJsonBookDoubleHmacSinglePropsFile() throws Exception { + String address = "https://localhost:" + PORT + "/jwsjsonhmac2"; + List<String> properties = new ArrayList<String>(); + properties.add("org/apache/cxf/systest/jaxrs/security/secret.jwk.hmac2.properties"); + BookStore bs = createBookStore(address, properties, null); + Book book = bs.echoBook2(new Book("book", 123L)); + assertEquals("book", book.getName()); + assertEquals(123L, book.getId()); + } + + // Test signing an XML payload + @Test + public void testJwsJsonPlainTextHmacXML() throws Exception { + String address = "https://localhost:" + PORT + "/jwsjsonhmac"; + BookStore bs = createBookStore(address, + "org/apache/cxf/systest/jaxrs/security/secret.jwk.properties", + null); + String text = bs.echoText("book"); + assertEquals("book", text); + } + + // Test signing with a bad signature key + @Test + public void testJwsJsonPlaintextHMACBadKey() throws Exception { + String address = "https://localhost:" + PORT + "/jwsjsonhmac"; + BookStore bs = createBookStore(address, + "org/apache/cxf/systest/jaxrs/security/secret.jwk.bad.properties", + null); + try { + bs.echoText("book"); + fail("Failure expected on a bad signature key"); + } catch (BadRequestException ex) { + // expected + } + } + + private BookStore createBookStore(String address, Object properties, + List<?> extraProviders) throws Exception { + return createBookStore(address, + Collections.singletonMap("rs.security.signature.list.properties", properties), + extraProviders); + } + private BookStore createBookStore(String address, + Map<String, Object> mapProperties, + List<?> extraProviders) throws Exception { + JAXRSClientFactoryBean bean = new JAXRSClientFactoryBean(); + SpringBusFactory bf = new SpringBusFactory(); + URL busFile = JAXRSJwsJsonTest.class.getResource("client.xml"); + Bus springBus = bf.createBus(busFile.toString()); + bean.setBus(springBus); + bean.setServiceClass(BookStore.class); + bean.setAddress(address); + List<Object> providers = new LinkedList<Object>(); + JwsJsonWriterInterceptor writer = new JwsJsonWriterInterceptor(); + writer.setUseJwsJsonOutputStream(true); + providers.add(writer); + providers.add(new JwsJsonClientResponseFilter()); + if (extraProviders != null) { + providers.addAll(extraProviders); + } + bean.setProviders(providers); + bean.getProperties(true).putAll(mapProperties); + return bean.create(BookStore.class); + } + +} http://git-wip-us.apache.org/repos/asf/cxf/blob/ecf6a384/systests/rs-security/src/test/java/org/apache/cxf/systest/jaxrs/security/jose/jwejws/JweJwsAlgorithmTest.java ---------------------------------------------------------------------- diff --git a/systests/rs-security/src/test/java/org/apache/cxf/systest/jaxrs/security/jose/jwejws/JweJwsAlgorithmTest.java b/systests/rs-security/src/test/java/org/apache/cxf/systest/jaxrs/security/jose/jwejws/JweJwsAlgorithmTest.java new file mode 100644 index 0000000..ed94b48 --- /dev/null +++ b/systests/rs-security/src/test/java/org/apache/cxf/systest/jaxrs/security/jose/jwejws/JweJwsAlgorithmTest.java @@ -0,0 +1,504 @@ +/** + * 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.jaxrs.security.jose.jwejws; + +import java.net.URL; +import java.security.Security; +import java.util.ArrayList; +import java.util.HashMap; +import java.util.List; +import java.util.Map; + +import javax.ws.rs.core.Response; + +import com.fasterxml.jackson.jaxrs.json.JacksonJsonProvider; + +import org.apache.cxf.jaxrs.client.WebClient; +import org.apache.cxf.rs.security.jose.jaxrs.JweWriterInterceptor; +import org.apache.cxf.rs.security.jose.jaxrs.JwsWriterInterceptor; +import org.apache.cxf.systest.jaxrs.security.Book; +import org.apache.cxf.systest.jaxrs.security.SecurityTestUtil; +import org.apache.cxf.testutil.common.AbstractBusClientServerTestBase; +import org.bouncycastle.jce.provider.BouncyCastleProvider; +import org.junit.AfterClass; +import org.junit.BeforeClass; + +/** + * Some encryption or signature tests, focus on algorithms. + */ +public class JweJwsAlgorithmTest extends AbstractBusClientServerTestBase { + public static final String PORT = BookServerAlgorithms.PORT; + + @BeforeClass + public static void startServers() throws Exception { + assertTrue("server did not launch correctly", + launchServer(BookServerAlgorithms.class, true)); + registerBouncyCastleIfNeeded(); + } + + private static void registerBouncyCastleIfNeeded() throws Exception { + // Still need it for Oracle Java 7 and Java 8 + Security.addProvider(new BouncyCastleProvider()); + } + + @AfterClass + public static void unregisterBouncyCastleIfNeeded() throws Exception { + Security.removeProvider(BouncyCastleProvider.PROVIDER_NAME); + } + + // + // Encryption tests + // + @org.junit.Test + public void testEncryptionProperties() throws Exception { + + URL busFile = JweJwsAlgorithmTest.class.getResource("client.xml"); + + List<Object> providers = new ArrayList<Object>(); + providers.add(new JacksonJsonProvider()); + providers.add(new JweWriterInterceptor()); + + String address = "http://localhost:" + PORT + "/jweoaepgcm/bookstore/books"; + WebClient client = + WebClient.create(address, providers, busFile.toString()); + client.type("application/json").accept("application/json"); + + Map<String, Object> properties = new HashMap<String, Object>(); + properties.put("rs.security.encryption.properties", + "org/apache/cxf/systest/jaxrs/security/bob.jwk.properties"); + WebClient.getConfig(client).getRequestContext().putAll(properties); + + Response response = client.post(new Book("book", 123L)); + assertEquals(response.getStatus(), 200); + + Book returnedBook = response.readEntity(Book.class); + assertEquals(returnedBook.getName(), "book"); + assertEquals(returnedBook.getId(), 123L); + } + + @org.junit.Test + public void testEncryptionDynamic() throws Exception { + + URL busFile = JweJwsAlgorithmTest.class.getResource("client.xml"); + + List<Object> providers = new ArrayList<Object>(); + providers.add(new JacksonJsonProvider()); + providers.add(new JweWriterInterceptor()); + + String address = "http://localhost:" + PORT + "/jweoaepgcm/bookstore/books"; + WebClient client = + WebClient.create(address, providers, busFile.toString()); + client.type("application/json").accept("application/json"); + + Map<String, Object> properties = new HashMap<String, Object>(); + properties.put("rs.security.keystore.type", "jwk"); + properties.put("rs.security.keystore.alias", "2011-04-29"); + properties.put("rs.security.keystore.file", "org/apache/cxf/systest/jaxrs/security/certs/jwkPublicSet.txt"); + properties.put("rs.security.encryption.content.algorithm", "A128GCM"); + properties.put("rs.security.encryption.key.algorithm", "RSA-OAEP"); + WebClient.getConfig(client).getRequestContext().putAll(properties); + + Response response = client.post(new Book("book", 123L)); + assertEquals(response.getStatus(), 200); + + Book returnedBook = response.readEntity(Book.class); + assertEquals(returnedBook.getName(), "book"); + assertEquals(returnedBook.getId(), 123L); + } + + @org.junit.Test + public void testWrongKeyEncryptionAlgorithm() throws Exception { + + URL busFile = JweJwsAlgorithmTest.class.getResource("client.xml"); + + List<Object> providers = new ArrayList<Object>(); + providers.add(new JacksonJsonProvider()); + providers.add(new JweWriterInterceptor()); + + String address = "http://localhost:" + PORT + "/jweoaepgcm/bookstore/books"; + WebClient client = + WebClient.create(address, providers, busFile.toString()); + client.type("application/json").accept("application/json"); + + Map<String, Object> properties = new HashMap<String, Object>(); + properties.put("rs.security.keystore.type", "jwk"); + properties.put("rs.security.keystore.alias", "2011-04-29"); + properties.put("rs.security.keystore.file", "org/apache/cxf/systest/jaxrs/security/certs/jwkPublicSet.txt"); + properties.put("rs.security.encryption.content.algorithm", "A128GCM"); + properties.put("rs.security.encryption.key.algorithm", "RSA1_5"); + WebClient.getConfig(client).getRequestContext().putAll(properties); + + Response response = client.post(new Book("book", 123L)); + assertNotEquals(response.getStatus(), 200); + } + + @org.junit.Test + public void testWrongKeyEncryptionAlgorithmKeyIncluded() throws Exception { + + URL busFile = JweJwsAlgorithmTest.class.getResource("client.xml"); + + List<Object> providers = new ArrayList<Object>(); + providers.add(new JacksonJsonProvider()); + providers.add(new JweWriterInterceptor()); + + String address = "http://localhost:" + PORT + "/jweoaepgcm/bookstore/books"; + WebClient client = + WebClient.create(address, providers, busFile.toString()); + client.type("application/json").accept("application/json"); + + Map<String, Object> properties = new HashMap<String, Object>(); + properties.put("rs.security.keystore.type", "jwk"); + properties.put("rs.security.keystore.alias", "2011-04-29"); + properties.put("rs.security.keystore.file", "org/apache/cxf/systest/jaxrs/security/certs/jwkPublicSet.txt"); + properties.put("rs.security.encryption.content.algorithm", "A128GCM"); + properties.put("rs.security.encryption.key.algorithm", "RSA1_5"); + properties.put("rs.security.encryption.include.public.key", "true"); + WebClient.getConfig(client).getRequestContext().putAll(properties); + + Response response = client.post(new Book("book", 123L)); + assertNotEquals(response.getStatus(), 200); + } + + @org.junit.Test + public void testWrongContentEncryptionAlgorithm() throws Exception { + if (!SecurityTestUtil.checkUnrestrictedPoliciesInstalled()) { + return; + } + + URL busFile = JweJwsAlgorithmTest.class.getResource("client.xml"); + + List<Object> providers = new ArrayList<Object>(); + providers.add(new JacksonJsonProvider()); + providers.add(new JweWriterInterceptor()); + + String address = "http://localhost:" + PORT + "/jweoaepgcm/bookstore/books"; + WebClient client = + WebClient.create(address, providers, busFile.toString()); + client.type("application/json").accept("application/json"); + + Map<String, Object> properties = new HashMap<String, Object>(); + properties.put("rs.security.keystore.type", "jwk"); + properties.put("rs.security.keystore.alias", "2011-04-29"); + properties.put("rs.security.keystore.file", "org/apache/cxf/systest/jaxrs/security/certs/jwkPublicSet.txt"); + properties.put("rs.security.encryption.content.algorithm", "A192GCM"); + properties.put("rs.security.encryption.key.algorithm", "RSA-OAEP"); + WebClient.getConfig(client).getRequestContext().putAll(properties); + + Response response = client.post(new Book("book", 123L)); + assertNotEquals(response.getStatus(), 200); + } + + @org.junit.Test + public void testBadEncryptingKey() throws Exception { + + URL busFile = JweJwsAlgorithmTest.class.getResource("client.xml"); + + List<Object> providers = new ArrayList<Object>(); + providers.add(new JacksonJsonProvider()); + providers.add(new JweWriterInterceptor()); + + String address = "http://localhost:" + PORT + "/jweoaepgcm/bookstore/books"; + WebClient client = + WebClient.create(address, providers, busFile.toString()); + client.type("application/json").accept("application/json"); + + Map<String, Object> properties = new HashMap<String, Object>(); + properties.put("rs.security.keystore.type", "jwk"); + properties.put("rs.security.keystore.alias", "AliceCert"); + properties.put("rs.security.keystore.file", "org/apache/cxf/systest/jaxrs/security/certs/jwkPublicSet.txt"); + properties.put("rs.security.encryption.content.algorithm", "A128GCM"); + properties.put("rs.security.encryption.key.algorithm", "RSA-OAEP"); + WebClient.getConfig(client).getRequestContext().putAll(properties); + + Response response = client.post(new Book("book", 123L)); + assertNotEquals(response.getStatus(), 200); + } + + // 1024 bits not allowed with RSA according to the spec + @org.junit.Test + public void testSmallEncryptionKeySize() throws Exception { + URL busFile = JweJwsAlgorithmTest.class.getResource("client.xml"); + + List<Object> providers = new ArrayList<Object>(); + providers.add(new JacksonJsonProvider()); + providers.add(new JweWriterInterceptor()); + + String address = "http://localhost:" + PORT + "/jwesmallkey/bookstore/books"; + WebClient client = + WebClient.create(address, providers, busFile.toString()); + client.type("application/json").accept("application/json"); + + Map<String, Object> properties = new HashMap<String, Object>(); + properties.put("rs.security.keystore.type", "jks"); + properties.put("rs.security.keystore.alias", "smallkey"); + properties.put("rs.security.keystore.password", "security"); + properties.put("rs.security.keystore.file", + "org/apache/cxf/systest/jaxrs/security/certs/smallkeysize.jks"); + properties.put("rs.security.encryption.content.algorithm", "A128GCM"); + properties.put("rs.security.encryption.key.algorithm", "RSA-OAEP"); + WebClient.getConfig(client).getRequestContext().putAll(properties); + + Response response = client.post(new Book("book", 123L)); + assertNotEquals(response.getStatus(), 200); + } + + // + // Signature tests + // + + @org.junit.Test + public void testSignatureProperties() throws Exception { + + URL busFile = JweJwsAlgorithmTest.class.getResource("client.xml"); + + List<Object> providers = new ArrayList<Object>(); + providers.add(new JacksonJsonProvider()); + providers.add(new JwsWriterInterceptor()); + + String address = "http://localhost:" + PORT + "/jws/bookstore/books"; + WebClient client = + WebClient.create(address, providers, busFile.toString()); + client.type("application/json").accept("application/json"); + + Map<String, Object> properties = new HashMap<String, Object>(); + properties.put("rs.security.signature.properties", + "org/apache/cxf/systest/jaxrs/security/alice.jwk.properties"); + WebClient.getConfig(client).getRequestContext().putAll(properties); + + Response response = client.post(new Book("book", 123L)); + assertEquals(response.getStatus(), 200); + + Book returnedBook = response.readEntity(Book.class); + assertEquals(returnedBook.getName(), "book"); + assertEquals(returnedBook.getId(), 123L); + } + + @org.junit.Test + public void testSignatureDynamic() throws Exception { + + URL busFile = JweJwsAlgorithmTest.class.getResource("client.xml"); + + List<Object> providers = new ArrayList<Object>(); + providers.add(new JacksonJsonProvider()); + providers.add(new JwsWriterInterceptor()); + + String address = "http://localhost:" + PORT + "/jws/bookstore/books"; + WebClient client = + WebClient.create(address, providers, busFile.toString()); + client.type("application/json").accept("application/json"); + + Map<String, Object> properties = new HashMap<String, Object>(); + properties.put("rs.security.keystore.type", "jwk"); + properties.put("rs.security.keystore.alias", "2011-04-29"); + properties.put("rs.security.keystore.file", + "org/apache/cxf/systest/jaxrs/security/certs/jwkPrivateSet.txt"); + properties.put("rs.security.signature.algorithm", "RS256"); + WebClient.getConfig(client).getRequestContext().putAll(properties); + + Response response = client.post(new Book("book", 123L)); + assertEquals(response.getStatus(), 200); + + Book returnedBook = response.readEntity(Book.class); + assertEquals(returnedBook.getName(), "book"); + assertEquals(returnedBook.getId(), 123L); + } + + @org.junit.Test + public void testWrongSignatureAlgorithm() throws Exception { + + URL busFile = JweJwsAlgorithmTest.class.getResource("client.xml"); + + List<Object> providers = new ArrayList<Object>(); + providers.add(new JacksonJsonProvider()); + providers.add(new JwsWriterInterceptor()); + + String address = "http://localhost:" + PORT + "/jws/bookstore/books"; + WebClient client = + WebClient.create(address, providers, busFile.toString()); + client.type("application/json").accept("application/json"); + + Map<String, Object> properties = new HashMap<String, Object>(); + properties.put("rs.security.keystore.type", "jwk"); + properties.put("rs.security.keystore.alias", "2011-04-29"); + properties.put("rs.security.keystore.file", + "org/apache/cxf/systest/jaxrs/security/certs/jwkPrivateSet.txt"); + properties.put("rs.security.signature.algorithm", "PS256"); + WebClient.getConfig(client).getRequestContext().putAll(properties); + + Response response = client.post(new Book("book", 123L)); + assertNotEquals(response.getStatus(), 200); + } + + @org.junit.Test + public void testWrongSignatureAlgorithmKeyIncluded() throws Exception { + + URL busFile = JweJwsAlgorithmTest.class.getResource("client.xml"); + + List<Object> providers = new ArrayList<Object>(); + providers.add(new JacksonJsonProvider()); + providers.add(new JwsWriterInterceptor()); + + String address = "http://localhost:" + PORT + "/jws/bookstore/books"; + WebClient client = + WebClient.create(address, providers, busFile.toString()); + client.type("application/json").accept("application/json"); + + Map<String, Object> properties = new HashMap<String, Object>(); + properties.put("rs.security.keystore.type", "jwk"); + properties.put("rs.security.keystore.alias", "2011-04-29"); + properties.put("rs.security.keystore.file", + "org/apache/cxf/systest/jaxrs/security/certs/jwkPrivateSet.txt"); + properties.put("rs.security.signature.algorithm", "PS256"); + properties.put("rs.security.signature.include.public.key", true); + WebClient.getConfig(client).getRequestContext().putAll(properties); + + Response response = client.post(new Book("book", 123L)); + assertNotEquals(response.getStatus(), 200); + } + + @org.junit.Test + public void testBadSigningKey() throws Exception { + + URL busFile = JweJwsAlgorithmTest.class.getResource("client.xml"); + + List<Object> providers = new ArrayList<Object>(); + providers.add(new JacksonJsonProvider()); + providers.add(new JwsWriterInterceptor()); + + String address = "http://localhost:" + PORT + "/jws/bookstore/books"; + WebClient client = + WebClient.create(address, providers, busFile.toString()); + client.type("application/json").accept("application/json"); + + Map<String, Object> properties = new HashMap<String, Object>(); + properties.put("rs.security.keystore.type", "jks"); + properties.put("rs.security.keystore.password", "password"); + properties.put("rs.security.key.password", "password"); + properties.put("rs.security.keystore.alias", "alice"); + properties.put("rs.security.keystore.file", + "org/apache/cxf/systest/jaxrs/security/certs/alice.jks"); + properties.put("rs.security.signature.algorithm", "RS256"); + WebClient.getConfig(client).getRequestContext().putAll(properties); + + Response response = client.post(new Book("book", 123L)); + assertNotEquals(response.getStatus(), 200); + } + + @org.junit.Test + public void testSignatureEllipticCurve() throws Exception { + + URL busFile = JweJwsAlgorithmTest.class.getResource("client.xml"); + + List<Object> providers = new ArrayList<Object>(); + providers.add(new JacksonJsonProvider()); + providers.add(new JwsWriterInterceptor()); + + String address = "http://localhost:" + PORT + "/jwsec/bookstore/books"; + WebClient client = + WebClient.create(address, providers, busFile.toString()); + client.type("application/json").accept("application/json"); + + Map<String, Object> properties = new HashMap<String, Object>(); + properties.put("rs.security.keystore.type", "jwk"); + properties.put("rs.security.keystore.alias", "ECKey"); + properties.put("rs.security.keystore.file", + "org/apache/cxf/systest/jaxrs/security/certs/jwkPrivateSet.txt"); + properties.put("rs.security.signature.algorithm", "ES256"); + WebClient.getConfig(client).getRequestContext().putAll(properties); + + Response response = client.post(new Book("book", 123L)); + assertEquals(response.getStatus(), 200); + + Book returnedBook = response.readEntity(Book.class); + assertEquals(returnedBook.getName(), "book"); + assertEquals(returnedBook.getId(), 123L); + } + + @org.junit.Test + public void testManualSignature() throws Exception { + + URL busFile = JweJwsAlgorithmTest.class.getResource("client.xml"); + + List<Object> providers = new ArrayList<Object>(); + providers.add(new JacksonJsonProvider()); + + String address = "http://localhost:" + PORT + "/jws/bookstore/books"; + WebClient client = + WebClient.create(address, providers, busFile.toString()); + client.type("application/json").accept("application/json"); + + Map<String, Object> properties = new HashMap<String, Object>(); + WebClient.getConfig(client).getRequestContext().putAll(properties); + + String header = "eyJhbGciOiJSUzI1NiIsImN0eSI6Impzb24ifQ"; + String payload = "eyJCb29rIjp7ImlkIjoxMjMsIm5hbWUiOiJib29rIn19"; + String sig = "mZJVPy83atFNxQMeJqkVbR8t1srr9LgKBGT0hgiymjNepRgqedvFG5B8E8UPAzfzNLsos91gGdneUEKrWauU4GoDPTzngX" + + "798aDP6lsn5bUoTMKLfaWp9uzHDIzLMjGkabn92nrIpdK4JKDYNjdSUJIT2L97jggg0aoLhJQHVw2LdF1fpYdM-HCyccNW" + + "HQbAR7bDZdITZFnDi8b22QfHCqeLV7m4mBvNDtNX337wtoUKyjPYBMoWc12hHDCwQyu_gfW6zFioF5TGx-Ifg8hrFlnyUr" + + "vnSdP-FUtXiGeWBIvE_L6gD7DfM4u9hkK757vTjjMR_pF2CW3pfSH-Ha8v0A"; + + // Successful test + Response response = client.post(header + "." + payload + "." + sig); + assertEquals(response.getStatus(), 200); + + Book returnedBook = response.readEntity(Book.class); + assertEquals(returnedBook.getName(), "book"); + assertEquals(returnedBook.getId(), 123L); + + // No signature + response = client.post(header + "." + payload + "."); + assertNotEquals(response.getStatus(), 200); + + // Modified signature + String sig2 = sig.replace('y', 'z'); + response = client.post(header + "." + payload + "." + sig2); + assertNotEquals(response.getStatus(), 200); + } + + // 1024 bits not allowed with RSA according to the spec + @org.junit.Test + public void testSmallSignatureKeySize() throws Exception { + + URL busFile = JweJwsAlgorithmTest.class.getResource("client.xml"); + + List<Object> providers = new ArrayList<Object>(); + providers.add(new JacksonJsonProvider()); + providers.add(new JwsWriterInterceptor()); + + String address = "http://localhost:" + PORT + "/jwssmallkey/bookstore/books"; + WebClient client = + WebClient.create(address, providers, busFile.toString()); + client.type("application/json").accept("application/json"); + + Map<String, Object> properties = new HashMap<String, Object>(); + properties.put("rs.security.keystore.type", "jks"); + properties.put("rs.security.keystore.alias", "smallkey"); + properties.put("rs.security.keystore.password", "security"); + properties.put("rs.security.key.password", "security"); + properties.put("rs.security.keystore.file", + "org/apache/cxf/systest/jaxrs/security/certs/smallkeysize.jks"); + properties.put("rs.security.signature.algorithm", "RS256"); + WebClient.getConfig(client).getRequestContext().putAll(properties); + + Response response = client.post(new Book("book", 123L)); + assertNotEquals(response.getStatus(), 200); + } + +}
