Repository: cxf Updated Branches: refs/heads/master 308f4dcc7 -> 1c092b9b3
Adding some JWT tests Project: http://git-wip-us.apache.org/repos/asf/cxf/repo Commit: http://git-wip-us.apache.org/repos/asf/cxf/commit/1c092b9b Tree: http://git-wip-us.apache.org/repos/asf/cxf/tree/1c092b9b Diff: http://git-wip-us.apache.org/repos/asf/cxf/diff/1c092b9b Branch: refs/heads/master Commit: 1c092b9b3b0d438e4e5d44d44b5942cb406f8ade Parents: 5dd07f5 Author: Colm O hEigeartaigh <[email protected]> Authored: Thu Nov 5 16:27:18 2015 +0000 Committer: Colm O hEigeartaigh <[email protected]> Committed: Thu Nov 5 16:27:34 2015 +0000 ---------------------------------------------------------------------- .../cxf/rs/security/jose/jwk/JwkUtils.java | 2 +- .../jose/jwejws/JweJwsAlgorithmTest.java | 5 + .../jose/jwt/BookServerJwtAlgorithms.java | 59 ++ .../security/jose/jwt/JWTAlgorithmTest.java | 609 +++++++++++++++++++ .../jwt/PrivateKeyPasswordProviderImpl.java | 40 ++ .../security/jose/jwt/algorithms-server.xml | 157 +++++ .../systest/jaxrs/security/jose/jwt/client.xml | 38 ++ 7 files changed, 909 insertions(+), 1 deletion(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/cxf/blob/1c092b9b/rt/rs/security/jose-parent/jose/src/main/java/org/apache/cxf/rs/security/jose/jwk/JwkUtils.java ---------------------------------------------------------------------- diff --git a/rt/rs/security/jose-parent/jose/src/main/java/org/apache/cxf/rs/security/jose/jwk/JwkUtils.java b/rt/rs/security/jose-parent/jose/src/main/java/org/apache/cxf/rs/security/jose/jwk/JwkUtils.java index a85d4dc..cd609f5 100644 --- a/rt/rs/security/jose-parent/jose/src/main/java/org/apache/cxf/rs/security/jose/jwk/JwkUtils.java +++ b/rt/rs/security/jose-parent/jose/src/main/java/org/apache/cxf/rs/security/jose/jwk/JwkUtils.java @@ -512,7 +512,7 @@ public final class JwkUtils { public static void includeCertChain(JsonWebKey jwk, JoseHeaders headers, String algo) { if (KeyType.RSA.equals(jwk.getKeyType())) { - List<String> chain = CastUtils.cast((List<?>)jwk.getProperty("x5c")); + List<String> chain = CastUtils.cast((List<?>)jwk.getProperty(JsonWebKey.X509_CHAIN)); if (chain != null) { headers.setX509Chain(chain); } http://git-wip-us.apache.org/repos/asf/cxf/blob/1c092b9b/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 index ed94b48..40b9010 100644 --- 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 @@ -470,6 +470,11 @@ public class JweJwsAlgorithmTest extends AbstractBusClientServerTestBase { String sig2 = sig.replace('y', 'z'); response = client.post(header + "." + payload + "." + sig2); assertNotEquals(response.getStatus(), 200); + + // Modified payload + String payload2 = payload.replace('y', 'z'); + response = client.post(header + "." + payload2 + "." + sig); + assertNotEquals(response.getStatus(), 200); } // 1024 bits not allowed with RSA according to the spec http://git-wip-us.apache.org/repos/asf/cxf/blob/1c092b9b/systests/rs-security/src/test/java/org/apache/cxf/systest/jaxrs/security/jose/jwt/BookServerJwtAlgorithms.java ---------------------------------------------------------------------- diff --git a/systests/rs-security/src/test/java/org/apache/cxf/systest/jaxrs/security/jose/jwt/BookServerJwtAlgorithms.java b/systests/rs-security/src/test/java/org/apache/cxf/systest/jaxrs/security/jose/jwt/BookServerJwtAlgorithms.java new file mode 100644 index 0000000..681bc6e --- /dev/null +++ b/systests/rs-security/src/test/java/org/apache/cxf/systest/jaxrs/security/jose/jwt/BookServerJwtAlgorithms.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.jwt; + +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 BookServerJwtAlgorithms extends AbstractBusTestServerBase { + public static final String PORT = TestUtil.getPortNumber("jaxrs-jwt-algorithms"); + private static final URL SERVER_CONFIG_FILE = + BookServerJwtAlgorithms.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 BookServerJwtAlgorithms(); + } catch (Exception e) { + throw new RuntimeException(e); + } + } + + public static void main(String[] args) { + try { + BookServerJwtAlgorithms s = new BookServerJwtAlgorithms(); + 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/1c092b9b/systests/rs-security/src/test/java/org/apache/cxf/systest/jaxrs/security/jose/jwt/JWTAlgorithmTest.java ---------------------------------------------------------------------- diff --git a/systests/rs-security/src/test/java/org/apache/cxf/systest/jaxrs/security/jose/jwt/JWTAlgorithmTest.java b/systests/rs-security/src/test/java/org/apache/cxf/systest/jaxrs/security/jose/jwt/JWTAlgorithmTest.java new file mode 100644 index 0000000..4b66f35 --- /dev/null +++ b/systests/rs-security/src/test/java/org/apache/cxf/systest/jaxrs/security/jose/jwt/JWTAlgorithmTest.java @@ -0,0 +1,609 @@ +/** + * 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.jwt; + +import java.net.URL; +import java.security.Security; +import java.util.ArrayList; +import java.util.Date; +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.JwtAuthenticationClientFilter; +import org.apache.cxf.rs.security.jose.jwt.JwtClaims; +import org.apache.cxf.rs.security.jose.jwt.JwtConstants; +import org.apache.cxf.rs.security.jose.jwt.JwtToken; +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 tests for JWT tokens. + */ +public class JWTAlgorithmTest extends AbstractBusClientServerTestBase { + public static final String PORT = BookServerJwtAlgorithms.PORT; + + @BeforeClass + public static void startServers() throws Exception { + assertTrue("server did not launch correctly", + launchServer(BookServerJwtAlgorithms.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 = JWTAlgorithmTest.class.getResource("client.xml"); + + List<Object> providers = new ArrayList<Object>(); + providers.add(new JacksonJsonProvider()); + JwtAuthenticationClientFilter clientFilter = new JwtAuthenticationClientFilter(); + clientFilter.setJwsRequired(false); + clientFilter.setJweRequired(true); + providers.add(clientFilter); + + String address = "https://localhost:" + PORT + "/encryptedjwt/bookstore/books"; + WebClient client = + WebClient.create(address, providers, busFile.toString()); + client.type("application/json").accept("application/json"); + + // Create the JWT Token + JwtClaims claims = new JwtClaims(); + claims.setSubject("alice"); + claims.setIssuer("DoubleItSTSIssuer"); + claims.setIssuedAt(new Date().getTime() / 1000L); + + JwtToken token = new JwtToken(claims); + + Map<String, Object> properties = new HashMap<String, Object>(); + properties.put("rs.security.encryption.properties", + "org/apache/cxf/systest/jaxrs/security/bob.jwk.properties"); + properties.put(JwtConstants.JWT_TOKEN, token); + 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 = JWTAlgorithmTest.class.getResource("client.xml"); + + List<Object> providers = new ArrayList<Object>(); + providers.add(new JacksonJsonProvider()); + JwtAuthenticationClientFilter clientFilter = new JwtAuthenticationClientFilter(); + clientFilter.setJwsRequired(false); + clientFilter.setJweRequired(true); + providers.add(clientFilter); + + String address = "https://localhost:" + PORT + "/encryptedjwt/bookstore/books"; + WebClient client = + WebClient.create(address, providers, busFile.toString()); + client.type("application/json").accept("application/json"); + + // Create the JWT Token + JwtClaims claims = new JwtClaims(); + claims.setSubject("alice"); + claims.setIssuer("DoubleItSTSIssuer"); + claims.setIssuedAt(new Date().getTime() / 1000L); + + JwtToken token = new JwtToken(claims); + + 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"); + properties.put(JwtConstants.JWT_TOKEN, token); + 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 = JWTAlgorithmTest.class.getResource("client.xml"); + + List<Object> providers = new ArrayList<Object>(); + providers.add(new JacksonJsonProvider()); + JwtAuthenticationClientFilter clientFilter = new JwtAuthenticationClientFilter(); + clientFilter.setJwsRequired(false); + clientFilter.setJweRequired(true); + providers.add(clientFilter); + + String address = "https://localhost:" + PORT + "/encryptedjwt/bookstore/books"; + WebClient client = + WebClient.create(address, providers, busFile.toString()); + client.type("application/json").accept("application/json"); + + // Create the JWT Token + JwtClaims claims = new JwtClaims(); + claims.setSubject("alice"); + claims.setIssuer("DoubleItSTSIssuer"); + claims.setIssuedAt(new Date().getTime() / 1000L); + + JwtToken token = new JwtToken(claims); + + 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(JwtConstants.JWT_TOKEN, token); + 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 = JWTAlgorithmTest.class.getResource("client.xml"); + + List<Object> providers = new ArrayList<Object>(); + providers.add(new JacksonJsonProvider()); + JwtAuthenticationClientFilter clientFilter = new JwtAuthenticationClientFilter(); + clientFilter.setJwsRequired(false); + clientFilter.setJweRequired(true); + providers.add(clientFilter); + + String address = "https://localhost:" + PORT + "/encryptedjwt/bookstore/books"; + WebClient client = + WebClient.create(address, providers, busFile.toString()); + client.type("application/json").accept("application/json"); + + // Create the JWT Token + JwtClaims claims = new JwtClaims(); + claims.setSubject("alice"); + claims.setIssuer("DoubleItSTSIssuer"); + claims.setIssuedAt(new Date().getTime() / 1000L); + + JwtToken token = new JwtToken(claims); + + 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.content.algorithm", "A192GCM"); + properties.put("rs.security.encryption.key.algorithm", "RSA-OAEP"); + properties.put(JwtConstants.JWT_TOKEN, token); + 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 { + if (!SecurityTestUtil.checkUnrestrictedPoliciesInstalled()) { + return; + } + + URL busFile = JWTAlgorithmTest.class.getResource("client.xml"); + + List<Object> providers = new ArrayList<Object>(); + providers.add(new JacksonJsonProvider()); + JwtAuthenticationClientFilter clientFilter = new JwtAuthenticationClientFilter(); + clientFilter.setJwsRequired(false); + clientFilter.setJweRequired(true); + providers.add(clientFilter); + + String address = "https://localhost:" + PORT + "/encryptedjwt/bookstore/books"; + WebClient client = + WebClient.create(address, providers, busFile.toString()); + client.type("application/json").accept("application/json"); + + // Create the JWT Token + JwtClaims claims = new JwtClaims(); + claims.setSubject("alice"); + claims.setIssuer("DoubleItSTSIssuer"); + claims.setIssuedAt(new Date().getTime() / 1000L); + + JwtToken token = new JwtToken(claims); + + 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"); + properties.put(JwtConstants.JWT_TOKEN, token); + 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 = JWTAlgorithmTest.class.getResource("client.xml"); + + List<Object> providers = new ArrayList<Object>(); + providers.add(new JacksonJsonProvider()); + providers.add(new JwtAuthenticationClientFilter()); + + String address = "https://localhost:" + PORT + "/signedjwt/bookstore/books"; + WebClient client = + WebClient.create(address, providers, busFile.toString()); + client.type("application/json").accept("application/json"); + + // Create the JWT Token + JwtClaims claims = new JwtClaims(); + claims.setSubject("alice"); + claims.setIssuer("DoubleItSTSIssuer"); + claims.setIssuedAt(new Date().getTime() / 1000L); + + JwtToken token = new JwtToken(claims); + + Map<String, Object> properties = new HashMap<String, Object>(); + properties.put("rs.security.signature.properties", + "org/apache/cxf/systest/jaxrs/security/alice.jwk.properties"); + properties.put(JwtConstants.JWT_TOKEN, token); + 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 = JWTAlgorithmTest.class.getResource("client.xml"); + + List<Object> providers = new ArrayList<Object>(); + providers.add(new JacksonJsonProvider()); + providers.add(new JwtAuthenticationClientFilter()); + + String address = "https://localhost:" + PORT + "/signedjwt/bookstore/books"; + WebClient client = + WebClient.create(address, providers, busFile.toString()); + client.type("application/json").accept("application/json"); + + // Create the JWT Token + JwtClaims claims = new JwtClaims(); + claims.setSubject("alice"); + claims.setIssuer("DoubleItSTSIssuer"); + claims.setIssuedAt(new Date().getTime() / 1000L); + + JwtToken token = new JwtToken(claims); + + 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"); + properties.put(JwtConstants.JWT_TOKEN, token); + 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 = JWTAlgorithmTest.class.getResource("client.xml"); + + List<Object> providers = new ArrayList<Object>(); + providers.add(new JacksonJsonProvider()); + providers.add(new JwtAuthenticationClientFilter()); + + String address = "https://localhost:" + PORT + "/signedjwt/bookstore/books"; + WebClient client = + WebClient.create(address, providers, busFile.toString()); + client.type("application/json").accept("application/json"); + + // Create the JWT Token + JwtClaims claims = new JwtClaims(); + claims.setSubject("alice"); + claims.setIssuer("DoubleItSTSIssuer"); + claims.setIssuedAt(new Date().getTime() / 1000L); + + JwtToken token = new JwtToken(claims); + + 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(JwtConstants.JWT_TOKEN, token); + 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 = JWTAlgorithmTest.class.getResource("client.xml"); + + List<Object> providers = new ArrayList<Object>(); + providers.add(new JacksonJsonProvider()); + providers.add(new JwtAuthenticationClientFilter()); + + String address = "https://localhost:" + PORT + "/signedjwt/bookstore/books"; + WebClient client = + WebClient.create(address, providers, busFile.toString()); + client.type("application/json").accept("application/json"); + + // Create the JWT Token + JwtClaims claims = new JwtClaims(); + claims.setSubject("alice"); + claims.setIssuer("DoubleItSTSIssuer"); + claims.setIssuedAt(new Date().getTime() / 1000L); + + JwtToken token = new JwtToken(claims); + + 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"); + properties.put(JwtConstants.JWT_TOKEN, token); + 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 = JWTAlgorithmTest.class.getResource("client.xml"); + + List<Object> providers = new ArrayList<Object>(); + providers.add(new JacksonJsonProvider()); + providers.add(new JwtAuthenticationClientFilter()); + + String address = "https://localhost:" + PORT + "/signedjwtec/bookstore/books"; + WebClient client = + WebClient.create(address, providers, busFile.toString()); + client.type("application/json").accept("application/json"); + + // Create the JWT Token + JwtClaims claims = new JwtClaims(); + claims.setSubject("alice"); + claims.setIssuer("DoubleItSTSIssuer"); + claims.setIssuedAt(new Date().getTime() / 1000L); + + JwtToken token = new JwtToken(claims); + + 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"); + properties.put(JwtConstants.JWT_TOKEN, token); + 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); + } + + // 1024 bits not allowed with RSA according to the spec + @org.junit.Test + public void testSmallSignatureKeySize() throws Exception { + + URL busFile = JWTAlgorithmTest.class.getResource("client.xml"); + + List<Object> providers = new ArrayList<Object>(); + providers.add(new JacksonJsonProvider()); + providers.add(new JwtAuthenticationClientFilter()); + + String address = "https://localhost:" + PORT + "/signedjwt/bookstore/books"; + WebClient client = + WebClient.create(address, providers, busFile.toString()); + client.type("application/json").accept("application/json"); + + // Create the JWT Token + JwtClaims claims = new JwtClaims(); + claims.setSubject("alice"); + claims.setIssuer("DoubleItSTSIssuer"); + claims.setIssuedAt(new Date().getTime() / 1000L); + + JwtToken token = new JwtToken(claims); + + 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"); + properties.put(JwtConstants.JWT_TOKEN, token); + WebClient.getConfig(client).getRequestContext().putAll(properties); + + Response response = client.post(new Book("book", 123L)); + assertNotEquals(response.getStatus(), 200); + } + + @org.junit.Test + public void testUnsignedTokenSuccess() throws Exception { + + URL busFile = JWTAlgorithmTest.class.getResource("client.xml"); + + List<Object> providers = new ArrayList<Object>(); + providers.add(new JacksonJsonProvider()); + providers.add(new JwtAuthenticationClientFilter()); + + String address = "https://localhost:" + PORT + "/unsignedjwt/bookstore/books"; + WebClient client = + WebClient.create(address, providers, busFile.toString()); + client.type("application/json").accept("application/json"); + + // Create the JWT Token + JwtClaims claims = new JwtClaims(); + claims.setSubject("alice"); + claims.setIssuer("DoubleItSTSIssuer"); + claims.setIssuedAt(new Date().getTime() / 1000L); + + JwtToken token = new JwtToken(claims); + + Map<String, Object> properties = new HashMap<String, Object>(); + properties.put("rs.security.signature.algorithm", "none"); + properties.put(JwtConstants.JWT_TOKEN, token); + 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 testUnsignedTokenFailure() throws Exception { + + URL busFile = JWTAlgorithmTest.class.getResource("client.xml"); + + List<Object> providers = new ArrayList<Object>(); + providers.add(new JacksonJsonProvider()); + providers.add(new JwtAuthenticationClientFilter()); + + String address = "https://localhost:" + PORT + "/signedjwt/bookstore/books"; + WebClient client = + WebClient.create(address, providers, busFile.toString()); + client.type("application/json").accept("application/json"); + + // Create the JWT Token + JwtClaims claims = new JwtClaims(); + claims.setSubject("alice"); + claims.setIssuer("DoubleItSTSIssuer"); + claims.setIssuedAt(new Date().getTime() / 1000L); + + JwtToken token = new JwtToken(claims); + + Map<String, Object> properties = new HashMap<String, Object>(); + properties.put("rs.security.signature.algorithm", "none"); + properties.put(JwtConstants.JWT_TOKEN, token); + WebClient.getConfig(client).getRequestContext().putAll(properties); + + Response response = client.post(new Book("book", 123L)); + assertNotEquals(response.getStatus(), 200); + } + + @org.junit.Test + public void testSignatureEncryptionProperties() throws Exception { + + URL busFile = JWTAlgorithmTest.class.getResource("client.xml"); + + List<Object> providers = new ArrayList<Object>(); + providers.add(new JacksonJsonProvider()); + JwtAuthenticationClientFilter clientFilter = new JwtAuthenticationClientFilter(); + clientFilter.setJwsRequired(true); + clientFilter.setJweRequired(true); + providers.add(clientFilter); + + String address = "https://localhost:" + PORT + "/signedencryptedjwt/bookstore/books"; + WebClient client = + WebClient.create(address, providers, busFile.toString()); + client.type("application/json").accept("application/json"); + + // Create the JWT Token + JwtClaims claims = new JwtClaims(); + claims.setSubject("alice"); + claims.setIssuer("DoubleItSTSIssuer"); + claims.setIssuedAt(new Date().getTime() / 1000L); + + JwtToken token = new JwtToken(claims); + + Map<String, Object> properties = new HashMap<String, Object>(); + properties.put("rs.security.signature.properties", + "org/apache/cxf/systest/jaxrs/security/alice.jwk.properties"); + properties.put("rs.security.encryption.properties", + "org/apache/cxf/systest/jaxrs/security/bob.jwk.properties"); + properties.put(JwtConstants.JWT_TOKEN, token); + 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); + } +} http://git-wip-us.apache.org/repos/asf/cxf/blob/1c092b9b/systests/rs-security/src/test/java/org/apache/cxf/systest/jaxrs/security/jose/jwt/PrivateKeyPasswordProviderImpl.java ---------------------------------------------------------------------- diff --git a/systests/rs-security/src/test/java/org/apache/cxf/systest/jaxrs/security/jose/jwt/PrivateKeyPasswordProviderImpl.java b/systests/rs-security/src/test/java/org/apache/cxf/systest/jaxrs/security/jose/jwt/PrivateKeyPasswordProviderImpl.java new file mode 100644 index 0000000..bd577c6 --- /dev/null +++ b/systests/rs-security/src/test/java/org/apache/cxf/systest/jaxrs/security/jose/jwt/PrivateKeyPasswordProviderImpl.java @@ -0,0 +1,40 @@ +/** + * 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.jwt; + +import java.util.Properties; + +import org.apache.cxf.rs.security.jose.common.PrivateKeyPasswordProvider; + +public class PrivateKeyPasswordProviderImpl implements PrivateKeyPasswordProvider { + + private String password = "password"; + public PrivateKeyPasswordProviderImpl() { + + } + public 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/1c092b9b/systests/rs-security/src/test/resources/org/apache/cxf/systest/jaxrs/security/jose/jwt/algorithms-server.xml ---------------------------------------------------------------------- diff --git a/systests/rs-security/src/test/resources/org/apache/cxf/systest/jaxrs/security/jose/jwt/algorithms-server.xml b/systests/rs-security/src/test/resources/org/apache/cxf/systest/jaxrs/security/jose/jwt/algorithms-server.xml new file mode 100644 index 0000000..5e270ce --- /dev/null +++ b/systests/rs-security/src/test/resources/org/apache/cxf/systest/jaxrs/security/jose/jwt/algorithms-server.xml @@ -0,0 +1,157 @@ +<?xml version="1.0" encoding="UTF-8"?> +<!-- +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. +--> +<beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:http="http://cxf.apache.org/transports/http/configuration" xmlns:httpj="http://cxf.apache.org/transports/http-jetty/configuration" xmlns:sec="http://cxf.apache.org/configuration/security" xmlns:cxf="http://cxf.apache.org/core" xmlns:jaxrs="http://cxf.apache.org/jaxrs" xsi:schemaLocation=" http://cxf.apache.org/jaxrs http://cxf.apache.org/schemas/jaxrs.xsd http://cxf.apache.org/core http://cxf.apache.org/schemas/core.xsd http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://cxf.apache.org/transports/http/configuration http://cxf.apache.org/schemas/configuration/http-conf.xsd http://cxf.apache.org/transports/http-jetty/configuration http://cxf.apache.org/schemas/configuration/http-jetty.xsd http://cxf.apache.org/configuration/security http://cxf.apache.org/schemas/configuration/security.xsd "> + <bean class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer"/> + <cxf:bus> + <cxf:features> + <cxf:logging/> + </cxf:features> + </cxf:bus> + <httpj:engine-factory id="port-9095-tls-config"> + <httpj:engine port="${testutil.ports.jaxrs-jwt-algorithms}"> + <httpj:tlsServerParameters> + <sec:keyManagers keyPassword="password"> + <sec:keyStore type="JKS" password="password" file="src/test/java/org/apache/cxf/systest/http/resources/Bethal.jks"/> + </sec:keyManagers> + <sec:trustManagers> + <sec:keyStore type="JKS" password="password" file="src/test/java/org/apache/cxf/systest/http/resources/Truststore.jks"/> + </sec:trustManagers> + <sec:clientAuthentication want="true" required="true"/> + </httpj:tlsServerParameters> + </httpj:engine> + </httpj:engine-factory> + + <bean id="serviceBean" class="org.apache.cxf.systest.jaxrs.security.jose.BookStore"/> + + <bean id="jwtEncFilter" class="org.apache.cxf.rs.security.jose.jaxrs.JwtAuthenticationFilter"> + <property name="jwsRequired" value="false"/> + <property name="jweRequired" value="true"/> + </bean> + + <jaxrs:server address="https://localhost:${testutil.ports.jaxrs-jwt-algorithms}/encryptedjwt"> + <jaxrs:serviceBeans> + <ref bean="serviceBean"/> + </jaxrs:serviceBeans> + <jaxrs:providers> + <ref bean="jwtEncFilter"/> + </jaxrs:providers> + <jaxrs:properties> + <entry key="rs.security.encryption.in.properties" + value="org/apache/cxf/systest/jaxrs/security/alice.jwk.properties"/> + </jaxrs:properties> + </jaxrs:server> + + <bean id="jwtSigFilter" class="org.apache.cxf.rs.security.jose.jaxrs.JwtAuthenticationFilter" /> + + <jaxrs:server address="https://localhost:${testutil.ports.jaxrs-jwt-algorithms}/signedjwt"> + <jaxrs:serviceBeans> + <ref bean="serviceBean"/> + </jaxrs:serviceBeans> + <jaxrs:providers> + <ref bean="jwtSigFilter"/> + </jaxrs:providers> + <jaxrs:properties> + <entry key="rs.security.signature.in.properties" + value="org/apache/cxf/systest/jaxrs/security/bob.jwk.properties"/> + </jaxrs:properties> + </jaxrs:server> + + <jaxrs:server address="https://localhost:${testutil.ports.jaxrs-jwt-algorithms}/signedjwtec"> + <jaxrs:serviceBeans> + <ref bean="serviceBean"/> + </jaxrs:serviceBeans> + <jaxrs:providers> + <ref bean="jwtSigFilter"/> + </jaxrs:providers> + <jaxrs:properties> + <entry key="rs.security.keystore.type" value="jwk"/> + <entry key="rs.security.keystore.alias" value="ECKey"/> + <entry key="rs.security.keystore.file" + value="org/apache/cxf/systest/jaxrs/security/certs/jwkPublicSet.txt"/> + <entry key="rs.security.signature.algorithm" value="ES256"/> + </jaxrs:properties> + </jaxrs:server> + + <jaxrs:server address="https://localhost:${testutil.ports.jaxrs-jwt-algorithms}/signedjwtsmallkey"> + <jaxrs:serviceBeans> + <ref bean="serviceBean"/> + </jaxrs:serviceBeans> + <jaxrs:providers> + <ref bean="jwtSigFilter"/> + </jaxrs:providers> + <jaxrs:properties> + <entry key="rs.security.keystore.type" value="jks"/> + <entry key="rs.security.keystore.alias" value="smallkey"/> + <entry key="rs.security.keystore.password" value="security"/> + <entry key="rs.security.key.password" value="security"/> + <entry key="rs.security.keystore.file" + value="org/apache/cxf/systest/jaxrs/security/certs/smallkeysize.jks"/> + <entry key="rs.security.signature.algorithm" value="RS256"/> + </jaxrs:properties> + </jaxrs:server> + + <jaxrs:server address="https://localhost:${testutil.ports.jaxrs-jwt-algorithms}/signedjwtincludecert"> + <jaxrs:serviceBeans> + <ref bean="serviceBean"/> + </jaxrs:serviceBeans> + <jaxrs:providers> + <ref bean="jwtSigFilter"/> + </jaxrs:providers> + <jaxrs:properties> + <entry key="rs.security.keystore.type" value="jks"/> + <entry key="rs.security.keystore.file" + value="org/apache/cxf/systest/jaxrs/security/certs/cxfca.jks"/> + <entry key="rs.security.keystore.password" value="password"/> + </jaxrs:properties> + </jaxrs:server> + + <jaxrs:server address="https://localhost:${testutil.ports.jaxrs-jwt-algorithms}/unsignedjwt"> + <jaxrs:serviceBeans> + <ref bean="serviceBean"/> + </jaxrs:serviceBeans> + <jaxrs:providers> + <ref bean="jwtSigFilter"/> + </jaxrs:providers> + <jaxrs:properties> + <entry key="rs.security.signature.algorithm" value="none" /> + </jaxrs:properties> + </jaxrs:server> + + <bean id="jwtSigEncFilter" class="org.apache.cxf.rs.security.jose.jaxrs.JwtAuthenticationFilter"> + <property name="jwsRequired" value="true"/> + <property name="jweRequired" value="true"/> + </bean> + + <jaxrs:server address="https://localhost:${testutil.ports.jaxrs-jwt-algorithms}/signedencryptedjwt"> + <jaxrs:serviceBeans> + <ref bean="serviceBean"/> + </jaxrs:serviceBeans> + <jaxrs:providers> + <ref bean="jwtSigEncFilter"/> + </jaxrs:providers> + <jaxrs:properties> + <entry key="rs.security.encryption.in.properties" + value="org/apache/cxf/systest/jaxrs/security/alice.jwk.properties"/> + <entry key="rs.security.signature.in.properties" + value="org/apache/cxf/systest/jaxrs/security/bob.jwk.properties"/> + </jaxrs:properties> + </jaxrs:server> + +</beans> http://git-wip-us.apache.org/repos/asf/cxf/blob/1c092b9b/systests/rs-security/src/test/resources/org/apache/cxf/systest/jaxrs/security/jose/jwt/client.xml ---------------------------------------------------------------------- diff --git a/systests/rs-security/src/test/resources/org/apache/cxf/systest/jaxrs/security/jose/jwt/client.xml b/systests/rs-security/src/test/resources/org/apache/cxf/systest/jaxrs/security/jose/jwt/client.xml new file mode 100644 index 0000000..13eaea1 --- /dev/null +++ b/systests/rs-security/src/test/resources/org/apache/cxf/systest/jaxrs/security/jose/jwt/client.xml @@ -0,0 +1,38 @@ +<?xml version="1.0" encoding="UTF-8"?> +<!-- + 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. +--> +<beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:http="http://cxf.apache.org/transports/http/configuration" xmlns:jaxws="http://cxf.apache.org/jaxws" xmlns:cxf="http://cxf.apache.org/core" xmlns:p="http://cxf.apache.org/policy" xmlns:sec="http://cxf.apache.org/configuration/security" xsi:schemaLocation=" http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://cxf.apache.org/jaxws http://cxf.apache.org/schemas/jaxws.xsd http://cxf.apache.org/transports/http/configuration http://cxf.apache.org/schemas/configuration/http-conf.xsd http://cxf.apache.org/configuration/security http://cxf.apache.org/schemas/configuration/security.xsd http://cxf.apache.org/core http://cxf.apache.org/schemas/core.xsd http://cxf.apache.org/policy http://cxf.apache.org/schemas/poli cy.xsd"> + <bean class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer"/> + <cxf:bus> + <cxf:features> + <cxf:logging/> + </cxf:features> + </cxf:bus> + <http:conduit name="https://localhost.*"> + <http:client ConnectionTimeout="3000000" ReceiveTimeout="3000000"/> + <http:tlsClientParameters disableCNCheck="true"> + <sec:keyManagers keyPassword="password"> + <sec:keyStore type="JKS" password="password" file="src/test/java/org/apache/cxf/systest/http/resources/Morpit.jks"/> + </sec:keyManagers> + <sec:trustManagers> + <sec:keyStore type="JKS" password="password" file="src/test/java/org/apache/cxf/systest/http/resources/Truststore.jks"/> + </sec:trustManagers> + </http:tlsClientParameters> + </http:conduit> +</beans>
