Search in sources :

Example 1 with AuthenticationScriptConfig

use of org.wso2.carbon.identity.application.common.model.script.AuthenticationScriptConfig in project carbon-identity-framework by wso2.

the class ApplicationDAOImpl method getScriptConfiguration.

private AuthenticationScriptConfig getScriptConfiguration(int applicationId, Connection connection) throws SQLException, IdentityApplicationManagementException {
    try (PreparedStatement localAndOutboundConfigScriptPrepStmt = connection.prepareStatement(LOAD_SCRIPT_BY_APP_ID_QUERY)) {
        localAndOutboundConfigScriptPrepStmt.setInt(1, applicationId);
        try (ResultSet localAndOutboundConfigScriptResultSet = localAndOutboundConfigScriptPrepStmt.executeQuery()) {
            if (localAndOutboundConfigScriptResultSet.next()) {
                AuthenticationScriptConfig authenticationScriptConfig = new AuthenticationScriptConfig();
                try {
                    boolean isEnabled = "1".equals(localAndOutboundConfigScriptResultSet.getString(2));
                    InputStream scriptBinaryStream = localAndOutboundConfigScriptResultSet.getBinaryStream(1);
                    String targetString = StringUtils.EMPTY;
                    if (scriptBinaryStream != null) {
                        targetString = IOUtils.toString(scriptBinaryStream);
                    }
                    authenticationScriptConfig.setContent(targetString);
                    authenticationScriptConfig.setEnabled(isEnabled);
                } catch (IOException e) {
                    throw new IdentityApplicationManagementException("Could not read the Script for application : " + applicationId, e);
                }
                return authenticationScriptConfig;
            }
        }
    }
    return null;
}
Also used : AuthenticationScriptConfig(org.wso2.carbon.identity.application.common.model.script.AuthenticationScriptConfig) ByteArrayInputStream(java.io.ByteArrayInputStream) InputStream(java.io.InputStream) IdentityApplicationManagementException(org.wso2.carbon.identity.application.common.IdentityApplicationManagementException) ResultSet(java.sql.ResultSet) PreparedStatement(java.sql.PreparedStatement) NamedPreparedStatement(org.wso2.carbon.database.utils.jdbc.NamedPreparedStatement) IOException(java.io.IOException)

Example 2 with AuthenticationScriptConfig

use of org.wso2.carbon.identity.application.common.model.script.AuthenticationScriptConfig in project carbon-identity-framework by wso2.

the class ApplicationManagementServiceImpl method getServiceProviderByClientId.

/**
 * @param clientId
 * @param clientType
 * @param tenantDomain
 * @return
 * @throws IdentityApplicationManagementException
 */
@Override
public ServiceProvider getServiceProviderByClientId(String clientId, String clientType, String tenantDomain) throws IdentityApplicationManagementException {
    // invoking the listeners
    Collection<ApplicationMgtListener> listeners = getApplicationMgtListeners();
    for (ApplicationMgtListener listener : listeners) {
        if (listener.isEnable() && !listener.doPreGetServiceProviderByClientId(clientId, clientType, tenantDomain)) {
            return null;
        }
    }
    // client id can contain the @ to identify the tenant domain.
    if (clientId != null && clientId.contains("@")) {
        clientId = clientId.split("@")[0];
    }
    String serviceProviderName;
    ServiceProvider serviceProvider = null;
    serviceProviderName = getServiceProviderNameByClientId(clientId, clientType, tenantDomain);
    try {
        startTenantFlow(tenantDomain);
        ApplicationDAO appDAO = ApplicationMgtSystemConfig.getInstance().getApplicationDAO();
        serviceProvider = appDAO.getApplication(serviceProviderName, tenantDomain);
        if (serviceProvider != null) {
            // if "Authentication Type" is "Default" we must get the steps from the default SP
            AuthenticationStep[] authenticationSteps = serviceProvider.getLocalAndOutBoundAuthenticationConfig().getAuthenticationSteps();
            if (authenticationSteps == null || authenticationSteps.length == 0) {
                ServiceProvider defaultSP = ApplicationManagementServiceComponent.getFileBasedSPs().get(IdentityApplicationConstants.DEFAULT_SP_CONFIG);
                authenticationSteps = defaultSP.getLocalAndOutBoundAuthenticationConfig().getAuthenticationSteps();
                AuthenticationScriptConfig scriptConfig = defaultSP.getLocalAndOutBoundAuthenticationConfig().getAuthenticationScriptConfig();
                serviceProvider.getLocalAndOutBoundAuthenticationConfig().setAuthenticationSteps(authenticationSteps);
                if (scriptConfig != null) {
                    serviceProvider.getLocalAndOutBoundAuthenticationConfig().setAuthenticationScriptConfig(scriptConfig);
                    serviceProvider.getLocalAndOutBoundAuthenticationConfig().setAuthenticationType(ApplicationConstants.AUTH_TYPE_FLOW);
                }
            }
        }
    } finally {
        endTenantFlow();
    }
    if (serviceProvider == null && serviceProviderName != null && ApplicationManagementServiceComponent.getFileBasedSPs().containsKey(serviceProviderName)) {
        serviceProvider = ApplicationManagementServiceComponent.getFileBasedSPs().get(serviceProviderName);
    }
    for (ApplicationMgtListener listener : listeners) {
        if (listener.isEnable() && !listener.doPostGetServiceProviderByClientId(serviceProvider, clientId, clientType, tenantDomain)) {
            return null;
        }
    }
    return serviceProvider;
}
Also used : AuthenticationScriptConfig(org.wso2.carbon.identity.application.common.model.script.AuthenticationScriptConfig) ServiceProvider(org.wso2.carbon.identity.application.common.model.ServiceProvider) AuthenticationStep(org.wso2.carbon.identity.application.common.model.AuthenticationStep) 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 3 with AuthenticationScriptConfig

use of org.wso2.carbon.identity.application.common.model.script.AuthenticationScriptConfig in project carbon-identity-framework by wso2.

the class ApplicationBean method conditionalAuthentication.

/**
 * @param request
 */
public void conditionalAuthentication(HttpServletRequest request) {
    AuthenticationScriptConfig authenticationScriptConfig = new AuthenticationScriptConfig();
    LocalAndOutboundAuthenticationConfig localAndOutboundAuthenticationConfig = serviceProvider.getLocalAndOutBoundAuthenticationConfig();
    String flawByScript = request.getParameter("scriptTextArea");
    if (StringUtils.isBlank(flawByScript)) {
        authenticationScriptConfig.setEnabled(false);
    } else {
        if ("true".equalsIgnoreCase(request.getParameter("enableScript"))) {
            authenticationScriptConfig.setEnabled(true);
        } else {
            authenticationScriptConfig.setEnabled(false);
        }
    }
    authenticationScriptConfig.setContent(flawByScript);
    localAndOutboundAuthenticationConfig.setAuthenticationScriptConfig(authenticationScriptConfig);
}
Also used : LocalAndOutboundAuthenticationConfig(org.wso2.carbon.identity.application.common.model.xsd.LocalAndOutboundAuthenticationConfig) AuthenticationScriptConfig(org.wso2.carbon.identity.application.common.model.script.xsd.AuthenticationScriptConfig)

Example 4 with AuthenticationScriptConfig

use of org.wso2.carbon.identity.application.common.model.script.AuthenticationScriptConfig in project carbon-identity-framework by wso2.

the class DefaultApplicationValidatorTest method validateAdaptiveAuthScriptTest.

@Test(dataProvider = "validateAdaptiveAuthScriptDataProvider")
public void validateAdaptiveAuthScriptTest(String isValidationFailScenario, String isLoopsAllowed, String script) throws Exception {
    DefaultApplicationValidator defaultApplicationValidator = new DefaultApplicationValidator();
    Field configuration = IdentityUtil.class.getDeclaredField("configuration");
    configuration.setAccessible(true);
    Map<String, Object> configMap = new HashMap<>();
    configMap.put("AdaptiveAuth.AllowLoops", isLoopsAllowed);
    configuration.set(IdentityUtil.class, configMap);
    Method validateAdaptiveAuthScript = DefaultApplicationValidator.class.getDeclaredMethod("validateAdaptiveAuthScript", List.class, AuthenticationScriptConfig.class);
    validateAdaptiveAuthScript.setAccessible(true);
    AuthenticationScriptConfig scriptConfig = new AuthenticationScriptConfig();
    scriptConfig.setContent(script);
    List<String> validationErrors = new ArrayList<>();
    validateAdaptiveAuthScript.invoke(defaultApplicationValidator, validationErrors, scriptConfig);
    if (Boolean.parseBoolean(isValidationFailScenario)) {
        Assert.assertFalse(validationErrors.isEmpty(), "This is an invalid scenario. There should be " + "validation messages.");
        List<String> filtered = validationErrors.stream().filter(error -> StringUtils.containsIgnoreCase(error, "loop")).collect(Collectors.toList());
        Assert.assertFalse(filtered.isEmpty(), "There should be a validation message related to loops");
    } else {
        Assert.assertTrue(validationErrors.isEmpty(), "There are validation messages. This is a valid case " + "there should not be any validation messages. Validation messages: " + String.join("|", validationErrors));
    }
}
Also used : StringUtils(org.apache.commons.lang.StringUtils) DataProvider(org.testng.annotations.DataProvider) Test(org.testng.annotations.Test) HashMap(java.util.HashMap) Field(java.lang.reflect.Field) Collectors(java.util.stream.Collectors) DefaultApplicationValidator(org.wso2.carbon.identity.application.mgt.validator.DefaultApplicationValidator) ArrayList(java.util.ArrayList) AuthenticationScriptConfig(org.wso2.carbon.identity.application.common.model.script.AuthenticationScriptConfig) List(java.util.List) Assert(org.testng.Assert) Map(java.util.Map) IdentityUtil(org.wso2.carbon.identity.core.util.IdentityUtil) Method(java.lang.reflect.Method) Field(java.lang.reflect.Field) DefaultApplicationValidator(org.wso2.carbon.identity.application.mgt.validator.DefaultApplicationValidator) HashMap(java.util.HashMap) AuthenticationScriptConfig(org.wso2.carbon.identity.application.common.model.script.AuthenticationScriptConfig) ArrayList(java.util.ArrayList) Method(java.lang.reflect.Method) Test(org.testng.annotations.Test)

Example 5 with AuthenticationScriptConfig

use of org.wso2.carbon.identity.application.common.model.script.AuthenticationScriptConfig in project product-is by wso2.

the class DefaultAuthSeqManagementTestCase method updateApplicationData.

private void updateApplicationData(String name) {
    try {
        ServiceProvider serviceProvider = applicationManagementServiceClient.getApplication(name);
        serviceProvider.getLocalAndOutBoundAuthenticationConfig().setAuthenticationType("flow");
        AuthenticationStep authStep = new AuthenticationStep();
        authStep.setStepOrder(1);
        authStep.setAttributeStep(true);
        authStep.setSubjectStep(true);
        LocalAuthenticatorConfig localAuthenticatorConfig = new LocalAuthenticatorConfig();
        localAuthenticatorConfig.setName("BasicAuthenticator");
        localAuthenticatorConfig.setDisplayName("basic");
        localAuthenticatorConfig.setEnabled(true);
        authStep.setLocalAuthenticatorConfigs(new LocalAuthenticatorConfig[] { localAuthenticatorConfig });
        serviceProvider.getLocalAndOutBoundAuthenticationConfig().setAuthenticationSteps(new AuthenticationStep[] { authStep });
        AuthenticationScriptConfig scriptConfig = new AuthenticationScriptConfig();
        scriptConfig.setLanguage("application/javascript");
        scriptConfig.setContent("function onLoginRequest(context) {\r\n  executeStep(1);\r\n}\r\n");
        serviceProvider.getLocalAndOutBoundAuthenticationConfig().setAuthenticationScriptConfig(scriptConfig);
        applicationManagementServiceClient.updateApplicationData(serviceProvider);
        ServiceProvider updatedServiceProvider = applicationManagementServiceClient.getApplication(applicationName);
        Assert.assertEquals(updatedServiceProvider.getLocalAndOutBoundAuthenticationConfig().getAuthenticationSteps()[0].getLocalAuthenticatorConfigs()[0].getDisplayName(), "basic", "Failed update Authentication step");
    } catch (Exception e) {
        fail("Error while trying to update Service Provider", e);
    }
}
Also used : AuthenticationScriptConfig(org.wso2.carbon.identity.application.common.model.script.xsd.AuthenticationScriptConfig) ServiceProvider(org.wso2.carbon.identity.application.common.model.xsd.ServiceProvider) LocalAuthenticatorConfig(org.wso2.carbon.identity.application.common.model.xsd.LocalAuthenticatorConfig) AuthenticationStep(org.wso2.carbon.identity.application.common.model.xsd.AuthenticationStep) URISyntaxException(java.net.URISyntaxException) IdentityDefaultSeqManagementServiceDefaultAuthSeqMgtException(org.wso2.carbon.identity.application.mgt.defaultsequence.stub.IdentityDefaultSeqManagementServiceDefaultAuthSeqMgtException) IOException(java.io.IOException)

Aggregations

AuthenticationScriptConfig (org.wso2.carbon.identity.application.common.model.script.AuthenticationScriptConfig)5 AuthenticationScriptConfig (org.wso2.carbon.identity.application.common.model.script.xsd.AuthenticationScriptConfig)4 IOException (java.io.IOException)3 PreparedStatement (java.sql.PreparedStatement)3 NamedPreparedStatement (org.wso2.carbon.database.utils.jdbc.NamedPreparedStatement)3 LocalAndOutboundAuthenticationConfig (org.wso2.carbon.identity.application.common.model.xsd.LocalAndOutboundAuthenticationConfig)3 ResultSet (java.sql.ResultSet)2 ArrayList (java.util.ArrayList)2 HashMap (java.util.HashMap)2 List (java.util.List)2 Map (java.util.Map)2 AuthenticationStep (org.wso2.carbon.identity.application.common.model.AuthenticationStep)2 ServiceProvider (org.wso2.carbon.identity.application.common.model.xsd.ServiceProvider)2 ByteArrayInputStream (java.io.ByteArrayInputStream)1 InputStream (java.io.InputStream)1 Field (java.lang.reflect.Field)1 Method (java.lang.reflect.Method)1 URISyntaxException (java.net.URISyntaxException)1 Collectors (java.util.stream.Collectors)1 StringUtils (org.apache.commons.lang.StringUtils)1