Java Code Examples for org.keycloak.common.util.MultivaluedHashMap#putSingle()

The following examples show how to use org.keycloak.common.util.MultivaluedHashMap#putSingle() . 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: 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 4
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 5
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 6
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 7
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 8
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 9
Source File: TestLDAPResource.java    From keycloak with Apache License 2.0 5 votes vote down vote up
/**
 * @param ldapCfg configuration of LDAP provider
 * @param importEnabled specify if LDAP provider will have import enabled
 * @return ID of newly created provider
 */
@POST
@Path("/create-ldap-provider")
@Produces(MediaType.APPLICATION_JSON)
@Consumes(MediaType.APPLICATION_JSON)
public String createLDAPProvider(Map<String,String> ldapCfg, @QueryParam("import") boolean importEnabled) {
    MultivaluedHashMap<String, String> ldapConfig = toComponentConfig(ldapCfg);
    ldapConfig.putSingle(LDAPConstants.SYNC_REGISTRATIONS, "true");
    ldapConfig.putSingle(LDAPConstants.EDIT_MODE, UserStorageProvider.EditMode.WRITABLE.toString());

    UserStorageProviderModel model = new UserStorageProviderModel();
    model.setLastSync(0);
    model.setChangedSyncPeriod(-1);
    model.setFullSyncPeriod(-1);
    model.setName("test-ldap");
    model.setPriority(0);
    model.setProviderId(LDAPStorageProviderFactory.PROVIDER_NAME);
    model.setConfig(ldapConfig);

    model.setImportEnabled(importEnabled);

    model.setCachePolicy(UserStorageProviderModel.CachePolicy.MAX_LIFESPAN);
    model.setMaxLifespan(600000); // Lifetime is 10 minutes

    ComponentModel ldapModel = realm.addComponentModel(model);
    return ldapModel.getId();
}
 
Example 10
Source File: LDAPServerCapabilitiesManager.java    From keycloak with Apache License 2.0 5 votes vote down vote up
public static LDAPConfig buildLDAPConfig(TestLdapConnectionRepresentation config, RealmModel realm) {
    String bindCredential = config.getBindCredential();
    if (config.getComponentId() != null && ComponentRepresentation.SECRET_VALUE.equals(bindCredential)) {
        bindCredential = realm.getComponent(config.getComponentId()).getConfig().getFirst(LDAPConstants.BIND_CREDENTIAL);
    }
    MultivaluedHashMap<String, String> configMap = new MultivaluedHashMap<>();
    configMap.putSingle(LDAPConstants.AUTH_TYPE, config.getAuthType());
    configMap.putSingle(LDAPConstants.BIND_DN, config.getBindDn());
    configMap.putSingle(LDAPConstants.BIND_CREDENTIAL, bindCredential);
    configMap.add(LDAPConstants.CONNECTION_URL, config.getConnectionUrl());
    configMap.add(LDAPConstants.USE_TRUSTSTORE_SPI, config.getUseTruststoreSpi());
    configMap.putSingle(LDAPConstants.CONNECTION_TIMEOUT, config.getConnectionTimeout());
    configMap.add(LDAPConstants.START_TLS, config.getStartTls());
    return new LDAPConfig(configMap);
}
 
Example 11
Source File: MigrateTo1_3_0.java    From keycloak with Apache License 2.0 4 votes vote down vote up
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);
                }
            }
        }
    }
}
 
Example 12
Source File: ClusterProviderTaskCommand.java    From keycloak with Apache License 2.0 4 votes vote down vote up
private void updateConfig(MultivaluedHashMap<String, String> cfg, int waitTime) {
    cfg.putSingle("wait-time", String.valueOf(waitTime));
}
 
Example 13
Source File: SyncDummyFederationProviderCommand.java    From keycloak with Apache License 2.0 4 votes vote down vote up
private void updateConfig(MultivaluedHashMap<String, String> cfg, int waitTime) {
    cfg.putSingle("wait-time", String.valueOf(waitTime));
}