Search in sources :

Example 1 with ApplicationMgtListener

use of org.wso2.carbon.identity.application.mgt.listener.ApplicationMgtListener in project carbon-identity-framework by wso2.

the class ApplicationManagementServiceImpl method importApplication.

private ImportResponse importApplication(ServiceProvider serviceProvider, String tenantDomain, String username, boolean isUpdate) throws IdentityApplicationManagementException {
    Collection<ApplicationMgtListener> listeners = getApplicationMgtListeners();
    ServiceProvider savedSP = null;
    String appName = serviceProvider.getApplicationName();
    try {
        if (isUpdate) {
            savedSP = getApplicationExcludingFileBasedSPs(appName, tenantDomain);
            if (savedSP == null) {
                String errorMsg = String.format("Service provider %s@%s is not found", appName, tenantDomain);
                throw new IdentityApplicationManagementClientException(APPLICATION_NOT_FOUND.getCode(), errorMsg);
            }
        }
        if (!isUpdate) {
            ServiceProvider basicApplication = new ServiceProvider();
            basicApplication.setApplicationName(serviceProvider.getApplicationName());
            basicApplication.setDescription(serviceProvider.getDescription());
            String resourceId = createApplication(basicApplication, tenantDomain, username);
            savedSP = getApplicationByResourceId(resourceId, tenantDomain);
        }
        serviceProvider.setApplicationResourceId(savedSP.getApplicationResourceId());
        serviceProvider.setApplicationID(savedSP.getApplicationID());
        serviceProvider.setOwner(getUser(tenantDomain, username));
        for (ApplicationMgtListener listener : listeners) {
            if (listener.isEnable()) {
                listener.onPreCreateInbound(serviceProvider, isUpdate);
            }
        }
        updateApplication(serviceProvider, tenantDomain, username);
        for (ApplicationMgtListener listener : listeners) {
            if (listener.isEnable()) {
                listener.doImportServiceProvider(serviceProvider);
            }
        }
        ImportResponse importResponse = new ImportResponse();
        if (isUpdate) {
            importResponse.setResponseCode(ImportResponse.UPDATED);
        } else {
            importResponse.setResponseCode(ImportResponse.CREATED);
        }
        importResponse.setApplicationName(appName);
        importResponse.setApplicationResourceId(serviceProvider.getApplicationResourceId());
        importResponse.setErrors(new String[0]);
        return importResponse;
    } catch (IdentityApplicationManagementClientException e) {
        deleteCreatedSP(savedSP, tenantDomain, username, isUpdate);
        return buildImportErrorResponse(e);
    } catch (IdentityApplicationManagementException e) {
        deleteCreatedSP(savedSP, tenantDomain, username, isUpdate);
        String errorMsg = String.format("Error in importing provided service provider %s@%s from file ", appName, tenantDomain);
        throw new IdentityApplicationManagementException(errorMsg, e);
    }
}
Also used : IdentityApplicationManagementClientException(org.wso2.carbon.identity.application.common.IdentityApplicationManagementClientException) ImportResponse(org.wso2.carbon.identity.application.common.model.ImportResponse) 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)

Example 2 with ApplicationMgtListener

use of org.wso2.carbon.identity.application.mgt.listener.ApplicationMgtListener in project carbon-identity-framework by wso2.

the class ApplicationManagementServiceImpl method deleteApplication.

// Will be supported with 'Advance Consent Management Feature'.
/*
    private void validateConsentPurposes(ServiceProvider serviceProvider) throws
            IdentityApplicationManagementException {

        ConsentManager consentManager = ApplicationManagementServiceComponentHolder.getInstance().getConsentManager();
        ConsentConfig consentConfig = serviceProvider.getConsentConfig();
        if (nonNull(consentConfig)) {
            ConsentPurposeConfigs consentPurposeConfigs = consentConfig.getConsentPurposeConfigs();
            if (nonNull(consentPurposeConfigs)) {
                ConsentPurpose[] consentPurposes = consentPurposeConfigs.getConsentPurpose();
                if (nonNull(consentPurposes)) {
                    for (ConsentPurpose consentPurpose : consentPurposes) {
                        int purposeId = consentPurpose.getPurposeId();
                        try {
                            Purpose purpose = consentManager.getPurpose(purposeId);
                            if (isNull(purpose)) {
                                if (log.isDebugEnabled()) {
                                    log.debug("ConsentManager returned null for Purpose ID: " + purposeId);
                                }
                                throw new IdentityApplicationManagementException("Invalid purpose ID: " + purposeId);
                            }

                            if (!isSPSpecificPurpose(serviceProvider, purpose) && !isSharedPurpose(purpose)) {
                                String message = "Purpose: %s with ID: %s is not defined under purposes for SP:" +
                                                 " %s or 'SHARED' purposes.";
                                String error = String.format(message, purpose.getName(), purpose.getId(),
                                                             serviceProvider.getApplicationName());
                                throw new IdentityApplicationManagementException(error);
                            }
                        } catch (ConsentManagementException e) {
                            if (ERROR_CODE_PURPOSE_ID_INVALID.getCode().equals(e.getErrorCode())) {
                                throw new IdentityApplicationManagementException("Invalid purpose ID: " + purposeId, e);
                            }
                            throw new IdentityApplicationManagementException("Error while retrieving consent purpose " +
                                                                             "with ID: " + purposeId, e);
                        }
                    }
                }
            }
        }
    }


    private boolean isSharedPurpose(Purpose purpose) {

        return PURPOSE_GROUP_SHARED.equals(purpose.getGroup()) && PURPOSE_GROUP_TYPE_SYSTEM.equals(
                purpose.getGroupType());
    }

    private boolean isSPSpecificPurpose(ServiceProvider serviceProvider, Purpose purpose) {

        return serviceProvider.getApplicationName().equals(purpose.getGroup())&& PURPOSE_GROUP_TYPE_SP.equals(
                purpose.getGroupType());
    }
    */
@Override
public void deleteApplication(String applicationName, String tenantDomain, String username) throws IdentityApplicationManagementException {
    ServiceProvider serviceProvider;
    // invoking the listeners
    Collection<ApplicationMgtListener> listeners = getApplicationMgtListeners();
    for (ApplicationMgtListener listener : listeners) {
        if (listener.isEnable() && !listener.doPreDeleteApplication(applicationName, tenantDomain, username)) {
            throw buildServerException("Pre Delete application operation of listener: " + getName(listener) + " failed for application: " + applicationName + " of tenantDomain: " + tenantDomain);
        }
    }
    try {
        startTenantFlow(tenantDomain, username);
        doPreDeleteChecks(applicationName, tenantDomain, username);
        ApplicationDAO appDAO = ApplicationMgtSystemConfig.getInstance().getApplicationDAO();
        serviceProvider = appDAO.getApplication(applicationName, tenantDomain);
        if (serviceProvider != null) {
            ApplicationMgtUtil.deleteAppRole(applicationName);
            ApplicationMgtUtil.deletePermissions(applicationName);
            appDAO.deleteApplication(applicationName);
        } else {
            if (log.isDebugEnabled()) {
                log.debug("Application cannot be found for name: " + applicationName + " in tenantDomain: " + tenantDomain);
            }
            return;
        }
    } catch (Exception e) {
        String error = "Error occurred while deleting the application: " + applicationName + ". " + e.getMessage();
        throw buildServerException(error, e);
    } finally {
        endTenantFlow();
    }
    for (ApplicationMgtListener listener : listeners) {
        if (listener.isEnable() && !listener.doPostDeleteApplication(serviceProvider, tenantDomain, username)) {
            log.error("Post Delete application operation of listener: " + getName(listener) + " failed for " + "application with name: " + applicationName + " of tenantDomain: " + tenantDomain);
            return;
        }
    }
    triggerAuditLogEvent(getInitiatorId(username, tenantDomain), getInitiatorId(username, tenantDomain), USER, CarbonConstants.LogEventConstants.EventCatalog.DELETE_APPLICATION.getEventId(), getAppId(serviceProvider), getApplicationName(serviceProvider), TARGET_APPLICATION, null);
}
Also used : ServiceProvider(org.wso2.carbon.identity.application.common.model.ServiceProvider) AbstractApplicationMgtListener(org.wso2.carbon.identity.application.mgt.listener.AbstractApplicationMgtListener) ApplicationMgtListener(org.wso2.carbon.identity.application.mgt.listener.ApplicationMgtListener) PaginatableFilterableApplicationDAO(org.wso2.carbon.identity.application.mgt.dao.PaginatableFilterableApplicationDAO) ApplicationDAO(org.wso2.carbon.identity.application.mgt.dao.ApplicationDAO) FileBasedApplicationDAO(org.wso2.carbon.identity.application.mgt.dao.impl.FileBasedApplicationDAO) IdentityApplicationManagementClientException(org.wso2.carbon.identity.application.common.IdentityApplicationManagementClientException) TransformerException(javax.xml.transform.TransformerException) RegistryException(org.wso2.carbon.registry.api.RegistryException) IOException(java.io.IOException) IdentityApplicationManagementValidationException(org.wso2.carbon.identity.application.common.IdentityApplicationManagementValidationException) IdentityApplicationManagementException(org.wso2.carbon.identity.application.common.IdentityApplicationManagementException) JAXBException(javax.xml.bind.JAXBException) IdentityApplicationRegistrationFailureException(org.wso2.carbon.identity.application.common.IdentityApplicationRegistrationFailureException) SAXException(org.xml.sax.SAXException) DefaultAuthSeqMgtException(org.wso2.carbon.identity.application.mgt.defaultsequence.DefaultAuthSeqMgtException) UserStoreException(org.wso2.carbon.user.api.UserStoreException) IdentityApplicationManagementServerException(org.wso2.carbon.identity.application.common.IdentityApplicationManagementServerException) ParserConfigurationException(javax.xml.parsers.ParserConfigurationException)

Example 3 with ApplicationMgtListener

use of org.wso2.carbon.identity.application.mgt.listener.ApplicationMgtListener 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 4 with ApplicationMgtListener

use of org.wso2.carbon.identity.application.mgt.listener.ApplicationMgtListener in project carbon-identity-framework by wso2.

the class ApplicationManagementServiceImpl method getServiceProvider.

/**
 * @param serviceProviderName
 * @param tenantDomain
 * @return
 * @throws IdentityApplicationManagementException
 */
@Override
public ServiceProvider getServiceProvider(String serviceProviderName, String tenantDomain) throws IdentityApplicationManagementException {
    // invoking the listeners
    Collection<ApplicationMgtListener> listeners = getApplicationMgtListeners();
    for (ApplicationMgtListener listener : listeners) {
        if (listener.isEnable() && !listener.doPreGetServiceProvider(serviceProviderName, tenantDomain)) {
            return null;
        }
    }
    ServiceProvider serviceProvider = null;
    try {
        startTenantFlow(tenantDomain);
        ApplicationDAO appDAO = ApplicationMgtSystemConfig.getInstance().getApplicationDAO();
        serviceProvider = appDAO.getApplication(serviceProviderName, tenantDomain);
        if (serviceProvider == null && ApplicationManagementServiceComponent.getFileBasedSPs().containsKey(serviceProviderName)) {
            serviceProvider = ApplicationManagementServiceComponent.getFileBasedSPs().get(serviceProviderName);
        }
    } finally {
        endTenantFlow();
    }
    // invoking the listeners
    for (ApplicationMgtListener listener : listeners) {
        if (listener.isEnable() && !listener.doPostGetServiceProvider(serviceProvider, serviceProviderName, tenantDomain)) {
            return null;
        }
    }
    return serviceProvider;
}
Also used : ServiceProvider(org.wso2.carbon.identity.application.common.model.ServiceProvider) AbstractApplicationMgtListener(org.wso2.carbon.identity.application.mgt.listener.AbstractApplicationMgtListener) ApplicationMgtListener(org.wso2.carbon.identity.application.mgt.listener.ApplicationMgtListener) PaginatableFilterableApplicationDAO(org.wso2.carbon.identity.application.mgt.dao.PaginatableFilterableApplicationDAO) ApplicationDAO(org.wso2.carbon.identity.application.mgt.dao.ApplicationDAO) FileBasedApplicationDAO(org.wso2.carbon.identity.application.mgt.dao.impl.FileBasedApplicationDAO)

Example 5 with ApplicationMgtListener

use of org.wso2.carbon.identity.application.mgt.listener.ApplicationMgtListener in project carbon-identity-framework by wso2.

the class ApplicationManagementServiceImpl method getServiceProvider.

/**
 * @param appId
 * @return
 * @throws IdentityApplicationManagementException
 */
@Override
public ServiceProvider getServiceProvider(int appId) throws IdentityApplicationManagementException {
    // TODO: Need to have pre listeners. Don't have them because we didn't want to add listener methods to the
    // TODO: ApplicationMgtListener interface since we didn't want to change APIs. Also pre listener aren't vital
    // TODO: for getters. Mostly post listeners are enough.
    ApplicationDAO appDAO = ApplicationMgtSystemConfig.getInstance().getApplicationDAO();
    ServiceProvider serviceProvider = appDAO.getApplication(appId);
    String serviceProviderName = serviceProvider.getApplicationName();
    String tenantDomain = serviceProvider.getOwner().getTenantDomain();
    // TODO: Since we didn't add post listener methods to the ApplicationMgtListener API to avoid API changes, we
    // TODO: are invoking doPostGetServiceProvider(serviceProvider, serviceProviderName, tenantDomain) listener
    // TODO: method here as well.
    // invoking the post listeners
    Collection<ApplicationMgtListener> listeners = getApplicationMgtListeners();
    for (ApplicationMgtListener listener : listeners) {
        if (listener.isEnable() && !listener.doPostGetServiceProvider(serviceProvider, serviceProviderName, tenantDomain)) {
            return null;
        }
    }
    return serviceProvider;
}
Also used : ServiceProvider(org.wso2.carbon.identity.application.common.model.ServiceProvider) PaginatableFilterableApplicationDAO(org.wso2.carbon.identity.application.mgt.dao.PaginatableFilterableApplicationDAO) ApplicationDAO(org.wso2.carbon.identity.application.mgt.dao.ApplicationDAO) FileBasedApplicationDAO(org.wso2.carbon.identity.application.mgt.dao.impl.FileBasedApplicationDAO) AbstractApplicationMgtListener(org.wso2.carbon.identity.application.mgt.listener.AbstractApplicationMgtListener) ApplicationMgtListener(org.wso2.carbon.identity.application.mgt.listener.ApplicationMgtListener)

Aggregations

ApplicationMgtListener (org.wso2.carbon.identity.application.mgt.listener.ApplicationMgtListener)19 AbstractApplicationMgtListener (org.wso2.carbon.identity.application.mgt.listener.AbstractApplicationMgtListener)18 ApplicationDAO (org.wso2.carbon.identity.application.mgt.dao.ApplicationDAO)13 PaginatableFilterableApplicationDAO (org.wso2.carbon.identity.application.mgt.dao.PaginatableFilterableApplicationDAO)13 FileBasedApplicationDAO (org.wso2.carbon.identity.application.mgt.dao.impl.FileBasedApplicationDAO)13 ServiceProvider (org.wso2.carbon.identity.application.common.model.ServiceProvider)11 IdentityApplicationManagementException (org.wso2.carbon.identity.application.common.IdentityApplicationManagementException)8 IdentityApplicationManagementClientException (org.wso2.carbon.identity.application.common.IdentityApplicationManagementClientException)7 IdentityApplicationManagementValidationException (org.wso2.carbon.identity.application.common.IdentityApplicationManagementValidationException)6 ApplicationBasicInfo (org.wso2.carbon.identity.application.common.model.ApplicationBasicInfo)4 IOException (java.io.IOException)3 JAXBException (javax.xml.bind.JAXBException)3 ParserConfigurationException (javax.xml.parsers.ParserConfigurationException)3 TransformerException (javax.xml.transform.TransformerException)3 IdentityApplicationManagementServerException (org.wso2.carbon.identity.application.common.IdentityApplicationManagementServerException)3 IdentityApplicationRegistrationFailureException (org.wso2.carbon.identity.application.common.IdentityApplicationRegistrationFailureException)3 DefaultAuthSeqMgtException (org.wso2.carbon.identity.application.mgt.defaultsequence.DefaultAuthSeqMgtException)3 RegistryException (org.wso2.carbon.registry.api.RegistryException)3 UserStoreException (org.wso2.carbon.user.api.UserStoreException)3 SAXException (org.xml.sax.SAXException)3