diff --git a/core/src/main/java/org/keycloak/representations/idm/RealmRepresentation.java b/core/src/main/java/org/keycloak/representations/idm/RealmRepresentation.java index 532807e53d..8a4727b441 100755 --- a/core/src/main/java/org/keycloak/representations/idm/RealmRepresentation.java +++ b/core/src/main/java/org/keycloak/representations/idm/RealmRepresentation.java @@ -25,6 +25,13 @@ public class RealmRepresentation { protected Boolean verifyEmail; protected Boolean resetPasswordAllowed; + @Deprecated + protected Boolean social; + @Deprecated + protected Boolean updateProfileOnInitialSocialLogin; + @Deprecated + protected Map socialProviders; + protected Boolean userCacheEnabled; protected Boolean realmCacheEnabled; @@ -313,6 +320,22 @@ public class RealmRepresentation { this.resetPasswordAllowed = resetPassword; } + public Boolean isSocial() { + return social; + } + + public void setSocial(Boolean social) { + this.social = social; + } + + public Boolean isUpdateProfileOnInitialSocialLogin() { + return updateProfileOnInitialSocialLogin; + } + + public void setUpdateProfileOnInitialSocialLogin(Boolean updateProfileOnInitialSocialLogin) { + this.updateProfileOnInitialSocialLogin = updateProfileOnInitialSocialLogin; + } + public Map getBrowserSecurityHeaders() { return browserSecurityHeaders; } @@ -321,6 +344,14 @@ public class RealmRepresentation { this.browserSecurityHeaders = browserSecurityHeaders; } + public Map getSocialProviders() { + return socialProviders; + } + + public void setSocialProviders(Map socialProviders) { + this.socialProviders = socialProviders; + } + public Map getSmtpServer() { return smtpServer; } @@ -482,10 +513,6 @@ public class RealmRepresentation { } public List getIdentityProviders() { - if (this.identityProviders == null) { - this.identityProviders = new ArrayList(); - } - return identityProviders; } @@ -494,11 +521,12 @@ public class RealmRepresentation { } public void addIdentityProvider(IdentityProviderRepresentation identityProviderRepresentation) { - getIdentityProviders().add(identityProviderRepresentation); + if (identityProviders == null) identityProviders = new LinkedList<>(); + identityProviders.add(identityProviderRepresentation); } public boolean isIdentityFederationEnabled() { - return !getIdentityProviders().isEmpty(); + return identityProviders != null && !identityProviders.isEmpty(); } public List getProtocolMappers() { diff --git a/core/src/main/java/org/keycloak/representations/idm/SocialLinkRepresentation.java b/core/src/main/java/org/keycloak/representations/idm/SocialLinkRepresentation.java new file mode 100644 index 0000000000..8203261fed --- /dev/null +++ b/core/src/main/java/org/keycloak/representations/idm/SocialLinkRepresentation.java @@ -0,0 +1,35 @@ +package org.keycloak.representations.idm; + +/** + * @author Marek Posolda + */ +public class SocialLinkRepresentation { + + protected String socialProvider; + protected String socialUserId; + protected String socialUsername; + + public String getSocialProvider() { + return socialProvider; + } + + public void setSocialProvider(String socialProvider) { + this.socialProvider = socialProvider; + } + + public String getSocialUserId() { + return socialUserId; + } + + public void setSocialUserId(String socialUserId) { + this.socialUserId = socialUserId; + } + + public String getSocialUsername() { + return socialUsername; + } + + public void setSocialUsername(String socialUsername) { + this.socialUsername = socialUsername; + } +} diff --git a/core/src/main/java/org/keycloak/representations/idm/UserRepresentation.java b/core/src/main/java/org/keycloak/representations/idm/UserRepresentation.java index 8fa1d6aadb..d2907d54a6 100755 --- a/core/src/main/java/org/keycloak/representations/idm/UserRepresentation.java +++ b/core/src/main/java/org/keycloak/representations/idm/UserRepresentation.java @@ -25,6 +25,8 @@ public class UserRepresentation { protected List credentials; protected List requiredActions; protected List federatedIdentities; + @Deprecated + protected List socialLinks; protected List realmRoles; protected Map> applicationRoles; @@ -147,6 +149,14 @@ public class UserRepresentation { this.federatedIdentities = federatedIdentities; } + public List getSocialLinks() { + return socialLinks; + } + + public void setSocialLinks(List socialLinks) { + this.socialLinks = socialLinks; + } + public List getRealmRoles() { return realmRoles; } diff --git a/model/api/src/main/java/org/keycloak/models/utils/RepresentationToModel.java b/model/api/src/main/java/org/keycloak/models/utils/RepresentationToModel.java index afdd6aa051..41f2ad42fe 100755 --- a/model/api/src/main/java/org/keycloak/models/utils/RepresentationToModel.java +++ b/model/api/src/main/java/org/keycloak/models/utils/RepresentationToModel.java @@ -31,6 +31,7 @@ import org.keycloak.representations.idm.ProtocolMapperRepresentation; import org.keycloak.representations.idm.RealmRepresentation; import org.keycloak.representations.idm.RoleRepresentation; import org.keycloak.representations.idm.ScopeMappingRepresentation; +import org.keycloak.representations.idm.SocialLinkRepresentation; import org.keycloak.representations.idm.UserFederationProviderRepresentation; import org.keycloak.representations.idm.UserRepresentation; @@ -39,6 +40,7 @@ import java.net.URI; import java.util.ArrayList; import java.util.HashMap; import java.util.HashSet; +import java.util.LinkedList; import java.util.List; import java.util.Map; import java.util.Set; @@ -48,6 +50,8 @@ public class RepresentationToModel { private static Logger logger = Logger.getLogger(RepresentationToModel.class); public static void importRealm(KeycloakSession session, RealmRepresentation rep, RealmModel newRealm) { + convertDeprecatedSocialProviders(rep); + newRealm.setName(rep.getRealm()); if (rep.isEnabled() != null) newRealm.setEnabled(rep.isEnabled()); if (rep.isBruteForceProtected() != null) newRealm.setBruteForceProtected(rep.isBruteForceProtected()); @@ -255,6 +259,57 @@ public class RepresentationToModel { } } + private static void convertDeprecatedSocialProviders(RealmRepresentation rep) { + if (rep.isSocial() != null && rep.isSocial() && rep.getSocialProviders() != null && !rep.getSocialProviders().isEmpty() && rep.getIdentityProviders() == null) { + Boolean updateProfileFirstLogin = rep.isUpdateProfileOnInitialSocialLogin() != null && rep.isUpdateProfileOnInitialSocialLogin(); + if (rep.getSocialProviders() != null) { + + List identityProviders = new LinkedList<>(); + for (String k : rep.getSocialProviders().keySet()) { + if (k.endsWith(".key")) { + String providerId = k.split("\\.")[0]; + String key = rep.getSocialProviders().get(k); + String secret = rep.getSocialProviders().get(k.replace(".key", ".secret")); + + IdentityProviderRepresentation identityProvider = new IdentityProviderRepresentation(); + identityProvider.setId(providerId); + identityProvider.setProviderId(providerId); + identityProvider.setName(providerId); + identityProvider.setEnabled(true); + identityProvider.setUpdateProfileFirstLogin(updateProfileFirstLogin); + + Map config = new HashMap<>(); + config.put("clientId", key); + config.put("clientSecret", secret); + identityProvider.setConfig(config); + + identityProviders.add(identityProvider); + } + } + rep.setIdentityProviders(identityProviders); + } + } + + rep.setSocial(null); + rep.setSocialProviders(null); + rep.setUpdateProfileOnInitialSocialLogin(false); + } + + private static void convertDeprecatedSocialProviders(UserRepresentation user) { + if (user.getSocialLinks() != null && !user.getSocialLinks().isEmpty() && user.getFederatedIdentities() == null) { + List federatedIdentities = new LinkedList<>(); + for (SocialLinkRepresentation social : user.getSocialLinks()) { + FederatedIdentityRepresentation federatedIdentity = new FederatedIdentityRepresentation(); + federatedIdentity.setIdentityProvider(social.getSocialProvider()); + federatedIdentity.setUserId(social.getSocialUserId()); + federatedIdentity.setUserName(social.getSocialUsername()); + } + user.setFederatedIdentities(federatedIdentities); + } + + user.setSocialLinks(null); + } + public static void updateRealm(RealmRepresentation rep, RealmModel realm) { if (rep.getRealm() != null) { realm.setName(rep.getRealm()); @@ -688,6 +743,8 @@ public class RepresentationToModel { // Users public static UserModel createUser(KeycloakSession session, RealmModel newRealm, UserRepresentation userRep, Map appMap) { + convertDeprecatedSocialProviders(userRep); + // Import users just to user storage. Don't federate UserModel user = session.userStorage().addUser(newRealm, userRep.getId(), userRep.getUsername(), false); user.setEnabled(userRep.isEnabled());