Repository navigation
Enable MapUserProvider storing username with the letter case signific… #10332
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -78,4 +78,8 @@ public void init(Scope config) { | |
| public boolean isSupported() { | ||
| return Profile.isFeatureEnabled(Profile.Feature.MAP_STORAGE); | ||
| } | ||
|
|
||
| public Scope getStorageConfigScope() { | ||
| return storageConfigScope; | ||
| } | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I'm not sure this is the correct So wouldn't we want to use the actual scope that was used to init the storage factory instead?
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 @vramik Factory should translate the configuration 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 |
||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,75 @@ | ||
| /* | ||
| * Copyright 2020 Red Hat, Inc. and/or its affiliates | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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) { | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 |
|---|---|---|
|
|
@@ -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); | ||
|
|
@@ -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() | ||
|
|
@@ -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()); | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. In |
||
|
|
||
| List<MapUserEntity> usersWithEmail = tx.read(withCriteria(mcb)) | ||
| .filter(userEntity -> Objects.equals(userEntity.getEmail(), email)) | ||
|
|
@@ -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 | ||
|
|
||
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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.