Make 3 more tests pass.

This commit is contained in:
Stan Silvert 2015-03-03 15:14:57 -05:00 committed by Stian Thorgersen
parent 72b3db2322
commit e0f670768f
6 changed files with 318 additions and 21 deletions

View file

@ -18,6 +18,7 @@ package org.keycloak.models.file;
import org.keycloak.Config;
import org.keycloak.models.KeycloakSession;
import org.keycloak.models.KeycloakSessionFactory;
import org.keycloak.models.RealmProvider;
import org.keycloak.models.RealmProviderFactory;
@ -58,4 +59,8 @@ public class FileRealmProviderFactory implements RealmProviderFactory {
public void close() {
}
@Override
public void postInit(KeycloakSessionFactory factory) {
}
}

View file

@ -35,6 +35,7 @@ import java.util.Map;
import java.util.Set;
import java.util.regex.Pattern;
import org.keycloak.models.ApplicationModel;
import org.keycloak.models.CredentialValidationOutput;
import org.keycloak.models.ModelDuplicateException;
import org.keycloak.models.entities.FederatedIdentityEntity;
import org.keycloak.models.entities.UserEntity;
@ -68,7 +69,7 @@ public class FileUserProvider implements UserProvider {
public UserModel getUserByUsername(String username, RealmModel realm) {
for (UserModel user : inMemoryModel.getUsers(realm.getId())) {
if (user.getUsername() == null) continue;
if (user.getUsername().equals(username)) return user;
if (user.getUsername().equals(username.toLowerCase())) return user;
}
return null;
@ -78,7 +79,7 @@ public class FileUserProvider implements UserProvider {
public UserModel getUserByEmail(String email, RealmModel realm) {
for (UserModel user : inMemoryModel.getUsers(realm.getId())) {
if (user.getEmail() == null) continue;
if (user.getEmail().equals(email)) return user;
if (user.getEmail().equals(email.toLowerCase())) return user;
}
return null;
@ -220,7 +221,7 @@ public class FileUserProvider implements UserProvider {
public Set<FederatedIdentityModel> getFederatedIdentities(UserModel userModel, RealmModel realm) {
UserModel user = getUserById(userModel.getId(), realm);
UserEntity userEntity = ((UserAdapter) user).getUserEntity();
List<FederatedIdentityEntity> linkEntities = userEntity.getSocialLinks();
List<FederatedIdentityEntity> linkEntities = userEntity.getFederatedIdentities();
if (linkEntities == null) {
return Collections.EMPTY_SET;
@ -238,7 +239,7 @@ public class FileUserProvider implements UserProvider {
private FederatedIdentityEntity findSocialLink(UserModel userModel, String socialProvider, RealmModel realm) {
UserModel user = getUserById(userModel.getId(), realm);
UserEntity userEntity = ((UserAdapter) user).getUserEntity();
List<FederatedIdentityEntity> linkEntities = userEntity.getSocialLinks();
List<FederatedIdentityEntity> linkEntities = userEntity.getFederatedIdentities();
if (linkEntities == null) {
return null;
}
@ -260,10 +261,10 @@ public class FileUserProvider implements UserProvider {
@Override
public UserAdapter addUser(RealmModel realm, String id, String username, boolean addDefaultRoles) {
if (inMemoryModel.hasUserWithUsername(realm.getId(), username))
if (inMemoryModel.hasUserWithUsername(realm.getId(), username.toLowerCase()))
throw new ModelDuplicateException("User with username " + username + " already exists in realm.");
UserAdapter userModel = addUserEntity(realm, id, username);
UserAdapter userModel = addUserEntity(realm, id, username.toLowerCase());
if (addDefaultRoles) {
for (String r : realm.getDefaultRoles()) {
@ -312,14 +313,14 @@ public class FileUserProvider implements UserProvider {
FederatedIdentityEntity federatedIdentityEntity = new FederatedIdentityEntity();
federatedIdentityEntity.setIdentityProvider(socialLink.getIdentityProvider());
federatedIdentityEntity.setUserId(socialLink.getUserId());
federatedIdentityEntity.setUserName(socialLink.getUserName());
federatedIdentityEntity.setUserName(socialLink.getUserName().toLowerCase());
//check if it already exitsts - do I need to do this?
for (FederatedIdentityEntity fedIdent : userEntity.getSocialLinks()) {
for (FederatedIdentityEntity fedIdent : userEntity.getFederatedIdentities()) {
if (fedIdent.equals(federatedIdentityEntity)) return;
}
userEntity.getSocialLinks().add(federatedIdentityEntity);
userEntity.getFederatedIdentities().add(federatedIdentityEntity);
}
@Override
@ -331,12 +332,12 @@ public class FileUserProvider implements UserProvider {
return false;
}
userEntity.getSocialLinks().remove(federatedIdentityEntity);
userEntity.getFederatedIdentities().remove(federatedIdentityEntity);
return true;
}
private FederatedIdentityEntity findSocialLink(UserEntity userEntity, String socialProvider) {
List<FederatedIdentityEntity> linkEntities = userEntity.getSocialLinks();
List<FederatedIdentityEntity> linkEntities = userEntity.getFederatedIdentities();
if (linkEntities == null) {
return null;
}
@ -351,7 +352,7 @@ public class FileUserProvider implements UserProvider {
@Override
public UserModel addUser(RealmModel realm, String username) {
return this.addUser(realm, KeycloakModelUtils.generateId(), username, true);
return this.addUser(realm, KeycloakModelUtils.generateId(), username.toLowerCase(), true);
}
@Override
@ -387,4 +388,15 @@ public class FileUserProvider implements UserProvider {
public boolean validCredentials(RealmModel realm, UserModel user, UserCredentialModel... input) {
return CredentialValidation.validCredentials(realm, user, input);
}
@Override
public void updateFederatedIdentity(RealmModel realm, UserModel federatedUser, FederatedIdentityModel federatedIdentityModel) {
throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
}
@Override
public CredentialValidationOutput validCredentials(RealmModel realm, UserCredentialModel... input) {
throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
}
}

View file

@ -18,12 +18,13 @@ package org.keycloak.models.file;
import org.keycloak.Config;
import org.keycloak.models.KeycloakSession;
import org.keycloak.models.KeycloakSessionFactory;
import org.keycloak.models.UserProvider;
import org.keycloak.models.UserProviderFactory;
/**
* UserProviderFactory for JSON persistence.
*
*
* @author Stan Silvert ssilvert@redhat.com (C) 2015 Red Hat Inc.
*/
public class FileUserProviderFactory implements UserProviderFactory {
@ -46,4 +47,8 @@ public class FileUserProviderFactory implements UserProviderFactory {
public void close() {
}
@Override
public void postInit(KeycloakSessionFactory factory) {
}
}

View file

@ -24,15 +24,22 @@ import org.keycloak.models.RoleModel;
import org.keycloak.models.entities.ClientEntity;
import java.util.ArrayList;
import java.util.Collection;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Iterator;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
import java.util.Set;
import org.keycloak.models.ClientIdentityProviderMappingModel;
import org.keycloak.models.ProtocolMapperModel;
import org.keycloak.models.entities.ClientIdentityProviderMappingEntity;
import org.keycloak.models.entities.ProtocolMapperEntity;
/**
* ClientModel for JSON persistence.
*
*
* @author Stan Silvert ssilvert@redhat.com (C) 2015 Red Hat Inc.
*/
public abstract class ClientAdapter implements ClientModel {
@ -275,4 +282,86 @@ public abstract class ClientAdapter implements ClientModel {
copy.putAll(clientEntity.getAttributes());
return copy;
}
@Override
public Set<ProtocolMapperModel> getProtocolMappers() {
Set<ProtocolMapperModel> result = new HashSet<ProtocolMapperModel>();
for (String id : clientEntity.getProtocolMappers()) {
ProtocolMapperModel model = getRealm().getProtocolMapperById(id);
if (model != null) result.add(model);
}
return result;
}
@Override
public void addProtocolMappers(Set<String> mapperIds) {
clientEntity.getProtocolMappers().addAll(mapperIds);
}
@Override
public void removeProtocolMappers(Set<String> mapperIds) {
clientEntity.getProtocolMappers().removeAll(mapperIds);
}
@Override
public void setProtocolMappers(Set<String> mapperIds) {
clientEntity.getProtocolMappers().clear();
clientEntity.getProtocolMappers().addAll(mapperIds);
}
@Override
public void updateAllowedIdentityProviders(List<ClientIdentityProviderMappingModel> identityProviders) {
List<ClientIdentityProviderMappingEntity> stored = new ArrayList<ClientIdentityProviderMappingEntity>();
for (ClientIdentityProviderMappingModel model : identityProviders) {
ClientIdentityProviderMappingEntity entity = new ClientIdentityProviderMappingEntity();
entity.setId(model.getIdentityProvider());
entity.setRetrieveToken(model.isRetrieveToken());
stored.add(entity);
}
clientEntity.setIdentityProviders(stored);
}
@Override
public List<ClientIdentityProviderMappingModel> getIdentityProviders() {
List<ClientIdentityProviderMappingModel> models = new ArrayList<ClientIdentityProviderMappingModel>();
for (ClientIdentityProviderMappingEntity entity : clientEntity.getIdentityProviders()) {
ClientIdentityProviderMappingModel model = new ClientIdentityProviderMappingModel();
model.setIdentityProvider(entity.getId());
model.setRetrieveToken(entity.isRetrieveToken());
models.add(model);
}
return models;
}
@Override
public boolean hasIdentityProvider(String providerId) {
for (ClientIdentityProviderMappingEntity identityProviderMappingModel : clientEntity.getIdentityProviders()) {
String identityProvider = identityProviderMappingModel.getId();
if (identityProvider.equals(providerId)) {
return true;
}
}
return false;
}
@Override
public boolean isAllowedRetrieveTokenFromIdentityProvider(String providerId) {
for (ClientIdentityProviderMappingEntity identityProviderMappingModel : clientEntity.getIdentityProviders()) {
if (identityProviderMappingModel.getId().equals(providerId)) {
return identityProviderMappingModel.isRetrieveToken();
}
}
return false;
}
}

View file

@ -46,11 +46,16 @@ import java.util.LinkedList;
import java.util.List;
import java.util.Map;
import java.util.Set;
import org.keycloak.models.ClaimTypeModel;
import org.keycloak.models.ModelDuplicateException;
import org.keycloak.models.ProtocolMapperModel;
import org.keycloak.models.UserModel;
import org.keycloak.models.entities.ApplicationEntity;
import org.keycloak.models.entities.ClaimTypeEntity;
import org.keycloak.models.entities.ClientEntity;
import org.keycloak.models.entities.IdentityProviderEntity;
import org.keycloak.models.entities.OAuthClientEntity;
import org.keycloak.models.entities.ProtocolMapperEntity;
import org.keycloak.models.entities.RealmEntity;
import org.keycloak.models.entities.RoleEntity;
import org.keycloak.models.file.InMemoryModel;
@ -840,16 +845,32 @@ public class RealmAdapter implements RealmModel {
return new ArrayList(allIdProviders.values());
}
@Override
public IdentityProviderModel getIdentityProviderById(String identityProviderId) {
for (IdentityProviderModel identityProviderModel : getIdentityProviders()) {
if (identityProviderModel.getId().equals(identityProviderId)) {
return identityProviderModel;
}
}
return null;
}
@Override
public void addIdentityProvider(IdentityProviderModel identityProvider) {
if (identityProvider.getId() == null) throw new NullPointerException("identityProvider.getId() == null");
allIdProviders.put(identityProvider.getId(), identityProvider);
if (identityProvider.getInternalId() == null) identityProvider.setInternalId(KeycloakModelUtils.generateId());
allIdProviders.put(identityProvider.getInternalId(), identityProvider);
}
@Override
public void removeIdentityProviderById(String providerId) {
allIdProviders.remove(providerId);
for (IdentityProviderModel provider : getIdentityProviders()) {
if (provider.getId().equals(providerId)) {
allIdProviders.remove(provider.getInternalId());
break;
}
}
}
@Override
@ -857,7 +878,7 @@ public class RealmAdapter implements RealmModel {
removeIdentityProviderById(identityProvider.getId());
addIdentityProvider(identityProvider);
}
//------------------------------------------------------------------------------
@Override
public UserFederationProviderModel addUserFederationProvider(String providerName, Map<String, String> config, int priority, String displayName, int fullSyncPeriod, int changedSyncPeriod, int lastSync) {
UserFederationProviderEntity entity = new UserFederationProviderEntity();
@ -1012,7 +1033,172 @@ public class RealmAdapter implements RealmModel {
@Override
public boolean isIdentityFederationEnabled() {
//TODO: not sure if we will support identity federation storage for file
return false;
return true;
}
@Override
public Set<ProtocolMapperModel> getProtocolMappers() {
Set<ProtocolMapperModel> result = new HashSet<ProtocolMapperModel>();
for (ProtocolMapperEntity entity : realm.getProtocolMappers()) {
ProtocolMapperModel mapping = new ProtocolMapperModel();
mapping.setId(entity.getId());
mapping.setName(entity.getName());
mapping.setProtocol(entity.getProtocol());
mapping.setAppliedByDefault(entity.isAppliedByDefault());
mapping.setConsentRequired(entity.isConsentRequired());
mapping.setConsentText(entity.getConsentText());
Map<String, String> config = new HashMap<String, String>();
if (entity.getConfig() != null) {
config.putAll(entity.getConfig());
}
mapping.setConfig(config);
}
return result;
}
@Override
public ProtocolMapperModel addProtocolMapper(ProtocolMapperModel model) {
if (getProtocolMapperByName(model.getProtocol(), model.getName()) != null) {
throw new RuntimeException("protocol mapper name must be unique per protocol");
}
ProtocolMapperEntity entity = new ProtocolMapperEntity();
entity.setId(KeycloakModelUtils.generateId());
entity.setProtocol(model.getProtocol());
entity.setName(model.getName());
entity.setAppliedByDefault(model.isAppliedByDefault());
entity.setProtocolMapper(model.getProtocolMapper());
entity.setConfig(model.getConfig());
entity.setConsentRequired(model.isConsentRequired());
entity.setConsentText(model.getConsentText());
realm.getProtocolMappers().add(entity);
return entityToModel(entity);
}
@Override
public void removeProtocolMapper(ProtocolMapperModel mapping) {
for (ProtocolMapperEntity entity : realm.getProtocolMappers()) {
if (entity.getId().equals(mapping.getId())) {
realm.getProtocolMappers().remove(entity);
break;
}
}
}
protected ProtocolMapperEntity getProtocolMapperyEntityById(String id) {
for (ProtocolMapperEntity entity : realm.getProtocolMappers()) {
if (entity.getId().equals(id)) {
return entity;
}
}
return null;
}
protected ProtocolMapperEntity getProtocolMapperEntityByName(String protocol, String name) {
for (ProtocolMapperEntity entity : realm.getProtocolMappers()) {
if (entity.getProtocol().equals(protocol) && entity.getName().equals(name)) {
return entity;
}
}
return null;
}
@Override
public void updateProtocolMapper(ProtocolMapperModel mapping) {
ProtocolMapperEntity entity = getProtocolMapperyEntityById(mapping.getId());
entity.setAppliedByDefault(mapping.isAppliedByDefault());
entity.setProtocolMapper(mapping.getProtocolMapper());
entity.setConsentRequired(mapping.isConsentRequired());
entity.setConsentText(mapping.getConsentText());
if (entity.getConfig() != null) {
entity.getConfig().clear();
entity.getConfig().putAll(mapping.getConfig());
} else {
entity.setConfig(mapping.getConfig());
}
}
@Override
public ProtocolMapperModel getProtocolMapperById(String id) {
ProtocolMapperEntity entity = getProtocolMapperyEntityById(id);
if (entity == null) return null;
return entityToModel(entity);
}
@Override
public ProtocolMapperModel getProtocolMapperByName(String protocol, String name) {
ProtocolMapperEntity entity = getProtocolMapperEntityByName(protocol, name);
if (entity == null) return null;
return entityToModel(entity);
}
protected ProtocolMapperModel entityToModel(ProtocolMapperEntity entity) {
ProtocolMapperModel mapping = new ProtocolMapperModel();
mapping.setId(entity.getId());
mapping.setName(entity.getName());
mapping.setProtocol(entity.getProtocol());
mapping.setAppliedByDefault(entity.isAppliedByDefault());
mapping.setProtocolMapper(entity.getProtocolMapper());
mapping.setConsentRequired(entity.isConsentRequired());
mapping.setConsentText(entity.getConsentText());
Map<String, String> config = new HashMap<String, String>();
if (entity.getConfig() != null) config.putAll(entity.getConfig());
mapping.setConfig(config);
return mapping;
}
@Override
public Set<ClaimTypeModel> getClaimTypes() {
Set<ClaimTypeModel> result = new HashSet<ClaimTypeModel>();
for (ClaimTypeEntity entity : realm.getClaimTypes()) {
result.add(new ClaimTypeModel(entity.getId(), entity.getName(), entity.isBuiltIn(), entity.getType()));
}
return result;
}
@Override
public ClaimTypeModel addClaimType(ClaimTypeModel model) {
String id = model.getId() == null ? KeycloakModelUtils.generateId() : model.getId();
ClaimTypeModel claim = new ClaimTypeModel(id, model.getName(), model.isBuiltIn(), model.getType());
ClaimTypeEntity entity = new ClaimTypeEntity();
entity.setId(claim.getId());
entity.setType(model.getType());
entity.setBuiltIn(model.isBuiltIn());
entity.setName(model.getName());
realm.getClaimTypes().add(entity);
return claim;
}
@Override
public void removeClaimType(ClaimTypeModel claimType) {
for (ClaimTypeEntity entity : realm.getClaimTypes()) {
if (entity.getId().equals(claimType.getId())) {
realm.getClaimTypes().remove(entity);
break;
}
}
}
@Override
public ClaimTypeModel getClaimType(String name) {
for (ClaimTypeModel claimType : getClaimTypes()) {
if (claimType.getName().equals(name)) return claimType;
}
return null;
}
@Override
public void updateClaimType(ClaimTypeModel claimType) {
for (ClaimTypeEntity entity : realm.getClaimTypes()) {
if (entity.getId().equals(claimType.getId())) {
entity.setName(claimType.getName());
entity.setBuiltIn(claimType.isBuiltIn());
entity.setType(claimType.getType());
break;
}
}
}
@Override

View file

@ -55,8 +55,8 @@ public class UserAdapter implements UserModel, Comparable {
public UserAdapter(RealmModel realm, UserEntity userEntity, InMemoryModel inMemoryModel) {
this.user = userEntity;
this.realm = realm;
if (userEntity.getSocialLinks() == null) {
userEntity.setSocialLinks(new ArrayList<FederatedIdentityEntity>());
if (userEntity.getFederatedIdentities() == null) {
userEntity.setFederatedIdentities(new ArrayList<FederatedIdentityEntity>());
}
this.inMemoryModel = inMemoryModel;
}