Java Code Examples for org.keycloak.models.RealmModel#getComponents()
The following examples show how to use
org.keycloak.models.RealmModel#getComponents() .
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: LDAPUtils.java From keycloak with Apache License 2.0 | 6 votes |
public static LDAPQuery createQueryForUserSearch(LDAPStorageProvider ldapProvider, RealmModel realm) { LDAPQuery ldapQuery = new LDAPQuery(ldapProvider); LDAPConfig config = ldapProvider.getLdapIdentityStore().getConfig(); ldapQuery.setSearchScope(config.getSearchScope()); ldapQuery.setSearchDn(config.getUsersDn()); ldapQuery.addObjectClasses(config.getUserObjectClasses()); String customFilter = config.getCustomUserSearchFilter(); if (customFilter != null) { Condition customFilterCondition = new LDAPQueryConditionsBuilder().addCustomLDAPFilter(customFilter); ldapQuery.addWhereCondition(customFilterCondition); } List<ComponentModel> mapperModels = realm.getComponents(ldapProvider.getModel().getId(), LDAPStorageMapper.class.getName()); ldapQuery.addMappers(mapperModels); return ldapQuery; }
Example 2
Source File: LDAPUtils.java From keycloak with Apache License 2.0 | 6 votes |
/** * @param ldapProvider * @param realm * @param user * @return newly created LDAPObject with all the attributes, uuid and DN properly set */ public static LDAPObject addUserToLDAP(LDAPStorageProvider ldapProvider, RealmModel realm, UserModel user) { LDAPObject ldapUser = new LDAPObject(); LDAPIdentityStore ldapStore = ldapProvider.getLdapIdentityStore(); LDAPConfig ldapConfig = ldapStore.getConfig(); ldapUser.setRdnAttributeName(ldapConfig.getRdnLdapAttribute()); ldapUser.setObjectClasses(ldapConfig.getUserObjectClasses()); List<ComponentModel> federationMappers = realm.getComponents(ldapProvider.getModel().getId(), LDAPStorageMapper.class.getName()); List<ComponentModel> sortedMappers = ldapProvider.getMapperManager().sortMappersAsc(federationMappers); for (ComponentModel mapperModel : sortedMappers) { LDAPStorageMapper ldapMapper = ldapProvider.getMapperManager().getMapper(mapperModel); ldapMapper.onRegisterUserToLDAP(ldapUser, user, realm); } LDAPUtils.computeAndSetDn(ldapConfig, ldapUser); ldapStore.add(ldapUser); return ldapUser; }
Example 3
Source File: TestingResourceProvider.java From keycloak with Apache License 2.0 | 6 votes |
@GET @Path("/test-component") @Produces(MediaType.APPLICATION_JSON) public Map<String, TestProvider.DetailsRepresentation> getTestComponentDetails() { Map<String, TestProvider.DetailsRepresentation> reps = new HashMap<>(); RealmModel realm = session.getContext().getRealm(); for (ComponentModel c : realm.getComponents(realm.getId(), TestProvider.class.getName())) { ProviderFactory<TestProvider> f = session.getKeycloakSessionFactory().getProviderFactory(TestProvider.class, c.getProviderId()); TestProviderFactory factory = (TestProviderFactory) f; TestProvider p = (TestProvider) factory.create(session, c); reps.put(c.getName(), p.getDetails()); } return reps; }
Example 4
Source File: DefaultKeyManager.java From keycloak with Apache License 2.0 | 6 votes |
private List<KeyProvider> getProviders(RealmModel realm) { List<KeyProvider> providers = providersMap.get(realm.getId()); if (providers == null) { providers = new LinkedList<>(); List<ComponentModel> components = new LinkedList<>(realm.getComponents(realm.getId(), KeyProvider.class.getName())); components.sort(new ProviderComparator()); for (ComponentModel c : components) { try { ProviderFactory<KeyProvider> f = session.getKeycloakSessionFactory().getProviderFactory(KeyProvider.class, c.getProviderId()); KeyProviderFactory factory = (KeyProviderFactory) f; KeyProvider provider = factory.create(session, c); session.enlistForClose(provider); providers.add(provider); } catch (Throwable t) { logger.errorv(t, "Failed to load provider {0}", c.getId()); } } providersMap.put(realm.getId(), providers); } return providers; }
Example 5
Source File: LdapManyGroupsInitializerCommand.java From keycloak with Apache License 2.0 | 5 votes |
private ComponentModel getMapperModel(RealmModel realm, ComponentModel ldapModel, String mapperName) { List<ComponentModel> ldapMappers = realm.getComponents(ldapModel.getId(), LDAPStorageMapper.class.getName()); Optional<ComponentModel> optional = ldapMappers.stream().filter((ComponentModel mapper) -> { return mapper.getName().equals(mapperName); }).findFirst(); if (!optional.isPresent()) { log.errorf("Not present LDAP mapper called '%s'", mapperName); throw new HandledException(); } return optional.get(); }
Example 6
Source File: LDAPIdentityStoreRegistry.java From keycloak with Apache License 2.0 | 5 votes |
private void logLDAPConfig(KeycloakSession session, ComponentModel ldapModel, LDAPConfig ldapConfig) { logger.infof("Creating new LDAP Store for the LDAP storage provider: '%s', LDAP Configuration: %s", ldapModel.getName(), ldapConfig.toString()); if (logger.isDebugEnabled()) { RealmModel realm = session.realms().getRealm(ldapModel.getParentId()); List<ComponentModel> mappers = realm.getComponents(ldapModel.getId()); mappers.stream().forEach((ComponentModel c) -> { logger.debugf("Mapper for provider: %s, Mapper name: %s, Provider: %s, Mapper configuration: %s", ldapModel.getName(), c.getName(), c.getProviderId(), c.getConfig().toString()); }); } }
Example 7
Source File: LDAPStorageProviderFactory.java From keycloak with Apache License 2.0 | 5 votes |
protected Map<ComponentModel, LDAPConfigDecorator> getLDAPConfigDecorators(KeycloakSession session, ComponentModel ldapModel) { RealmModel realm = session.realms().getRealm(ldapModel.getParentId()); List<ComponentModel> mapperComponents = realm.getComponents(ldapModel.getId(), LDAPStorageMapper.class.getName()); Map<ComponentModel, LDAPConfigDecorator> result = new HashMap<>(); for (ComponentModel mapperModel : mapperComponents) { LDAPStorageMapperFactory mapperFactory = (LDAPStorageMapperFactory) session.getKeycloakSessionFactory().getProviderFactory(LDAPStorageMapper.class, mapperModel.getProviderId()); if (mapperFactory instanceof LDAPConfigDecorator) { result.put(mapperModel, (LDAPConfigDecorator) mapperFactory); } } return result; }
Example 8
Source File: DefaultClientRegistrationPolicies.java From keycloak with Apache License 2.0 | 5 votes |
public static void addDefaultPolicies(RealmModel realm) { String anonPolicyType = ClientRegistrationPolicyManager.getComponentTypeKey(RegistrationAuth.ANONYMOUS); String authPolicyType = ClientRegistrationPolicyManager.getComponentTypeKey(RegistrationAuth.AUTHENTICATED); List<ComponentModel> policies = realm.getComponents(realm.getId(), ClientRegistrationPolicy.class.getName()); // Probably an issue if admin removes all policies intentionally... if (policies == null ||policies.isEmpty()) { addAnonymousPolicies(realm, anonPolicyType); addAuthPolicies(realm, authPolicyType); } }
Example 9
Source File: ClientRegistrationPolicyManager.java From keycloak with Apache License 2.0 | 5 votes |
private static void triggerPolicies(KeycloakSession session, ClientRegistrationProvider provider, RegistrationAuth authType, String opDescription, ClientRegOperation op) throws ClientRegistrationPolicyException { RealmModel realm = session.getContext().getRealm(); String policyTypeKey = getComponentTypeKey(authType); List<ComponentModel> policyModels = realm.getComponents(realm.getId(), ClientRegistrationPolicy.class.getName()); policyModels = policyModels.stream().filter((ComponentModel model) -> { return policyTypeKey.equals(model.getSubType()); }).collect(Collectors.toList()); for (ComponentModel policyModel : policyModels) { ClientRegistrationPolicy policy = session.getProvider(ClientRegistrationPolicy.class, policyModel); if (policy == null) { throw new ClientRegistrationPolicyException("Policy of type '" + policyModel.getProviderId() + "' not found"); } if (logger.isTraceEnabled()) { logger.tracef("Running policy '%s' %s", policyModel.getName(), opDescription); } try { op.run(policy); } catch (ClientRegistrationPolicyException crpe) { provider.getEvent().detail(Details.CLIENT_REGISTRATION_POLICY, policyModel.getName()); crpe.setPolicyModel(policyModel); ServicesLogger.LOGGER.clientRegistrationRequestRejected(opDescription, crpe.getMessage()); throw crpe; } } }
Example 10
Source File: ExportUtils.java From keycloak with Apache License 2.0 | 5 votes |
public static MultivaluedHashMap<String, ComponentExportRepresentation> exportComponents(RealmModel realm, String parentId) { List<ComponentModel> componentList = realm.getComponents(parentId); MultivaluedHashMap<String, ComponentExportRepresentation> components = new MultivaluedHashMap<>(); for (ComponentModel component : componentList) { ComponentExportRepresentation compRep = new ComponentExportRepresentation(); compRep.setId(component.getId()); compRep.setProviderId(component.getProviderId()); compRep.setConfig(component.getConfig()); compRep.setName(component.getName()); compRep.setSubType(component.getSubType()); compRep.setSubComponents(exportComponents(realm, component.getId())); components.add(component.getProviderType(), compRep); } return components; }
Example 11
Source File: MigrateTo1_8_0.java From keycloak with Apache License 2.0 | 5 votes |
public static ComponentModel getMapperByName(RealmModel realm, ComponentModel providerModel, String name) { List<ComponentModel> components = realm.getComponents(providerModel.getId(), "org.keycloak.storage.ldap.mappers.LDAPStorageMapper"); for (ComponentModel component : components) { if (component.getName().equals(name)) { return component; } } return null; }
Example 12
Source File: LdapManyObjectsInitializerCommand.java From keycloak with Apache License 2.0 | 5 votes |
private ComponentModel getMapperModel(RealmModel realm, ComponentModel ldapModel, String mapperName) { List<ComponentModel> ldapMappers = realm.getComponents(ldapModel.getId(), LDAPStorageMapper.class.getName()); Optional<ComponentModel> optional = ldapMappers.stream().filter((ComponentModel mapper) -> { return mapper.getName().equals(mapperName); }).findFirst(); if (!optional.isPresent()) { log.errorf("Not present LDAP mapper called '%s'", mapperName); throw new HandledException(); } return optional.get(); }
Example 13
Source File: LDAPTestUtils.java From keycloak with Apache License 2.0 | 5 votes |
public static ComponentModel getSubcomponentByName(RealmModel realm, ComponentModel providerModel, String name) { List<ComponentModel> components = realm.getComponents(providerModel.getId(), LDAPStorageMapper.class.getName()); for (ComponentModel component : components) { if (component.getName().equals(name)) { return component; } } return null; }
Example 14
Source File: LDAPTestUtils.java From keycloak with Apache License 2.0 | 5 votes |
public static ComponentModel getLdapProviderModel(KeycloakSession session, RealmModel realm) { List<ComponentModel> components = realm.getComponents(realm.getId(), UserStorageProvider.class.getName()); for (ComponentModel component : components) { if (LDAPStorageProviderFactory.PROVIDER_NAME.equals(component.getProviderId())) { return component; } } return null; }
Example 15
Source File: SyncFederationTest.java From keycloak with Apache License 2.0 | 5 votes |
private static final UserStorageProviderModel findDummyProviderModel(RealmModel realm) { for (ComponentModel component : realm.getComponents()) { if ("test-sync-dummy".equals(component.getName())) { return new UserStorageProviderModel(component); } } return null; }
Example 16
Source File: DefaultKeyProviders.java From keycloak with Apache License 2.0 | 5 votes |
protected static boolean hasProvider(RealmModel realm, String providerId) { List<ComponentModel> currentComponents = realm.getComponents(realm.getId(), KeyProvider.class.getName()); for (ComponentModel current : currentComponents) { if (current.getProviderId().equals(providerId)) { return true; } } return false; }
Example 17
Source File: LdapManyGroupsInitializerCommand.java From keycloak with Apache License 2.0 | 4 votes |
@Override protected void doRunCommand(KeycloakSession session) { String realmName = getArg(0); String groupsDn = getArg(1); int startOffsetTopGroups = getIntArg(2); int topGroupsCount = getIntArg(3); int subgroupsInEveryGroup = getIntArg(4); RealmModel realm = session.realms().getRealmByName(realmName); List<ComponentModel> components = realm.getComponents(realm.getId(), UserStorageProvider.class.getName()); if (components.size() != 1) { log.errorf("Expected 1 LDAP Provider, but found: %d providers", components.size()); throw new HandledException(); } ComponentModel ldapModel = components.get(0); // Check that street mapper exists. It's required for now, so that "street" attribute is written to the LDAP ComponentModel groupMapperModel = getMapperModel(realm, ldapModel, "groupsMapper"); // Create groups for (int i=startOffsetTopGroups ; i<startOffsetTopGroups+topGroupsCount ; i++) { final int iFinal = i; KeycloakModelUtils.runJobInTransaction(session.getKeycloakSessionFactory(), (KeycloakSession kcSession) -> { LDAPStorageProvider ldapProvider = (LDAPStorageProvider)session.getProvider(UserStorageProvider.class, ldapModel); RealmModel appRealm = session.realms().getRealmByName(realmName); GroupLDAPStorageMapper groupMapper = (GroupLDAPStorageMapper) session.getProvider(LDAPStorageMapper.class, groupMapperModel); Set<String> childGroupDns = new HashSet<>(); for (int j=0 ; j<subgroupsInEveryGroup ; j++) { String groupName = "group-" + iFinal + "-" + j; LDAPObject createdGroup = groupMapper.createLDAPGroup(groupName, new HashMap<>()); childGroupDns.add(createdGroup.getDn().toString()); } String topGroupName = "group-" + iFinal; Map<String, Set<String>> groupAttrs = new HashMap<>(); groupAttrs.put("member", new HashSet<>(childGroupDns)); groupMapper.createLDAPGroup(topGroupName, groupAttrs); }); } }
Example 18
Source File: MigrateTo1_3_0.java From keycloak with Apache License 2.0 | 4 votes |
private void migrateLDAPProviders(KeycloakSession session, RealmModel realm) { List<UserStorageProviderModel> federationProviders = realm.getUserStorageProviders(); for (UserStorageProviderModel fedProvider : federationProviders) { if (fedProvider.getProviderId().equals(LDAPConstants.LDAP_PROVIDER)) { fedProvider = new UserStorageProviderModel(fedProvider); // copy don't want to muck with cache MultivaluedHashMap<String, String> config = fedProvider.getConfig(); // Update config properties for LDAP federation provider if (config.get(LDAPConstants.SEARCH_SCOPE) == null) { config.putSingle(LDAPConstants.SEARCH_SCOPE, String.valueOf(SearchControls.SUBTREE_SCOPE)); } List<String> usersDn = config.remove("userDnSuffix"); if (usersDn != null && !usersDn.isEmpty() && config.getFirst(LDAPConstants.USERS_DN) == null) { config.put(LDAPConstants.USERS_DN, usersDn); } String usernameLdapAttribute = config.getFirst(LDAPConstants.USERNAME_LDAP_ATTRIBUTE); if (usernameLdapAttribute != null && config.getFirst(LDAPConstants.RDN_LDAP_ATTRIBUTE) == null) { if (usernameLdapAttribute.equalsIgnoreCase(LDAPConstants.SAM_ACCOUNT_NAME)) { config.putSingle(LDAPConstants.RDN_LDAP_ATTRIBUTE, LDAPConstants.CN); } else { config.putSingle(LDAPConstants.RDN_LDAP_ATTRIBUTE, usernameLdapAttribute); } } if (config.getFirst(LDAPConstants.UUID_LDAP_ATTRIBUTE) == null) { String uuidAttrName = LDAPConstants.getUuidAttributeName(config.getFirst(LDAPConstants.VENDOR)); config.putSingle(LDAPConstants.UUID_LDAP_ATTRIBUTE, uuidAttrName); } realm.updateComponent(fedProvider); // Create default mappers for LDAP List<ComponentModel> mappers = realm.getComponents(fedProvider.getId()); if (mappers.isEmpty()) { ProviderFactory ldapFactory = session.getKeycloakSessionFactory().getProviderFactory(UserStorageProvider.class, LDAPConstants.LDAP_PROVIDER); if (ldapFactory != null) { ((ComponentFactory) ldapFactory).onCreate(session, realm, fedProvider); } } } } }