Search in sources :

Example 1 with UserFederatedStorageProvider

use of org.keycloak.storage.federated.UserFederatedStorageProvider in project keycloak by keycloak.

the class UserStorageManager method query.

protected Stream<UserModel> query(PaginatedQuery pagedQuery, CountQuery countQuery, RealmModel realm, Integer firstResult, Integer maxResults) {
    if (maxResults != null && maxResults == 0)
        return Stream.empty();
    Stream<Object> providersStream = Stream.concat(Stream.of((Object) localStorage()), getEnabledStorageProviders(realm, UserQueryProvider.class));
    UserFederatedStorageProvider federatedStorageProvider = getFederatedStorage();
    if (federatedStorageProvider != null) {
        providersStream = Stream.concat(providersStream, Stream.of(federatedStorageProvider));
    }
    final AtomicInteger currentFirst;
    if (firstResult == null || firstResult <= 0) {
        // We don't want to skip any users so we don't need to do firstResult filtering
        currentFirst = new AtomicInteger(0);
    } else {
        AtomicBoolean droppingProviders = new AtomicBoolean(true);
        currentFirst = new AtomicInteger(firstResult);
        providersStream = providersStream.filter(provider -> {
            // We have already gathered enough users to pass firstResult number in previous providers, we can take all following providers
            if (!droppingProviders.get())
                return true;
            // check how many users we can obtain from this provider
            long expectedNumberOfUsersForProvider = countQuery.query(provider, 0, currentFirst.get() + 1);
            if (expectedNumberOfUsersForProvider == currentFirst.get()) {
                // This provider provides exactly the amount of users we need for passing firstResult, we can set currentFirst to 0 and drop this provider
                currentFirst.set(0);
                droppingProviders.set(false);
                return false;
            }
            if (expectedNumberOfUsersForProvider > currentFirst.get()) {
                // If we can obtain enough enough users from this provider to fulfill our need we can stop dropping providers
                droppingProviders.set(false);
                // don't filter out this provider because we are going to return some users from it
                return true;
            }
            // This provider cannot provide enough users to pass firstResult so we are going to filter it out and change firstResult for next provider
            currentFirst.set((int) (currentFirst.get() - expectedNumberOfUsersForProvider));
            return false;
        });
    }
    // Actual user querying
    if (maxResults == null || maxResults < 0) {
        // No maxResult set, we want all users
        return providersStream.flatMap(provider -> pagedQuery.query(provider, currentFirst.getAndSet(0), null));
    } else {
        final AtomicInteger currentMax = new AtomicInteger(maxResults);
        // Query users with currentMax variable counting how many users we return
        return providersStream.filter(// If we reach currentMax == 0, we can skip querying all following providers
        provider -> currentMax.get() != 0).flatMap(provider -> pagedQuery.query(provider, currentFirst.getAndSet(0), currentMax.get())).peek(userModel -> {
            currentMax.updateAndGet(i -> i > 0 ? i - 1 : i);
        });
    }
}
Also used : ClientModel(org.keycloak.models.ClientModel) ReadOnlyUserModelDelegate(org.keycloak.models.utils.ReadOnlyUserModelDelegate) ClientStorageProvider(org.keycloak.storage.client.ClientStorageProvider) ProtocolMapperModel(org.keycloak.models.ProtocolMapperModel) UserLookupProvider(org.keycloak.storage.user.UserLookupProvider) Logger(org.jboss.logging.Logger) ImportedUserValidation(org.keycloak.storage.user.ImportedUserValidation) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) OnUserCache(org.keycloak.models.cache.OnUserCache) UserFederatedStorageProvider(org.keycloak.storage.federated.UserFederatedStorageProvider) UserModel(org.keycloak.models.UserModel) UserRegistrationProvider(org.keycloak.storage.user.UserRegistrationProvider) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) UserConsentModel(org.keycloak.models.UserConsentModel) Map(java.util.Map) ComponentModel(org.keycloak.component.ComponentModel) GroupModel(org.keycloak.models.GroupModel) ComponentUtil(org.keycloak.models.utils.ComponentUtil) FederatedIdentityModel(org.keycloak.models.FederatedIdentityModel) UserManager(org.keycloak.models.UserManager) ClientScopeModel(org.keycloak.models.ClientScopeModel) RealmModel(org.keycloak.models.RealmModel) KeycloakModelUtils.runJobInTransaction(org.keycloak.models.utils.KeycloakModelUtils.runJobInTransaction) KeycloakSession(org.keycloak.models.KeycloakSession) Set(java.util.Set) RoleModel(org.keycloak.models.RoleModel) IdentityProviderModel(org.keycloak.models.IdentityProviderModel) UserBulkUpdateProvider(org.keycloak.storage.user.UserBulkUpdateProvider) CachedUserModel(org.keycloak.models.cache.CachedUserModel) UserQueryProvider(org.keycloak.storage.user.UserQueryProvider) StreamsUtil.distinctByKey(org.keycloak.utils.StreamsUtil.distinctByKey) UserStorageSyncManager(org.keycloak.services.managers.UserStorageSyncManager) Objects(java.util.Objects) UserProvider(org.keycloak.models.UserProvider) Stream(java.util.stream.Stream) UserCache(org.keycloak.models.cache.UserCache) ComponentFactory(org.keycloak.component.ComponentFactory) ModelException(org.keycloak.models.ModelException) Optional(java.util.Optional) StreamsUtil.paginatedStream(org.keycloak.utils.StreamsUtil.paginatedStream) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) UserFederatedStorageProvider(org.keycloak.storage.federated.UserFederatedStorageProvider) UserQueryProvider(org.keycloak.storage.user.UserQueryProvider) AtomicInteger(java.util.concurrent.atomic.AtomicInteger)

Example 2 with UserFederatedStorageProvider

use of org.keycloak.storage.federated.UserFederatedStorageProvider in project keycloak by keycloak.

the class RepresentationToModel method importFederatedUser.

public static void importFederatedUser(KeycloakSession session, RealmModel newRealm, UserRepresentation userRep) {
    UserFederatedStorageProvider federatedStorage = session.userFederatedStorage();
    if (userRep.getAttributes() != null) {
        for (Map.Entry<String, List<String>> entry : userRep.getAttributes().entrySet()) {
            String key = entry.getKey();
            List<String> value = entry.getValue();
            if (value != null) {
                federatedStorage.setAttribute(newRealm, userRep.getId(), key, new LinkedList<>(value));
            }
        }
    }
    if (userRep.getRequiredActions() != null) {
        for (String action : userRep.getRequiredActions()) {
            federatedStorage.addRequiredAction(newRealm, userRep.getId(), action);
        }
    }
    if (userRep.getCredentials() != null) {
        for (CredentialRepresentation cred : userRep.getCredentials()) {
            federatedStorage.createCredential(newRealm, userRep.getId(), toModel(cred));
        }
    }
    createFederatedRoleMappings(federatedStorage, userRep, newRealm);
    if (userRep.getGroups() != null) {
        for (String path : userRep.getGroups()) {
            GroupModel group = KeycloakModelUtils.findGroupByPath(newRealm, path);
            if (group == null) {
                throw new RuntimeException("Unable to find group specified by path: " + path);
            }
            federatedStorage.joinGroup(newRealm, userRep.getId(), group);
        }
    }
    if (userRep.getFederatedIdentities() != null) {
        for (FederatedIdentityRepresentation identity : userRep.getFederatedIdentities()) {
            FederatedIdentityModel mappingModel = new FederatedIdentityModel(identity.getIdentityProvider(), identity.getUserId(), identity.getUserName());
            federatedStorage.addFederatedIdentity(newRealm, userRep.getId(), mappingModel);
        }
    }
    if (userRep.getClientConsents() != null) {
        for (UserConsentRepresentation consentRep : userRep.getClientConsents()) {
            UserConsentModel consentModel = toModel(newRealm, consentRep);
            federatedStorage.addConsent(newRealm, userRep.getId(), consentModel);
        }
    }
    if (userRep.getNotBefore() != null) {
        federatedStorage.setNotBeforeForUser(newRealm, userRep.getId(), userRep.getNotBefore());
    }
}
Also used : FederatedIdentityModel(org.keycloak.models.FederatedIdentityModel) GroupModel(org.keycloak.models.GroupModel) ArtifactBindingUtils.computeArtifactBindingIdentifierString(org.keycloak.protocol.saml.util.ArtifactBindingUtils.computeArtifactBindingIdentifierString) UserConsentModel(org.keycloak.models.UserConsentModel) UserConsentRepresentation(org.keycloak.representations.idm.UserConsentRepresentation) CredentialRepresentation(org.keycloak.representations.idm.CredentialRepresentation) UserFederatedStorageProvider(org.keycloak.storage.federated.UserFederatedStorageProvider) ArrayList(java.util.ArrayList) List(java.util.List) LinkedList(java.util.LinkedList) Map(java.util.Map) MultivaluedHashMap(org.keycloak.common.util.MultivaluedHashMap) HashMap(java.util.HashMap) FederatedIdentityRepresentation(org.keycloak.representations.idm.FederatedIdentityRepresentation)

Aggregations

Map (java.util.Map)2 FederatedIdentityModel (org.keycloak.models.FederatedIdentityModel)2 GroupModel (org.keycloak.models.GroupModel)2 UserConsentModel (org.keycloak.models.UserConsentModel)2 UserFederatedStorageProvider (org.keycloak.storage.federated.UserFederatedStorageProvider)2 ArrayList (java.util.ArrayList)1 HashMap (java.util.HashMap)1 LinkedList (java.util.LinkedList)1 List (java.util.List)1 Objects (java.util.Objects)1 Optional (java.util.Optional)1 Set (java.util.Set)1 AtomicBoolean (java.util.concurrent.atomic.AtomicBoolean)1 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)1 Stream (java.util.stream.Stream)1 Logger (org.jboss.logging.Logger)1 MultivaluedHashMap (org.keycloak.common.util.MultivaluedHashMap)1 ComponentFactory (org.keycloak.component.ComponentFactory)1 ComponentModel (org.keycloak.component.ComponentModel)1 ClientModel (org.keycloak.models.ClientModel)1