Search in sources :

Example 1 with IdentityApplicationManagementValidationException

use of org.wso2.carbon.identity.application.common.IdentityApplicationManagementValidationException in project carbon-identity-framework by wso2.

the class ApplicationManagementServiceImpl method updateApplicationTemplate.

@Override
public void updateApplicationTemplate(String oldTemplateName, SpTemplate spTemplate, String tenantDomain) throws IdentityApplicationManagementException {
    try {
        validateSPTemplateExists(oldTemplateName, spTemplate, tenantDomain);
        ServiceProvider serviceProvider = unmarshalSPTemplate(spTemplate.getContent());
        validateUnsupportedTemplateConfigs(serviceProvider);
        applicationValidatorManager.validateSPConfigurations(serviceProvider, tenantDomain, CarbonContext.getThreadLocalCarbonContext().getUsername());
        Collection<ApplicationMgtListener> listeners = getApplicationMgtListeners();
        for (ApplicationMgtListener listener : listeners) {
            if (listener.isEnable()) {
                listener.doPreUpdateApplicationTemplate(serviceProvider, tenantDomain);
            }
        }
        doUpdateApplicationTemplate(oldTemplateName, spTemplate, tenantDomain);
    } catch (IdentityApplicationManagementValidationException e) {
        log.error("Validation error when updating the application template: " + oldTemplateName + " in:" + tenantDomain);
        logValidationErrorMessages(e);
        throw new IdentityApplicationManagementClientException(e.getValidationMsg());
    } catch (IdentityApplicationManagementException e) {
        String errorMsg = String.format("Error in updating the application template: %s in tenant: %s", oldTemplateName, tenantDomain);
        throw new IdentityApplicationManagementException(errorMsg, e);
    }
}
Also used : IdentityApplicationManagementClientException(org.wso2.carbon.identity.application.common.IdentityApplicationManagementClientException) ServiceProvider(org.wso2.carbon.identity.application.common.model.ServiceProvider) IdentityApplicationManagementException(org.wso2.carbon.identity.application.common.IdentityApplicationManagementException) AbstractApplicationMgtListener(org.wso2.carbon.identity.application.mgt.listener.AbstractApplicationMgtListener) ApplicationMgtListener(org.wso2.carbon.identity.application.mgt.listener.ApplicationMgtListener) IdentityApplicationManagementValidationException(org.wso2.carbon.identity.application.common.IdentityApplicationManagementValidationException)

Example 2 with IdentityApplicationManagementValidationException

use of org.wso2.carbon.identity.application.common.IdentityApplicationManagementValidationException in project carbon-identity-framework by wso2.

the class ApplicationManagementServiceImpl method unmarshalSPTemplate.

private ServiceProvider unmarshalSPTemplate(String spTemplateXml) throws IdentityApplicationManagementValidationException {
    if (StringUtils.isEmpty(spTemplateXml)) {
        throw new IdentityApplicationManagementValidationException(new String[] { "Empty SP template configuration" + " is provided." });
    }
    try {
        SAXParserFactory spf = SAXParserFactory.newInstance();
        spf.setNamespaceAware(true);
        spf.setXIncludeAware(false);
        try {
            spf.setFeature(Constants.SAX_FEATURE_PREFIX + Constants.EXTERNAL_GENERAL_ENTITIES_FEATURE, false);
            spf.setFeature(Constants.SAX_FEATURE_PREFIX + Constants.EXTERNAL_PARAMETER_ENTITIES_FEATURE, false);
            spf.setFeature(Constants.XERCES_FEATURE_PREFIX + Constants.LOAD_EXTERNAL_DTD_FEATURE, false);
            spf.setFeature(XMLConstants.FEATURE_SECURE_PROCESSING, true);
        } catch (SAXException | ParserConfigurationException e) {
            log.error("Failed to load XML Processor Feature " + Constants.EXTERNAL_GENERAL_ENTITIES_FEATURE + " or " + Constants.EXTERNAL_PARAMETER_ENTITIES_FEATURE + " or " + Constants.LOAD_EXTERNAL_DTD_FEATURE + " or secure-processing.");
        }
        JAXBContext jc = JAXBContext.newInstance(ServiceProvider.class);
        UnmarshallerHandler unmarshallerHandler = jc.createUnmarshaller().getUnmarshallerHandler();
        SAXParser sp = spf.newSAXParser();
        XMLReader xr = sp.getXMLReader();
        xr.setContentHandler(unmarshallerHandler);
        ByteArrayInputStream inputStream = new ByteArrayInputStream(spTemplateXml.getBytes(StandardCharsets.UTF_8));
        InputSource inputSource = new InputSource(inputStream);
        xr.parse(inputSource);
        inputStream.close();
        return (ServiceProvider) unmarshallerHandler.getResult();
    } catch (JAXBException | SAXException | ParserConfigurationException | IOException e) {
        String msg = "Error in reading Service Provider template configuration.";
        log.error(msg, e);
        throw new IdentityApplicationManagementValidationException(new String[] { msg });
    }
}
Also used : InputSource(org.xml.sax.InputSource) JAXBException(javax.xml.bind.JAXBException) JAXBContext(javax.xml.bind.JAXBContext) UnmarshallerHandler(javax.xml.bind.UnmarshallerHandler) IOException(java.io.IOException) IdentityApplicationManagementValidationException(org.wso2.carbon.identity.application.common.IdentityApplicationManagementValidationException) SAXException(org.xml.sax.SAXException) ByteArrayInputStream(java.io.ByteArrayInputStream) ServiceProvider(org.wso2.carbon.identity.application.common.model.ServiceProvider) SAXParser(javax.xml.parsers.SAXParser) ParserConfigurationException(javax.xml.parsers.ParserConfigurationException) XMLReader(org.xml.sax.XMLReader) SAXParserFactory(javax.xml.parsers.SAXParserFactory)

Example 3 with IdentityApplicationManagementValidationException

use of org.wso2.carbon.identity.application.common.IdentityApplicationManagementValidationException in project identity-inbound-auth-oauth by wso2-extensions.

the class OAuthApplicationMgtListener method validateOAuthInbound.

/**
 * Validate Oauth inbound config.
 *
 * @param serviceProvider service provider.
 * @param isUpdate        whether the application update or create
 * @throws IdentityApplicationManagementValidationException Identity Application Management Exception
 */
private void validateOAuthInbound(ServiceProvider serviceProvider, boolean isUpdate) throws IdentityApplicationManagementValidationException {
    List<String> validationMsg = new ArrayList<>();
    if (serviceProvider.getInboundAuthenticationConfig() != null && serviceProvider.getInboundAuthenticationConfig().getInboundAuthenticationRequestConfigs() != null) {
        for (InboundAuthenticationRequestConfig authConfig : serviceProvider.getInboundAuthenticationConfig().getInboundAuthenticationRequestConfigs()) {
            if (OAUTH.equals(authConfig.getInboundAuthType()) || OAUTH2.equals(authConfig.getInboundAuthType())) {
                String inboundConfiguration = authConfig.getInboundConfiguration();
                if (inboundConfiguration == null) {
                    return;
                }
                String inboundAuthKey = authConfig.getInboundAuthKey();
                OAuthAppDAO dao = new OAuthAppDAO();
                OAuthAppDO oAuthAppDO;
                String tenantDomain = serviceProvider.getOwner().getTenantDomain();
                String userName = serviceProvider.getOwner().getUserName();
                try {
                    oAuthAppDO = marshelOAuthDO(inboundConfiguration, serviceProvider.getApplicationName(), tenantDomain);
                } catch (IdentityApplicationManagementException e) {
                    validationMsg.add("OAuth inbound configuration in the file is not valid.");
                    break;
                }
                if (!inboundAuthKey.equals(oAuthAppDO.getOauthConsumerKey())) {
                    validationMsg.add(String.format("The Inbound Auth Key of the  application name %s " + "is not match with Oauth Consumer Key %s.", authConfig.getInboundAuthKey(), oAuthAppDO.getOauthConsumerKey()));
                }
                try {
                    if (!isUpdate) {
                        if (dao.isDuplicateConsumer(inboundAuthKey)) {
                            validationMsg.add(String.format("An OAuth application already exists with %s as " + "consumer key", inboundAuthKey));
                            break;
                        } else if (dao.isDuplicateApplication(userName, IdentityTenantUtil.getTenantId(tenantDomain), tenantDomain, oAuthAppDO)) {
                            validationMsg.add(String.format("An OAuth application already exists with %s as " + "consumer key", oAuthAppDO.getApplicationName()));
                            break;
                        }
                    }
                } catch (IdentityOAuthAdminException e) {
                // Do nothing, the key does exists.
                }
                if (oAuthAppDO.getGrantTypes() != null && (oAuthAppDO.getGrantTypes().contains(OAuthConstants.GrantTypes.AUTHORIZATION_CODE) || oAuthAppDO.getGrantTypes().contains(OAuthConstants.GrantTypes.IMPLICIT)) && StringUtils.isEmpty(oAuthAppDO.getCallbackUrl())) {
                    validationMsg.add("Callback Url is required for Code or Implicit grant types");
                }
                validateScopeValidators(oAuthAppDO.getScopeValidators(), validationMsg);
                if (OAuthConstants.OAuthVersions.VERSION_2.equals(oAuthAppDO.getOauthVersion())) {
                    validateGrants(oAuthAppDO.getGrantTypes().split("\\s"), validationMsg);
                }
                break;
            }
        }
    }
    if (!validationMsg.isEmpty()) {
        throw new IdentityApplicationManagementValidationException(validationMsg.toArray(new String[0]));
    }
}
Also used : IdentityOAuthAdminException(org.wso2.carbon.identity.oauth.IdentityOAuthAdminException) OAuthAppDAO(org.wso2.carbon.identity.oauth.dao.OAuthAppDAO) OAuthAppDO(org.wso2.carbon.identity.oauth.dao.OAuthAppDO) IdentityApplicationManagementException(org.wso2.carbon.identity.application.common.IdentityApplicationManagementException) ArrayList(java.util.ArrayList) InboundAuthenticationRequestConfig(org.wso2.carbon.identity.application.common.model.InboundAuthenticationRequestConfig) IdentityApplicationManagementValidationException(org.wso2.carbon.identity.application.common.IdentityApplicationManagementValidationException)

Example 4 with IdentityApplicationManagementValidationException

use of org.wso2.carbon.identity.application.common.IdentityApplicationManagementValidationException in project carbon-identity-framework by wso2.

the class ApplicationManagementServiceImpl method createApplicationTemplateFromSP.

@Override
public void createApplicationTemplateFromSP(ServiceProvider serviceProvider, SpTemplate spTemplate, String tenantDomain) throws IdentityApplicationManagementException {
    if (serviceProvider != null) {
        try {
            validateSPTemplateExists(spTemplate, tenantDomain);
            ServiceProvider updatedSP = removeUnsupportedTemplateConfigs(serviceProvider);
            applicationValidatorManager.validateSPConfigurations(updatedSP, tenantDomain, CarbonContext.getThreadLocalCarbonContext().getUsername());
            Collection<ApplicationMgtListener> listeners = getApplicationMgtListeners();
            for (ApplicationMgtListener listener : listeners) {
                if (listener.isEnable()) {
                    listener.doPreCreateApplicationTemplate(serviceProvider, tenantDomain);
                }
            }
            String serviceProviderTemplateXml = marshalSPTemplate(updatedSP, tenantDomain);
            spTemplate.setContent(serviceProviderTemplateXml);
            doAddApplicationTemplate(spTemplate, tenantDomain);
        } catch (IdentityApplicationManagementValidationException e) {
            log.error("Validation error when creating the application template:" + spTemplate.getName() + "from service provider: " + serviceProvider.getApplicationName() + " in:" + tenantDomain);
            logValidationErrorMessages(e);
            throw new IdentityApplicationManagementClientException(e.getValidationMsg());
        } catch (IdentityApplicationManagementException e) {
            String errorMsg = String.format("Error when creating the application template: %s from " + "service provider: %s in: ", spTemplate.getName(), serviceProvider.getApplicationName(), tenantDomain);
            throw new IdentityApplicationManagementException(errorMsg, e);
        }
    } else {
        createApplicationTemplate(spTemplate, tenantDomain);
    }
}
Also used : IdentityApplicationManagementClientException(org.wso2.carbon.identity.application.common.IdentityApplicationManagementClientException) ServiceProvider(org.wso2.carbon.identity.application.common.model.ServiceProvider) IdentityApplicationManagementException(org.wso2.carbon.identity.application.common.IdentityApplicationManagementException) AbstractApplicationMgtListener(org.wso2.carbon.identity.application.mgt.listener.AbstractApplicationMgtListener) ApplicationMgtListener(org.wso2.carbon.identity.application.mgt.listener.ApplicationMgtListener) IdentityApplicationManagementValidationException(org.wso2.carbon.identity.application.common.IdentityApplicationManagementValidationException)

Example 5 with IdentityApplicationManagementValidationException

use of org.wso2.carbon.identity.application.common.IdentityApplicationManagementValidationException in project carbon-identity-framework by wso2.

the class ApplicationManagementServiceImpl method createApplicationTemplate.

@Override
public void createApplicationTemplate(SpTemplate spTemplate, String tenantDomain) throws IdentityApplicationManagementException {
    try {
        ServiceProvider serviceProvider = unmarshalSPTemplate(spTemplate.getContent());
        validateSPTemplateExists(spTemplate, tenantDomain);
        validateUnsupportedTemplateConfigs(serviceProvider);
        applicationValidatorManager.validateSPConfigurations(serviceProvider, tenantDomain, CarbonContext.getThreadLocalCarbonContext().getUsername());
        Collection<ApplicationMgtListener> listeners = getApplicationMgtListeners();
        for (ApplicationMgtListener listener : listeners) {
            if (listener.isEnable()) {
                listener.doPreCreateApplicationTemplate(serviceProvider, tenantDomain);
            }
        }
        doAddApplicationTemplate(spTemplate, tenantDomain);
    } catch (IdentityApplicationManagementValidationException e) {
        log.error("Validation error when creating the application template: " + spTemplate.getName() + " in:" + tenantDomain);
        logValidationErrorMessages(e);
        throw new IdentityApplicationManagementClientException(e.getValidationMsg());
    } catch (IdentityApplicationManagementException e) {
        String errorMsg = String.format("Error when creating the application template: %s in tenant: %s", spTemplate.getName(), tenantDomain);
        throw new IdentityApplicationManagementException(errorMsg, e);
    }
}
Also used : IdentityApplicationManagementClientException(org.wso2.carbon.identity.application.common.IdentityApplicationManagementClientException) ServiceProvider(org.wso2.carbon.identity.application.common.model.ServiceProvider) IdentityApplicationManagementException(org.wso2.carbon.identity.application.common.IdentityApplicationManagementException) AbstractApplicationMgtListener(org.wso2.carbon.identity.application.mgt.listener.AbstractApplicationMgtListener) ApplicationMgtListener(org.wso2.carbon.identity.application.mgt.listener.ApplicationMgtListener) IdentityApplicationManagementValidationException(org.wso2.carbon.identity.application.common.IdentityApplicationManagementValidationException)

Aggregations

IdentityApplicationManagementValidationException (org.wso2.carbon.identity.application.common.IdentityApplicationManagementValidationException)6 IdentityApplicationManagementException (org.wso2.carbon.identity.application.common.IdentityApplicationManagementException)4 ServiceProvider (org.wso2.carbon.identity.application.common.model.ServiceProvider)4 IdentityApplicationManagementClientException (org.wso2.carbon.identity.application.common.IdentityApplicationManagementClientException)3 AbstractApplicationMgtListener (org.wso2.carbon.identity.application.mgt.listener.AbstractApplicationMgtListener)3 ApplicationMgtListener (org.wso2.carbon.identity.application.mgt.listener.ApplicationMgtListener)3 ByteArrayInputStream (java.io.ByteArrayInputStream)1 IOException (java.io.IOException)1 ArrayList (java.util.ArrayList)1 JAXBContext (javax.xml.bind.JAXBContext)1 JAXBException (javax.xml.bind.JAXBException)1 UnmarshallerHandler (javax.xml.bind.UnmarshallerHandler)1 ParserConfigurationException (javax.xml.parsers.ParserConfigurationException)1 SAXParser (javax.xml.parsers.SAXParser)1 SAXParserFactory (javax.xml.parsers.SAXParserFactory)1 ImportResponse (org.wso2.carbon.identity.application.common.model.ImportResponse)1 InboundAuthenticationRequestConfig (org.wso2.carbon.identity.application.common.model.InboundAuthenticationRequestConfig)1 IdentityOAuthAdminException (org.wso2.carbon.identity.oauth.IdentityOAuthAdminException)1 OAuthAppDAO (org.wso2.carbon.identity.oauth.dao.OAuthAppDAO)1 OAuthAppDO (org.wso2.carbon.identity.oauth.dao.OAuthAppDO)1