Repository: cxf Updated Branches: refs/heads/3.0.x-fixes c3bb80b0c -> 4f06e0011
Adding some JWT authentication/authorization tests Project: http://git-wip-us.apache.org/repos/asf/cxf/repo Commit: http://git-wip-us.apache.org/repos/asf/cxf/commit/4f06e001 Tree: http://git-wip-us.apache.org/repos/asf/cxf/tree/4f06e001 Diff: http://git-wip-us.apache.org/repos/asf/cxf/diff/4f06e001 Branch: refs/heads/3.0.x-fixes Commit: 4f06e0011ac94882500ad9817c79a22fa694eecf Parents: c3bb80b Author: Colm O hEigeartaigh <[email protected]> Authored: Wed Nov 11 12:37:30 2015 +0000 Committer: Colm O hEigeartaigh <[email protected]> Committed: Wed Nov 11 12:39:28 2015 +0000 ---------------------------------------------------------------------- .../jose/jwt/BookServerJwtAuthnAuthz.java | 59 +++++ .../jaxrs/security/jose/jwt/BookStoreAuthn.java | 75 ++++++ .../security/jose/jwt/JWTAuthnAuthzTest.java | 256 +++++++++++++++++++ .../security/jose/jwt/authn-authz-server.xml | 89 +++++++ 4 files changed, 479 insertions(+) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/cxf/blob/4f06e001/systests/rs-security/src/test/java/org/apache/cxf/systest/jaxrs/security/jose/jwt/BookServerJwtAuthnAuthz.java ---------------------------------------------------------------------- diff --git a/systests/rs-security/src/test/java/org/apache/cxf/systest/jaxrs/security/jose/jwt/BookServerJwtAuthnAuthz.java b/systests/rs-security/src/test/java/org/apache/cxf/systest/jaxrs/security/jose/jwt/BookServerJwtAuthnAuthz.java new file mode 100644 index 0000000..3fb107d --- /dev/null +++ b/systests/rs-security/src/test/java/org/apache/cxf/systest/jaxrs/security/jose/jwt/BookServerJwtAuthnAuthz.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 BookServerJwtAuthnAuthz extends AbstractBusTestServerBase { + public static final String PORT = TestUtil.getPortNumber("jaxrs-jwt-authn-authz"); + private static final URL SERVER_CONFIG_FILE = + BookServerJwtAuthnAuthz.class.getResource("authn-authz-server.xml"); + + protected void run() { + SpringBusFactory bf = new SpringBusFactory(); + Bus springBus = bf.createBus(SERVER_CONFIG_FILE); + BusFactory.setDefaultBus(springBus); + setBus(springBus); + + try { + new BookServerJwtAuthnAuthz(); + } catch (Exception e) { + throw new RuntimeException(e); + } + } + + public static void main(String[] args) { + try { + BookServerJwtAuthnAuthz s = new BookServerJwtAuthnAuthz(); + 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/4f06e001/systests/rs-security/src/test/java/org/apache/cxf/systest/jaxrs/security/jose/jwt/BookStoreAuthn.java ---------------------------------------------------------------------- diff --git a/systests/rs-security/src/test/java/org/apache/cxf/systest/jaxrs/security/jose/jwt/BookStoreAuthn.java b/systests/rs-security/src/test/java/org/apache/cxf/systest/jaxrs/security/jose/jwt/BookStoreAuthn.java new file mode 100644 index 0000000..5d5116d --- /dev/null +++ b/systests/rs-security/src/test/java/org/apache/cxf/systest/jaxrs/security/jose/jwt/BookStoreAuthn.java @@ -0,0 +1,75 @@ +/** + * 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 javax.ws.rs.Consumes; +import javax.ws.rs.POST; +import javax.ws.rs.Path; +import javax.ws.rs.Produces; +import javax.ws.rs.core.Context; + +import org.apache.cxf.jaxrs.ext.MessageContext; +import org.apache.cxf.systest.jaxrs.security.Book; +import org.junit.Assert; + +@Path("/bookstore") +public class BookStoreAuthn { + + @Context + MessageContext jaxrsContext; + + public BookStoreAuthn() { + } + + @POST + @Path("/books") + @Produces("text/plain") + @Consumes("text/plain") + public String echoText(String text) { + checkAuthentication(); + return text; + } + + @POST + @Path("/books") + @Produces("application/json") + @Consumes("application/json") + public Book echoBook(Book book) { + checkAuthentication(); + return book; + } + + @POST + @Path("/books") + @Produces("application/xml") + @Consumes("application/xml") + public Book echoBook2(Book book) { + checkAuthentication(); + return book; + } + + private void checkAuthentication() { + // Check that we have an authenticated principal + Assert.assertNotNull(jaxrsContext.getSecurityContext().getUserPrincipal()); + } +} + + http://git-wip-us.apache.org/repos/asf/cxf/blob/4f06e001/systests/rs-security/src/test/java/org/apache/cxf/systest/jaxrs/security/jose/jwt/JWTAuthnAuthzTest.java ---------------------------------------------------------------------- diff --git a/systests/rs-security/src/test/java/org/apache/cxf/systest/jaxrs/security/jose/jwt/JWTAuthnAuthzTest.java b/systests/rs-security/src/test/java/org/apache/cxf/systest/jaxrs/security/jose/jwt/JWTAuthnAuthzTest.java new file mode 100644 index 0000000..7f62b83 --- /dev/null +++ b/systests/rs-security/src/test/java/org/apache/cxf/systest/jaxrs/security/jose/jwt/JWTAuthnAuthzTest.java @@ -0,0 +1,256 @@ +/** + * 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.testutil.common.AbstractBusClientServerTestBase; +import org.bouncycastle.jce.provider.BouncyCastleProvider; +import org.junit.AfterClass; +import org.junit.BeforeClass; + +/** + * Some tests for authentication and authorization using JWT tokens. + */ +public class JWTAuthnAuthzTest extends AbstractBusClientServerTestBase { + public static final String PORT = BookServerJwtAuthnAuthz.PORT; + + @BeforeClass + public static void startServers() throws Exception { + assertTrue("server did not launch correctly", + launchServer(BookServerJwtAuthnAuthz.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); + } + + @org.junit.Test + public void testAuthentication() throws Exception { + + URL busFile = JWTAuthnAuthzTest.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 testAuthenticationFailure() throws Exception { + + URL busFile = JWTAuthnAuthzTest.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 testAuthorization() throws Exception { + + URL busFile = JWTAuthnAuthzTest.class.getResource("client.xml"); + + List<Object> providers = new ArrayList<Object>(); + providers.add(new JacksonJsonProvider()); + providers.add(new JwtAuthenticationClientFilter()); + + String address = "https://localhost:" + PORT + "/signedjwtauthz/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); + // The endpoint requires a role of "boss" + claims.setProperty("role", "boss"); + + 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 testAuthorizationNoRole() throws Exception { + + URL busFile = JWTAuthnAuthzTest.class.getResource("client.xml"); + + List<Object> providers = new ArrayList<Object>(); + providers.add(new JacksonJsonProvider()); + providers.add(new JwtAuthenticationClientFilter()); + + String address = "https://localhost:" + PORT + "/signedjwtauthz/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)); + assertNotEquals(response.getStatus(), 200); + } + + @org.junit.Test + public void testAuthorizationWrongRole() throws Exception { + + URL busFile = JWTAuthnAuthzTest.class.getResource("client.xml"); + + List<Object> providers = new ArrayList<Object>(); + providers.add(new JacksonJsonProvider()); + providers.add(new JwtAuthenticationClientFilter()); + + String address = "https://localhost:" + PORT + "/signedjwtauthz/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); + claims.setProperty("role", "manager"); + + 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)); + assertNotEquals(response.getStatus(), 200); + } + +} http://git-wip-us.apache.org/repos/asf/cxf/blob/4f06e001/systests/rs-security/src/test/resources/org/apache/cxf/systest/jaxrs/security/jose/jwt/authn-authz-server.xml ---------------------------------------------------------------------- diff --git a/systests/rs-security/src/test/resources/org/apache/cxf/systest/jaxrs/security/jose/jwt/authn-authz-server.xml b/systests/rs-security/src/test/resources/org/apache/cxf/systest/jaxrs/security/jose/jwt/authn-authz-server.xml new file mode 100644 index 0000000..9f7f477 --- /dev/null +++ b/systests/rs-security/src/test/resources/org/apache/cxf/systest/jaxrs/security/jose/jwt/authn-authz-server.xml @@ -0,0 +1,89 @@ +<?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-authn-authz}"> + <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.jwt.BookStoreAuthn"/> + + <bean id="jwtSigFilter" class="org.apache.cxf.rs.security.jose.jaxrs.JwtAuthenticationFilter" /> + + <jaxrs:server address="https://localhost:${testutil.ports.jaxrs-jwt-authn-authz}/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> + + <bean id="jwtAuthzFilter" class="org.apache.cxf.rs.security.jose.jaxrs.JwtAuthenticationFilter"> + <property name="roleClaim" value="role"/> + </bean> + + <bean id="authorizationInterceptor" + class="org.apache.cxf.interceptor.security.SimpleAuthorizingInterceptor"> + <property name="methodRolesMap"> + <map> + <entry key="echoBook" value="boss"/> + <entry key="echoBook2" value="boss"/> + <entry key="echoText" value="boss"/> + </map> + </property> + </bean> + + <jaxrs:server address="https://localhost:${testutil.ports.jaxrs-jwt-authn-authz}/signedjwtauthz"> + <jaxrs:serviceBeans> + <ref bean="serviceBean"/> + </jaxrs:serviceBeans> + <jaxrs:providers> + <ref bean="jwtAuthzFilter"/> + </jaxrs:providers> + <jaxrs:inInterceptors> + <ref bean="authorizationInterceptor"/> + </jaxrs:inInterceptors> + <jaxrs:properties> + <entry key="rs.security.signature.in.properties" + value="org/apache/cxf/systest/jaxrs/security/bob.jwk.properties"/> + </jaxrs:properties> + </jaxrs:server> + +</beans>
