Impossible to update a federated user credential label

Closes #16613
This commit is contained in:
Arnaud Martin 2023-01-25 13:59:58 +01:00 committed by Marek Posolda
parent 35736b86ae
commit ae5a47d548
2 changed files with 64 additions and 1 deletions

View file

@ -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

View file

@ -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<CredentialRepresentation> 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");