diff --git a/forms/src/main/java/org/keycloak/forms/SocialBean.java b/forms/src/main/java/org/keycloak/forms/SocialBean.java index 2492ca96bb..19baaa78bc 100644 --- a/forms/src/main/java/org/keycloak/forms/SocialBean.java +++ b/forms/src/main/java/org/keycloak/forms/SocialBean.java @@ -22,9 +22,7 @@ package org.keycloak.forms; import java.net.URI; -import java.util.Iterator; -import java.util.LinkedList; -import java.util.List; +import java.util.*; import javax.imageio.spi.ServiceRegistry; import javax.ws.rs.core.UriBuilder; @@ -45,7 +43,7 @@ public class SocialBean { private List providers; - public SocialBean(RealmBean realm, RegisterBean registerBean, UrlBean url) { + public SocialBean(RealmBean realm, List providers, RegisterBean registerBean, UrlBean url) { this.realm = realm; this.registerBean = registerBean; this.url = url; @@ -54,13 +52,10 @@ public class SocialBean { UriBuilder socialLoginUrlBuilder = UriBuilder.fromUri(Urls.socialRedirectToProviderAuth(baseURI, realm.getId())); - providers = new LinkedList(); - for (Iterator itr = ServiceRegistry - .lookupProviders(org.keycloak.social.SocialProvider.class); itr.hasNext();) { - org.keycloak.social.SocialProvider p = itr.next(); - + this.providers = new LinkedList(); + for (org.keycloak.social.SocialProvider p : providers) { String loginUrl = socialLoginUrlBuilder.replaceQueryParam("provider_id", p.getId()).build().toString(); - providers.add(new SocialProvider(p.getId(), p.getName(), loginUrl)); + this.providers.add(new SocialProvider(p.getId(), p.getName(), loginUrl)); } } @@ -70,7 +65,7 @@ public class SocialBean { // Display panel with social providers just in case that social is enabled for realm, but we are not in the middle of registration with social public boolean isDisplaySocialProviders() { - return realm.isSocial() && !registerBean.isSocialRegistration(); + return realm.isSocial() && !providers.isEmpty() && !registerBean.isSocialRegistration(); } public RealmBean getRealm() { diff --git a/forms/src/main/java/org/keycloak/service/FormServiceImpl.java b/forms/src/main/java/org/keycloak/service/FormServiceImpl.java index 64624e471e..6ec433ef5b 100644 --- a/forms/src/main/java/org/keycloak/service/FormServiceImpl.java +++ b/forms/src/main/java/org/keycloak/service/FormServiceImpl.java @@ -174,7 +174,7 @@ public class FormServiceImpl implements FormService { RegisterBean register = new RegisterBean(dataBean.getFormData(), dataBean.getSocialRegistration()); - SocialBean social = new SocialBean(realm, register, url); + SocialBean social = new SocialBean(realm, dataBean.getSocialProviders(), register, url); attributes.put("social", social); } } @@ -215,7 +215,7 @@ public class FormServiceImpl implements FormService { RegisterBean register = new RegisterBean(dataBean.getFormData(), dataBean.getSocialRegistration()); - SocialBean social = new SocialBean(realm, register, url); + SocialBean social = new SocialBean(realm, dataBean.getSocialProviders(), register, url); attributes.put("social", social); } } @@ -236,7 +236,7 @@ public class FormServiceImpl implements FormService { RegisterBean register = new RegisterBean(dataBean.getFormData(), dataBean.getSocialRegistration()); attributes.put("register", register); - SocialBean social = new SocialBean(realm, register, url); + SocialBean social = new SocialBean(realm, dataBean.getSocialProviders(), register, url); attributes.put("social", social); } } diff --git a/services/src/main/java/org/keycloak/services/FormService.java b/services/src/main/java/org/keycloak/services/FormService.java index 70e4c0fc03..02e4af9e0d 100755 --- a/services/src/main/java/org/keycloak/services/FormService.java +++ b/services/src/main/java/org/keycloak/services/FormService.java @@ -22,14 +22,19 @@ package org.keycloak.services; import java.net.URI; +import java.util.HashMap; +import java.util.Iterator; +import java.util.LinkedList; import java.util.List; +import javax.imageio.spi.ServiceRegistry; import javax.ws.rs.core.MultivaluedMap; import org.keycloak.models.RealmModel; import org.keycloak.models.RoleModel; import org.keycloak.models.UserModel; import org.keycloak.services.resources.flows.FormFlows; +import org.keycloak.social.SocialProvider; /** * @author Viliam Rockai @@ -51,6 +56,8 @@ public interface FormService { private MultivaluedMap formData; private URI baseURI; + private List socialProviders; + public Boolean getSocialRegistration() { return socialRegistration; } @@ -86,6 +93,15 @@ public interface FormService { this.userModel = userModel; this.formData = formData; this.message = message; + + socialProviders = new LinkedList(); + HashMap socialConfig = realm.getSocialConfig(); + for (Iterator itr = ServiceRegistry.lookupProviders(org.keycloak.social.SocialProvider.class); itr.hasNext();) { + SocialProvider p = itr.next(); + if (socialConfig.containsKey(p.getId() + ".key") && socialConfig.containsKey(p.getId() + ".secret")) { + socialProviders.add(p); + } + } } public URI getBaseURI() { @@ -128,6 +144,10 @@ public interface FormService { this.userModel = userModel; } + public List getSocialProviders() { + return socialProviders; + } + public FormFlows.MessageType getMessageType() { return messageType; } 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 57a5aafc8b..b25285b155 100755 --- a/services/src/main/java/org/keycloak/services/resources/SocialResource.java +++ b/services/src/main/java/org/keycloak/services/resources/SocialResource.java @@ -136,8 +136,8 @@ public class SocialResource { return oauth.forwardToSecurityFailure("Login requester not enabled."); } - String key = System.getProperty("keycloak.social." + requestData.getProviderId() + ".key"); - String secret = System.getProperty("keycloak.social." + requestData.getProviderId() + ".secret"); + String key = realm.getSocialConfig().get(requestData.getProviderId() + ".key"); + String secret = realm.getSocialConfig().get(requestData.getProviderId() + ".secret"); String callbackUri = Urls.socialCallback(uriInfo.getBaseUri()).toString(); SocialProviderConfig config = new SocialProviderConfig(key, secret, callbackUri); @@ -228,8 +228,8 @@ public class SocialResource { return Flows.forms(realm, request, uriInfo).setError("Social provider not found").forwardToErrorPage(); } - String key = System.getProperty("keycloak.social." + providerId + ".key"); - String secret = System.getProperty("keycloak.social." + providerId + ".secret"); + String key = realm.getSocialConfig().get(providerId + ".key"); + String secret = realm.getSocialConfig().get(providerId + ".secret"); String callbackUri = Urls.socialCallback(uriInfo.getBaseUri()).toString(); SocialProviderConfig config = new SocialProviderConfig(key, secret, callbackUri); diff --git a/testsuite/integration/src/test/java/org/keycloak/testsuite/social/SocialLoginTest.java b/testsuite/integration/src/test/java/org/keycloak/testsuite/social/SocialLoginTest.java index 056af5c30f..4f87a1058d 100755 --- a/testsuite/integration/src/test/java/org/keycloak/testsuite/social/SocialLoginTest.java +++ b/testsuite/integration/src/test/java/org/keycloak/testsuite/social/SocialLoginTest.java @@ -43,6 +43,9 @@ import org.keycloak.testsuite.rule.WebRule; import org.openqa.selenium.By; import org.openqa.selenium.WebDriver; +import java.util.HashMap; +import java.util.Map; + /** * @author Stian Thorgersen */ @@ -54,6 +57,11 @@ public class SocialLoginTest { public void config(RealmManager manager, RealmModel defaultRealm, RealmModel appRealm) { appRealm.setSocial(true); appRealm.setAutomaticRegistrationAfterSocialLogin(true); + + HashMap socialConfig = new HashMap(); + socialConfig.put("dummy.key", "1234"); + socialConfig.put("dummy.secret", "1234"); + appRealm.setSocialConfig(socialConfig); } });