Search in sources :

Example 16 with ServiceProvider

use of org.wso2.carbon.identity.application.common.model.xsd.ServiceProvider in project carbon-identity-framework by wso2.

the class ApplicationManagementServiceImpl method marshalSPTemplate.

private String marshalSPTemplate(ServiceProvider serviceProvider, String tenantDomain) throws IdentityApplicationManagementException {
    try {
        JAXBContext jaxbContext = JAXBContext.newInstance(ServiceProvider.class);
        Marshaller marshaller = jaxbContext.createMarshaller();
        DocumentBuilderFactory docBuilderFactory = IdentityUtil.getSecuredDocumentBuilderFactory();
        Document document = docBuilderFactory.newDocumentBuilder().newDocument();
        marshaller.marshal(serviceProvider, document);
        TransformerFactory transformerFactory = TransformerFactory.newInstance();
        Transformer transformer = transformerFactory.newTransformer();
        transformer.setOutputProperty(OutputKeys.INDENT, "yes");
        transformer.setOutputProperty("{http://xml.apache.org/xslt}indent-amount", "2");
        transformer.setOutputProperty(OutputKeys.CDATA_SECTION_ELEMENTS, "AuthenticationScript inboundConfiguration");
        StringWriter stringBuilder = new StringWriter();
        StreamResult result = new StreamResult(stringBuilder);
        transformer.transform(new DOMSource(document), result);
        return stringBuilder.getBuffer().toString();
    } catch (JAXBException | ParserConfigurationException | TransformerException e) {
        throw new IdentityApplicationManagementException(String.format("Error in exporting Service Provider " + "template from SP %s@%s", serviceProvider.getApplicationName(), tenantDomain), e);
    }
}
Also used : Marshaller(javax.xml.bind.Marshaller) DOMSource(javax.xml.transform.dom.DOMSource) DocumentBuilderFactory(javax.xml.parsers.DocumentBuilderFactory) TransformerFactory(javax.xml.transform.TransformerFactory) Transformer(javax.xml.transform.Transformer) StreamResult(javax.xml.transform.stream.StreamResult) JAXBException(javax.xml.bind.JAXBException) IdentityApplicationManagementException(org.wso2.carbon.identity.application.common.IdentityApplicationManagementException) JAXBContext(javax.xml.bind.JAXBContext) Document(org.w3c.dom.Document) StringWriter(java.io.StringWriter) ParserConfigurationException(javax.xml.parsers.ParserConfigurationException) TransformerException(javax.xml.transform.TransformerException)

Example 17 with ServiceProvider

use of org.wso2.carbon.identity.application.common.model.xsd.ServiceProvider 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 18 with ServiceProvider

use of org.wso2.carbon.identity.application.common.model.xsd.ServiceProvider in project carbon-identity-framework by wso2.

the class ApplicationManagementServiceImpl method createApplication.

@Override
public String createApplication(ServiceProvider application, String tenantDomain, String username) throws IdentityApplicationManagementException {
    // Invoking the listeners.
    Collection<ApplicationResourceManagementListener> listeners = ApplicationMgtListenerServiceComponent.getApplicationResourceMgtListeners();
    for (ApplicationResourceManagementListener listener : listeners) {
        if (listener.isEnabled() && !listener.doPreCreateApplication(application, tenantDomain, username)) {
            throw buildServerException("Pre create application operation of listener: " + getName(listener) + " failed for application: " + application.getApplicationName() + " of tenantDomain: " + tenantDomain);
        }
    }
    doPreAddApplicationChecks(application, tenantDomain, username);
    ApplicationDAO applicationDAO = ApplicationMgtSystemConfig.getInstance().getApplicationDAO();
    String resourceId = doAddApplication(application, tenantDomain, username, applicationDAO::addApplication);
    for (ApplicationResourceManagementListener listener : listeners) {
        try {
            if (listener.isEnabled() && !listener.doPostCreateApplication(resourceId, application, tenantDomain, username)) {
                log.error("Post create application operation of listener:" + getName(listener) + " failed for " + "application: " + application.getApplicationName() + " of tenantDomain: " + tenantDomain);
                break;
            }
        } catch (Throwable e) {
            /*
                 * For more information read https://github.com/wso2/product-is/issues/12579. This is to overcome the
                 * above issue.
                 */
            log.error("Post create application operation of listener:" + getName(listener) + " failed for " + "application: " + application.getApplicationName() + " of tenantDomain: " + tenantDomain + " due to: " + e);
            deleteApplicationByResourceId(resourceId, tenantDomain, username);
            if (log.isDebugEnabled()) {
                log.debug("Removed the application created with id: " + resourceId + " of tenantDomain: " + tenantDomain);
            }
            throw buildServerException("Server encountered an unexpected error when creating the application.");
        }
    }
    triggerAuditLogEvent(getInitiatorId(username, tenantDomain), getInitiatorId(username, tenantDomain), USER, CarbonConstants.LogEventConstants.EventCatalog.CREATE_APPLICATION.getEventId(), getAppId(application), getApplicationName(application), TARGET_APPLICATION, buildSPData(application));
    return resourceId;
}
Also used : ApplicationResourceManagementListener(org.wso2.carbon.identity.application.mgt.listener.ApplicationResourceManagementListener) 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 19 with ServiceProvider

use of org.wso2.carbon.identity.application.common.model.xsd.ServiceProvider 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 20 with ServiceProvider

use of org.wso2.carbon.identity.application.common.model.xsd.ServiceProvider 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)

Aggregations

ServiceProvider (org.wso2.carbon.identity.application.common.model.ServiceProvider)222 Test (org.testng.annotations.Test)120 ServiceProvider (org.wso2.carbon.identity.application.common.model.xsd.ServiceProvider)96 IdentityApplicationManagementException (org.wso2.carbon.identity.application.common.IdentityApplicationManagementException)85 ArrayList (java.util.ArrayList)65 PrepareForTest (org.powermock.core.classloader.annotations.PrepareForTest)58 HashMap (java.util.HashMap)50 InboundAuthenticationRequestConfig (org.wso2.carbon.identity.application.common.model.xsd.InboundAuthenticationRequestConfig)49 ApplicationManagementService (org.wso2.carbon.identity.application.mgt.ApplicationManagementService)40 ClaimMapping (org.wso2.carbon.identity.application.common.model.ClaimMapping)35 AuthenticatedUser (org.wso2.carbon.identity.application.authentication.framework.model.AuthenticatedUser)33 AuthenticationContext (org.wso2.carbon.identity.application.authentication.framework.context.AuthenticationContext)29 InboundAuthenticationRequestConfig (org.wso2.carbon.identity.application.common.model.InboundAuthenticationRequestConfig)26 SequenceConfig (org.wso2.carbon.identity.application.authentication.framework.config.model.SequenceConfig)25 IdentityException (org.wso2.carbon.identity.base.IdentityException)23 Property (org.wso2.carbon.identity.application.common.model.xsd.Property)21 LocalAndOutboundAuthenticationConfig (org.wso2.carbon.identity.application.common.model.LocalAndOutboundAuthenticationConfig)20 InboundAuthenticationConfig (org.wso2.carbon.identity.application.common.model.xsd.InboundAuthenticationConfig)20 IdentityOAuth2Exception (org.wso2.carbon.identity.oauth2.IdentityOAuth2Exception)20 Matchers.anyString (org.mockito.Matchers.anyString)19