Java Code Examples for org.keycloak.provider.ProviderFactory#create()

The following examples show how to use org.keycloak.provider.ProviderFactory#create() . 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: RealmSynchronizer.java    From keycloak with Apache License 2.0 6 votes vote down vote up
@Override
public void synchronize(RealmRemovedEvent event, KeycloakSessionFactory factory) {
    ProviderFactory<AuthorizationProvider> providerFactory = factory.getProviderFactory(AuthorizationProvider.class);
    AuthorizationProvider authorizationProvider = providerFactory.create(event.getKeycloakSession());
    StoreFactory storeFactory = authorizationProvider.getStoreFactory();

    event.getRealm().getClients().forEach(clientModel -> {
        ResourceServer resourceServer = storeFactory.getResourceServerStore().findById(clientModel.getId());

        if (resourceServer != null) {
            String id = resourceServer.getId();
            //storeFactory.getResourceStore().findByResourceServer(id).forEach(resource -> storeFactory.getResourceStore().delete(resource.getId()));
            //storeFactory.getScopeStore().findByResourceServer(id).forEach(scope -> storeFactory.getScopeStore().delete(scope.getId()));
            //storeFactory.getPolicyStore().findByResourceServer(id).forEach(scope -> storeFactory.getPolicyStore().delete(scope.getId()));
            storeFactory.getResourceServerStore().delete(id);
        }
    });
}
 
Example 2
Source File: DefaultKeycloakSession.java    From keycloak with Apache License 2.0 6 votes vote down vote up
@SuppressWarnings("unchecked")
public <T extends Provider> T getProvider(Class<T> clazz) {
    Integer hash = clazz.hashCode();
    T provider = (T) providers.get(hash);
    // KEYCLOAK-11890 - Avoid using HashMap.computeIfAbsent() to implement logic in outer if() block below,
    // since per JDK-8071667 the remapping function should not modify the map during computation. While
    // allowed on JDK 1.8, attempt of such a modification throws ConcurrentModificationException with JDK 9+
    if (provider == null) {
        ProviderFactory<T> providerFactory = factory.getProviderFactory(clazz);
        if (providerFactory != null) {
            provider = providerFactory.create(DefaultKeycloakSession.this);
            providers.put(hash, provider);
        }
    }
    return provider;
}
 
Example 3
Source File: DefaultKeycloakSession.java    From keycloak with Apache License 2.0 6 votes vote down vote up
@SuppressWarnings("unchecked")
public <T extends Provider> T getProvider(Class<T> clazz, String id) {
    Integer hash = clazz.hashCode() + id.hashCode();
    T provider = (T) providers.get(hash);
    // KEYCLOAK-11890 - Avoid using HashMap.computeIfAbsent() to implement logic in outer if() block below,
    // since per JDK-8071667 the remapping function should not modify the map during computation. While
    // allowed on JDK 1.8, attempt of such a modification throws ConcurrentModificationException with JDK 9+
    if (provider == null) {
        ProviderFactory<T> providerFactory = factory.getProviderFactory(clazz, id);
        if (providerFactory != null) {
            provider = providerFactory.create(DefaultKeycloakSession.this);
            providers.put(hash, provider);
        }
    }
    return provider;
}
 
Example 4
Source File: ClientApplicationSynchronizer.java    From keycloak with Apache License 2.0 5 votes vote down vote up
@Override
public void synchronize(ClientRemovedEvent event, KeycloakSessionFactory factory) {
    ProviderFactory<AuthorizationProvider> providerFactory = factory.getProviderFactory(AuthorizationProvider.class);
    AuthorizationProvider authorizationProvider = providerFactory.create(event.getKeycloakSession());

    removeFromClientPolicies(event, authorizationProvider);
}
 
Example 5
Source File: UserSynchronizer.java    From keycloak with Apache License 2.0 5 votes vote down vote up
@Override
public void synchronize(UserRemovedEvent event, KeycloakSessionFactory factory) {
    ProviderFactory<AuthorizationProvider> providerFactory = factory.getProviderFactory(AuthorizationProvider.class);
    AuthorizationProvider authorizationProvider = providerFactory.create(event.getKeycloakSession());

    removeFromUserPermissionTickets(event, authorizationProvider);
    removeUserResources(event, authorizationProvider);
    removeFromUserPolicies(event, authorizationProvider);
}
 
Example 6
Source File: GroupSynchronizer.java    From keycloak with Apache License 2.0 5 votes vote down vote up
@Override
public void synchronize(GroupModel.GroupRemovedEvent event, KeycloakSessionFactory factory) {
    ProviderFactory<AuthorizationProvider> providerFactory = factory.getProviderFactory(AuthorizationProvider.class);
    AuthorizationProvider authorizationProvider = providerFactory.create(event.getKeycloakSession());

    StoreFactory storeFactory = authorizationProvider.getStoreFactory();
    PolicyStore policyStore = storeFactory.getPolicyStore();
    GroupModel group = event.getGroup();
    Map<String, String[]> attributes = new HashMap<>();

    attributes.put("type", new String[] {"group"});
    attributes.put("config:groups", new String[] {group.getId()});

    List<Policy> search = policyStore.findByResourceServer(attributes, null, -1, -1);

    for (Policy policy : search) {
        PolicyProviderFactory policyFactory = authorizationProvider.getProviderFactory(policy.getType());
        GroupPolicyRepresentation representation = GroupPolicyRepresentation.class.cast(policyFactory.toRepresentation(policy, authorizationProvider));
        Set<GroupPolicyRepresentation.GroupDefinition> groups = representation.getGroups();

        groups.removeIf(groupDefinition -> groupDefinition.getId().equals(group.getId()));

        if (groups.isEmpty()) {
            policyFactory.onRemove(policy, authorizationProvider);
            policyStore.delete(policy.getId());
        } else {
            policyFactory.onUpdate(policy, representation, authorizationProvider);
        }
    }
}