Search in sources :

Example 16 with FederatedIdentityRepresentation

use of org.keycloak.representations.idm.FederatedIdentityRepresentation in project keycloak by keycloak.

the class ExportUtils method exportFederatedUser.

/**
 * Full export of user data stored in federated storage (including role mappings and credentials)
 *
 * @param id
 * @return fully exported user representation
 */
public static UserRepresentation exportFederatedUser(KeycloakSession session, RealmModel realm, String id, ExportOptions options) {
    UserRepresentation userRep = new UserRepresentation();
    userRep.setId(id);
    MultivaluedHashMap<String, String> attributes = session.userFederatedStorage().getAttributes(realm, id);
    if (attributes.size() > 0) {
        Map<String, List<String>> attrs = new HashMap<>();
        attrs.putAll(attributes);
        userRep.setAttributes(attrs);
    }
    List<String> requiredActions = session.userFederatedStorage().getRequiredActionsStream(realm, id).collect(Collectors.toList());
    if (requiredActions.size() > 0) {
        userRep.setRequiredActions(requiredActions);
    }
    // Social links
    List<FederatedIdentityRepresentation> socialLinkReps = session.userFederatedStorage().getFederatedIdentitiesStream(id, realm).map(ExportUtils::exportSocialLink).collect(Collectors.toList());
    if (socialLinkReps.size() > 0) {
        userRep.setFederatedIdentities(socialLinkReps);
    }
    // Role mappings
    if (options.isGroupsAndRolesIncluded()) {
        Set<RoleModel> roles = session.userFederatedStorage().getRoleMappingsStream(realm, id).collect(Collectors.toSet());
        List<String> realmRoleNames = new ArrayList<>();
        Map<String, List<String>> clientRoleNames = new HashMap<>();
        for (RoleModel role : roles) {
            if (role.getContainer() instanceof RealmModel) {
                realmRoleNames.add(role.getName());
            } else {
                ClientModel client = (ClientModel) role.getContainer();
                String clientId = client.getClientId();
                List<String> currentClientRoles = clientRoleNames.get(clientId);
                if (currentClientRoles == null) {
                    currentClientRoles = new ArrayList<>();
                    clientRoleNames.put(clientId, currentClientRoles);
                }
                currentClientRoles.add(role.getName());
            }
        }
        if (realmRoleNames.size() > 0) {
            userRep.setRealmRoles(realmRoleNames);
        }
        if (clientRoleNames.size() > 0) {
            userRep.setClientRoles(clientRoleNames);
        }
    }
    // Credentials
    List<CredentialRepresentation> credReps = session.userFederatedStorage().getStoredCredentialsStream(realm, id).map(ExportUtils::exportCredential).collect(Collectors.toList());
    userRep.setCredentials(credReps);
    // Grants
    List<UserConsentRepresentation> consentReps = session.users().getConsentsStream(realm, id).map(ModelToRepresentation::toRepresentation).collect(Collectors.toList());
    if (consentReps.size() > 0) {
        userRep.setClientConsents(consentReps);
    }
    // Not Before
    int notBefore = session.userFederatedStorage().getNotBeforeOfUser(realm, userRep.getId());
    userRep.setNotBefore(notBefore);
    if (options.isGroupsAndRolesIncluded()) {
        List<String> groups = session.userFederatedStorage().getGroupsStream(realm, id).map(ModelToRepresentation::buildGroupPath).collect(Collectors.toList());
        userRep.setGroups(groups);
    }
    return userRep;
}
Also used : HashMap(java.util.HashMap) MultivaluedHashMap(org.keycloak.common.util.MultivaluedHashMap) ArrayList(java.util.ArrayList) RoleModel(org.keycloak.models.RoleModel) UserConsentRepresentation(org.keycloak.representations.idm.UserConsentRepresentation) RealmModel(org.keycloak.models.RealmModel) CredentialRepresentation(org.keycloak.representations.idm.CredentialRepresentation) ClientModel(org.keycloak.models.ClientModel) List(java.util.List) ArrayList(java.util.ArrayList) LinkedList(java.util.LinkedList) FederatedIdentityRepresentation(org.keycloak.representations.idm.FederatedIdentityRepresentation) UserRepresentation(org.keycloak.representations.idm.UserRepresentation)

Example 17 with FederatedIdentityRepresentation

use of org.keycloak.representations.idm.FederatedIdentityRepresentation in project keycloak by keycloak.

the class ExportUtils method exportUser.

/**
 * Full export of user (including role mappings and credentials)
 *
 * @param user
 * @return fully exported user representation
 */
public static UserRepresentation exportUser(KeycloakSession session, RealmModel realm, UserModel user, ExportOptions options, boolean internal) {
    UserRepresentation userRep = ModelToRepresentation.toRepresentation(session, realm, user);
    // Social links
    List<FederatedIdentityRepresentation> socialLinkReps = session.users().getFederatedIdentitiesStream(realm, user).map(ExportUtils::exportSocialLink).collect(Collectors.toList());
    if (socialLinkReps.size() > 0) {
        userRep.setFederatedIdentities(socialLinkReps);
    }
    // Role mappings
    if (options.isGroupsAndRolesIncluded()) {
        Set<RoleModel> roles = user.getRoleMappingsStream().collect(Collectors.toSet());
        List<String> realmRoleNames = new ArrayList<>();
        Map<String, List<String>> clientRoleNames = new HashMap<>();
        for (RoleModel role : roles) {
            if (role.getContainer() instanceof RealmModel) {
                realmRoleNames.add(role.getName());
            } else {
                ClientModel client = (ClientModel) role.getContainer();
                String clientId = client.getClientId();
                List<String> currentClientRoles = clientRoleNames.get(clientId);
                if (currentClientRoles == null) {
                    currentClientRoles = new ArrayList<>();
                    clientRoleNames.put(clientId, currentClientRoles);
                }
                currentClientRoles.add(role.getName());
            }
        }
        if (realmRoleNames.size() > 0) {
            userRep.setRealmRoles(realmRoleNames);
        }
        if (clientRoleNames.size() > 0) {
            userRep.setClientRoles(clientRoleNames);
        }
    }
    // Credentials - extra security, do not export credentials if service accounts
    if (internal) {
        List<CredentialRepresentation> credReps = session.userCredentialManager().getStoredCredentialsStream(realm, user).map(ExportUtils::exportCredential).collect(Collectors.toList());
        userRep.setCredentials(credReps);
    }
    userRep.setFederationLink(user.getFederationLink());
    // Grants
    List<UserConsentRepresentation> consentReps = session.users().getConsentsStream(realm, user.getId()).map(ModelToRepresentation::toRepresentation).collect(Collectors.toList());
    if (consentReps.size() > 0) {
        userRep.setClientConsents(consentReps);
    }
    // Not Before
    int notBefore = session.users().getNotBeforeOfUser(realm, user);
    userRep.setNotBefore(notBefore);
    // Service account
    if (user.getServiceAccountClientLink() != null) {
        String clientInternalId = user.getServiceAccountClientLink();
        ClientModel client = realm.getClientById(clientInternalId);
        if (client != null) {
            userRep.setServiceAccountClientId(client.getClientId());
        }
    }
    if (options.isGroupsAndRolesIncluded()) {
        List<String> groups = user.getGroupsStream().map(ModelToRepresentation::buildGroupPath).collect(Collectors.toList());
        userRep.setGroups(groups);
    }
    return userRep;
}
Also used : HashMap(java.util.HashMap) MultivaluedHashMap(org.keycloak.common.util.MultivaluedHashMap) ArrayList(java.util.ArrayList) RoleModel(org.keycloak.models.RoleModel) UserConsentRepresentation(org.keycloak.representations.idm.UserConsentRepresentation) RealmModel(org.keycloak.models.RealmModel) CredentialRepresentation(org.keycloak.representations.idm.CredentialRepresentation) ClientModel(org.keycloak.models.ClientModel) List(java.util.List) ArrayList(java.util.ArrayList) LinkedList(java.util.LinkedList) FederatedIdentityRepresentation(org.keycloak.representations.idm.FederatedIdentityRepresentation) UserRepresentation(org.keycloak.representations.idm.UserRepresentation)

Example 18 with FederatedIdentityRepresentation

use of org.keycloak.representations.idm.FederatedIdentityRepresentation in project keycloak by keycloak.

the class UserBuilder method federatedLink.

public UserBuilder federatedLink(String identityProvider, String federatedUserId) {
    if (rep.getFederatedIdentities() == null) {
        rep.setFederatedIdentities(new LinkedList<>());
    }
    FederatedIdentityRepresentation federatedIdentity = new FederatedIdentityRepresentation();
    federatedIdentity.setUserId(federatedUserId);
    federatedIdentity.setUserName(rep.getUsername());
    federatedIdentity.setIdentityProvider(identityProvider);
    rep.getFederatedIdentities().add(federatedIdentity);
    return this;
}
Also used : FederatedIdentityRepresentation(org.keycloak.representations.idm.FederatedIdentityRepresentation)

Example 19 with FederatedIdentityRepresentation

use of org.keycloak.representations.idm.FederatedIdentityRepresentation in project keycloak by keycloak.

the class AccountLinkTest method testDeleteIdentityOnProviderRemoval.

@Test
public void testDeleteIdentityOnProviderRemoval() {
    String childUsername = "child";
    String childPassword = "password";
    String childIdp = CHILD_IDP;
    assertFederatedIdentity(childUsername, childPassword, childIdp);
    RealmResource realm = adminClient.realm(CHILD_IDP);
    UsersResource users = realm.users();
    List<UserRepresentation> search = users.search(childUsername);
    assertFalse(search.isEmpty());
    String userId = search.get(0).getId();
    List<FederatedIdentityRepresentation> identities = users.get(userId).getFederatedIdentity();
    assertFalse(identities.isEmpty());
    realm.identityProviders().get(PARENT_IDP).remove();
    identities = users.get(userId).getFederatedIdentity();
    assertTrue(identities.isEmpty());
    getTestingClient().server(CHILD_IDP).run(AccountLinkTest::checkEmptyFederatedIdentities);
}
Also used : RealmResource(org.keycloak.admin.client.resource.RealmResource) UsersResource(org.keycloak.admin.client.resource.UsersResource) FederatedIdentityRepresentation(org.keycloak.representations.idm.FederatedIdentityRepresentation) UserRepresentation(org.keycloak.representations.idm.UserRepresentation) Test(org.junit.Test) AbstractKeycloakTest(org.keycloak.testsuite.AbstractKeycloakTest)

Example 20 with FederatedIdentityRepresentation

use of org.keycloak.representations.idm.FederatedIdentityRepresentation in project keycloak by keycloak.

the class BrokerWithLegacyIdTest method beforeBrokerTest.

@Override
public void beforeBrokerTest() {
    super.beforeBrokerTest();
    RealmResource consumerRealm = realmsResouce().realm(bc.consumerRealmName());
    String consumerUserId = createUserWithAdminClient(consumerRealm, consumerUser);
    FederatedIdentityRepresentation identity = FederatedIdentityBuilder.create().userId(LEGACY_ID).userName(bc.getUserLogin()).identityProvider(IDP_OIDC_ALIAS).build();
    consumerUserResource = consumerRealm.users().get(consumerUserId);
    consumerUserResource.addFederatedIdentity(IDP_OIDC_ALIAS, identity);
}
Also used : RealmResource(org.keycloak.admin.client.resource.RealmResource) FederatedIdentityRepresentation(org.keycloak.representations.idm.FederatedIdentityRepresentation)

Aggregations

FederatedIdentityRepresentation (org.keycloak.representations.idm.FederatedIdentityRepresentation)30 Test (org.junit.Test)18 UserRepresentation (org.keycloak.representations.idm.UserRepresentation)12 RealmResource (org.keycloak.admin.client.resource.RealmResource)9 LinkedList (java.util.LinkedList)7 List (java.util.List)7 UriBuilder (javax.ws.rs.core.UriBuilder)6 ArrayList (java.util.ArrayList)5 AbstractServletsAdapterTest (org.keycloak.testsuite.adapter.AbstractServletsAdapterTest)5 Map (java.util.Map)4 UserResource (org.keycloak.admin.client.resource.UserResource)4 ClientRepresentation (org.keycloak.representations.idm.ClientRepresentation)4 CredentialRepresentation (org.keycloak.representations.idm.CredentialRepresentation)4 IdentityProviderRepresentation (org.keycloak.representations.idm.IdentityProviderRepresentation)4 IOException (java.io.IOException)3 HashMap (java.util.HashMap)3 Response (javax.ws.rs.core.Response)3 UsersResource (org.keycloak.admin.client.resource.UsersResource)3 MultivaluedHashMap (org.keycloak.common.util.MultivaluedHashMap)3 UserConsentRepresentation (org.keycloak.representations.idm.UserConsentRepresentation)3