8000 Enable MapUserProvider storing username with the letter case signific… by vramik · Pull Request #10332 · keycloak/keycloak · GitHub
[go: up one dir, main page]

Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -240,6 +240,7 @@ static String getFederatedIdentityLinksCacheKey(String userId) {
@Override
public UserModel getUserByUsername(RealmModel realm, String username) {
logger.tracev("getUserByUsername: {0}", username);
//TODO remove following line?
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good question. I'd think we shouldn't change legacy behavior.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No, we should keep it. It is the storage responsibility to deal with letter case. In the case of legacy storage, lettercase is intentionally ignored.

username = username.toLowerCase();
if (realmInvalidations.contains(realm.getId())) {
logger.tracev("realmInvalidations");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -193,6 +193,6 @@ public IckleQueryMapModelCriteriaBuilder<E, M> createCriteriaBuilder() {
@Override
public MapKeycloakTransaction<V, M> createTransaction(KeycloakSession session) {
Map<SearchableModelField<? super M>, MapModelCriteriaBuilder.UpdatePredicatesFunc<K, V, M>> fieldPredicates = MapFieldPredicates.getPredicates((Class<M>) storedEntityDescriptor.getModelTypeClass());
return new ConcurrentHashMapKeycloakTransaction<>(this, keyConvertor, cloner, fieldPredicates);
return new ConcurrentHashMapKeycloakTransaction<>(this, keyConvertor, cloner, fieldPredicates, null);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -78,4 +78,8 @@ public void init(Scope config) {
public boolean isSupported() {
return Profile.isFeatureEnabled(Profile.Feature.MAP_STORAGE);
}

public Scope getStorageConfigScope() {
return storageConfigScope;
}
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not sure this is the correct scope we would want to use. This only contains the storage config that is found inside the particular areas (client, group, etc). The final storage factory that is instantiated actually gets a scope that is broader than this - it combines the this area-specific config with the root config found in the mapStorage configuration (i.e. it falls back to the root config if a property is not defined in the area config). Also, if we go forward with multiple profiles for a particular storage, that would have to be taken into consideration as well.

So wouldn't we want to use the actual scope that was used to init the storage factory instead?

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@sguilhen @vramik This scope is the right one for the storage config, the options inherited from the root (and perhaps profile) are available transparently from this scope. See ComponentModelScope. However, storage maintains the capability of the storage to respect letter case. Whether the lettercase should be respected if the store supports it is however setting on the realm, so it has to be a property of the map user provider.


@vramik Factory should translate the configuration Scope values into properties and the objects created by the factory should not be given access to the Scope instance unless justified.

The reason is to keep the configuration management maintained in a single place (factory), and translated once (in the factory) rather than multiple times (in each created instance).

It seems that the only reason for this method is the eventually be searched for username-lettercase option. This by itself is not sufficient justification. Is there any other?

}
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
import java.util.stream.Collectors;
import java.util.stream.Stream;
import org.jboss.logging.Logger;
import org.keycloak.Config.Scope;
import org.keycloak.models.map.storage.MapKeycloakTransaction;
import org.keycloak.models.map.storage.QueryParameters;
import org.keycloak.models.map.storage.chm.MapModelCriteriaBuilder.UpdatePredicatesFunc;
Expand All @@ -47,16 +48,18 @@ public class ConcurrentHashMapKeycloakTransaction<K, V extends AbstractEntity &
protected final StringKeyConvertor<K> keyConvertor;
protected final DeepCloner cloner;
protected final Map<SearchableModelField<? super M>, UpdatePredicatesFunc<K, V, M>> fieldPredicates;
private final Scope config;

enum MapOperation {
CREATE, UPDATE, DELETE,
}

public ConcurrentHashMapKeycloakTransaction(ConcurrentHashMapCrudOperations<V, M> map, StringKeyConvertor<K> keyConvertor, DeepCloner cloner, Map<SearchableModelField<? super M>, UpdatePredicatesFunc<K, V, M>> fieldPredicates) {
public ConcurrentHashMapKeycloakTransaction(ConcurrentHashMapCrudOperations<V, M> map, StringKeyConvertor<K> keyConvertor, DeepCloner cloner, Map<SearchableModelField<? super M>, UpdatePredicatesFunc<K, V, M>> fieldPredicates, Scope config) {
this.map = map;
this.keyConvertor = keyConvertor;
this.cloner = cloner;
this.fieldPredicates = fieldPredicates;
this.config = config;
}

@Override
Expand Down Expand Up @@ -99,7 +102,7 @@ public boolean isActive() {
}

private MapModelCriteriaBuilder<K, V, M> createCriteriaBuilder() {
return new MapModelCriteriaBuilder<K, V, M>(keyConvertor, fieldPredicates);
return new MapModelCriteriaBuilder<>(keyConvertor, fieldPredicates, config);
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -129,11 +129,11 @@ public long delete(QueryParameters<M> queryParameters) {
@SuppressWarnings("unchecked")
public MapKeycloakTransaction<V, M> createTransaction(KeycloakSession session) {
MapKeycloakTransaction<V, M> sessionTransaction = session.getAttribute("map-transaction-" + hashCode(), MapKeycloakTransaction.class);
return sessionTransaction == null ? new ConcurrentHashMapKeycloakTransaction<>(this, keyConvertor, cloner, fieldPredicates) : sessionTransaction;
return sessionTransaction == null ? new ConcurrentHashMapKeycloakTransaction<>(this, keyConvertor, cloner, fieldPredicates, null) : sessionTransaction;
}

public MapModelCriteriaBuilder<K, V, M> createCriteriaBuilder() {
return new MapModelCriteriaBuilder<>(keyConvertor, fieldPredicates);
return new MapModelCriteriaBuilder<>(keyConvertor, fieldPredicates, null);
}

public StringKeyConvertor<K> getKeyConvertor() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -65,11 +65,14 @@
import java.util.HashMap;
import java.util.LinkedList;
import java.util.Map;
import org.keycloak.Config;
import org.keycloak.models.UserModel;

import static org.keycloak.models.map.storage.ModelEntityUtil.getModelName;
import static org.keycloak.models.map.storage.ModelEntityUtil.getModelNames;
import static org.keycloak.models.map.storage.QueryParameters.withCriteria;
import static org.keycloak.models.map.storage.criteria.DefaultModelCriteria.criteria;
import org.keycloak.models.map.user.MapUserProviderFactory;

/**
*
Expand All @@ -91,6 +94,8 @@ public class ConcurrentHashMapStorageProviderFactory implements AmphibianProvide

private StringKeyConvertor defaultKeyConvertor;

private Scope userStorageConfigScope;

private final static DeepCloner CLONER = new DeepCloner.Builder()
.genericCloner(Serialization::from)
.constructor(MapClientEntityImpl.class, MapClientEntityImpl::new)
Expand Down Expand Up @@ -167,6 +172,8 @@ private StringKeyConvertor getKeyConvertor(final String keyType) throws IllegalA

@Override
public void postInit(KeycloakSessionFactory factory) {
MapUserProviderFactory providerFactory = (MapUserProviderFactory) factory.getProviderFactory(org.keycloak.models.UserProvider.class, "map");
userStorageConfigScope = providerFactory.getStorageConfigScope();
}

@Override
Expand Down Expand Up @@ -208,6 +215,13 @@ public String toString() {
return "ConcurrentHashMapStorage(" + mapName + suffix + ")";
}
};
} else if (modelType == UserModel.class) {
store = new UserConcurrentHashMapStorage(modelType, kc, CLONER, userStorageConfigScope) {
@Override
public String toString() {
return "ConcurrentHashMapStorage(" + mapName + suffix + ")";
}
};
} else {
store = new ConcurrentHashMapStorage(modelType, kc, CLONER) {
@Override
Expand Down
Original file line number Diff line number Diff line change
< 10BC8 tool-tip id="tooltip-8873ded2-f08b-4fd1-9dba-00078fc19971" for="expand-up-link-0-diff-5123292ffc11219524fe4e9f81dabd91308bb35bd8a391e2094a9c2a54421570" popover="manual" data-direction="ne" data-type="label" data-view-component="true" class="sr-only position-absolute">Expand Up @@ -27,6 +27,9 @@
import java.util.Collection;
import java.util.List;
import java.util.stream.Collectors;
import static org.jboss.resteasy.mock.MockHttpRequest.options;
import org.keycloak.Config.Scope;
import org.keycloak.models.UserModel;

/**
*
Expand All @@ -45,16 +48,18 @@ public static interface UpdatePredicatesFunc<K, V extends AbstractEntity, M> {
private final Predicate<? super V> entityFilter;
private final Map<SearchableModelField<? super M>, UpdatePredicatesFunc<K, V, M>> fieldPredicates;
private final StringKeyConvertor<K> keyConvertor;
private final Scope config;

public MapModelCriteriaBuilder(StringKeyConvertor<K> keyConvertor, Map<SearchableModelField<? super M>, UpdatePredicatesFunc<K, V, M>> fieldPredicates) {
this(keyConvertor, fieldPredicates, ALWAYS_TRUE, ALWAYS_TRUE);
public MapModelCriteriaBuilder(StringKeyConvertor<K> keyConvertor, Map<SearchableModelField<? super M>, UpdatePredicatesFunc<K, V, M>> fieldPredicates, Scope config) {
this(keyConvertor, fieldPredicates, ALWAYS_TRUE, ALWAYS_TRUE, config);
}

private MapModelCriteriaBuilder(StringKeyConvertor<K> keyConvertor, Map<SearchableModelField<? super M>, UpdatePredicatesFunc<K, V, M>> fieldPredicates, Predicate<? super K> indexReadFilter, Predicate<? super V> sequentialReadFilter) {
private MapModelCriteriaBuilder(StringKeyConvertor<K> keyConvertor, Map<SearchableModelField<? super M>, UpdatePredicatesFunc<K, V, M>> fieldPredicates, Predicate<? super K> indexReadFilter, Predicate<? super V> sequentialReadFilter, Scope config) {
this.keyConvertor = keyConvertor;
this.fieldPredicates = fieldPredicates;
this.keyFilter = indexReadFilter;
this.entityFilter = sequentialReadFilter;
this.config = config;
}

@Override
Expand All @@ -64,6 +69,10 @@ public MapModelCriteriaBuilder<K, V, M> compare(SearchableModelField<? super M>
throw new IllegalArgumentException("Filter not implemented for field " + modelField);
}

if (modelField.equals(UserModel.SearchableFields.USERNAME)) {
updateUsernameLettercase(values);
}

return method.apply(this, op, values);
}

Expand All @@ -73,7 +82,7 @@ public MapModelCriteriaBuilder<K, V, M> compare(SearchableModelField<? super M>
public final MapModelCriteriaBuilder<K, V, M> and(MapModelCriteriaBuilder<K, V, M>... builders) {
Predicate<? super K> resIndexFilter = Stream.of(builders).map(MapModelCriteriaBuilder.class::cast).map(MapModelCriteriaBuilder::getKeyFilter).reduce(keyFilter, Predicate::and);
Predicate<V> resEntityFilter = Stream.of(builders).map(MapModelCriteriaBuilder.class::cast).map(MapModelCriteriaBuilder::getEntityFilter).reduce(entityFilter, Predicate::and);
return new MapModelCriteriaBuilder<>(keyConvertor, fieldPredicates, resIndexFilter, resEntityFilter);
return new MapModelCriteriaBuilder<>(keyConvertor, fieldPredicates, resIndexFilter, resEntityFilter, config);
}

@SafeVarargs
Expand All @@ -86,7 +95,8 @@ public final MapModelCriteriaBuilder<K, V, M> or(MapModelCriteriaBuilder<K, V, M
keyConvertor,
fieldPredicates,
v -> keyFilter.test(v) && resIndexFilter.test(v),
v -> entityFilter.test(v) && resEntityFilter.test(v)
v -> entityFilter.test(v) && resEntityFilter.test(v),
config
);
}

Expand All @@ -101,7 +111,8 @@ public MapModelCriteriaBuilder<K, V, M> not(MapModelCriteriaBuilder<K, V, M> bui
keyConvertor,
fieldPredicates,
v -> keyFilter.test(v) && resIndexFilter.test(v),
v -> entityFilter.test(v) && resEntityFilter.test(v)
v -> entityFilter.test(v) && resEntityFilter.test(v),
config
);
}

Expand All @@ -125,7 +136,7 @@ protected MapModelCriteriaBuilder<K, V, M> idCompare(Operator op, Object[] value
case EXISTS:
case NOT_EXISTS:
case IN:
return new MapModelCriteriaBuilder<>(keyConvertor, fieldPredicates, this.keyFilter.and(CriteriaOperator.predicateFor(op, convertedValues)), this.entityFilter);
return new MapModelCriteriaBuilder<>(keyConvertor, fieldPredicates, this.keyFilter.and(CriteriaOperator.predicateFor(op, convertedValues)), this.entityFilter, config);
default:
throw new AssertionError("Invalid operator: " + op);
}
Expand Down Expand Up @@ -167,6 +178,26 @@ protected MapModelCriteriaBuilder<K, V, M> fieldCompare(Predicate<Object> valueC
final Predicate<V> p = v -> valueComparator.test(getter.apply(v));
resEntityFilter = p.and(entityFilter);
}
return new MapModelCriteriaBuilder<>(keyConvertor, fieldPredicates, this.keyFilter, resEntityFilter);
return new MapModelCriteriaBuilder<>(keyConvertor, fieldPredicates, this.keyFilter, resEntityFilter, config);
}

private void updateUsernameLettercase(Object[] values) throws IllegalStateException {
if (config == null || values.length != 1 || ! (values[0] instanceof String)) {
throw new IllegalStateException();
}
String lettercase = config.get("username-lettercase", "lowercase");
switch (lettercase) {
case "lowercase":
values[0] = ((String) values[0]).toLowerCase();
break;
case "uppercase":
values[0] = ((String) values[0]).toUpperCase();
break;
case "as_is":
// do nothing
break;
default:
throw new IllegalStateException("Unknown username lettercase: " + config.get("username-lettercase"));
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
/*
* Copyright 2020 Red Hat, Inc. and/or its affiliates
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nitpick: year

* and other contributors as indicated by the @author tags.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.keycloak.models.map.storage.chm;

import org.keycloak.Config.Scope;
import org.keycloak.models.KeycloakSession;
import org.keycloak.models.map.common.StringKeyConvertor;
import org.keycloak.models.map.common.DeepCloner;
import org.keycloak.models.UserModel;
import org.keycloak.models.map.storage.MapKeycloakTransaction;
import org.keycloak.models.map.user.MapUserEntity;


public class UserConcurrentHashMapStorage<K> extends ConcurrentHashMapStorage<K, MapUserEntity, UserModel> {

private final Scope config;

public UserConcurrentHashMapStorage(Class<UserModel> modelClass, StringKeyConvertor<K> keyConvertor, DeepCloner cloner, Scope config) {
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Perhaps we should only propagate the one property that this storage needs access to instead of the whole scope object.

super(modelClass, keyConvertor, cloner);
this.config = config;
}

@Override
public MapUserEntity create(MapUserEntity user) {

updateUsernameLettercase(user);

return super.create(user);
}

@Override
@SuppressWarnings("unchecked")
public MapKeycloakTransaction<MapUserEntity, UserModel> createTransaction(KeycloakSession session) {
MapKeycloakTransaction<MapUserEntity, UserModel> sessionTransaction = session.getAttribute("map-transaction-" + hashCode(), MapKeycloakTransaction.class);
return sessionTransaction == null ? new ConcurrentHashMapKeycloakTransaction<>(this, keyConvertor, cloner, fieldPredicates, config) : sessionTransaction;
}

@Override
public MapModelCriteriaBuilder<K, MapUserEntity, UserModel> createCriteriaBuilder() {
return new MapModelCriteriaBuilder<>(keyConvertor, fieldPredicates, config);
}

private void updateUsernameLettercase(MapUserEntity user) throws IllegalStateException {
if (config == null) {
throw new IllegalStateException();
}
switch (config.get("username-lettercase", "lowercase")) {
case "lowercase":
user.setUsername(user.getUsername().toLowerCase());
break;
case "uppercase":
user.setUsername(user.getUsername().toUpperCase());
break;
case "as_is":
// do nothing
break;
default:
throw new IllegalStateException("Unknown Username lettercase: " + config.get("username-lettercase"));
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ public Transaction(MapKeycloakTransaction<MapAuthenticatedClientSessionEntity, A
UpdatePredicatesFunc<K,
MapUserSessionEntity,
UserSessionModel>> fieldPredicates) {
super(UserSessionConcurrentHashMapStorage.this, keyConvertor, cloner, fieldPredicates);
super(UserSessionConcurrentHashMapStorage.this, keyConvertor, cloner, fieldPredicates, null);
this.clientSessionTr = clientSessionTr;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,6 @@ public String getUsername() {

@Override
public void setUsername(String username) {
username = KeycloakModelUtils.toLowerCaseSafe(username);
// Do not continue if current username of entity is the requested username
if (username != null && username.equals(entity.getUsername())) return;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -334,7 +334,7 @@ public UserModel addUser(RealmModel realm, String id, String username, boolean a
entity.setId(id);
entity.setRealmId(realm.getId());
entity.setEmailConstraint(KeycloakModelUtils.generateId());
entity.setUsername(username.toLowerCase());
entity.setUsername(username);
entity.setCreatedTimestamp(Time.currentTimeMillis());

entity = tx.create(entity);
Expand Down Expand Up @@ -508,7 +508,7 @@ public UserModel getUserByUsername(RealmModel realm, String username) {
LOG.tracef("getUserByUsername(%s, %s)%s", realm, username, getShortStackTrace());
DefaultModelCriteria<UserModel> mcb = criteria();
mcb = mcb.compare(SearchableFields.REALM_ID, Operator.EQ, realm.getId())
.compare(SearchableFields.USERNAME, Operator.ILIKE, username);
.compare(SearchableFields.USERNAME, Operator.EQ, username);

try (Stream<MapUserEntity> s = tx.read(withCriteria(mcb))) {
return s.findFirst()
Expand All @@ -521,7 +521,7 @@ public UserModel getUserByEmail(RealmModel realm, String email) {
LOG.tracef("getUserByEmail(%s, %s)%s", realm, email, getShortStackTrace());
DefaultModelCriteria<UserModel> mcb = criteria();
mcb = mcb.compare(SearchableFields.REALM_ID, Operator.EQ, realm.getId())
.compare(SearchableFields.EMAIL, Operator.EQ, email);
.compare(SearchableFields.EMAIL, Operator.EQ, email.toLowerCase());
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In UserLookupProvider bellow the javadoc now says " * @PARAM email email address (case-sensitivity is controlled by storage)". This line is doing the opposite, isn't it?


List<MapUserEntity> usersWithEmail = tx.read(withCriteria(mcb))
.filter(userEntity -> Objects.equals(userEntity.getEmail(), email))
Expand Down Expand Up @@ -719,7 +719,7 @@ public Stream<UserModel> searchForUserByUserAttributeStream(RealmModel realm, St

@Override
public UserModel addUser(RealmModel realm, String username) {
return addUser(realm, null, username.toLowerCase(), true, true);
return addUser(realm, null, username, true, true);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,9 +50,10 @@ default UserModel getUserById(RealmModel realm, String id) {
UserModel getUserById(String id, RealmModel realm);

/**
* Exact search for a user by its username.
* Returns a user with the given username belonging to the realm
*
* @param username case insensitive username (case-sensitivity is controlled by storage)
* @param username username (case-sensitivity is controlled by storage)
* @param realm the realm model
* @return found user model, or {@code null} if no such user exists
*/
Expand All @@ -66,9 +67,10 @@ default UserModel getUserByUsername(RealmModel realm, String username) {
UserModel getUserByUsername(String username, RealmModel realm);

/**
* Exact search for a user by its email.
* Returns a user with the given email belonging to the realm
*
* @param email case insensitive email address (case-sensitivity is controlled by storage)
* @param email email address (case-sensitivity is controlled by storage)
* @param realm the realm model
* @return found user model, or {@code null} if no such user exists
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -244,7 +244,7 @@ public UserModel addUser(RealmModel realm, String username) {
.map(provider -> provider.addUser(realm, username))
.filter(Objects::nonNull)
.findFirst()
.orElseGet(() -> localStorage().addUser(realm, username.toLowerCase()));
.orElseGet(() -> localStorage().addUser(realm, username.toLowerCase())); //TODO remove toLowerCase?
}

@Override
Expand Down
0