KEYCLOAK-116

Use social provider settings from realm
This commit is contained in:
Stian Thorgersen 2013-10-22 14:05:56 +01:00
parent 6e3dd959eb
commit bb3a82f932
5 changed files with 41 additions and 18 deletions

View file

@ -22,9 +22,7 @@
package org.keycloak.forms; package org.keycloak.forms;
import java.net.URI; import java.net.URI;
import java.util.Iterator; import java.util.*;
import java.util.LinkedList;
import java.util.List;
import javax.imageio.spi.ServiceRegistry; import javax.imageio.spi.ServiceRegistry;
import javax.ws.rs.core.UriBuilder; import javax.ws.rs.core.UriBuilder;
@ -45,7 +43,7 @@ public class SocialBean {
private List<SocialProvider> providers; private List<SocialProvider> providers;
public SocialBean(RealmBean realm, RegisterBean registerBean, UrlBean url) { public SocialBean(RealmBean realm, List<org.keycloak.social.SocialProvider> providers, RegisterBean registerBean, UrlBean url) {
this.realm = realm; this.realm = realm;
this.registerBean = registerBean; this.registerBean = registerBean;
this.url = url; this.url = url;
@ -54,13 +52,10 @@ public class SocialBean {
UriBuilder socialLoginUrlBuilder = UriBuilder.fromUri(Urls.socialRedirectToProviderAuth(baseURI, realm.getId())); UriBuilder socialLoginUrlBuilder = UriBuilder.fromUri(Urls.socialRedirectToProviderAuth(baseURI, realm.getId()));
providers = new LinkedList<SocialProvider>(); this.providers = new LinkedList<SocialProvider>();
for (Iterator<org.keycloak.social.SocialProvider> itr = ServiceRegistry for (org.keycloak.social.SocialProvider p : providers) {
.lookupProviders(org.keycloak.social.SocialProvider.class); itr.hasNext();) {
org.keycloak.social.SocialProvider p = itr.next();
String loginUrl = socialLoginUrlBuilder.replaceQueryParam("provider_id", p.getId()).build().toString(); 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 // 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() { public boolean isDisplaySocialProviders() {
return realm.isSocial() && !registerBean.isSocialRegistration(); return realm.isSocial() && !providers.isEmpty() && !registerBean.isSocialRegistration();
} }
public RealmBean getRealm() { public RealmBean getRealm() {

View file

@ -174,7 +174,7 @@ public class FormServiceImpl implements FormService {
RegisterBean register = new RegisterBean(dataBean.getFormData(), dataBean.getSocialRegistration()); 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); attributes.put("social", social);
} }
} }
@ -215,7 +215,7 @@ public class FormServiceImpl implements FormService {
RegisterBean register = new RegisterBean(dataBean.getFormData(), dataBean.getSocialRegistration()); 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); attributes.put("social", social);
} }
} }
@ -236,7 +236,7 @@ public class FormServiceImpl implements FormService {
RegisterBean register = new RegisterBean(dataBean.getFormData(), dataBean.getSocialRegistration()); RegisterBean register = new RegisterBean(dataBean.getFormData(), dataBean.getSocialRegistration());
attributes.put("register", register); attributes.put("register", register);
SocialBean social = new SocialBean(realm, register, url); SocialBean social = new SocialBean(realm, dataBean.getSocialProviders(), register, url);
attributes.put("social", social); attributes.put("social", social);
} }
} }

View file

@ -22,14 +22,19 @@
package org.keycloak.services; package org.keycloak.services;
import java.net.URI; import java.net.URI;
import java.util.HashMap;
import java.util.Iterator;
import java.util.LinkedList;
import java.util.List; import java.util.List;
import javax.imageio.spi.ServiceRegistry;
import javax.ws.rs.core.MultivaluedMap; import javax.ws.rs.core.MultivaluedMap;
import org.keycloak.models.RealmModel; import org.keycloak.models.RealmModel;
import org.keycloak.models.RoleModel; import org.keycloak.models.RoleModel;
import org.keycloak.models.UserModel; import org.keycloak.models.UserModel;
import org.keycloak.services.resources.flows.FormFlows; import org.keycloak.services.resources.flows.FormFlows;
import org.keycloak.social.SocialProvider;
/** /**
* @author <a href="mailto:vrockai@redhat.com">Viliam Rockai</a> * @author <a href="mailto:vrockai@redhat.com">Viliam Rockai</a>
@ -51,6 +56,8 @@ public interface FormService {
private MultivaluedMap<String, String> formData; private MultivaluedMap<String, String> formData;
private URI baseURI; private URI baseURI;
private List<SocialProvider> socialProviders;
public Boolean getSocialRegistration() { public Boolean getSocialRegistration() {
return socialRegistration; return socialRegistration;
} }
@ -86,6 +93,15 @@ public interface FormService {
this.userModel = userModel; this.userModel = userModel;
this.formData = formData; this.formData = formData;
this.message = message; this.message = message;
socialProviders = new LinkedList<SocialProvider>();
HashMap<String,String> socialConfig = realm.getSocialConfig();
for (Iterator<SocialProvider> 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() { public URI getBaseURI() {
@ -128,6 +144,10 @@ public interface FormService {
this.userModel = userModel; this.userModel = userModel;
} }
public List<SocialProvider> getSocialProviders() {
return socialProviders;
}
public FormFlows.MessageType getMessageType() { public FormFlows.MessageType getMessageType() {
return messageType; return messageType;
} }

View file

@ -136,8 +136,8 @@ public class SocialResource {
return oauth.forwardToSecurityFailure("Login requester not enabled."); return oauth.forwardToSecurityFailure("Login requester not enabled.");
} }
String key = System.getProperty("keycloak.social." + requestData.getProviderId() + ".key"); String key = realm.getSocialConfig().get(requestData.getProviderId() + ".key");
String secret = System.getProperty("keycloak.social." + requestData.getProviderId() + ".secret"); String secret = realm.getSocialConfig().get(requestData.getProviderId() + ".secret");
String callbackUri = Urls.socialCallback(uriInfo.getBaseUri()).toString(); String callbackUri = Urls.socialCallback(uriInfo.getBaseUri()).toString();
SocialProviderConfig config = new SocialProviderConfig(key, secret, callbackUri); 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(); return Flows.forms(realm, request, uriInfo).setError("Social provider not found").forwardToErrorPage();
} }
String key = System.getProperty("keycloak.social." + providerId + ".key"); String key = realm.getSocialConfig().get(providerId + ".key");
String secret = System.getProperty("keycloak.social." + providerId + ".secret"); String secret = realm.getSocialConfig().get(providerId + ".secret");
String callbackUri = Urls.socialCallback(uriInfo.getBaseUri()).toString(); String callbackUri = Urls.socialCallback(uriInfo.getBaseUri()).toString();
SocialProviderConfig config = new SocialProviderConfig(key, secret, callbackUri); SocialProviderConfig config = new SocialProviderConfig(key, secret, callbackUri);

View file

@ -43,6 +43,9 @@ import org.keycloak.testsuite.rule.WebRule;
import org.openqa.selenium.By; import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver; import org.openqa.selenium.WebDriver;
import java.util.HashMap;
import java.util.Map;
/** /**
* @author <a href="mailto:sthorger@redhat.com">Stian Thorgersen</a> * @author <a href="mailto:sthorger@redhat.com">Stian Thorgersen</a>
*/ */
@ -54,6 +57,11 @@ public class SocialLoginTest {
public void config(RealmManager manager, RealmModel defaultRealm, RealmModel appRealm) { public void config(RealmManager manager, RealmModel defaultRealm, RealmModel appRealm) {
appRealm.setSocial(true); appRealm.setSocial(true);
appRealm.setAutomaticRegistrationAfterSocialLogin(true); appRealm.setAutomaticRegistrationAfterSocialLogin(true);
HashMap<String, String> socialConfig = new HashMap<String, String>();
socialConfig.put("dummy.key", "1234");
socialConfig.put("dummy.secret", "1234");
appRealm.setSocialConfig(socialConfig);
} }
}); });