Search in sources :

Example 6 with ComponentValidationException

use of org.keycloak.component.ComponentValidationException in project keycloak by keycloak.

the class ComponentResource method create.

@POST
@Consumes(MediaType.APPLICATION_JSON)
public Response create(ComponentRepresentation rep) {
    auth.realm().requireManageRealm();
    try {
        ComponentModel model = RepresentationToModel.toModel(session, rep);
        if (model.getParentId() == null)
            model.setParentId(realm.getId());
        model = realm.addComponentModel(model);
        adminEvent.operation(OperationType.CREATE).resourcePath(session.getContext().getUri(), model.getId()).representation(StripSecretsUtils.strip(session, rep)).success();
        return Response.created(session.getContext().getUri().getAbsolutePathBuilder().path(model.getId()).build()).build();
    } catch (ComponentValidationException e) {
        return localizedErrorResponse(e);
    } catch (IllegalArgumentException e) {
        throw new BadRequestException(e);
    }
}
Also used : ComponentValidationException(org.keycloak.component.ComponentValidationException) ComponentModel(org.keycloak.component.ComponentModel) BadRequestException(javax.ws.rs.BadRequestException) POST(javax.ws.rs.POST) Consumes(javax.ws.rs.Consumes)

Example 7 with ComponentValidationException

use of org.keycloak.component.ComponentValidationException in project keycloak by keycloak.

the class ComponentResource method updateComponent.

@PUT
@Path("{id}")
@Consumes(MediaType.APPLICATION_JSON)
public Response updateComponent(@PathParam("id") String id, ComponentRepresentation rep) {
    auth.realm().requireManageRealm();
    try {
        ComponentModel model = realm.getComponent(id);
        if (model == null) {
            throw new NotFoundException("Could not find component");
        }
        RepresentationToModel.updateComponent(session, rep, model, false);
        adminEvent.operation(OperationType.UPDATE).resourcePath(session.getContext().getUri()).representation(StripSecretsUtils.strip(session, rep)).success();
        realm.updateComponent(model);
        return Response.noContent().build();
    } catch (ComponentValidationException e) {
        return localizedErrorResponse(e);
    } catch (IllegalArgumentException e) {
        throw new BadRequestException();
    }
}
Also used : ComponentValidationException(org.keycloak.component.ComponentValidationException) ComponentModel(org.keycloak.component.ComponentModel) NotFoundException(javax.ws.rs.NotFoundException) BadRequestException(javax.ws.rs.BadRequestException) Path(javax.ws.rs.Path) Consumes(javax.ws.rs.Consumes) PUT(javax.ws.rs.PUT)

Example 8 with ComponentValidationException

use of org.keycloak.component.ComponentValidationException in project keycloak by keycloak.

the class UserProfileTest method testInvalidConfiguration.

private static void testInvalidConfiguration(KeycloakSession session) {
    DeclarativeUserProfileProvider provider = getDynamicUserProfileProvider(session);
    try {
        provider.setConfiguration("{\"validateConfigAttribute\": true}");
        fail("Should fail validation");
    } catch (ComponentValidationException ve) {
    // OK
    }
}
Also used : ComponentValidationException(org.keycloak.component.ComponentValidationException) DeclarativeUserProfileProvider(org.keycloak.userprofile.DeclarativeUserProfileProvider)

Example 9 with ComponentValidationException

use of org.keycloak.component.ComponentValidationException in project keycloak by keycloak.

the class UserProfileTest method testConfigurationInvalidScope.

private static void testConfigurationInvalidScope(KeycloakSession session) throws IOException {
    RealmModel realm = session.getContext().getRealm();
    DeclarativeUserProfileProvider provider = getDynamicUserProfileProvider(session);
    ComponentModel component = provider.getComponentModel();
    assertNotNull(component);
    UPConfig config = new UPConfig();
    UPAttribute attribute = new UPAttribute();
    attribute.setName(ATT_ADDRESS);
    UPAttributeRequired requirements = new UPAttributeRequired();
    requirements.setScopes(Collections.singleton("invalid"));
    attribute.setRequired(requirements);
    attribute.setSelector(new UPAttributeSelector());
    attribute.getSelector().setScopes(Collections.singleton("invalid"));
    config.addAttribute(attribute);
    try {
        provider.setConfiguration(JsonSerialization.writeValueAsString(config));
        Assert.fail("Expected to fail due to invalid client scope");
    } catch (ComponentValidationException cve) {
    // ignore
    }
}
Also used : RealmModel(org.keycloak.models.RealmModel) ComponentValidationException(org.keycloak.component.ComponentValidationException) DeclarativeUserProfileProvider(org.keycloak.userprofile.DeclarativeUserProfileProvider) UPConfig(org.keycloak.userprofile.config.UPConfig) ComponentModel(org.keycloak.component.ComponentModel) UPAttributeRequired(org.keycloak.userprofile.config.UPAttributeRequired) UPAttribute(org.keycloak.userprofile.config.UPAttribute) UPAttributeSelector(org.keycloak.userprofile.config.UPAttributeSelector)

Example 10 with ComponentValidationException

use of org.keycloak.component.ComponentValidationException in project keycloak by keycloak.

the class LDAPStorageProviderFactory method validateConfiguration.

@Override
public void validateConfiguration(KeycloakSession session, RealmModel realm, ComponentModel config) throws ComponentValidationException {
    LDAPConfig cfg = new LDAPConfig(config.getConfig());
    UserStorageProviderModel userStorageModel = new UserStorageProviderModel(config);
    String customFilter = cfg.getCustomUserSearchFilter();
    LDAPUtils.validateCustomLdapFilter(customFilter);
    String connectionTimeout = cfg.getConnectionTimeout();
    if (connectionTimeout != null && !connectionTimeout.isEmpty()) {
        try {
            Long.parseLong(connectionTimeout);
        } catch (NumberFormatException nfe) {
            throw new ComponentValidationException("ldapErrorConnectionTimeoutNotNumber");
        }
    }
    String readTimeout = cfg.getReadTimeout();
    if (readTimeout != null && !readTimeout.isEmpty()) {
        try {
            Long.parseLong(readTimeout);
        } catch (NumberFormatException nfe) {
            throw new ComponentValidationException("ldapErrorReadTimeoutNotNumber");
        }
    }
    if (cfg.isStartTls() && cfg.getConnectionPooling() != null) {
        throw new ComponentValidationException("ldapErrorCantEnableStartTlsAndConnectionPooling");
    }
    // editMode is mandatory
    if (config.get(LDAPConstants.EDIT_MODE) == null) {
        throw new ComponentValidationException("ldapErrorEditModeMandatory");
    }
    // validatePasswordPolicy applicable only for WRITABLE mode
    if (cfg.getEditMode() != UserStorageProvider.EditMode.WRITABLE) {
        if (cfg.isValidatePasswordPolicy()) {
            throw new ComponentValidationException("ldapErrorValidatePasswordPolicyAvailableForWritableOnly");
        }
    }
    if (!userStorageModel.isImportEnabled() && cfg.getEditMode() == UserStorageProvider.EditMode.UNSYNCED) {
        throw new ComponentValidationException("ldapErrorCantEnableUnsyncedAndImportOff");
    }
}
Also used : ComponentValidationException(org.keycloak.component.ComponentValidationException) UserStorageProviderModel(org.keycloak.storage.UserStorageProviderModel)

Aggregations

ComponentValidationException (org.keycloak.component.ComponentValidationException)24 ComponentModel (org.keycloak.component.ComponentModel)5 KeyPair (java.security.KeyPair)3 Consumes (javax.ws.rs.Consumes)3 DeclarativeUserProfileProvider (org.keycloak.userprofile.DeclarativeUserProfileProvider)3 Certificate (java.security.cert.Certificate)2 BadRequestException (javax.ws.rs.BadRequestException)2 PUT (javax.ws.rs.PUT)2 RealmModel (org.keycloak.models.RealmModel)2 UPConfig (org.keycloak.userprofile.config.UPConfig)2 ByteArrayInputStream (java.io.ByteArrayInputStream)1 File (java.io.File)1 IOException (java.io.IOException)1 KeyFactory (java.security.KeyFactory)1 PrivateKey (java.security.PrivateKey)1 PublicKey (java.security.PublicKey)1 ECPublicKey (java.security.interfaces.ECPublicKey)1 X509EncodedKeySpec (java.security.spec.X509EncodedKeySpec)1 NotFoundException (javax.ws.rs.NotFoundException)1 POST (javax.ws.rs.POST)1