From 16f5bbc54eb05a9b79212952feb2bf2a26d3f310 Mon Sep 17 00:00:00 2001 From: Bill Burke Date: Wed, 18 Dec 2013 18:01:47 -0500 Subject: [PATCH 1/3] undertow/wildfly adapter fixes --- .../org/keycloak/util/JsonSerialization.java | 2 +- .../example/CustomerDatabaseClient.java | 4 +- .../src/main/webapp/customers/view.jsp | 2 +- .../example/oauth/ProductDatabaseClient.java | 5 +- .../src/main/webapp/products/view.jsp | 2 +- examples/pom.xml | 1 + examples/wildfly-demo/customer-app/pom.xml | 17 +---- .../example/oauth/CustomerDatabaseClient.java | 36 ----------- .../example/CustomerDatabaseClient.java | 50 +++++++++++++++ .../WEB-INF/jboss-deployment-structure.xml | 4 -- .../src/main/webapp/WEB-INF/jboss-web.xml | 5 -- .../src/main/webapp/admin/admin.jsp | 2 +- .../src/main/webapp/customers/view.jsp | 14 +++-- .../wildfly-demo/database-service/pom.xml | 29 ++++++--- .../WEB-INF/jboss-deployment-structure.xml | 2 - .../src/main/webapp/WEB-INF/jboss-web.xml | 5 -- .../src/main/webapp/WEB-INF/keycloak.json | 4 +- examples/wildfly-demo/pom.xml | 2 +- examples/wildfly-demo/product-app/pom.xml | 19 +----- .../example/oauth/ProductDatabaseClient.java | 36 ----------- .../example/oauth/ProductDatabaseClient.java | 50 +++++++++++++++ .../WEB-INF/jboss-deployment-structure.xml | 4 -- .../src/main/webapp/WEB-INF/jboss-web.xml | 5 -- .../src/main/webapp/products/view.jsp | 11 ++-- examples/wildfly-demo/server/pom.xml | 58 +++++++++++++++-- .../example/demo/DemoApplication.java | 14 +---- .../main/resources/META-INF/testrealm.json | 62 ++++++++++++------- .../WEB-INF/jboss-deployment-structure.xml | 29 ++++++--- examples/wildfly-demo/third-party/pom.xml | 16 +---- .../example/oauth/Bootstrap.java | 4 +- .../example/oauth/ProductDatabaseClient.java | 59 +++++++++--------- .../WEB-INF/jboss-deployment-structure.xml | 2 - .../src/main/webapp/WEB-INF/web.xml | 2 +- .../third-party/src/main/webapp/pull_data.jsp | 3 +- .../third-party/src/main/webapp/redirect.jsp | 4 +- .../undertow/BearerTokenAuthenticator.java | 2 +- .../KeycloakAuthenticationMechanism.java | 2 +- .../undertow/KeycloakServletExtension.java | 32 ++++++++-- .../adapters/undertow/OAuthAuthenticator.java | 6 +- pom.xml | 2 +- 40 files changed, 337 insertions(+), 271 deletions(-) delete mode 100755 examples/wildfly-demo/customer-app/src/main/java/org/jboss/resteasy/example/oauth/CustomerDatabaseClient.java create mode 100755 examples/wildfly-demo/customer-app/src/main/java/org/keycloak/example/CustomerDatabaseClient.java delete mode 100755 examples/wildfly-demo/customer-app/src/main/webapp/WEB-INF/jboss-web.xml mode change 100644 => 100755 examples/wildfly-demo/customer-app/src/main/webapp/admin/admin.jsp delete mode 100755 examples/wildfly-demo/database-service/src/main/webapp/WEB-INF/jboss-web.xml delete mode 100755 examples/wildfly-demo/product-app/src/main/java/org/jboss/resteasy/example/oauth/ProductDatabaseClient.java create mode 100755 examples/wildfly-demo/product-app/src/main/java/org/keycloak/example/oauth/ProductDatabaseClient.java delete mode 100755 examples/wildfly-demo/product-app/src/main/webapp/WEB-INF/jboss-web.xml rename examples/wildfly-demo/third-party/src/main/java/org/{jboss/resteasy => keycloak}/example/oauth/Bootstrap.java (93%) rename examples/wildfly-demo/third-party/src/main/java/org/{jboss/resteasy => keycloak}/example/oauth/ProductDatabaseClient.java (54%) mode change 100644 => 100755 examples/wildfly-demo/third-party/src/main/webapp/pull_data.jsp mode change 100644 => 100755 examples/wildfly-demo/third-party/src/main/webapp/redirect.jsp diff --git a/core/src/main/java/org/keycloak/util/JsonSerialization.java b/core/src/main/java/org/keycloak/util/JsonSerialization.java index 3eb2d35c72..f69c971416 100755 --- a/core/src/main/java/org/keycloak/util/JsonSerialization.java +++ b/core/src/main/java/org/keycloak/util/JsonSerialization.java @@ -8,7 +8,7 @@ import java.io.IOException; import java.io.InputStream; /** - * Any class that extends JsonWebToken will use NON_DEFAULT inclusion + * Utility class to handle simple JSON serializable for Keycloak. * * @author Bill Burke * @version $Revision: 1 $ diff --git a/examples/as7-eap-demo/customer-app/src/main/java/org/keycloak/example/CustomerDatabaseClient.java b/examples/as7-eap-demo/customer-app/src/main/java/org/keycloak/example/CustomerDatabaseClient.java index 6da784323c..2d6249849d 100755 --- a/examples/as7-eap-demo/customer-app/src/main/java/org/keycloak/example/CustomerDatabaseClient.java +++ b/examples/as7-eap-demo/customer-app/src/main/java/org/keycloak/example/CustomerDatabaseClient.java @@ -22,8 +22,8 @@ public class CustomerDatabaseClient { static class TypedList extends ArrayList {} - public static List getCustomers() { - SkeletonKeySession session = SkeletonKeySession.getContext(); + public static List getCustomers(HttpServletRequest req) { + SkeletonKeySession session = (SkeletonKeySession)req.getAttribute(SkeletonKeySession.class.getName()); HttpClient client = new HttpClientBuilder() .trustStore(session.getMetadata().getTruststore()) .hostnameVerification(HttpClientBuilder.HostnameVerificationPolicy.ANY).build(); diff --git a/examples/as7-eap-demo/customer-app/src/main/webapp/customers/view.jsp b/examples/as7-eap-demo/customer-app/src/main/webapp/customers/view.jsp index 344bd3e6f6..71b4efc70f 100755 --- a/examples/as7-eap-demo/customer-app/src/main/webapp/customers/view.jsp +++ b/examples/as7-eap-demo/customer-app/src/main/webapp/customers/view.jsp @@ -16,7 +16,7 @@ User <%=request.getUserPrincipal().getName()%> made this request.

Customer Listing

<% -java.util.List list = CustomerDatabaseClient.getCustomers(); +java.util.List list = CustomerDatabaseClient.getCustomers(request); for (String cust : list) { out.print("

"); diff --git a/examples/as7-eap-demo/product-app/src/main/java/org/keycloak/example/oauth/ProductDatabaseClient.java b/examples/as7-eap-demo/product-app/src/main/java/org/keycloak/example/oauth/ProductDatabaseClient.java index 61dcec6c13..e512597bed 100755 --- a/examples/as7-eap-demo/product-app/src/main/java/org/keycloak/example/oauth/ProductDatabaseClient.java +++ b/examples/as7-eap-demo/product-app/src/main/java/org/keycloak/example/oauth/ProductDatabaseClient.java @@ -8,6 +8,7 @@ import org.keycloak.SkeletonKeySession; import org.keycloak.adapters.HttpClientBuilder; import org.keycloak.util.JsonSerialization; +import javax.servlet.http.HttpServletRequest; import java.io.IOException; import java.io.InputStream; import java.util.ArrayList; @@ -21,8 +22,8 @@ public class ProductDatabaseClient { static class TypedList extends ArrayList {} - public static List getProducts() { - SkeletonKeySession session = SkeletonKeySession.getContext(); + public static List getProducts(HttpServletRequest req) { + SkeletonKeySession session = (SkeletonKeySession)req.getAttribute(SkeletonKeySession.class.getName()); HttpClient client = new HttpClientBuilder() .trustStore(session.getMetadata().getTruststore()) .hostnameVerification(HttpClientBuilder.HostnameVerificationPolicy.ANY).build(); diff --git a/examples/as7-eap-demo/product-app/src/main/webapp/products/view.jsp b/examples/as7-eap-demo/product-app/src/main/webapp/products/view.jsp index cd3d8d0ac9..bc9ef81992 100755 --- a/examples/as7-eap-demo/product-app/src/main/webapp/products/view.jsp +++ b/examples/as7-eap-demo/product-app/src/main/webapp/products/view.jsp @@ -17,7 +17,7 @@ User <%=request.getUserPrincipal().getName()%> made this request.

Product Listing

<% -java.util.List list = ProductDatabaseClient.getProducts(); +java.util.List list = ProductDatabaseClient.getProducts(request); for (String cust : list) { out.print("

"); diff --git a/examples/pom.xml b/examples/pom.xml index 77c7171982..d44bde6d2c 100755 --- a/examples/pom.xml +++ b/examples/pom.xml @@ -35,5 +35,6 @@ as7-eap-demo + wildfly-demo diff --git a/examples/wildfly-demo/customer-app/pom.xml b/examples/wildfly-demo/customer-app/pom.xml index a7ca1ec0b7..3619e6c2e6 100755 --- a/examples/wildfly-demo/customer-app/pom.xml +++ b/examples/wildfly-demo/customer-app/pom.xml @@ -11,7 +11,7 @@ org.keycloak.example.wildfly.demo customer-portal-example war - Customer Portal - Secured via Undertow + Customer Portal - Secured via Valve @@ -28,21 +28,6 @@ jboss-servlet-api_3.0_spec provided - - org.jboss.resteasy - resteasy-client - provided - - - org.keycloak - keycloak-core - ${project.version} - - - org.keycloak - keycloak-adapter-core - ${project.version} - org.keycloak keycloak-undertow-adapter diff --git a/examples/wildfly-demo/customer-app/src/main/java/org/jboss/resteasy/example/oauth/CustomerDatabaseClient.java b/examples/wildfly-demo/customer-app/src/main/java/org/jboss/resteasy/example/oauth/CustomerDatabaseClient.java deleted file mode 100755 index 2da2c84af1..0000000000 --- a/examples/wildfly-demo/customer-app/src/main/java/org/jboss/resteasy/example/oauth/CustomerDatabaseClient.java +++ /dev/null @@ -1,36 +0,0 @@ -package org.jboss.resteasy.example.oauth; - -import org.jboss.resteasy.client.jaxrs.ResteasyClient; -import org.jboss.resteasy.client.jaxrs.ResteasyClientBuilder; -import org.keycloak.SkeletonKeySession; - -import javax.servlet.http.HttpServletRequest; -import javax.ws.rs.core.GenericType; -import javax.ws.rs.core.HttpHeaders; -import javax.ws.rs.core.Response; -import java.util.List; - -/** - * @author Bill Burke - * @version $Revision: 1 $ - */ -public class CustomerDatabaseClient -{ - public static List getCustomers(HttpServletRequest request) - { - SkeletonKeySession session = (SkeletonKeySession)request.getAttribute(SkeletonKeySession.class.getName()); - ResteasyClient client = new ResteasyClientBuilder() - .trustStore(session.getMetadata().getTruststore()) - .hostnameVerification(ResteasyClientBuilder.HostnameVerificationPolicy.ANY).build(); - try - { - Response response = client.target("http://localhost:8080/database/customers").request() - .header(HttpHeaders.AUTHORIZATION, "Bearer " + session.getTokenString()).get(); - return response.readEntity(new GenericType>(){}); - } - finally - { - client.close(); - } - } -} diff --git a/examples/wildfly-demo/customer-app/src/main/java/org/keycloak/example/CustomerDatabaseClient.java b/examples/wildfly-demo/customer-app/src/main/java/org/keycloak/example/CustomerDatabaseClient.java new file mode 100755 index 0000000000..81898b03a0 --- /dev/null +++ b/examples/wildfly-demo/customer-app/src/main/java/org/keycloak/example/CustomerDatabaseClient.java @@ -0,0 +1,50 @@ +package org.keycloak.example; + +import org.apache.http.HttpEntity; +import org.apache.http.HttpResponse; +import org.apache.http.client.HttpClient; +import org.apache.http.client.methods.HttpGet; +import org.keycloak.SkeletonKeySession; +import org.keycloak.adapters.HttpClientBuilder; +import org.keycloak.util.JsonSerialization; + +import javax.servlet.http.HttpServletRequest; +import java.io.IOException; +import java.io.InputStream; +import java.util.ArrayList; +import java.util.List; + +/** + * @author Bill Burke + * @version $Revision: 1 $ + */ +public class CustomerDatabaseClient { + + static class TypedList extends ArrayList {} + + public static List getCustomers(HttpServletRequest req) { + SkeletonKeySession session = (SkeletonKeySession)req.getAttribute(SkeletonKeySession.class.getName()); + + HttpClient client = new HttpClientBuilder() + .trustStore(session.getMetadata().getTruststore()) + .hostnameVerification(HttpClientBuilder.HostnameVerificationPolicy.ANY).build(); + try { + HttpGet get = new HttpGet("http://localhost:8080/database/customers"); + get.addHeader("Authorization", "Bearer " + session.getTokenString()); + try { + HttpResponse response = client.execute(get); + HttpEntity entity = response.getEntity(); + InputStream is = entity.getContent(); + try { + return JsonSerialization.readValue(is, TypedList.class); + } finally { + is.close(); + } + } catch (IOException e) { + throw new RuntimeException(e); + } + } finally { + client.getConnectionManager().shutdown(); + } + } +} diff --git a/examples/wildfly-demo/customer-app/src/main/webapp/WEB-INF/jboss-deployment-structure.xml b/examples/wildfly-demo/customer-app/src/main/webapp/WEB-INF/jboss-deployment-structure.xml index 1469973bc4..c54e4abec6 100755 --- a/examples/wildfly-demo/customer-app/src/main/webapp/WEB-INF/jboss-deployment-structure.xml +++ b/examples/wildfly-demo/customer-app/src/main/webapp/WEB-INF/jboss-deployment-structure.xml @@ -2,10 +2,6 @@ - - - - \ No newline at end of file diff --git a/examples/wildfly-demo/customer-app/src/main/webapp/WEB-INF/jboss-web.xml b/examples/wildfly-demo/customer-app/src/main/webapp/WEB-INF/jboss-web.xml deleted file mode 100755 index 3cec19cc47..0000000000 --- a/examples/wildfly-demo/customer-app/src/main/webapp/WEB-INF/jboss-web.xml +++ /dev/null @@ -1,5 +0,0 @@ - - - org.keycloak.adapters.as7.OAuthManagedResourceValve - - \ No newline at end of file diff --git a/examples/wildfly-demo/customer-app/src/main/webapp/admin/admin.jsp b/examples/wildfly-demo/customer-app/src/main/webapp/admin/admin.jsp old mode 100644 new mode 100755 index e132e3701f..39c2a439fe --- a/examples/wildfly-demo/customer-app/src/main/webapp/admin/admin.jsp +++ b/examples/wildfly-demo/customer-app/src/main/webapp/admin/admin.jsp @@ -2,7 +2,7 @@ pageEncoding="ISO-8859-1"%> - Customer Admin Iterface + Customer Admin Interface

Customer Admin Interface

diff --git a/examples/wildfly-demo/customer-app/src/main/webapp/customers/view.jsp b/examples/wildfly-demo/customer-app/src/main/webapp/customers/view.jsp index 6e825f7137..71b4efc70f 100755 --- a/examples/wildfly-demo/customer-app/src/main/webapp/customers/view.jsp +++ b/examples/wildfly-demo/customer-app/src/main/webapp/customers/view.jsp @@ -1,20 +1,22 @@ -<%@ page import="javax.ws.rs.core.UriBuilder" language="java" contentType="text/html; charset=ISO-8859-1" +<%@ page language="java" contentType="text/html; charset=ISO-8859-1" pageEncoding="ISO-8859-1"%> +<%@ page import="org.keycloak.example.CustomerDatabaseClient" %> +<%@ page import="org.keycloak.util.KeycloakUriBuilder" %> Customer View Page <% - - String logoutUri = UriBuilder.fromUri("http://localhost:8080/auth-server/rest/realms/demo/tokens/logout") - .queryParam("redirect_uri", "http://localhost:8080/customer-portal").build().toString(); + String logoutUri = KeycloakUriBuilder.fromUri("http://localhost:8080/auth-server/rest/realms/demo/tokens/logout") + .queryParam("redirect_uri", "http://localhost:8080/customer-portal").build().toString(); + String acctUri = "http://localhost:8080/auth-server/rest/realms/demo/account"; %> -

Goto: products | logout

+

Goto: products | logout | manage acct

User <%=request.getUserPrincipal().getName()%> made this request.

Customer Listing

<% -java.util.List list = org.jboss.resteasy.example.oauth.CustomerDatabaseClient.getCustomers(request); +java.util.List list = CustomerDatabaseClient.getCustomers(request); for (String cust : list) { out.print("

"); diff --git a/examples/wildfly-demo/database-service/pom.xml b/examples/wildfly-demo/database-service/pom.xml index 160a1be4fa..5229b7dddf 100755 --- a/examples/wildfly-demo/database-service/pom.xml +++ b/examples/wildfly-demo/database-service/pom.xml @@ -30,29 +30,40 @@ provided - org.keycloak - keycloak-core - ${project.version} - - - org.keycloak - keycloak-adapter-core - ${project.version} + org.jboss.resteasy + resteasy-jaxrs + provided + org.keycloak keycloak-undertow-adapter ${project.version} + database + + org.apache.maven.plugins + maven-deploy-plugin + + true + + org.jboss.as.plugins jboss-as-maven-plugin - 7.5.Final + 7.4.Final org.apache.maven.plugins diff --git a/examples/wildfly-demo/database-service/src/main/webapp/WEB-INF/jboss-deployment-structure.xml b/examples/wildfly-demo/database-service/src/main/webapp/WEB-INF/jboss-deployment-structure.xml index f1f1ffa354..c54e4abec6 100755 --- a/examples/wildfly-demo/database-service/src/main/webapp/WEB-INF/jboss-deployment-structure.xml +++ b/examples/wildfly-demo/database-service/src/main/webapp/WEB-INF/jboss-deployment-structure.xml @@ -2,8 +2,6 @@ - - \ No newline at end of file diff --git a/examples/wildfly-demo/database-service/src/main/webapp/WEB-INF/jboss-web.xml b/examples/wildfly-demo/database-service/src/main/webapp/WEB-INF/jboss-web.xml deleted file mode 100755 index d1ca3931f9..0000000000 --- a/examples/wildfly-demo/database-service/src/main/webapp/WEB-INF/jboss-web.xml +++ /dev/null @@ -1,5 +0,0 @@ - - - org.keycloak.adapters.as7.BearerTokenAuthenticatorValve - - \ No newline at end of file diff --git a/examples/wildfly-demo/database-service/src/main/webapp/WEB-INF/keycloak.json b/examples/wildfly-demo/database-service/src/main/webapp/WEB-INF/keycloak.json index 6b707d9046..2e02cc3590 100755 --- a/examples/wildfly-demo/database-service/src/main/webapp/WEB-INF/keycloak.json +++ b/examples/wildfly-demo/database-service/src/main/webapp/WEB-INF/keycloak.json @@ -2,7 +2,7 @@ "realm" : "demo", "resource" : "database-service", "realm-public-key" : "MIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQCrVrCuTtArbgaZzL1hvh0xtL5mc7o0NqPVnYXkLvgcwiC3BjLGw1tGEGoJaXDuSaRllobm53JBhjx33UNv+5z/UMG4kytBWxheNVKnL6GgqlNabMaFfPLPCF8kAgKnsi79NMo+n6KnSY8YeUmec/p2vjO2NjsSAVcWEQMVhJ31LwIDAQAB", - "enable-cors" : true, - "bearer-only" : true + "bearer-only" : true, + "enable-cors" : true } diff --git a/examples/wildfly-demo/pom.xml b/examples/wildfly-demo/pom.xml index 2f8020cc13..e170820bf7 100755 --- a/examples/wildfly-demo/pom.xml +++ b/examples/wildfly-demo/pom.xml @@ -26,7 +26,7 @@ org.jboss.as.plugins jboss-as-maven-plugin - 7.5.Final + 7.1.1.Final true diff --git a/examples/wildfly-demo/product-app/pom.xml b/examples/wildfly-demo/product-app/pom.xml index e7191c7ed5..2830bc169e 100755 --- a/examples/wildfly-demo/product-app/pom.xml +++ b/examples/wildfly-demo/product-app/pom.xml @@ -11,7 +11,7 @@ org.keycloak.example.wildfly.demo product-portal-example war - Product Portal - Secured via Undertow + Product Portal @@ -28,21 +28,6 @@ jboss-servlet-api_3.0_spec provided - - org.jboss.resteasy - resteasy-client - provided - - - org.keycloak - keycloak-core - ${project.version} - - - org.keycloak - keycloak-adapter-core - ${project.version} - org.keycloak keycloak-undertow-adapter @@ -56,7 +41,7 @@ org.jboss.as.plugins jboss-as-maven-plugin - 7.5.Final + 7.4.Final org.apache.maven.plugins diff --git a/examples/wildfly-demo/product-app/src/main/java/org/jboss/resteasy/example/oauth/ProductDatabaseClient.java b/examples/wildfly-demo/product-app/src/main/java/org/jboss/resteasy/example/oauth/ProductDatabaseClient.java deleted file mode 100755 index 1111268381..0000000000 --- a/examples/wildfly-demo/product-app/src/main/java/org/jboss/resteasy/example/oauth/ProductDatabaseClient.java +++ /dev/null @@ -1,36 +0,0 @@ -package org.jboss.resteasy.example.oauth; - -import org.jboss.resteasy.client.jaxrs.ResteasyClient; -import org.jboss.resteasy.client.jaxrs.ResteasyClientBuilder; -import org.keycloak.SkeletonKeySession; - -import javax.servlet.http.HttpServletRequest; -import javax.ws.rs.core.GenericType; -import javax.ws.rs.core.HttpHeaders; -import javax.ws.rs.core.Response; -import java.util.List; - -/** - * @author Bill Burke - * @version $Revision: 1 $ - */ -public class ProductDatabaseClient -{ - public static List getProducts(HttpServletRequest request) - { - SkeletonKeySession session = (SkeletonKeySession)request.getAttribute(SkeletonKeySession.class.getName()); - ResteasyClient client = new ResteasyClientBuilder() - .trustStore(session.getMetadata().getTruststore()) - .hostnameVerification(ResteasyClientBuilder.HostnameVerificationPolicy.ANY).build(); - try - { - Response response = client.target("http://localhost:8080/database/products").request() - .header(HttpHeaders.AUTHORIZATION, "Bearer " + session.getTokenString()).get(); - return response.readEntity(new GenericType>(){}); - } - finally - { - client.close(); - } - } -} diff --git a/examples/wildfly-demo/product-app/src/main/java/org/keycloak/example/oauth/ProductDatabaseClient.java b/examples/wildfly-demo/product-app/src/main/java/org/keycloak/example/oauth/ProductDatabaseClient.java new file mode 100755 index 0000000000..e512597bed --- /dev/null +++ b/examples/wildfly-demo/product-app/src/main/java/org/keycloak/example/oauth/ProductDatabaseClient.java @@ -0,0 +1,50 @@ +package org.keycloak.example.oauth; + +import org.apache.http.HttpEntity; +import org.apache.http.HttpResponse; +import org.apache.http.client.HttpClient; +import org.apache.http.client.methods.HttpGet; +import org.keycloak.SkeletonKeySession; +import org.keycloak.adapters.HttpClientBuilder; +import org.keycloak.util.JsonSerialization; + +import javax.servlet.http.HttpServletRequest; +import java.io.IOException; +import java.io.InputStream; +import java.util.ArrayList; +import java.util.List; + +/** + * @author Bill Burke + * @version $Revision: 1 $ + */ +public class ProductDatabaseClient +{ + static class TypedList extends ArrayList {} + + public static List getProducts(HttpServletRequest req) { + SkeletonKeySession session = (SkeletonKeySession)req.getAttribute(SkeletonKeySession.class.getName()); + HttpClient client = new HttpClientBuilder() + .trustStore(session.getMetadata().getTruststore()) + .hostnameVerification(HttpClientBuilder.HostnameVerificationPolicy.ANY).build(); + try { + HttpGet get = new HttpGet("http://localhost:8080/database/products"); + get.addHeader("Authorization", "Bearer " + session.getTokenString()); + try { + HttpResponse response = client.execute(get); + HttpEntity entity = response.getEntity(); + InputStream is = entity.getContent(); + try { + return JsonSerialization.readValue(is, TypedList.class); + } finally { + is.close(); + } + } catch (IOException e) { + throw new RuntimeException(e); + } + } finally { + client.getConnectionManager().shutdown(); + } + } + +} diff --git a/examples/wildfly-demo/product-app/src/main/webapp/WEB-INF/jboss-deployment-structure.xml b/examples/wildfly-demo/product-app/src/main/webapp/WEB-INF/jboss-deployment-structure.xml index 1469973bc4..c54e4abec6 100755 --- a/examples/wildfly-demo/product-app/src/main/webapp/WEB-INF/jboss-deployment-structure.xml +++ b/examples/wildfly-demo/product-app/src/main/webapp/WEB-INF/jboss-deployment-structure.xml @@ -2,10 +2,6 @@ - - - - \ No newline at end of file diff --git a/examples/wildfly-demo/product-app/src/main/webapp/WEB-INF/jboss-web.xml b/examples/wildfly-demo/product-app/src/main/webapp/WEB-INF/jboss-web.xml deleted file mode 100755 index 3cec19cc47..0000000000 --- a/examples/wildfly-demo/product-app/src/main/webapp/WEB-INF/jboss-web.xml +++ /dev/null @@ -1,5 +0,0 @@ - - - org.keycloak.adapters.as7.OAuthManagedResourceValve - - \ No newline at end of file diff --git a/examples/wildfly-demo/product-app/src/main/webapp/products/view.jsp b/examples/wildfly-demo/product-app/src/main/webapp/products/view.jsp index bf1ca5ad91..bc9ef81992 100755 --- a/examples/wildfly-demo/product-app/src/main/webapp/products/view.jsp +++ b/examples/wildfly-demo/product-app/src/main/webapp/products/view.jsp @@ -1,20 +1,23 @@ -<%@ page import="javax.ws.rs.core.UriBuilder" language="java" contentType="text/html; charset=ISO-8859-1" +<%@ page language="java" contentType="text/html; charset=ISO-8859-1" pageEncoding="ISO-8859-1"%> +<%@ page import="org.keycloak.example.oauth.ProductDatabaseClient" %> +<%@ page import="org.keycloak.util.KeycloakUriBuilder" %> Product View Page <% - String logoutUri = UriBuilder.fromUri("http://localhost:8080/auth-server/rest/realms/demo/tokens/logout") + String logoutUri = KeycloakUriBuilder.fromUri("http://localhost:8080/auth-server/rest/realms/demo/tokens/logout") .queryParam("redirect_uri", "http://localhost:8080/product-portal").build().toString(); + String acctUri = "http://localhost:8080/auth-server/rest/realms/demo/account"; %> -

Goto: customers | logout

+

Goto: customers | logout | manage acct

User <%=request.getUserPrincipal().getName()%> made this request.

Product Listing

<% -java.util.List list = org.jboss.resteasy.example.oauth.ProductDatabaseClient.getProducts(request); +java.util.List list = ProductDatabaseClient.getProducts(request); for (String cust : list) { out.print("

"); diff --git a/examples/wildfly-demo/server/pom.xml b/examples/wildfly-demo/server/pom.xml index 1870a90c35..2b4207d7d4 100755 --- a/examples/wildfly-demo/server/pom.xml +++ b/examples/wildfly-demo/server/pom.xml @@ -15,11 +15,20 @@ + + org.bouncycastle + bcprov-jdk16 + org.keycloak keycloak-core ${project.version} + + org.keycloak + keycloak-core-jaxrs + ${project.version} + org.keycloak keycloak-services @@ -30,6 +39,11 @@ keycloak-model-api ${project.version} + + org.keycloak + keycloak-model-jpa + ${project.version} + org.keycloak keycloak-social-core @@ -65,6 +79,7 @@ keycloak-admin-ui-styles ${project.version} + com.h2database h2 @@ -101,16 +117,48 @@ de.flapdoodle.embed de.flapdoodle.embed.mongo - - org.jboss.spec.javax.servlet - jboss-servlet-api_3.0_spec - junit junit 4.1 test + + org.jboss.spec.javax.servlet + jboss-servlet-api_3.0_spec + provided + + + + org.jboss.resteasy + resteasy-jaxrs + ${resteasy.version} + + + org.jboss.resteasy + resteasy-multipart-provider + ${resteasy.version} + + + org.jboss.resteasy + resteasy-client + ${resteasy.version} + + + org.jboss.resteasy + async-http-servlet-3.0 + ${resteasy.version} + + + org.jboss.resteasy + jaxrs-api + ${resteasy.version} + + + org.jboss.resteasy + resteasy-jackson-provider + ${resteasy.version} + @@ -119,7 +167,7 @@ org.jboss.as.plugins jboss-as-maven-plugin - 7.5.Final + 7.4.Final org.apache.maven.plugins diff --git a/examples/wildfly-demo/server/src/main/java/org/keycloak/example/demo/DemoApplication.java b/examples/wildfly-demo/server/src/main/java/org/keycloak/example/demo/DemoApplication.java index 0b7b49d934..ee95beb654 100755 --- a/examples/wildfly-demo/server/src/main/java/org/keycloak/example/demo/DemoApplication.java +++ b/examples/wildfly-demo/server/src/main/java/org/keycloak/example/demo/DemoApplication.java @@ -1,6 +1,6 @@ package org.keycloak.example.demo; -import org.jboss.resteasy.jwt.JsonSerialization; +import org.keycloak.util.JsonSerialization; import org.keycloak.models.KeycloakSession; import org.keycloak.models.RealmModel; import org.keycloak.representations.idm.RealmRepresentation; @@ -10,7 +10,6 @@ import org.keycloak.services.resources.KeycloakApplication; import javax.servlet.ServletContext; import javax.ws.rs.core.Context; -import java.io.ByteArrayOutputStream; import java.io.IOException; import java.io.InputStream; /** @@ -39,17 +38,8 @@ public class DemoApplication extends KeycloakApplication { public static RealmRepresentation loadJson(String path) { InputStream is = Thread.currentThread().getContextClassLoader().getResourceAsStream(path); - ByteArrayOutputStream os = new ByteArrayOutputStream(); - int c; try { - while ( (c = is.read()) != -1) - { - os.write(c); - } - byte[] bytes = os.toByteArray(); - //System.out.println(new String(bytes)); - - return JsonSerialization.fromBytes(RealmRepresentation.class, bytes); + return JsonSerialization.readValue(is, RealmRepresentation.class); } catch (IOException e) { throw new RuntimeException(e); } diff --git a/examples/wildfly-demo/server/src/main/resources/META-INF/testrealm.json b/examples/wildfly-demo/server/src/main/resources/META-INF/testrealm.json index 284a4be550..fca38a9348 100755 --- a/examples/wildfly-demo/server/src/main/resources/META-INF/testrealm.json +++ b/examples/wildfly-demo/server/src/main/resources/META-INF/testrealm.json @@ -1,20 +1,20 @@ { "realm": "demo", "enabled": true, - "tokenLifespan": 300, + "tokenLifespan": 3000, "accessCodeLifespan": 10, - "accessCodeLifespanUserAction": 600, + "accessCodeLifespanUserAction": 6000, + "accountManagement": true, "sslNotRequired": true, "cookieLoginAllowed": true, - "registrationAllowed": true, - "social": true, + "registrationAllowed": false, + "social": false, "automaticRegistrationAfterSocialLogin": false, "privateKey": "MIICXAIBAAKBgQCrVrCuTtArbgaZzL1hvh0xtL5mc7o0NqPVnYXkLvgcwiC3BjLGw1tGEGoJaXDuSaRllobm53JBhjx33UNv+5z/UMG4kytBWxheNVKnL6GgqlNabMaFfPLPCF8kAgKnsi79NMo+n6KnSY8YeUmec/p2vjO2NjsSAVcWEQMVhJ31LwIDAQABAoGAfmO8gVhyBxdqlxmIuglbz8bcjQbhXJLR2EoS8ngTXmN1bo2L90M0mUKSdc7qF10LgETBzqL8jYlQIbt+e6TH8fcEpKCjUlyq0Mf/vVbfZSNaVycY13nTzo27iPyWQHK5NLuJzn1xvxxrUeXI6A2WFpGEBLbHjwpx5WQG9A+2scECQQDvdn9NE75HPTVPxBqsEd2z10TKkl9CZxu10Qby3iQQmWLEJ9LNmy3acvKrE3gMiYNWb6xHPKiIqOR1as7L24aTAkEAtyvQOlCvr5kAjVqrEKXalj0Tzewjweuxc0pskvArTI2Oo070h65GpoIKLc9jf+UA69cRtquwP93aZKtW06U8dQJAF2Y44ks/mK5+eyDqik3koCI08qaC8HYq2wVl7G2QkJ6sbAaILtcvD92ToOvyGyeE0flvmDZxMYlvaZnaQ0lcSQJBAKZU6umJi3/xeEbkJqMfeLclD27XGEFoPeNrmdx0q10Azp4NfJAY+Z8KRyQCR2BEG+oNitBOZ+YXF9KCpH3cdmECQHEigJhYg+ykOvr1aiZUMFT72HU0jnmQe2FVekuG+LJUt2Tm7GtMjTFoGpf0JwrVuZN39fOYAlo+nTixgeW7X8Y=", "publicKey": "MIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQCrVrCuTtArbgaZzL1hvh0xtL5mc7o0NqPVnYXkLvgcwiC3BjLGw1tGEGoJaXDuSaRllobm53JBhjx33UNv+5z/UMG4kytBWxheNVKnL6GgqlNabMaFfPLPCF8kAgKnsi79NMo+n6KnSY8YeUmec/p2vjO2NjsSAVcWEQMVhJ31LwIDAQAB", "requiredCredentials": [ "password" ], "requiredApplicationCredentials": [ "password" ], "requiredOAuthClientCredentials": [ "password" ], - "defaultRoles": [ "user" ], "users" : [ { "username" : "bburke@redhat.com", @@ -26,48 +26,44 @@ { "type" : "password", "value" : "password" } ] - }, - { - "username" : "third-party", - "enabled": true, - "credentials" : [ - { "type" : "password", - "value" : "password" } - ] } ], "roles": [ { "name": "user", - "description": "Have User privileges" + "description": "User privileges" }, { "name": "admin", - "description": "Have Administrator privileges" + "description": "Administrator privileges" } ], "roleMappings": [ { "username": "bburke@redhat.com", "roles": ["user"] - }, - { - "username": "third-party", - "roles": ["KEYCLOAK_IDENTITY_REQUESTER"] } ], "scopeMappings": [ { "username": "third-party", "roles": ["user"] + }, + { + "username": "customer-portal", + "roles": ["user"] + }, + { + "username": "product-portal", + "roles": ["user"] } + ], "applications": [ { "name": "customer-portal", "enabled": true, "adminUrl": "http://localhost:8080/customer-portal/j_admin_request", - "useRealmMappings": true, "credentials": [ { "type": "password", @@ -79,7 +75,6 @@ "name": "product-portal", "enabled": true, "adminUrl": "http://localhost:8080/product-portal/j_admin_request", - "useRealmMappings": true, "credentials": [ { "type": "password", @@ -87,5 +82,26 @@ } ] } - ] -} \ No newline at end of file + ], + "oauthClients": [ + { + "name": "third-party", + "enabled": true, + "credentials": [ + { + "type": "password", + "value": "password" + } + ] + } + ], + "applicationRoleMappings": { + "Account": [ + { + "username": "bburke@redhat.com", + "roles": ["manage-account"] + } + ] + } + +} diff --git a/examples/wildfly-demo/server/src/main/webapp/WEB-INF/jboss-deployment-structure.xml b/examples/wildfly-demo/server/src/main/webapp/WEB-INF/jboss-deployment-structure.xml index 8caa96f6a0..2654f310d6 100755 --- a/examples/wildfly-demo/server/src/main/webapp/WEB-INF/jboss-deployment-structure.xml +++ b/examples/wildfly-demo/server/src/main/webapp/WEB-INF/jboss-deployment-structure.xml @@ -1,10 +1,21 @@ - - - - - - - - - + + + + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/examples/wildfly-demo/third-party/pom.xml b/examples/wildfly-demo/third-party/pom.xml index 79cfe97fa4..b708b835a2 100755 --- a/examples/wildfly-demo/third-party/pom.xml +++ b/examples/wildfly-demo/third-party/pom.xml @@ -11,7 +11,7 @@ org.keycloak.example.wildfly.demo oauth-client-example war - Simple OAuth Wildfly Client + Simple OAuth Client @@ -21,19 +21,9 @@ 1.0.1.Final provided - - org.jboss.resteasy - resteasy-client - provided - org.keycloak - keycloak-core - ${project.version} - - - org.keycloak - keycloak-adapter-core + keycloak-servlet-oauth-client ${project.version} @@ -44,7 +34,7 @@ org.jboss.as.plugins jboss-as-maven-plugin - 7.5.Final + 7.4.Final org.apache.maven.plugins diff --git a/examples/wildfly-demo/third-party/src/main/java/org/jboss/resteasy/example/oauth/Bootstrap.java b/examples/wildfly-demo/third-party/src/main/java/org/keycloak/example/oauth/Bootstrap.java similarity index 93% rename from examples/wildfly-demo/third-party/src/main/java/org/jboss/resteasy/example/oauth/Bootstrap.java rename to examples/wildfly-demo/third-party/src/main/java/org/keycloak/example/oauth/Bootstrap.java index 717cd3e5a1..121af39b0a 100755 --- a/examples/wildfly-demo/third-party/src/main/java/org/jboss/resteasy/example/oauth/Bootstrap.java +++ b/examples/wildfly-demo/third-party/src/main/java/org/keycloak/example/oauth/Bootstrap.java @@ -1,6 +1,5 @@ -package org.jboss.resteasy.example.oauth; +package org.keycloak.example.oauth; -import org.jboss.resteasy.client.jaxrs.ResteasyClientBuilder; import org.keycloak.servlet.ServletOAuthClient; import javax.servlet.ServletContextEvent; @@ -55,7 +54,6 @@ public class Bootstrap implements ServletContextListener { client.setPassword("password"); client.setAuthUrl("http://localhost:8080/auth-server/rest/realms/demo/tokens/login"); client.setCodeUrl("http://localhost:8080/auth-server/rest/realms/demo/tokens/access/codes"); - client.setClient(new ResteasyClientBuilder().build()); client.start(); sce.getServletContext().setAttribute(ServletOAuthClient.class.getName(), client); diff --git a/examples/wildfly-demo/third-party/src/main/java/org/jboss/resteasy/example/oauth/ProductDatabaseClient.java b/examples/wildfly-demo/third-party/src/main/java/org/keycloak/example/oauth/ProductDatabaseClient.java similarity index 54% rename from examples/wildfly-demo/third-party/src/main/java/org/jboss/resteasy/example/oauth/ProductDatabaseClient.java rename to examples/wildfly-demo/third-party/src/main/java/org/keycloak/example/oauth/ProductDatabaseClient.java index d21c823bd8..2b4cbd5c72 100755 --- a/examples/wildfly-demo/third-party/src/main/java/org/jboss/resteasy/example/oauth/ProductDatabaseClient.java +++ b/examples/wildfly-demo/third-party/src/main/java/org/keycloak/example/oauth/ProductDatabaseClient.java @@ -1,15 +1,18 @@ -package org.jboss.resteasy.example.oauth; +package org.keycloak.example.oauth; -import org.jboss.resteasy.client.jaxrs.ResteasyClient; -import org.jboss.resteasy.client.jaxrs.ResteasyClientBuilder; +import org.apache.http.HttpEntity; +import org.apache.http.HttpResponse; +import org.apache.http.client.HttpClient; +import org.apache.http.client.methods.HttpGet; +import org.keycloak.adapters.TokenGrantRequest; import org.keycloak.servlet.ServletOAuthClient; +import org.keycloak.util.JsonSerialization; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; -import javax.ws.rs.core.GenericType; -import javax.ws.rs.core.HttpHeaders; -import javax.ws.rs.core.Response; import java.io.IOException; +import java.io.InputStream; +import java.util.ArrayList; import java.util.List; /** @@ -31,6 +34,8 @@ public class ProductDatabaseClient { } } + static class TypedList extends ArrayList {} + public static List getProducts(HttpServletRequest request) { // This is really the worst code ever. The ServletOAuthClient is obtained by getting a context attribute // that is set in the Bootstrap context listenr in this project. @@ -38,32 +43,30 @@ public class ProductDatabaseClient { // and obtain the ServletOAuthClient. I actually suggest downloading the ServletOAuthClient code // and take a look how it works. ServletOAuthClient oAuthClient = (ServletOAuthClient) request.getServletContext().getAttribute(ServletOAuthClient.class.getName()); - String token = oAuthClient.getBearerToken(request); - ResteasyClient client = new ResteasyClientBuilder() - .trustStore(oAuthClient.getTruststore()) - .hostnameVerification(ResteasyClientBuilder.HostnameVerificationPolicy.ANY).build(); + String token = null; try { - // invoke without the Authorization header - Response response = client.target("http://localhost:8080/database/products").request().get(); - response.close(); - if (response.getStatus() != 401) { - response.close(); - client.close(); - throw new RuntimeException("Expecting an auth status code: " + response.getStatus()); - } - } finally { + token = oAuthClient.getBearerToken(request); + } catch (IOException e) { + throw new RuntimeException(e); + } catch (TokenGrantRequest.HttpFailure failure) { + throw new RuntimeException(failure); } + + HttpClient client = oAuthClient.getClient(); + + HttpGet get = new HttpGet("http://localhost:8080/database/products"); + get.addHeader("Authorization", "Bearer " + token); try { - Response response = client.target("http://localhost:8080/database/products").request() - .header(HttpHeaders.AUTHORIZATION, "Bearer " + token).get(); - if (response.getStatus() != 200) { - response.close(); - throw new RuntimeException("Failed to access!: " + response.getStatus()); + HttpResponse response = client.execute(get); + HttpEntity entity = response.getEntity(); + InputStream is = entity.getContent(); + try { + return JsonSerialization.readValue(is, TypedList.class); + } finally { + is.close(); } - return response.readEntity(new GenericType>() { - }); - } finally { - client.close(); + } catch (IOException e) { + throw new RuntimeException(e); } } } diff --git a/examples/wildfly-demo/third-party/src/main/webapp/WEB-INF/jboss-deployment-structure.xml b/examples/wildfly-demo/third-party/src/main/webapp/WEB-INF/jboss-deployment-structure.xml index 74f5dff8db..c54e4abec6 100755 --- a/examples/wildfly-demo/third-party/src/main/webapp/WEB-INF/jboss-deployment-structure.xml +++ b/examples/wildfly-demo/third-party/src/main/webapp/WEB-INF/jboss-deployment-structure.xml @@ -2,8 +2,6 @@ - - \ No newline at end of file diff --git a/examples/wildfly-demo/third-party/src/main/webapp/WEB-INF/web.xml b/examples/wildfly-demo/third-party/src/main/webapp/WEB-INF/web.xml index 501b203606..958839db9f 100755 --- a/examples/wildfly-demo/third-party/src/main/webapp/WEB-INF/web.xml +++ b/examples/wildfly-demo/third-party/src/main/webapp/WEB-INF/web.xml @@ -7,7 +7,7 @@ oauth-client - org.jboss.resteasy.example.oauth.Bootstrap + org.keycloak.example.oauth.Bootstrap