diff --git a/testsuite/performance-web/src/main/java/org/keycloak/testsuite/performance/web/KeycloakPerfServer.java b/testsuite/performance-web/src/main/java/org/keycloak/testsuite/performance/web/KeycloakPerfServer.java index b15101955a..9a64680c52 100644 --- a/testsuite/performance-web/src/main/java/org/keycloak/testsuite/performance/web/KeycloakPerfServer.java +++ b/testsuite/performance-web/src/main/java/org/keycloak/testsuite/performance/web/KeycloakPerfServer.java @@ -78,7 +78,7 @@ public class KeycloakPerfServer { deploymentInfo.setContextPath("/perf-app"); ServletInfo servlet = new ServletInfo("PerfAppServlet", PerfAppServlet.class); - servlet.addMapping("/perf-servlet"); + servlet.addMapping("/perf-servlet/*"); deploymentInfo.addServlet(servlet); diff --git a/testsuite/performance-web/src/main/java/org/keycloak/testsuite/performance/web/PerfAppServlet.java b/testsuite/performance-web/src/main/java/org/keycloak/testsuite/performance/web/PerfAppServlet.java index 6de11f9b45..a781b31218 100644 --- a/testsuite/performance-web/src/main/java/org/keycloak/testsuite/performance/web/PerfAppServlet.java +++ b/testsuite/performance-web/src/main/java/org/keycloak/testsuite/performance/web/PerfAppServlet.java @@ -15,8 +15,10 @@ import freemarker.cache.ClassTemplateLoader; import freemarker.template.Configuration; import freemarker.template.Template; import freemarker.template.TemplateException; +import org.keycloak.adapters.AdapterConstants; import org.keycloak.representations.AccessToken; import org.keycloak.representations.RefreshToken; +import org.keycloak.util.Time; /** * @author Marek Posolda @@ -43,6 +45,7 @@ public class PerfAppServlet extends HttpServlet { protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { resp.setContentType("text/html"); String action = req.getParameter("action"); + String actionDone = null; if (action != null) { if (action.equals("code")) { @@ -50,8 +53,10 @@ public class PerfAppServlet extends HttpServlet { return; } else if (action.equals("exchangeCode")) { exchangeCodeForToken(req, resp); + actionDone = "Token retrieved"; } else if (action.equals("refresh")) { refreshToken(req, resp); + actionDone = "Token refreshed"; } else if (action.equals("logout")) { logoutRedirect(req, resp); return; @@ -61,13 +66,22 @@ public class PerfAppServlet extends HttpServlet { String code = req.getParameter("code"); if (code != null) { req.getSession().setAttribute("code", code); + actionDone = "Code retrieved"; } - String freemarkerRedirect = freemarkerRedirect(req, resp); + String freemarkerRedirect = freemarkerRedirect(req, resp, actionDone); resp.getWriter().println(freemarkerRedirect); resp.getWriter().flush(); } + @Override + protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { + if (req.getRequestURI().endsWith(AdapterConstants.K_LOGOUT)) { + // System.out.println("Logout callback triggered"); + resp.setStatus(204); + } + } + protected void keycloakLoginRedirect(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { String loginUrl = oauthClient.getLoginFormUrl(); resp.sendRedirect(loginUrl); @@ -77,32 +91,60 @@ public class PerfAppServlet extends HttpServlet { String code = (String)req.getSession().getAttribute("code"); OAuthClient.AccessTokenResponse atResponse = oauthClient.doAccessTokenRequest(code, "password"); - String accessToken = atResponse.getAccessToken(); - String refreshToken = atResponse.getRefreshToken(); - req.getSession().setAttribute("accessToken", accessToken); - req.getSession().setAttribute("refreshToken", refreshToken); + updateTokensInSession(req, atResponse); } protected void refreshToken(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { String refreshToken = (String)req.getSession().getAttribute("refreshToken"); OAuthClient.AccessTokenResponse atResponse = oauthClient.doRefreshTokenRequest(refreshToken, "password"); + updateTokensInSession(req, atResponse); + } + + private void updateTokensInSession(HttpServletRequest req, OAuthClient.AccessTokenResponse atResponse) { String accessToken = atResponse.getAccessToken(); - refreshToken = atResponse.getRefreshToken(); + String refreshToken = atResponse.getRefreshToken(); + AccessToken accessTokenParsed = oauthClient.verifyToken(accessToken); + RefreshToken refreshTokenParsed = oauthClient.verifyRefreshToken(refreshToken); req.getSession().setAttribute("accessToken", accessToken); req.getSession().setAttribute("refreshToken", refreshToken); + req.getSession().setAttribute("accessTokenParsed", accessTokenParsed); + req.getSession().setAttribute("refreshTokenParsed", refreshTokenParsed); } protected void logoutRedirect(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { + String sessionState = null; + AccessToken accessTokenParsed = (AccessToken)req.getSession().getAttribute("accessTokenParsed"); + if (accessTokenParsed != null) { + sessionState = accessTokenParsed.getSessionState(); + } + // Invalidate http session + req.getSession(false).invalidate(); + + String logoutURL = oauthClient.getLogoutUrl(oauthClient.getRedirectUri(), sessionState); + resp.sendRedirect(logoutURL); } - private String freemarkerRedirect(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { + private String freemarkerRedirect(HttpServletRequest req, HttpServletResponse resp, String actionDone) throws ServletException, IOException { + AccessToken accessTokenParsed = (AccessToken)req.getSession().getAttribute("accessTokenParsed"); + RefreshToken refreshTokenParsed = (RefreshToken)req.getSession().getAttribute("refreshTokenParsed"); + Map attributes = new HashMap(); attributes.put("requestURI", req.getRequestURI()); attributes.put("code", req.getSession().getAttribute("code")); attributes.put("accessToken", req.getSession().getAttribute("accessToken")); attributes.put("refreshToken", req.getSession().getAttribute("refreshToken")); + attributes.put("accessTokenParsed", accessTokenParsed); + attributes.put("refreshTokenParsed", refreshTokenParsed); + attributes.put("actionDone", actionDone); + + if (accessTokenParsed != null) { + attributes.put("accessTokenExpiration", Time.toDate(accessTokenParsed.getExpiration()).toString()); + } + if (refreshTokenParsed != null) { + attributes.put("refreshTokenExpiration", Time.toDate(refreshTokenParsed.getExpiration()).toString()); + } try { Writer out = new StringWriter(); diff --git a/testsuite/performance-web/src/main/resources/perf-app-resources/index.ftl b/testsuite/performance-web/src/main/resources/perf-app-resources/index.ftl index b8e92eb2f9..74af2bece7 100644 --- a/testsuite/performance-web/src/main/resources/perf-app-resources/index.ftl +++ b/testsuite/performance-web/src/main/resources/perf-app-resources/index.ftl @@ -19,30 +19,30 @@

<#if code??> - Code: ${code} + Code Available
+ Code=${code}


<#if accessToken??> - accessToken: ${accessToken} -
-

+        Access Token Available
+ AccessToken=${accessToken}
+ Username=${accessTokenParsed.preferredUsername}
+ SessionState=${accessTokenParsed.sessionState}
+ Expiration=${accessTokenExpiration}

- - <#if refreshToken??> - refreshToken: ${refreshToken} -
-

+        Refresh token available
+ RefreshToken=${refreshToken}
+ Expiration=${refreshTokenExpiration}

+ - + <#if actionDone??> + RequestAction=${actionDone} +

diff --git a/testsuite/performance-web/src/main/resources/perf-app-resources/js-console.html b/testsuite/performance-web/src/main/resources/perf-app-resources/js-console.html deleted file mode 100644 index c5b7c532c1..0000000000 --- a/testsuite/performance-web/src/main/resources/perf-app-resources/js-console.html +++ /dev/null @@ -1,110 +0,0 @@ - - - - - - -
- - - - - - - - - - - - -
- -

Result

-

-
-

Events

-

-
-
-
-
-
\ No newline at end of file
diff --git a/testsuite/performance-web/src/main/resources/perfrealm.json b/testsuite/performance-web/src/main/resources/perfrealm.json
index aaa7fc46c6..2dadcc2be6 100644
--- a/testsuite/performance-web/src/main/resources/perfrealm.json
+++ b/testsuite/performance-web/src/main/resources/perfrealm.json
@@ -59,7 +59,7 @@
             "redirectUris": [
                 "http://localhost:8081/perf-app/*"
             ],
-            "adminUrl": "http://localhost:8081/perf-app/logout",
+            "adminUrl": "http://localhost:8081/perf-app/perf-servlet",
             "secret": "password"
          }
     ],