Java Code Examples for org.keycloak.models.KeycloakSessionFactory#register()

The following examples show how to use org.keycloak.models.KeycloakSessionFactory#register() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: UserSessionPersisterProviderFactory.java    From keycloak with Apache License 2.0 6 votes vote down vote up
@Override
default void postInit(KeycloakSessionFactory factory) {
    factory.register(new ProviderEventListener() {

        @Override
        public void onEvent(ProviderEvent event) {
            if (event instanceof UserModel.UserRemovedEvent) {
                UserModel.UserRemovedEvent userRemovedEvent = (UserModel.UserRemovedEvent) event;

                UserSessionPersisterProvider provider = userRemovedEvent.getKeycloakSession().getProvider(UserSessionPersisterProvider.class, getId());
                provider.onUserRemoved(userRemovedEvent.getRealm(), userRemovedEvent.getUser());
            }
        }

    });
}
 
Example 2
Source File: AuthorizationStoreFactory.java    From keycloak with Apache License 2.0 6 votes vote down vote up
default void registerSynchronizationListeners(KeycloakSessionFactory factory) {
    Map<Class<? extends ProviderEvent>, Synchronizer> synchronizers = new HashMap<>();

    synchronizers.put(ClientRemovedEvent.class, new ClientApplicationSynchronizer());
    synchronizers.put(RealmRemovedEvent.class, new RealmSynchronizer());
    synchronizers.put(UserRemovedEvent.class, new UserSynchronizer());
    synchronizers.put(GroupModel.GroupRemovedEvent.class, new GroupSynchronizer());

    factory.register(event -> {
        try {
            synchronizers.forEach((eventType, synchronizer) -> {
                if (eventType.isInstance(event)) {
                    synchronizer.synchronize(event, factory);
                }
            });
        } catch (Exception e) {
            throw new RuntimeException("Error synchronizing authorization data.", e);
        }
    });
}
 
Example 3
Source File: InfinispanAuthenticationSessionProviderFactory.java    From keycloak with Apache License 2.0 6 votes vote down vote up
@Override
public void postInit(KeycloakSessionFactory factory) {
    factory.register(new ProviderEventListener() {

        @Override
        public void onEvent(ProviderEvent event) {
            if (event instanceof PostMigrationEvent) {

                KeycloakModelUtils.runJobInTransaction(factory, (KeycloakSession session) -> {

                    registerClusterListeners(session);

                });
            }
        }
    });
}
 
Example 4
Source File: InfinispanPublicKeyStorageProviderFactory.java    From keycloak with Apache License 2.0 6 votes vote down vote up
@Override
public void postInit(KeycloakSessionFactory factory) {
    factory.register(new ProviderEventListener() {

        @Override
        public void onEvent(ProviderEvent event) {
            if (keysCache == null) {
                return;
            }

            SessionAndKeyHolder cacheKey = getCacheKeyToInvalidate(event);
            if (cacheKey != null) {
                log.debugf("Invalidating %s from keysCache", cacheKey);
                InfinispanPublicKeyStorageProvider provider = (InfinispanPublicKeyStorageProvider) cacheKey.session.getProvider(PublicKeyStorageProvider.class, getId());
                for (String ck : cacheKey.cacheKeys) provider.addInvalidation(ck);
            }
        }

    });
}
 
Example 5
Source File: AbstractLoginProtocolFactory.java    From keycloak with Apache License 2.0 5 votes vote down vote up
@Override
public void postInit(KeycloakSessionFactory factory) {
    factory.register(new ProviderEventListener() {
        @Override
        public void onEvent(ProviderEvent event) {
            if (event instanceof RealmModel.ClientCreationEvent) {
                ClientModel client = ((RealmModel.ClientCreationEvent)event).getCreatedClient();
                addDefaultClientScopes(client.getRealm(), client);
                addDefaults(client);
            }
        }
    });
}
 
Example 6
Source File: UserPolicyProviderFactory.java    From keycloak with Apache License 2.0 5 votes vote down vote up
@Override
public void postInit(KeycloakSessionFactory factory) {
    factory.register(event -> {
        if (event instanceof UserRemovedEvent) {
            KeycloakSession keycloakSession = ((UserRemovedEvent) event).getKeycloakSession();
            AuthorizationProvider provider = keycloakSession.getProvider(AuthorizationProvider.class);
            StoreFactory storeFactory = provider.getStoreFactory();
            PolicyStore policyStore = storeFactory.getPolicyStore();
            UserModel removedUser = ((UserRemovedEvent) event).getUser();
            RealmModel realm = ((UserRemovedEvent) event).getRealm();
            ResourceServerStore resourceServerStore = storeFactory.getResourceServerStore();
            realm.getClients().forEach(clientModel -> {
                ResourceServer resourceServer = resourceServerStore.findById(clientModel.getId());

                if (resourceServer != null) {
                    policyStore.findByType(getId(), resourceServer.getId()).forEach(policy -> {
                        List<String> users = new ArrayList<>();

                        for (String userId : getUsers(policy)) {
                            if (!userId.equals(removedUser.getId())) {
                                users.add(userId);
                            }
                        }

                        try {
                            // just update the policy, let the UserSynchronizer to actually remove the policy if necessary
                            if (!users.isEmpty()) {
                                policy.putConfig("users", JsonSerialization.writeValueAsString(users));
                            }
                        } catch (IOException e) {
                            throw new RuntimeException("Error while synchronizing users with policy [" + policy.getName() + "].", e);
                        }
                    });
                }
            });
        }
    });
}
 
Example 7
Source File: ClientPolicyProviderFactory.java    From keycloak with Apache License 2.0 5 votes vote down vote up
@Override
public void postInit(KeycloakSessionFactory factory) {
    factory.register(event -> {
        if (event instanceof ClientRemovedEvent) {
            KeycloakSession keycloakSession = ((ClientRemovedEvent) event).getKeycloakSession();
            AuthorizationProvider provider = keycloakSession.getProvider(AuthorizationProvider.class);
            StoreFactory storeFactory = provider.getStoreFactory();
            PolicyStore policyStore = storeFactory.getPolicyStore();
            ClientModel removedClient = ((ClientRemovedEvent) event).getClient();
            ResourceServerStore resourceServerStore = storeFactory.getResourceServerStore();
            ResourceServer resourceServer = resourceServerStore.findById(removedClient.getId());

            if (resourceServer != null) {
                policyStore.findByType(getId(), resourceServer.getId()).forEach(policy -> {
                    List<String> clients = new ArrayList<>();

                    for (String clientId : getClients(policy)) {
                        if (!clientId.equals(removedClient.getId())) {
                            clients.add(clientId);
                        }
                    }

                    try {
                        if (clients.isEmpty()) {
                            policyStore.delete(policy.getId());
                        } else {
                            policy.putConfig("clients", JsonSerialization.writeValueAsString(clients));
                        }
                    } catch (IOException e) {
                        throw new RuntimeException("Error while synchronizing clients with policy [" + policy.getName() + "].", e);
                    }
                });
            }
        }
    });
}
 
Example 8
Source File: InfinispanUserSessionProviderFactory.java    From keycloak with Apache License 2.0 4 votes vote down vote up
@Override
public void postInit(final KeycloakSessionFactory factory) {

    factory.register(new ProviderEventListener() {

        @Override
        public void onEvent(ProviderEvent event) {
            if (event instanceof PostMigrationEvent) {

                int preloadTransactionTimeout = getTimeoutForPreloadingSessionsSeconds();
                log.debugf("Will preload sessions with transaction timeout %d seconds", preloadTransactionTimeout);

                KeycloakModelUtils.runJobInTransactionWithTimeout(factory, (KeycloakSession session) -> {

                    keyGenerator = new InfinispanKeyGenerator();
                    checkRemoteCaches(session);
                    loadPersistentSessions(factory, getMaxErrors(), getSessionsPerSegment());
                    registerClusterListeners(session);
                    loadSessionsFromRemoteCaches(session);

                }, preloadTransactionTimeout);

            } else if (event instanceof UserModel.UserRemovedEvent) {
                UserModel.UserRemovedEvent userRemovedEvent = (UserModel.UserRemovedEvent) event;

                InfinispanUserSessionProvider provider = (InfinispanUserSessionProvider) userRemovedEvent.getKeycloakSession().getProvider(UserSessionProvider.class, getId());
                provider.onUserRemoved(userRemovedEvent.getRealm(), userRemovedEvent.getUser());
            } else if (event instanceof ResetTimeOffsetEvent) {
                if (persisterLastSessionRefreshStore != null) {
                    persisterLastSessionRefreshStore.reset();
                }
                if (lastSessionRefreshStore != null) {
                    lastSessionRefreshStore.reset();
                }
                if (offlineLastSessionRefreshStore != null) {
                    offlineLastSessionRefreshStore.reset();
                }
            }
        }
    });
}
 
Example 9
Source File: GroupPolicyProviderFactory.java    From keycloak with Apache License 2.0 4 votes vote down vote up
@Override
public void postInit(KeycloakSessionFactory factory) {
    factory.register(event -> {
    });
}