Search in sources :

Example 1 with ComponentTypeRepresentation

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

the class ServerInfoAdminResource method setProviders.

private void setProviders(ServerInfoRepresentation info) {
    info.setComponentTypes(new HashMap<>());
    LinkedHashMap<String, SpiInfoRepresentation> spiReps = new LinkedHashMap<>();
    List<Spi> spis = new LinkedList<>(session.getKeycloakSessionFactory().getSpis());
    Collections.sort(spis, new Comparator<Spi>() {

        @Override
        public int compare(Spi s1, Spi s2) {
            return s1.getName().compareTo(s2.getName());
        }
    });
    for (Spi spi : spis) {
        SpiInfoRepresentation spiRep = new SpiInfoRepresentation();
        spiRep.setInternal(spi.isInternal());
        List<String> providerIds = new LinkedList<>(session.listProviderIds(spi.getProviderClass()));
        Collections.sort(providerIds);
        Map<String, ProviderRepresentation> providers = new HashMap<>();
        if (providerIds != null) {
            for (String name : providerIds) {
                ProviderRepresentation provider = new ProviderRepresentation();
                ProviderFactory<?> pi = session.getKeycloakSessionFactory().getProviderFactory(spi.getProviderClass(), name);
                provider.setOrder(pi.order());
                if (ServerInfoAwareProviderFactory.class.isAssignableFrom(pi.getClass())) {
                    provider.setOperationalInfo(((ServerInfoAwareProviderFactory) pi).getOperationalInfo());
                }
                if (pi instanceof ConfiguredProvider) {
                    ComponentTypeRepresentation rep = new ComponentTypeRepresentation();
                    rep.setId(pi.getId());
                    ConfiguredProvider configured = (ConfiguredProvider) pi;
                    rep.setHelpText(configured.getHelpText());
                    List<ProviderConfigProperty> configProperties = configured.getConfigProperties();
                    if (configProperties == null)
                        configProperties = Collections.EMPTY_LIST;
                    rep.setProperties(ModelToRepresentation.toRepresentation(configProperties));
                    if (pi instanceof ComponentFactory) {
                        rep.setMetadata(((ComponentFactory) pi).getTypeMetadata());
                    }
                    List<ComponentTypeRepresentation> reps = info.getComponentTypes().get(spi.getProviderClass().getName());
                    if (reps == null) {
                        reps = new LinkedList<>();
                        info.getComponentTypes().put(spi.getProviderClass().getName(), reps);
                    }
                    reps.add(rep);
                }
                providers.put(name, provider);
            }
        }
        spiRep.setProviders(providers);
        spiReps.put(spi.getName(), spiRep);
    }
    info.setProviders(spiReps);
}
Also used : SpiInfoRepresentation(org.keycloak.representations.info.SpiInfoRepresentation) HashMap(java.util.HashMap) LinkedHashMap(java.util.LinkedHashMap) ProviderConfigProperty(org.keycloak.provider.ProviderConfigProperty) ComponentFactory(org.keycloak.component.ComponentFactory) ProviderRepresentation(org.keycloak.representations.info.ProviderRepresentation) Spi(org.keycloak.provider.Spi) LinkedList(java.util.LinkedList) LinkedHashMap(java.util.LinkedHashMap) ConfiguredProvider(org.keycloak.provider.ConfiguredProvider) ComponentTypeRepresentation(org.keycloak.representations.idm.ComponentTypeRepresentation)

Example 2 with ComponentTypeRepresentation

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

the class UserStorageRestTest method testLDAPMapperProviderConfigurationForVendorOther.

// KEYCLOAK-12934
@Test
public void testLDAPMapperProviderConfigurationForVendorOther() {
    ComponentRepresentation ldapRep = createBasicLDAPProviderRep();
    ldapRep.getConfig().putSingle(LDAPConstants.VENDOR, LDAPConstants.VENDOR_OTHER);
    String ldapModelId = createComponent(ldapRep);
    ComponentTypeRepresentation groupLDAPMapperType = findMapperTypeConfiguration(ldapModelId, GroupLDAPStorageMapperFactory.PROVIDER_ID);
    ConfigPropertyRepresentation groupRetrieverConfigProperty = getUserRolesRetrieveStrategyConfigProperty(groupLDAPMapperType, CommonLDAPGroupMapperConfig.USER_ROLES_RETRIEVE_STRATEGY);
    // LOAD_GROUPS_BY_MEMBER_ATTRIBUTE_RECURSIVELY is expected to be present just for the active directory
    List<String> options = groupRetrieverConfigProperty.getOptions();
    Assert.assertNames(options, GroupMapperConfig.LOAD_GROUPS_BY_MEMBER_ATTRIBUTE, GroupMapperConfig.GET_GROUPS_FROM_USER_MEMBEROF_ATTRIBUTE);
    Assert.assertFalse(groupRetrieverConfigProperty.getHelpText().contains("LOAD_GROUPS_BY_MEMBER_ATTRIBUTE_RECURSIVELY"));
    ComponentTypeRepresentation roleLDAPMapperType = findMapperTypeConfiguration(ldapModelId, RoleLDAPStorageMapperFactory.PROVIDER_ID);
    ConfigPropertyRepresentation roleRetrieverConfigProperty = getUserRolesRetrieveStrategyConfigProperty(roleLDAPMapperType, CommonLDAPGroupMapperConfig.USER_ROLES_RETRIEVE_STRATEGY);
    // LOAD_ROLES_BY_MEMBER_ATTRIBUTE_RECURSIVELY is expected to be present just for the active directory
    options = roleRetrieverConfigProperty.getOptions();
    Assert.assertNames(options, RoleMapperConfig.LOAD_ROLES_BY_MEMBER_ATTRIBUTE, RoleMapperConfig.GET_ROLES_FROM_USER_MEMBEROF_ATTRIBUTE);
    Assert.assertFalse(roleRetrieverConfigProperty.getHelpText().contains("LOAD_ROLES_BY_MEMBER_ATTRIBUTE_RECURSIVELY"));
    // Cleanup including mappers
    removeComponent(ldapModelId);
}
Also used : ComponentRepresentation(org.keycloak.representations.idm.ComponentRepresentation) ComponentTypeRepresentation(org.keycloak.representations.idm.ComponentTypeRepresentation) ConfigPropertyRepresentation(org.keycloak.representations.idm.ConfigPropertyRepresentation) Test(org.junit.Test) AbstractAuthenticationTest(org.keycloak.testsuite.admin.authentication.AbstractAuthenticationTest)

Example 3 with ComponentTypeRepresentation

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

the class ClientRegistrationPolicyResource method getProviders.

/**
 * Base path for retrieve providers with the configProperties properly filled
 *
 * @return
 */
@Path("providers")
@GET
@NoCache
@Produces(MediaType.APPLICATION_JSON)
public Stream<ComponentTypeRepresentation> getProviders() {
    return session.getKeycloakSessionFactory().getProviderFactoriesStream(ClientRegistrationPolicy.class).map((ProviderFactory factory) -> {
        ClientRegistrationPolicyFactory clientRegFactory = (ClientRegistrationPolicyFactory) factory;
        List<ProviderConfigProperty> configProps = clientRegFactory.getConfigProperties(session);
        ComponentTypeRepresentation rep = new ComponentTypeRepresentation();
        rep.setId(clientRegFactory.getId());
        rep.setHelpText(clientRegFactory.getHelpText());
        rep.setProperties(ModelToRepresentation.toRepresentation(configProps));
        return rep;
    });
}
Also used : ClientRegistrationPolicy(org.keycloak.services.clientregistration.policy.ClientRegistrationPolicy) ComponentTypeRepresentation(org.keycloak.representations.idm.ComponentTypeRepresentation) ProviderFactory(org.keycloak.provider.ProviderFactory) ProviderConfigProperty(org.keycloak.provider.ProviderConfigProperty) ClientRegistrationPolicyFactory(org.keycloak.services.clientregistration.policy.ClientRegistrationPolicyFactory) Path(javax.ws.rs.Path) Produces(javax.ws.rs.Produces) GET(javax.ws.rs.GET) NoCache(org.jboss.resteasy.annotations.cache.NoCache)

Example 4 with ComponentTypeRepresentation

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

the class ComponentResource method toComponentTypeRepresentation.

private ComponentTypeRepresentation toComponentTypeRepresentation(ProviderFactory factory, ComponentModel parent) {
    ComponentTypeRepresentation rep = new ComponentTypeRepresentation();
    rep.setId(factory.getId());
    ComponentFactory componentFactory = (ComponentFactory) factory;
    rep.setHelpText(componentFactory.getHelpText());
    List<ProviderConfigProperty> props;
    Map<String, Object> metadata;
    if (factory instanceof SubComponentFactory) {
        props = ((SubComponentFactory) factory).getConfigProperties(realm, parent);
        metadata = ((SubComponentFactory) factory).getTypeMetadata(realm, parent);
    } else {
        props = componentFactory.getConfigProperties();
        metadata = componentFactory.getTypeMetadata();
    }
    List<ConfigPropertyRepresentation> propReps = ModelToRepresentation.toRepresentation(props);
    rep.setProperties(propReps);
    rep.setMetadata(metadata);
    return rep;
}
Also used : ComponentTypeRepresentation(org.keycloak.representations.idm.ComponentTypeRepresentation) SubComponentFactory(org.keycloak.component.SubComponentFactory) SubComponentFactory(org.keycloak.component.SubComponentFactory) ComponentFactory(org.keycloak.component.ComponentFactory) ProviderConfigProperty(org.keycloak.provider.ProviderConfigProperty) ConfigPropertyRepresentation(org.keycloak.representations.idm.ConfigPropertyRepresentation)

Example 5 with ComponentTypeRepresentation

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

the class ClientRegistrationPoliciesTest method testProviders.

@Test
public void testProviders() throws Exception {
    List<ComponentTypeRepresentation> reps = realmResource().clientRegistrationPolicy().getProviders();
    Map<String, ComponentTypeRepresentation> providersMap = reps.stream().collect(Collectors.toMap((ComponentTypeRepresentation rep) -> {
        return rep.getId();
    }, (ComponentTypeRepresentation rep) -> {
        return rep;
    }));
    // test that ProtocolMappersClientRegistrationPolicy provider contains available protocol mappers
    ComponentTypeRepresentation protMappersRep = providersMap.get(ProtocolMappersClientRegistrationPolicyFactory.PROVIDER_ID);
    List<String> availableMappers = getProviderConfigProperty(protMappersRep, ProtocolMappersClientRegistrationPolicyFactory.ALLOWED_PROTOCOL_MAPPER_TYPES);
    List<String> someExpectedMappers = Arrays.asList(UserAttributeStatementMapper.PROVIDER_ID, UserAttributeMapper.PROVIDER_ID, UserPropertyAttributeStatementMapper.PROVIDER_ID, UserPropertyMapper.PROVIDER_ID, HardcodedRole.PROVIDER_ID);
    availableMappers.containsAll(someExpectedMappers);
    // test that clientScope provider contains just the default client scopes
    ComponentTypeRepresentation clientScopeRep = providersMap.get(ClientScopesClientRegistrationPolicyFactory.PROVIDER_ID);
    List<String> clientScopes = getProviderConfigProperty(clientScopeRep, ClientScopesClientRegistrationPolicyFactory.ALLOWED_CLIENT_SCOPES);
    Assert.assertFalse(clientScopes.isEmpty());
    Assert.assertTrue(clientScopes.contains(OAuth2Constants.SCOPE_PROFILE));
    Assert.assertFalse(clientScopes.contains("foo"));
    Assert.assertFalse(clientScopes.contains("bar"));
    // Add some clientScopes
    ClientScopeRepresentation clientScope = new ClientScopeRepresentation();
    clientScope.setName("foo");
    clientScope.setProtocol(OIDCLoginProtocol.LOGIN_PROTOCOL);
    Response response = realmResource().clientScopes().create(clientScope);
    String fooScopeId = ApiUtil.getCreatedId(response);
    response.close();
    clientScope = new ClientScopeRepresentation();
    clientScope.setName("bar");
    clientScope.setProtocol(OIDCLoginProtocol.LOGIN_PROTOCOL);
    response = realmResource().clientScopes().create(clientScope);
    String barScopeId = ApiUtil.getCreatedId(response);
    response.close();
    // send request again and test that clientScope provider contains added client scopes
    reps = realmResource().clientRegistrationPolicy().getProviders();
    clientScopeRep = reps.stream().filter((ComponentTypeRepresentation rep1) -> {
        return rep1.getId().equals(ClientScopesClientRegistrationPolicyFactory.PROVIDER_ID);
    }).findFirst().get();
    clientScopes = getProviderConfigProperty(clientScopeRep, ClientScopesClientRegistrationPolicyFactory.ALLOWED_CLIENT_SCOPES);
    Assert.assertTrue(clientScopes.contains("foo"));
    Assert.assertTrue(clientScopes.contains("bar"));
    // Revert client scopes
    realmResource().clientScopes().get(fooScopeId).remove();
    realmResource().clientScopes().get(barScopeId).remove();
}
Also used : Response(javax.ws.rs.core.Response) ComponentTypeRepresentation(org.keycloak.representations.idm.ComponentTypeRepresentation) ClientScopeRepresentation(org.keycloak.representations.idm.ClientScopeRepresentation) Test(org.junit.Test)

Aggregations

ComponentTypeRepresentation (org.keycloak.representations.idm.ComponentTypeRepresentation)7 Test (org.junit.Test)3 ProviderConfigProperty (org.keycloak.provider.ProviderConfigProperty)3 ConfigPropertyRepresentation (org.keycloak.representations.idm.ConfigPropertyRepresentation)3 ComponentFactory (org.keycloak.component.ComponentFactory)2 ComponentRepresentation (org.keycloak.representations.idm.ComponentRepresentation)2 AbstractAuthenticationTest (org.keycloak.testsuite.admin.authentication.AbstractAuthenticationTest)2 HashMap (java.util.HashMap)1 LinkedHashMap (java.util.LinkedHashMap)1 LinkedList (java.util.LinkedList)1 GET (javax.ws.rs.GET)1 Path (javax.ws.rs.Path)1 Produces (javax.ws.rs.Produces)1 Response (javax.ws.rs.core.Response)1 NoCache (org.jboss.resteasy.annotations.cache.NoCache)1 ComponentResource (org.keycloak.admin.client.resource.ComponentResource)1 SubComponentFactory (org.keycloak.component.SubComponentFactory)1 ConfiguredProvider (org.keycloak.provider.ConfiguredProvider)1 ProviderFactory (org.keycloak.provider.ProviderFactory)1 Spi (org.keycloak.provider.Spi)1