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());
}
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());
}
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());
}
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;
}
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);
}
}
Aggregations