keycloak-memoizeit

Details

diff --git a/forms/src/main/java/org/keycloak/service/FormServiceImpl.java b/forms/src/main/java/org/keycloak/service/FormServiceImpl.java
index 7efa87d..3dedc5e 100644
--- a/forms/src/main/java/org/keycloak/service/FormServiceImpl.java
+++ b/forms/src/main/java/org/keycloak/service/FormServiceImpl.java
@@ -65,12 +65,12 @@ public class FormServiceImpl implements FormService {
         commandMap.put(Pages.LOGIN_RESET_PASSWORD, new CommandPassword());
         commandMap.put(Pages.LOGIN_UPDATE_PASSWORD, new CommandPassword());
         commandMap.put(Pages.ACCESS, new CommandAccess());
-        commandMap.put(Pages.SECURITY_FAILURE, new CommandSecurityFailure());
         commandMap.put(Pages.SOCIAL, new CommandSocial());
         commandMap.put(Pages.TOTP, new CommandTotp());
         commandMap.put(Pages.LOGIN_CONFIG_TOTP, new CommandTotp());
         commandMap.put(Pages.LOGIN_TOTP, new CommandLoginTotp());
         commandMap.put(Pages.LOGIN_VERIFY_EMAIL, new CommandLoginTotp());
+        commandMap.put(Pages.ERROR, new CommandError());
     }
 
     public String getId(){
@@ -143,11 +143,6 @@ public class FormServiceImpl implements FormService {
         }
     }
 
-    private class CommandSecurityFailure implements Command {
-        public void exec(Map<String, Object> attributes, FormServiceDataBean dataBean) {
-        }
-    }
-
     private class CommandPassword implements Command {
         public void exec(Map<String, Object> attributes, FormServiceDataBean dataBean) {
             if (dataBean.getError() != null){
@@ -253,6 +248,14 @@ public class FormServiceImpl implements FormService {
         }
     }
 
+    private class CommandError implements Command {
+        public void exec(Map<String, Object> attributes, FormServiceDataBean dataBean) {
+            if (dataBean.getError() != null){
+                attributes.put("error", new ErrorBean(dataBean.getError()));
+            }
+        }
+    }
+
     private interface Command {
         public void exec(Map<String, Object> attributes, FormServiceDataBean dataBean);
     }
diff --git a/forms/src/main/resources/META-INF/resources/forms/theme/default/error.ftl b/forms/src/main/resources/META-INF/resources/forms/theme/default/error.ftl
index a7f1f5b..64ca460 100755
--- a/forms/src/main/resources/META-INF/resources/forms/theme/default/error.ftl
+++ b/forms/src/main/resources/META-INF/resources/forms/theme/default/error.ftl
@@ -1,6 +1,6 @@
 <#-- TODO: Only a placeholder, implementation needed -->
 <#import "template-login-action.ftl" as layout>
-<@layout.registrationLayout bodyClass="reset"; section>
+<@layout.registrationLayout bodyClass="reset" isErrorPage=true; section>
     <#if section = "title">
 
     We're sorry...
@@ -12,7 +12,7 @@
     <#elseif section = "form">
 
         <p class="instruction">Something happened and we could not process your request.</p>
-        <p class="instruction second">Please make sure the URL you entered is correct.</p>
+        <p class="instruction second">${error.summary}</p>
         <a href="saas-login.html" class="link-right">Go to the homepage »</a>
 
     <#elseif section = "info" >
diff --git a/forms/src/main/resources/META-INF/resources/forms/theme/default/template-login-action.ftl b/forms/src/main/resources/META-INF/resources/forms/theme/default/template-login-action.ftl
index 70794b1..c2920cf 100644
--- a/forms/src/main/resources/META-INF/resources/forms/theme/default/template-login-action.ftl
+++ b/forms/src/main/resources/META-INF/resources/forms/theme/default/template-login-action.ftl
@@ -1,4 +1,4 @@
-<#macro registrationLayout bodyClass>
+<#macro registrationLayout bodyClass isErrorPage=false>
 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"  "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
 <html xmlns="http://www.w3.org/1999/xhtml">
 
@@ -37,7 +37,7 @@
                     <#nested "form">
                 </div>
 
-                <#if error?has_content>
+                <#if !isErrorPage && error?has_content>
                     <div class="feedback error bottom-left show">
                         <p>
                             <strong id="loginError">${rb.getString(error.summary)}</strong>
diff --git a/services/src/main/java/org/keycloak/services/resources/flows/Flows.java b/services/src/main/java/org/keycloak/services/resources/flows/Flows.java
index ddb1afe..c8711ea 100755
--- a/services/src/main/java/org/keycloak/services/resources/flows/Flows.java
+++ b/services/src/main/java/org/keycloak/services/resources/flows/Flows.java
@@ -36,10 +36,6 @@ public class Flows {
     private Flows() {
     }
 
-    public static PageFlows pages(HttpRequest request) {
-        return new PageFlows(request);
-    }
-
     public static FormFlows forms(RealmModel realm, HttpRequest request, UriInfo uriInfo) {
         return new FormFlows(realm, request, uriInfo);
     }
diff --git a/services/src/main/java/org/keycloak/services/resources/flows/FormFlows.java b/services/src/main/java/org/keycloak/services/resources/flows/FormFlows.java
index d45588a..cdb588e 100755
--- a/services/src/main/java/org/keycloak/services/resources/flows/FormFlows.java
+++ b/services/src/main/java/org/keycloak/services/resources/flows/FormFlows.java
@@ -168,6 +168,10 @@ public class FormFlows {
         return forwardToForm(Pages.TOTP);
     }
 
+    public Response forwardToErrorPage() {
+        return forwardToForm(Pages.ERROR);
+    }
+
     public FormFlows setAccessCode(AccessCodeEntry accessCode) {
         this.accessCode = accessCode;
         return this;
diff --git a/services/src/main/java/org/keycloak/services/resources/flows/OAuthFlows.java b/services/src/main/java/org/keycloak/services/resources/flows/OAuthFlows.java
index dafc11a..5802e30 100755
--- a/services/src/main/java/org/keycloak/services/resources/flows/OAuthFlows.java
+++ b/services/src/main/java/org/keycloak/services/resources/flows/OAuthFlows.java
@@ -126,7 +126,7 @@ public class OAuthFlows {
     }
 
     public Response forwardToSecurityFailure(String message) {
-        return Flows.pages(request).forwardToSecurityFailure(message);
+        return Flows.forms(realm, request, uriInfo).setError(message).forwardToErrorPage();
     }
 
 }
diff --git a/services/src/main/java/org/keycloak/services/resources/flows/Pages.java b/services/src/main/java/org/keycloak/services/resources/flows/Pages.java
index a5f913d..110da3a 100644
--- a/services/src/main/java/org/keycloak/services/resources/flows/Pages.java
+++ b/services/src/main/java/org/keycloak/services/resources/flows/Pages.java
@@ -48,7 +48,7 @@ public class Pages {
 
     public final static String REGISTER = "/forms/register.ftl";
 
-    public final static String SECURITY_FAILURE = "/saas/securityFailure.jsp";
+    public final static String ERROR = "/forms/error.ftl";
 
     public final static String SOCIAL = "/forms/social.ftl";
 
diff --git a/services/src/main/java/org/keycloak/services/resources/SocialResource.java b/services/src/main/java/org/keycloak/services/resources/SocialResource.java
index c13eb25..57a5aaf 100755
--- a/services/src/main/java/org/keycloak/services/resources/SocialResource.java
+++ b/services/src/main/java/org/keycloak/services/resources/SocialResource.java
@@ -57,7 +57,6 @@ import org.keycloak.services.managers.RealmManager;
 import org.keycloak.services.managers.TokenManager;
 import org.keycloak.services.resources.flows.Flows;
 import org.keycloak.services.resources.flows.OAuthFlows;
-import org.keycloak.services.resources.flows.PageFlows;
 import org.keycloak.services.resources.flows.Urls;
 import org.keycloak.social.AuthCallback;
 import org.keycloak.social.AuthRequest;
@@ -221,9 +220,12 @@ public class SocialResource {
             @QueryParam("provider_id") final String providerId, @QueryParam("client_id") final String clientId,
             @QueryParam("scope") final String scope, @QueryParam("state") final String state,
             @QueryParam("redirect_uri") final String redirectUri) {
+        RealmManager realmManager = new RealmManager(session);
+        RealmModel realm = realmManager.getRealm(realmId);
+
         SocialProvider provider = getProvider(providerId);
         if (provider == null) {
-            return Flows.pages(request).forwardToSecurityFailure("Social provider not found");
+            return Flows.forms(realm, request, uriInfo).setError("Social provider not found").forwardToErrorPage();
         }
 
         String key = System.getProperty("keycloak.social." + providerId + ".key");
@@ -244,7 +246,7 @@ public class SocialResource {
 
             return Response.status(Status.FOUND).location(authRequest.getAuthUri()).build();
         } catch (Throwable t) {
-            return Flows.pages(request).forwardToSecurityFailure("Failed to redirect to social auth");
+            return Flows.forms(realm, request, uriInfo).setError("Failed to redirect to social auth").forwardToErrorPage();
         }
     }
 
@@ -253,24 +255,24 @@ public class SocialResource {
     @Consumes(MediaType.APPLICATION_FORM_URLENCODED)
     public Response socialRegistration(@PathParam("realm") final String realmId,
                                        final MultivaluedMap<String, String> formData) {
-        PageFlows pageFlows = Flows.pages(request);
+        RealmManager realmManager = new RealmManager(session);
+        RealmModel realm = realmManager.getRealm(realmId);
+
         Cookie cookie = headers.getCookies().get(SocialConstants.SOCIAL_REGISTRATION_COOKIE);
         if (cookie == null) {
-            return pageFlows.forwardToSecurityFailure("Social registration cookie not found");
+            return Flows.forms(realm, request, uriInfo).setError("Social registration cookie not found").forwardToErrorPage();
         }
 
         String requestId = cookie.getValue();
         if (!socialRequestManager.isRequestId(requestId)) {
             logger.error("Unknown requestId found in cookie. Maybe it's expired. requestId=" + requestId);
-            return pageFlows.forwardToSecurityFailure("Unknown requestId found in cookie. Maybe it's expired.");
+            return Flows.forms(realm, request, uriInfo).setError("Unknown requestId found in cookie. Maybe it's expired.").forwardToErrorPage();
         }
 
         RequestDetails requestData = socialRequestManager.getData(requestId);
 
-        RealmManager realmManager = new RealmManager(session);
-        RealmModel realm = realmManager.getRealm(realmId);
         if (realm == null || !realm.isEnabled()) {
-            return pageFlows.forwardToSecurityFailure("Realm doesn't exists or is not enabled.");
+            return Flows.forms(realm, request, uriInfo).setError("Realm doesn't exists or is not enabled.").forwardToErrorPage();
         }
         TokenService tokenService = new TokenService(realm, tokenManager);
         resourceContext.initResource(tokenService);