Added SocialLoader to social core
This commit is contained in:
parent
00a7d88dee
commit
fbe8c5cdec
3 changed files with 41 additions and 30 deletions
|
@ -25,15 +25,14 @@ 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.SocialLoader;
|
||||||
import org.keycloak.social.SocialProvider;
|
import org.keycloak.social.SocialProvider;
|
||||||
|
|
||||||
import javax.ws.rs.core.MultivaluedMap;
|
import javax.ws.rs.core.MultivaluedMap;
|
||||||
import java.net.URI;
|
import java.net.URI;
|
||||||
import java.util.Iterator;
|
|
||||||
import java.util.LinkedList;
|
import java.util.LinkedList;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
import java.util.Map;
|
import java.util.Map;
|
||||||
import java.util.ServiceLoader;
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @author <a href="mailto:vrockai@redhat.com">Viliam Rockai</a>
|
* @author <a href="mailto:vrockai@redhat.com">Viliam Rockai</a>
|
||||||
|
@ -98,8 +97,7 @@ public interface FormService {
|
||||||
socialProviders = new LinkedList<SocialProvider>();
|
socialProviders = new LinkedList<SocialProvider>();
|
||||||
Map<String, String> socialConfig = realm.getSocialConfig();
|
Map<String, String> socialConfig = realm.getSocialConfig();
|
||||||
if (socialConfig != null) {
|
if (socialConfig != null) {
|
||||||
for (Iterator<SocialProvider> itr = ServiceLoader.load(org.keycloak.social.SocialProvider.class).iterator(); itr.hasNext(); ) {
|
for (SocialProvider p : SocialLoader.load()) {
|
||||||
SocialProvider p = itr.next();
|
|
||||||
if (socialConfig.containsKey(p.getId() + ".key") && socialConfig.containsKey(p.getId() + ".secret")) {
|
if (socialConfig.containsKey(p.getId() + ".key") && socialConfig.containsKey(p.getId() + ".secret")) {
|
||||||
socialProviders.add(p);
|
socialProviders.add(p);
|
||||||
}
|
}
|
||||||
|
|
|
@ -39,6 +39,7 @@ import org.keycloak.social.AuthCallback;
|
||||||
import org.keycloak.social.AuthRequest;
|
import org.keycloak.social.AuthRequest;
|
||||||
import org.keycloak.social.RequestDetails;
|
import org.keycloak.social.RequestDetails;
|
||||||
import org.keycloak.social.SocialConstants;
|
import org.keycloak.social.SocialConstants;
|
||||||
|
import org.keycloak.social.SocialLoader;
|
||||||
import org.keycloak.social.SocialProvider;
|
import org.keycloak.social.SocialProvider;
|
||||||
import org.keycloak.social.SocialProviderConfig;
|
import org.keycloak.social.SocialProviderConfig;
|
||||||
import org.keycloak.social.SocialProviderException;
|
import org.keycloak.social.SocialProviderException;
|
||||||
|
@ -63,11 +64,9 @@ import javax.ws.rs.core.Response.Status;
|
||||||
import javax.ws.rs.core.UriInfo;
|
import javax.ws.rs.core.UriInfo;
|
||||||
import java.net.URISyntaxException;
|
import java.net.URISyntaxException;
|
||||||
import java.util.HashMap;
|
import java.util.HashMap;
|
||||||
import java.util.Iterator;
|
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
import java.util.Map;
|
import java.util.Map;
|
||||||
import java.util.Map.Entry;
|
import java.util.Map.Entry;
|
||||||
import java.util.ServiceLoader;
|
|
||||||
import java.util.UUID;
|
import java.util.UUID;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -114,7 +113,7 @@ public class SocialResource {
|
||||||
Map<String, String[]> queryParams = getQueryParams();
|
Map<String, String[]> queryParams = getQueryParams();
|
||||||
|
|
||||||
RequestDetails requestData = getRequestDetails(queryParams);
|
RequestDetails requestData = getRequestDetails(queryParams);
|
||||||
SocialProvider provider = getProvider(requestData.getProviderId());
|
SocialProvider provider = SocialLoader.load(requestData.getProviderId());
|
||||||
|
|
||||||
String realmId = requestData.getClientAttribute("realmId");
|
String realmId = requestData.getClientAttribute("realmId");
|
||||||
|
|
||||||
|
@ -179,7 +178,7 @@ public class SocialResource {
|
||||||
}
|
}
|
||||||
|
|
||||||
realm.addSocialLink(user, socialLink);
|
realm.addSocialLink(user, socialLink);
|
||||||
} else {
|
} else {
|
||||||
// Redirect user to registration screen with prefilled data from social provider
|
// Redirect user to registration screen with prefilled data from social provider
|
||||||
MultivaluedMap<String, String> formData = fillRegistrationFormWithSocialData(socialUser);
|
MultivaluedMap<String, String> formData = fillRegistrationFormWithSocialData(socialUser);
|
||||||
|
|
||||||
|
@ -212,13 +211,13 @@ public class SocialResource {
|
||||||
@GET
|
@GET
|
||||||
@Path("{realm}/login")
|
@Path("{realm}/login")
|
||||||
public Response redirectToProviderAuth(@PathParam("realm") final String realmId,
|
public Response redirectToProviderAuth(@PathParam("realm") final String realmId,
|
||||||
@QueryParam("provider_id") final String providerId, @QueryParam("client_id") final String clientId,
|
@QueryParam("provider_id") final String providerId, @QueryParam("client_id") final String clientId,
|
||||||
@QueryParam("scope") final String scope, @QueryParam("state") final String state,
|
@QueryParam("scope") final String scope, @QueryParam("state") final String state,
|
||||||
@QueryParam("redirect_uri") final String redirectUri) {
|
@QueryParam("redirect_uri") final String redirectUri) {
|
||||||
RealmManager realmManager = new RealmManager(session);
|
RealmManager realmManager = new RealmManager(session);
|
||||||
RealmModel realm = realmManager.getRealm(realmId);
|
RealmModel realm = realmManager.getRealm(realmId);
|
||||||
|
|
||||||
SocialProvider provider = getProvider(providerId);
|
SocialProvider provider = SocialLoader.load(providerId);
|
||||||
if (provider == null) {
|
if (provider == null) {
|
||||||
return Flows.forms(realm, request, uriInfo).setError("Social provider not found").forwardToErrorPage();
|
return Flows.forms(realm, request, uriInfo).setError("Social provider not found").forwardToErrorPage();
|
||||||
}
|
}
|
||||||
|
@ -305,11 +304,7 @@ public class SocialResource {
|
||||||
}
|
}
|
||||||
|
|
||||||
private RequestDetails getRequestDetails(Map<String, String[]> queryParams) {
|
private RequestDetails getRequestDetails(Map<String, String[]> queryParams) {
|
||||||
Iterator<SocialProvider> itr = ServiceLoader.load(SocialProvider.class).iterator();
|
for (SocialProvider provider : SocialLoader.load()) {
|
||||||
|
|
||||||
while (itr.hasNext()) {
|
|
||||||
SocialProvider provider = itr.next();
|
|
||||||
|
|
||||||
if (queryParams.containsKey(provider.getRequestIdParamName())) {
|
if (queryParams.containsKey(provider.getRequestIdParamName())) {
|
||||||
String requestId = queryParams.get(provider.getRequestIdParamName())[0];
|
String requestId = queryParams.get(provider.getRequestIdParamName())[0];
|
||||||
if (socialRequestManager.isRequestId(requestId)) {
|
if (socialRequestManager.isRequestId(requestId)) {
|
||||||
|
@ -321,19 +316,6 @@ public class SocialResource {
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
|
|
||||||
private SocialProvider getProvider(String providerId) {
|
|
||||||
Iterator<SocialProvider> itr = ServiceLoader.load(SocialProvider.class).iterator();
|
|
||||||
|
|
||||||
while (itr.hasNext()) {
|
|
||||||
SocialProvider provider = itr.next();
|
|
||||||
if (provider.getId().equals(providerId)) {
|
|
||||||
return provider;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
return null;
|
|
||||||
}
|
|
||||||
|
|
||||||
private Map<String, String[]> getQueryParams() {
|
private Map<String, String[]> getQueryParams() {
|
||||||
Map<String, String[]> queryParams = new HashMap<String, String[]>();
|
Map<String, String[]> queryParams = new HashMap<String, String[]>();
|
||||||
for (Entry<String, List<String>> e : uriInfo.getQueryParameters().entrySet()) {
|
for (Entry<String, List<String>> e : uriInfo.getQueryParameters().entrySet()) {
|
||||||
|
|
|
@ -0,0 +1,31 @@
|
||||||
|
package org.keycloak.social;
|
||||||
|
|
||||||
|
import java.util.ServiceLoader;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @author <a href="mailto:sthorger@redhat.com">Stian Thorgersen</a>
|
||||||
|
*/
|
||||||
|
public class SocialLoader {
|
||||||
|
|
||||||
|
private SocialLoader() {
|
||||||
|
}
|
||||||
|
|
||||||
|
public static SocialProvider load(String id) {
|
||||||
|
if (id == null) {
|
||||||
|
throw new NullPointerException();
|
||||||
|
}
|
||||||
|
|
||||||
|
for (SocialProvider s : load()) {
|
||||||
|
if (id.equals(s.getId())) {
|
||||||
|
return s;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
|
public static Iterable<SocialProvider> load() {
|
||||||
|
return ServiceLoader.load(SocialProvider.class);
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
Loading…
Reference in a new issue