Java Code Examples for org.keycloak.component.ComponentModel#setProviderId()

The following examples show how to use org.keycloak.component.ComponentModel#setProviderId() . 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: DefaultKeyProviders.java    From keycloak with Apache License 2.0 6 votes vote down vote up
public static void createProviders(RealmModel realm) {
    if (!hasProvider(realm, "rsa-generated")) {
        ComponentModel generated = new ComponentModel();
        generated.setName("rsa-generated");
        generated.setParentId(realm.getId());
        generated.setProviderId("rsa-generated");
        generated.setProviderType(KeyProvider.class.getName());

        MultivaluedHashMap<String, String> config = new MultivaluedHashMap<>();
        config.putSingle("priority", "100");
        generated.setConfig(config);

        realm.addComponentModel(generated);
    }

    createSecretProvider(realm);
    createAesProvider(realm);
}
 
Example 2
Source File: DefaultKeyProviders.java    From keycloak with Apache License 2.0 6 votes vote down vote up
public static void createProviders(RealmModel realm, String privateKeyPem, String certificatePem) {
    if (!hasProvider(realm, "rsa")) {
        ComponentModel rsa = new ComponentModel();
        rsa.setName("rsa");
        rsa.setParentId(realm.getId());
        rsa.setProviderId("rsa");
        rsa.setProviderType(KeyProvider.class.getName());

        MultivaluedHashMap<String, String> config = new MultivaluedHashMap<>();
        config.putSingle("priority", "100");
        config.putSingle("privateKey", privateKeyPem);
        if (certificatePem != null) {
            config.putSingle("certificate", certificatePem);
        }
        rsa.setConfig(config);

        realm.addComponentModel(rsa);
    }

    createSecretProvider(realm);
    createAesProvider(realm);
}
 
Example 3
Source File: KeycloakModelUtils.java    From keycloak with Apache License 2.0 6 votes vote down vote up
public static ComponentModel createComponentModel(String name, String parentId, String providerId, String providerType, String... config) {
    ComponentModel mapperModel = new ComponentModel();
    mapperModel.setParentId(parentId);
    mapperModel.setName(name);
    mapperModel.setProviderId(providerId);
    mapperModel.setProviderType(providerType);

    String key = null;
    for (String configEntry : config) {
        if (key == null) {
            key = configEntry;
        } else {
            mapperModel.getConfig().add(key, configEntry);
            key = null;
        }
    }
    if (key != null) {
        throw new IllegalStateException("Invalid count of arguments for config. Maybe mistake?");
    }

    return mapperModel;
}
 
Example 4
Source File: RepresentationToModel.java    From keycloak with Apache License 2.0 6 votes vote down vote up
protected static void importComponents(RealmModel newRealm, MultivaluedHashMap<String, ComponentExportRepresentation> components, String parentId) {
    for (Map.Entry<String, List<ComponentExportRepresentation>> entry : components.entrySet()) {
        String providerType = entry.getKey();
        for (ComponentExportRepresentation compRep : entry.getValue()) {
            ComponentModel component = new ComponentModel();
            component.setId(compRep.getId());
            component.setName(compRep.getName());
            component.setConfig(compRep.getConfig());
            component.setProviderType(providerType);
            component.setProviderId(compRep.getProviderId());
            component.setSubType(compRep.getSubType());
            component.setParentId(parentId);
            component = newRealm.importComponentModel(component);
            if (compRep.getSubComponents() != null) {
                importComponents(newRealm, compRep.getSubComponents(), component.getId());
            }
        }
    }
}
 
Example 5
Source File: GeneratedAesKeyProviderFactory.java    From keycloak with Apache License 2.0 6 votes vote down vote up
@Override
public boolean createFallbackKeys(KeycloakSession session, KeyUse keyUse, String algorithm) {
    if (keyUse.equals(KeyUse.ENC) && algorithm.equals(Algorithm.AES)) {
        RealmModel realm = session.getContext().getRealm();

        ComponentModel generated = new ComponentModel();
        generated.setName("fallback-" + algorithm);
        generated.setParentId(realm.getId());
        generated.setProviderId(ID);
        generated.setProviderType(KeyProvider.class.getName());

        MultivaluedHashMap<String, String> config = new MultivaluedHashMap<>();
        config.putSingle(Attributes.PRIORITY_KEY, "-100");
        generated.setConfig(config);

        realm.addComponentModel(generated);

        return true;
    } else {
        return false;
    }
}
 
Example 6
Source File: GeneratedHmacKeyProviderFactory.java    From keycloak with Apache License 2.0 6 votes vote down vote up
@Override
public boolean createFallbackKeys(KeycloakSession session, KeyUse keyUse, String algorithm) {
    if (keyUse.equals(KeyUse.SIG) && (algorithm.equals(Algorithm.HS256) || algorithm.equals(Algorithm.HS384) || algorithm.equals(Algorithm.HS512))) {
        RealmModel realm = session.getContext().getRealm();

        ComponentModel generated = new ComponentModel();
        generated.setName("fallback-" + algorithm);
        generated.setParentId(realm.getId());
        generated.setProviderId(ID);
        generated.setProviderType(KeyProvider.class.getName());

        MultivaluedHashMap<String, String> config = new MultivaluedHashMap<>();
        config.putSingle(Attributes.PRIORITY_KEY, "-100");
        config.putSingle(Attributes.ALGORITHM_KEY, algorithm);
        generated.setConfig(config);

        realm.addComponentModel(generated);

        return true;
    } else {
        return false;
    }
}
 
Example 7
Source File: GeneratedRsaKeyProviderFactory.java    From keycloak with Apache License 2.0 6 votes vote down vote up
@Override
public boolean createFallbackKeys(KeycloakSession session, KeyUse keyUse, String algorithm) {
    if (keyUse.equals(KeyUse.SIG) && isSupportedRsaAlgorithm(algorithm)) {
        RealmModel realm = session.getContext().getRealm();

        ComponentModel generated = new ComponentModel();
        generated.setName("fallback-" + algorithm);
        generated.setParentId(realm.getId());
        generated.setProviderId(ID);
        generated.setProviderType(KeyProvider.class.getName());

        MultivaluedHashMap<String, String> config = new MultivaluedHashMap<>();
        config.putSingle(Attributes.PRIORITY_KEY, "-100");
        config.putSingle(Attributes.ALGORITHM_KEY, algorithm);
        generated.setConfig(config);

        realm.addComponentModel(generated);

        return true;
    } else {
        return false;
    }
}
 
Example 8
Source File: GeneratedEcdsaKeyProviderFactory.java    From keycloak with Apache License 2.0 6 votes vote down vote up
@Override
public boolean createFallbackKeys(KeycloakSession session, KeyUse keyUse, String algorithm) {
    if (keyUse.equals(KeyUse.SIG) && (algorithm.equals(Algorithm.ES256) || algorithm.equals(Algorithm.ES384) || algorithm.equals(Algorithm.ES512))) {
        RealmModel realm = session.getContext().getRealm();

        ComponentModel generated = new ComponentModel();
        generated.setName("fallback-" + algorithm);
        generated.setParentId(realm.getId());
        generated.setProviderId(ID);
        generated.setProviderType(KeyProvider.class.getName());

        MultivaluedHashMap<String, String> config = new MultivaluedHashMap<>();
        config.putSingle(Attributes.PRIORITY_KEY, "-100");
        config.putSingle(ECDSA_ELLIPTIC_CURVE_KEY, convertAlgorithmToECDomainParmNistRep(algorithm));
        generated.setConfig(config);

        realm.addComponentModel(generated);

        return true;
    } else {
        return false;
    }
}
 
Example 9
Source File: DefaultKeyProviders.java    From keycloak with Apache License 2.0 5 votes vote down vote up
public static void createSecretProvider(RealmModel realm) {
    if (hasProvider(realm, "hmac-generated")) return;
    ComponentModel generated = new ComponentModel();
    generated.setName("hmac-generated");
    generated.setParentId(realm.getId());
    generated.setProviderId("hmac-generated");
    generated.setProviderType(KeyProvider.class.getName());

    MultivaluedHashMap<String, String> config = new MultivaluedHashMap<>();
    config.putSingle("priority", "100");
    config.putSingle("algorithm", Algorithm.HS256);
    generated.setConfig(config);

    realm.addComponentModel(generated);
}
 
Example 10
Source File: DefaultKeyProviders.java    From keycloak with Apache License 2.0 5 votes vote down vote up
public static void createAesProvider(RealmModel realm) {
    if (hasProvider(realm, "aes-generated")) return;
    ComponentModel generated = new ComponentModel();
    generated.setName("aes-generated");
    generated.setParentId(realm.getId());
    generated.setProviderId("aes-generated");
    generated.setProviderType(KeyProvider.class.getName());

    MultivaluedHashMap<String, String> config = new MultivaluedHashMap<>();
    config.putSingle("priority", "100");
    generated.setConfig(config);

    realm.addComponentModel(generated);
}
 
Example 11
Source File: RepresentationToModel.java    From keycloak with Apache License 2.0 5 votes vote down vote up
public static ComponentModel convertFedMapperToComponent(RealmModel realm, ComponentModel parent, UserFederationMapperRepresentation rep, String newMapperType) {
    ComponentModel mapper = new ComponentModel();
    mapper.setId(rep.getId());
    mapper.setName(rep.getName());
    mapper.setProviderId(rep.getFederationMapperType());
    mapper.setProviderType(newMapperType);
    mapper.setParentId(parent.getId());
    if (rep.getConfig() != null) {
        for (Map.Entry<String, String> entry : rep.getConfig().entrySet()) {
            mapper.getConfig().putSingle(entry.getKey(), entry.getValue());
        }
    }
    return mapper;
}
 
Example 12
Source File: RepresentationToModel.java    From keycloak with Apache License 2.0 5 votes vote down vote up
public static ComponentModel toModel(KeycloakSession session, ComponentRepresentation rep) {
    ComponentModel model = new ComponentModel();
    model.setId(rep.getId());
    model.setParentId(rep.getParentId());
    model.setProviderType(rep.getProviderType());
    model.setProviderId(rep.getProviderId());
    model.setConfig(new MultivaluedHashMap<>());
    model.setName(rep.getName());
    model.setSubType(rep.getSubType());

    if (rep.getConfig() != null) {
        Set<String> keys = new HashSet<>(rep.getConfig().keySet());
        for (String k : keys) {
            List<String> values = rep.getConfig().get(k);
            if (values != null) {
                ListIterator<String> itr = values.listIterator();
                while (itr.hasNext()) {
                    String v = itr.next();
                    if (v == null || v.trim().isEmpty()) {
                        itr.remove();
                    }
                }

                if (!values.isEmpty()) {
                    model.getConfig().put(k, values);
                }
            }
        }
    }

    return model;
}
 
Example 13
Source File: RealmAdapter.java    From keycloak with Apache License 2.0 5 votes vote down vote up
protected ComponentModel entityToModel(ComponentEntity c) {
    ComponentModel model = new ComponentModel();
    model.setId(c.getId());
    model.setName(c.getName());
    model.setProviderType(c.getProviderType());
    model.setProviderId(c.getProviderId());
    model.setSubType(c.getSubType());
    model.setParentId(c.getParentId());
    MultivaluedHashMap<String, String> config = new MultivaluedHashMap<>();
    for (ComponentConfigEntity configEntity : c.getComponentConfigs()) {
        config.add(configEntity.getName(), configEntity.getValue());
    }
    model.setConfig(config);
    return model;
}
 
Example 14
Source File: DefaultClientRegistrationPolicies.java    From keycloak with Apache License 2.0 5 votes vote down vote up
private static ComponentModel createModelInstance(String name, RealmModel realm, String providerId, String policyType) {
    ComponentModel model = new ComponentModel();
    model.setName(name);
    model.setParentId(realm.getId());
    model.setProviderId(providerId);
    model.setProviderType(ClientRegistrationPolicy.class.getName());
    model.setSubType(policyType);
    return model;
}
 
Example 15
Source File: RepresentationToModel.java    From keycloak with Apache License 2.0 4 votes vote down vote up
public static void updateComponent(KeycloakSession session, ComponentRepresentation rep, ComponentModel component, boolean internal) {
    if (rep.getName() != null) {
        component.setName(rep.getName());
    }

    if (rep.getParentId() != null) {
        component.setParentId(rep.getParentId());
    }

    if (rep.getProviderType() != null) {
        component.setProviderType(rep.getProviderType());
    }

    if (rep.getProviderId() != null) {
        component.setProviderId(rep.getProviderId());
    }

    if (rep.getSubType() != null) {
        component.setSubType(rep.getSubType());
    }

    Map<String, ProviderConfigProperty> providerConfiguration = null;
    if (!internal) {
        providerConfiguration = ComponentUtil.getComponentConfigProperties(session, component);
    }

    if (rep.getConfig() != null) {
        Set<String> keys = new HashSet<>(rep.getConfig().keySet());
        for (String k : keys) {
            if (!internal && !providerConfiguration.containsKey(k)) {
                break;
            }

            List<String> values = rep.getConfig().get(k);
            if (values == null || values.isEmpty() || values.get(0) == null || values.get(0).trim().isEmpty()) {
                component.getConfig().remove(k);
            } else {
                ListIterator<String> itr = values.listIterator();
                while (itr.hasNext()) {
                    String v = itr.next();
                    if (v == null || v.trim().isEmpty() || v.equals(ComponentRepresentation.SECRET_VALUE)) {
                        itr.remove();
                    }
                }

                if (!values.isEmpty()) {
                    component.getConfig().put(k, values);
                }
            }
        }
    }
}