diff --git a/model/jpa/src/main/java/org/keycloak/storage/jpa/JpaUserFederatedStorageProvider.java b/model/jpa/src/main/java/org/keycloak/storage/jpa/JpaUserFederatedStorageProvider.java index 72d5033604..0d244a26d4 100644 --- a/model/jpa/src/main/java/org/keycloak/storage/jpa/JpaUserFederatedStorageProvider.java +++ b/model/jpa/src/main/java/org/keycloak/storage/jpa/JpaUserFederatedStorageProvider.java @@ -551,7 +551,7 @@ public class JpaUserFederatedStorageProvider implements entity.setType(cred.getType()); entity.setCredentialData(cred.getCredentialData()); entity.setSecretData(cred.getSecretData()); - cred.setUserLabel(entity.getUserLabel()); + entity.setUserLabel(cred.getUserLabel()); } @Override diff --git a/testsuite/integration-arquillian/tests/base/src/test/java/org/keycloak/testsuite/admin/UserTest.java b/testsuite/integration-arquillian/tests/base/src/test/java/org/keycloak/testsuite/admin/UserTest.java index f896ca0daa..87e9112153 100755 --- a/testsuite/integration-arquillian/tests/base/src/test/java/org/keycloak/testsuite/admin/UserTest.java +++ b/testsuite/integration-arquillian/tests/base/src/test/java/org/keycloak/testsuite/admin/UserTest.java @@ -33,8 +33,11 @@ import org.keycloak.admin.client.resource.RealmResource; import org.keycloak.admin.client.resource.RoleMappingResource; import org.keycloak.admin.client.resource.UserResource; import org.keycloak.admin.client.resource.UsersResource; +import org.keycloak.common.Profile; +import org.keycloak.common.Profile.Feature; import org.keycloak.common.VerificationException; import org.keycloak.common.util.Base64; +import org.keycloak.common.util.MultivaluedHashMap; import org.keycloak.common.util.ObjectUtil; import org.keycloak.credential.CredentialModel; import org.keycloak.events.admin.OperationType; @@ -61,8 +64,13 @@ import org.keycloak.representations.idm.RealmRepresentation; import org.keycloak.representations.idm.RequiredActionProviderRepresentation; import org.keycloak.representations.idm.RoleRepresentation; import org.keycloak.representations.idm.UserRepresentation; +import org.keycloak.services.resources.RealmsResource; +import org.keycloak.storage.StorageId; import org.keycloak.storage.UserStorageProvider; +import org.keycloak.testsuite.ProfileAssume; +import org.keycloak.testsuite.arquillian.annotation.DisableFeature; import org.keycloak.testsuite.federation.DummyUserFederationProviderFactory; +import org.keycloak.testsuite.federation.UserMapStorageFactory; import org.keycloak.testsuite.page.LoginPasswordUpdatePage; import org.keycloak.testsuite.pages.ErrorPage; import org.keycloak.testsuite.pages.InfoPage; @@ -118,6 +126,7 @@ import static org.junit.Assert.assertNotNull; import static org.junit.Assert.assertNull; import static org.junit.Assert.assertTrue; import static org.junit.Assert.fail; +import static org.keycloak.storage.UserStorageProviderModel.IMPORT_ENABLED; import static org.keycloak.testsuite.Assert.assertNames; import static org.keycloak.testsuite.auth.page.AuthRealm.TEST; @@ -2830,6 +2839,60 @@ public class UserTest extends AbstractAdminTest { Assert.assertEquals(newLabel, user.credentials().get(0).getUserLabel()); } + @Test + public void testUpdateCredentialLabelForFederatedUser() { + ProfileAssume.assumeFeatureDisabled(Feature.MAP_STORAGE); + + // Create user federation + ComponentRepresentation memProvider = new ComponentRepresentation(); + memProvider.setName("memory"); + memProvider.setProviderId(UserMapStorageFactory.PROVIDER_ID); + memProvider.setProviderType(UserStorageProvider.class.getName()); + memProvider.setConfig(new MultivaluedHashMap<>()); + memProvider.getConfig().putSingle("priority", Integer.toString(0)); + memProvider.getConfig().putSingle(IMPORT_ENABLED, Boolean.toString(false)); + + RealmResource realm = adminClient.realms().realm(REALM_NAME); + Response resp = realm.components().add(memProvider); + resp.close(); + String memProviderId = ApiUtil.getCreatedId(resp); + getCleanup().addComponentId(memProviderId); + + assertAdminEvents.assertEvent(realmId, OperationType.CREATE, AdminEventPaths.componentPath(memProviderId), memProvider, ResourceType.COMPONENT); + + // Create federated user + String username = "fed-user1"; + UserRepresentation userRepresentation = new UserRepresentation(); + userRepresentation.setUsername(username); + userRepresentation.setEmail("feduser1@mail.com"); + userRepresentation.setRequiredActions(Collections.emptyList()); + userRepresentation.setEnabled(true); + userRepresentation.setFederationLink(memProviderId); + + PasswordCredentialModel pcm = PasswordCredentialModel.createFromValues("my-algorithm", "theSalt".getBytes(), 22, "ABC"); + CredentialRepresentation hashedPassword = ModelToRepresentation.toRepresentation(pcm); + hashedPassword.setCreatedDate(1001L); + hashedPassword.setUserLabel("label"); + hashedPassword.setType(CredentialRepresentation.PASSWORD); + + userRepresentation.setCredentials(Arrays.asList(hashedPassword)); + String userId = createUser(userRepresentation); + Assert.assertFalse(StorageId.isLocalStorage(userId)); + + UserResource user = ApiUtil.findUserByUsernameId(realm, username); + List credentials = user.credentials(); + Assert.assertNotNull(credentials); + Assert.assertEquals(1, credentials.size()); + Assert.assertEquals("label", credentials.get(0).getUserLabel()); + + // Update federated credential user label + user.setCredentialUserLabel(credentials.get(0).getId(), "updatedLabel"); + credentials = user.credentials(); + Assert.assertNotNull(credentials); + Assert.assertEquals(1, credentials.size()); + Assert.assertEquals("updatedLabel", credentials.get(0).getUserLabel()); + } + @Test public void testDeleteCredentials() { UserResource user = ApiUtil.findUserByUsernameId(testRealm(), "john-doh@localhost");