Search in sources :

Example 21 with UPConfig

use of org.keycloak.userprofile.config.UPConfig in project keycloak by keycloak.

the class UPConfigParserTest method validateConfiguration_attributePermissionsErrors.

public static void validateConfiguration_attributePermissionsErrors(KeycloakSession session) throws IOException {
    UPConfig config = loadValidConfig();
    // we run this test without KeycloakSession so validator configs are not validated here
    UPAttribute attConfig = config.getAttributes().get(1);
    // no permissions configures at all
    attConfig.setPermissions(null);
    List<String> errors = validate(session, config);
    Assert.assertEquals(0, errors.size());
    // no permissions structure fields configured
    UPAttributePermissions permsConfig = new UPAttributePermissions();
    attConfig.setPermissions(permsConfig);
    errors = validate(session, config);
    Assert.assertTrue(errors.isEmpty());
    // valid if both are present, even empty
    permsConfig.setEdit(Collections.emptySet());
    permsConfig.setView(Collections.emptySet());
    attConfig.setPermissions(permsConfig);
    errors = validate(session, config);
    Assert.assertEquals(0, errors.size());
    Set<String> withInvRole = Collections.singleton("invalid");
    // invalid role used for view
    permsConfig.setView(withInvRole);
    errors = validate(session, config);
    Assert.assertEquals(1, errors.size());
    // invalid role used for edit also
    permsConfig.setEdit(withInvRole);
    errors = validate(session, config);
    Assert.assertEquals(2, errors.size());
}
Also used : UPAttributePermissions(org.keycloak.userprofile.config.UPAttributePermissions) UPConfig(org.keycloak.userprofile.config.UPConfig) UPAttribute(org.keycloak.userprofile.config.UPAttribute)

Example 22 with UPConfig

use of org.keycloak.userprofile.config.UPConfig in project keycloak by keycloak.

the class UPConfigParserTest method validateConfiguration_attributeRequirementsErrors.

public static void validateConfiguration_attributeRequirementsErrors(KeycloakSession session) throws IOException {
    UPConfig config = loadValidConfig();
    // we run this test without KeycloakSession so validator configs are not validated here
    UPAttribute attConfig = config.getAttributes().get(1);
    // it is OK without requirements configures at all
    attConfig.setRequired(null);
    List<String> errors = validate(session, config);
    Assert.assertEquals(0, errors.size());
    // it is OK with empty config as it means ALWAYS required
    UPAttributeRequired reqConfig = new UPAttributeRequired();
    attConfig.setRequired(reqConfig);
    errors = validate(session, config);
    Assert.assertEquals(0, errors.size());
    Assert.assertTrue(reqConfig.isAlways());
    // invalid role used
    reqConfig.setRoles(Collections.singleton("invalid"));
    errors = validate(session, config);
    Assert.assertEquals(1, errors.size());
    Assert.assertFalse(reqConfig.isAlways());
}
Also used : UPConfig(org.keycloak.userprofile.config.UPConfig) UPAttributeRequired(org.keycloak.userprofile.config.UPAttributeRequired) UPAttribute(org.keycloak.userprofile.config.UPAttribute)

Example 23 with UPConfig

use of org.keycloak.userprofile.config.UPConfig in project keycloak by keycloak.

the class UPConfigParserTest method loadConfigurationFromJsonFile.

@Test
public void loadConfigurationFromJsonFile() throws IOException {
    UPConfig config = readConfig(getValidConfigFileIS());
    // only basic assertion to check config is loaded, more detailed tests follow
    Assert.assertEquals(5, config.getAttributes().size());
}
Also used : UPConfig(org.keycloak.userprofile.config.UPConfig) Test(org.junit.Test) AbstractTestRealmKeycloakTest(org.keycloak.testsuite.AbstractTestRealmKeycloakTest)

Example 24 with UPConfig

use of org.keycloak.userprofile.config.UPConfig in project keycloak by keycloak.

the class DeclarativeUserProfileProvider method getParsedConfig.

/**
 * Get parsed config file configured in model. Default one used if not configured.
 *
 * @param model to take config from
 * @return parsed configuration
 */
protected UPConfig getParsedConfig(ComponentModel model) {
    String rawConfig = getConfigJsonFromComponentModel(model);
    if (!isBlank(rawConfig)) {
        try {
            UPConfig upc = readConfig(new ByteArrayInputStream(rawConfig.getBytes("UTF-8")));
            // validate configuration to catch things like changed/removed validators etc, and warn early and clearly about this problem
            List<String> errors = UPConfigUtils.validate(session, upc);
            if (!errors.isEmpty()) {
                throw new RuntimeException("UserProfile configuration for realm '" + session.getContext().getRealm().getName() + "' is invalid: " + errors.toString());
            }
            return upc;
        } catch (IOException e) {
            throw new RuntimeException("UserProfile configuration for realm '" + session.getContext().getRealm().getName() + "' is invalid:" + e.getMessage(), e);
        }
    }
    return null;
}
Also used : ByteArrayInputStream(java.io.ByteArrayInputStream) UPConfig(org.keycloak.userprofile.config.UPConfig) IOException(java.io.IOException)

Example 25 with UPConfig

use of org.keycloak.userprofile.config.UPConfig in project keycloak by keycloak.

the class DeclarativeUserProfileProvider method validateConfiguration.

@Override
public void validateConfiguration(KeycloakSession session, RealmModel realm, ComponentModel model) throws ComponentValidationException {
    String upConfigJson = getConfigJsonFromComponentModel(model);
    if (!isBlank(upConfigJson)) {
        try {
            UPConfig upc = readConfig(new ByteArrayInputStream(upConfigJson.getBytes("UTF-8")));
            List<String> errors = UPConfigUtils.validate(session, upc);
            if (!errors.isEmpty()) {
                throw new ComponentValidationException(errors.toString());
            }
        } catch (IOException e) {
            throw new ComponentValidationException(e.getMessage(), e);
        }
    }
    // throught #configureUserProfile(metadata, session)
    if (model != null) {
        model.removeNote(PARSED_CONFIG_COMPONENT_KEY);
    }
}
Also used : ComponentValidationException(org.keycloak.component.ComponentValidationException) ByteArrayInputStream(java.io.ByteArrayInputStream) UPConfig(org.keycloak.userprofile.config.UPConfig) IOException(java.io.IOException)

Aggregations

UPConfig (org.keycloak.userprofile.config.UPConfig)26 UPAttribute (org.keycloak.userprofile.config.UPAttribute)22 HashMap (java.util.HashMap)13 ComponentValidationException (org.keycloak.component.ComponentValidationException)13 DeclarativeUserProfileProvider (org.keycloak.userprofile.DeclarativeUserProfileProvider)12 ComponentModel (org.keycloak.component.ComponentModel)11 UserProfile (org.keycloak.userprofile.UserProfile)11 UPAttributePermissions (org.keycloak.userprofile.config.UPAttributePermissions)11 ValidationException (org.keycloak.userprofile.ValidationException)10 UPAttributeRequired (org.keycloak.userprofile.config.UPAttributeRequired)10 ByteArrayInputStream (java.io.ByteArrayInputStream)3 IOException (java.io.IOException)3 RealmModel (org.keycloak.models.RealmModel)3 UserModel (org.keycloak.models.UserModel)3 UPGroup (org.keycloak.userprofile.config.UPGroup)3 Map (java.util.Map)2 Test (org.junit.Test)2 AbstractTestRealmKeycloakTest (org.keycloak.testsuite.AbstractTestRealmKeycloakTest)2 UserProfileProvider (org.keycloak.userprofile.UserProfileProvider)2 UPAttributeSelector (org.keycloak.userprofile.config.UPAttributeSelector)2