Search in sources :

Example 6 with AuthenticationScriptConfig

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

the class AbstractAdaptiveAuthenticationTestCase method createServiceProvider.

protected ServiceProvider createServiceProvider(String appName, ApplicationManagementServiceClient applicationManagementServiceClient, OauthAdminClient oauthAdminClient, String script) throws Exception {
    OAuthConsumerAppDTO[] appDtos = oauthAdminClient.getAllOAuthApplicationData();
    for (OAuthConsumerAppDTO appDto : appDtos) {
        if (appDto.getApplicationName().equals(appName)) {
            consumerKey = appDto.getOauthConsumerKey();
            consumerSecret = appDto.getOauthConsumerSecret();
        }
    }
    ServiceProvider serviceProvider = new ServiceProvider();
    serviceProvider.setApplicationName(appName);
    serviceProvider.setDescription("This is a test Service Provider for conditional authentication flow test.");
    applicationManagementServiceClient.createApplication(serviceProvider);
    serviceProvider = applicationManagementServiceClient.getApplication(appName);
    InboundAuthenticationRequestConfig requestConfig = new InboundAuthenticationRequestConfig();
    requestConfig.setInboundAuthKey(consumerKey);
    requestConfig.setInboundAuthType("oauth2");
    if (StringUtils.isNotBlank(consumerSecret)) {
        Property property = new Property();
        property.setName("oauthConsumerSecret");
        property.setValue(consumerSecret);
        Property[] properties = { property };
        requestConfig.setProperties(properties);
    }
    InboundAuthenticationConfig inboundAuthenticationConfig = new InboundAuthenticationConfig();
    inboundAuthenticationConfig.setInboundAuthenticationRequestConfigs(new InboundAuthenticationRequestConfig[] { requestConfig });
    serviceProvider.setInboundAuthenticationConfig(inboundAuthenticationConfig);
    LocalAndOutboundAuthenticationConfig outboundAuthConfig = createLocalAndOutboundAuthenticationConfig();
    outboundAuthConfig.setEnableAuthorization(false);
    AuthenticationScriptConfig config = new AuthenticationScriptConfig();
    config.setContent(script);
    config.setEnabled(true);
    outboundAuthConfig.setAuthenticationScriptConfig(config);
    serviceProvider.setLocalAndOutBoundAuthenticationConfig(outboundAuthConfig);
    applicationManagementServiceClient.updateApplicationData(serviceProvider);
    return serviceProvider;
}
Also used : LocalAndOutboundAuthenticationConfig(org.wso2.carbon.identity.application.common.model.xsd.LocalAndOutboundAuthenticationConfig) InboundAuthenticationConfig(org.wso2.carbon.identity.application.common.model.xsd.InboundAuthenticationConfig) AuthenticationScriptConfig(org.wso2.carbon.identity.application.common.model.script.xsd.AuthenticationScriptConfig) ServiceProvider(org.wso2.carbon.identity.application.common.model.xsd.ServiceProvider) OAuthConsumerAppDTO(org.wso2.carbon.identity.oauth.stub.dto.OAuthConsumerAppDTO) InboundAuthenticationRequestConfig(org.wso2.carbon.identity.application.common.model.xsd.InboundAuthenticationRequestConfig) Property(org.wso2.carbon.identity.application.common.model.xsd.Property) IdentityProviderProperty(org.wso2.carbon.identity.application.common.model.idp.xsd.IdentityProviderProperty)

Example 7 with AuthenticationScriptConfig

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

the class ApplicationDAOImpl method getLocalAndOutboundAuthenticationConfig.

/**
 * @param applicationId
 * @param connection
 * @param propertyList
 * @return
 * @throws SQLException
 */
private LocalAndOutboundAuthenticationConfig getLocalAndOutboundAuthenticationConfig(int applicationId, Connection connection, int tenantId, List<ServiceProviderProperty> propertyList) throws SQLException, IdentityApplicationManagementException {
    PreparedStatement getStepInfoPrepStmt = null;
    ResultSet stepInfoResultSet = null;
    if (log.isDebugEnabled()) {
        log.debug("Reading Steps of Application " + applicationId);
    }
    try {
        getStepInfoPrepStmt = connection.prepareStatement(LOAD_STEPS_INFO_BY_APP_ID);
        // STEP_ORDER, AUTHENTICATOR_ID, IS_SUBJECT_STEP, IS_ATTRIBUTE_STEP
        getStepInfoPrepStmt.setInt(1, applicationId);
        stepInfoResultSet = getStepInfoPrepStmt.executeQuery();
        Map<String, AuthenticationStep> authSteps = new HashMap<>();
        Map<String, Map<String, List<FederatedAuthenticatorConfig>>> stepFedIdPAuthenticators = new HashMap<>();
        Map<String, List<LocalAuthenticatorConfig>> stepLocalAuth = new HashMap<>();
        while (stepInfoResultSet.next()) {
            String step = String.valueOf(stepInfoResultSet.getInt(1));
            AuthenticationStep authStep;
            if (authSteps.containsKey(step)) {
                authStep = authSteps.get(step);
            } else {
                authStep = new AuthenticationStep();
                authStep.setStepOrder(stepInfoResultSet.getInt(1));
                stepLocalAuth.put(step, new ArrayList<LocalAuthenticatorConfig>());
                stepFedIdPAuthenticators.put(step, new HashMap<String, List<FederatedAuthenticatorConfig>>());
            }
            int authenticatorId = stepInfoResultSet.getInt(2);
            Map<String, String> authenticatorInfo = getAuthenticatorInfo(connection, tenantId, authenticatorId);
            if (authenticatorInfo != null && authenticatorInfo.get(ApplicationConstants.IDP_NAME) != null && ApplicationConstants.LOCAL_IDP_NAME.equals(authenticatorInfo.get("idpName"))) {
                LocalAuthenticatorConfig localAuthenticator = new LocalAuthenticatorConfig();
                localAuthenticator.setName(authenticatorInfo.get(ApplicationConstants.IDP_AUTHENTICATOR_NAME));
                localAuthenticator.setDisplayName(authenticatorInfo.get(ApplicationConstants.IDP_AUTHENTICATOR_DISPLAY_NAME));
                stepLocalAuth.get(step).add(localAuthenticator);
            } else {
                Map<String, List<FederatedAuthenticatorConfig>> stepFedIdps = stepFedIdPAuthenticators.get(step);
                if (!stepFedIdps.containsKey(authenticatorInfo.get(ApplicationConstants.IDP_NAME))) {
                    stepFedIdps.put(authenticatorInfo.get(ApplicationConstants.IDP_NAME), new ArrayList<FederatedAuthenticatorConfig>());
                }
                List<FederatedAuthenticatorConfig> idpAuths = stepFedIdps.get(authenticatorInfo.get(ApplicationConstants.IDP_NAME));
                FederatedAuthenticatorConfig fedAuthenticator = new FederatedAuthenticatorConfig();
                fedAuthenticator.setName(authenticatorInfo.get(ApplicationConstants.IDP_AUTHENTICATOR_NAME));
                fedAuthenticator.setDisplayName(authenticatorInfo.get(ApplicationConstants.IDP_AUTHENTICATOR_DISPLAY_NAME));
                idpAuths.add(fedAuthenticator);
            }
            authStep.setSubjectStep("1".equals(stepInfoResultSet.getString(3)));
            authStep.setAttributeStep("1".equals(stepInfoResultSet.getString(4)));
            authSteps.put(step, authStep);
        }
        LocalAndOutboundAuthenticationConfig localAndOutboundConfiguration = new LocalAndOutboundAuthenticationConfig();
        AuthenticationStep[] authenticationSteps = new AuthenticationStep[authSteps.size()];
        int authStepCount = 0;
        for (Entry<String, AuthenticationStep> entry : authSteps.entrySet()) {
            AuthenticationStep authStep = entry.getValue();
            String stepId = entry.getKey();
            List<LocalAuthenticatorConfig> localAuthenticatorList = stepLocalAuth.get(stepId);
            if (localAuthenticatorList != null && localAuthenticatorList.size() > 0) {
                authStep.setLocalAuthenticatorConfigs(localAuthenticatorList.toArray(new LocalAuthenticatorConfig[localAuthenticatorList.size()]));
            }
            Map<String, List<FederatedAuthenticatorConfig>> idpList = stepFedIdPAuthenticators.get(stepId);
            if (idpList != null && idpList.size() > 0) {
                IdentityProvider[] fedIdpList = new IdentityProvider[idpList.size()];
                int idpCount = 0;
                for (Entry<String, List<FederatedAuthenticatorConfig>> idpEntry : idpList.entrySet()) {
                    String idpName = idpEntry.getKey();
                    List<FederatedAuthenticatorConfig> fedAuthenticators = idpEntry.getValue();
                    IdentityProvider idp = new IdentityProvider();
                    idp.setIdentityProviderName(idpName);
                    idp.setFederationHub(isFederationHubIdP(idpName, connection, tenantId));
                    idp.setFederatedAuthenticatorConfigs(fedAuthenticators.toArray(new FederatedAuthenticatorConfig[fedAuthenticators.size()]));
                    idp.setDefaultAuthenticatorConfig(idp.getFederatedAuthenticatorConfigs()[0]);
                    fedIdpList[idpCount++] = idp;
                }
                authStep.setFederatedIdentityProviders(fedIdpList);
            }
            authenticationSteps[authStepCount++] = authStep;
        }
        Arrays.sort(authenticationSteps, Comparator.comparingInt(AuthenticationStep::getStepOrder));
        int numSteps = authenticationSteps.length;
        // We check if the steps have consecutive step numbers.
        if (numSteps > 0 && authenticationSteps[numSteps - 1].getStepOrder() != numSteps) {
            if (log.isDebugEnabled()) {
                log.debug("Authentication steps of Application with id: " + applicationId + "  do not have " + "consecutive numbers. This was possibility due to a IDP force deletion. Fixing the step " + "order.");
            }
            // Iterate through the steps and fix step order.
            int count = 1;
            for (AuthenticationStep step : authenticationSteps) {
                step.setStepOrder(count++);
            }
        }
        localAndOutboundConfiguration.setAuthenticationSteps(authenticationSteps);
        String authType = getAuthenticationType(applicationId, connection);
        if (StringUtils.equalsIgnoreCase(authType, ApplicationConstants.AUTH_TYPE_FEDERATED) || StringUtils.equalsIgnoreCase(authType, ApplicationConstants.AUTH_TYPE_FLOW)) {
            if (ArrayUtils.isEmpty(authenticationSteps)) {
                // the authType to 'default'.
                if (log.isDebugEnabled()) {
                    log.debug("Authentication type is '" + authType + "' eventhough the application with id: " + applicationId + " has zero authentication step. This was possibility due to a IDP force deletion. " + " Defaulting authentication type to " + ApplicationConstants.AUTH_TYPE_DEFAULT);
                }
                authType = ApplicationConstants.AUTH_TYPE_DEFAULT;
            }
        }
        localAndOutboundConfiguration.setAuthenticationType(authType);
        AuthenticationScriptConfig authenticationScriptConfig = getScriptConfiguration(applicationId, connection);
        if (authenticationScriptConfig != null) {
            localAndOutboundConfiguration.setAuthenticationScriptConfig(authenticationScriptConfig);
        }
        PreparedStatement localAndOutboundConfigPrepStmt = null;
        ResultSet localAndOutboundConfigResultSet = null;
        try {
            localAndOutboundConfigPrepStmt = connection.prepareStatement(LOAD_LOCAL_AND_OUTBOUND_CONFIG_BY_APP_ID);
            localAndOutboundConfigPrepStmt.setInt(1, tenantId);
            localAndOutboundConfigPrepStmt.setInt(2, applicationId);
            localAndOutboundConfigResultSet = localAndOutboundConfigPrepStmt.executeQuery();
            if (localAndOutboundConfigResultSet.next()) {
                localAndOutboundConfiguration.setUseTenantDomainInLocalSubjectIdentifier("1".equals(localAndOutboundConfigResultSet.getString(1)));
                localAndOutboundConfiguration.setUseUserstoreDomainInLocalSubjectIdentifier("1".equals(localAndOutboundConfigResultSet.getString(2)));
                localAndOutboundConfiguration.setEnableAuthorization("1".equals(localAndOutboundConfigResultSet.getString(3)));
                localAndOutboundConfiguration.setAlwaysSendBackAuthenticatedListOfIdPs("1".equals(localAndOutboundConfigResultSet.getString(4)));
                localAndOutboundConfiguration.setSubjectClaimUri(localAndOutboundConfigResultSet.getString(5));
                readAndSetConfigurationsFromProperties(propertyList, localAndOutboundConfiguration);
            }
        } finally {
            IdentityApplicationManagementUtil.closeStatement(localAndOutboundConfigPrepStmt);
            IdentityApplicationManagementUtil.closeResultSet(localAndOutboundConfigResultSet);
        }
        return localAndOutboundConfiguration;
    } finally {
        IdentityApplicationManagementUtil.closeStatement(getStepInfoPrepStmt);
        IdentityApplicationManagementUtil.closeResultSet(stepInfoResultSet);
    }
}
Also used : HashMap(java.util.HashMap) FederatedAuthenticatorConfig(org.wso2.carbon.identity.application.common.model.FederatedAuthenticatorConfig) LocalAuthenticatorConfig(org.wso2.carbon.identity.application.common.model.LocalAuthenticatorConfig) LocalAndOutboundAuthenticationConfig(org.wso2.carbon.identity.application.common.model.LocalAndOutboundAuthenticationConfig) AuthenticationScriptConfig(org.wso2.carbon.identity.application.common.model.script.AuthenticationScriptConfig) ResultSet(java.sql.ResultSet) ArrayList(java.util.ArrayList) List(java.util.List) AuthenticationStep(org.wso2.carbon.identity.application.common.model.AuthenticationStep) PreparedStatement(java.sql.PreparedStatement) NamedPreparedStatement(org.wso2.carbon.database.utils.jdbc.NamedPreparedStatement) IdentityProvider(org.wso2.carbon.identity.application.common.model.IdentityProvider) Map(java.util.Map) HashMap(java.util.HashMap)

Example 8 with AuthenticationScriptConfig

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

the class ApplicationDAOImpl method updateAuthenticationScriptConfiguration.

/**
 * Updates the authentication script configuration.
 *
 * @param applicationId
 * @param localAndOutboundAuthConfig
 * @param connection
 * @param tenantID
 * @throws SQLException
 */
private void updateAuthenticationScriptConfiguration(int applicationId, LocalAndOutboundAuthenticationConfig localAndOutboundAuthConfig, Connection connection, int tenantID) throws SQLException {
    if (localAndOutboundAuthConfig.getAuthenticationScriptConfig() != null) {
        AuthenticationScriptConfig authenticationScriptConfig = localAndOutboundAuthConfig.getAuthenticationScriptConfig();
        try (PreparedStatement storeAuthScriptPrepStmt = connection.prepareStatement(STORE_SP_AUTH_SCRIPT)) {
            storeAuthScriptPrepStmt.setInt(1, tenantID);
            storeAuthScriptPrepStmt.setInt(2, applicationId);
            storeAuthScriptPrepStmt.setString(3, authenticationScriptConfig.getLanguage());
            setBlobValue(authenticationScriptConfig.getContent(), storeAuthScriptPrepStmt, 4);
            storeAuthScriptPrepStmt.setString(5, authenticationScriptConfig.isEnabled() ? "1" : "0");
            storeAuthScriptPrepStmt.execute();
        } catch (IOException ex) {
            log.error("Error occurred while updating authentication script configuration.", ex);
        }
    }
}
Also used : AuthenticationScriptConfig(org.wso2.carbon.identity.application.common.model.script.AuthenticationScriptConfig) PreparedStatement(java.sql.PreparedStatement) NamedPreparedStatement(org.wso2.carbon.database.utils.jdbc.NamedPreparedStatement) IOException(java.io.IOException)

Example 9 with AuthenticationScriptConfig

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

the class ConditionalAuthenticationTestCase method updateAuthScript.

/**
 * Update service provider authentication script config.
 *
 * @param filename File Name of the authentication script.
 * @throws Exception
 */
private void updateAuthScript(String filename) throws Exception {
    LocalAndOutboundAuthenticationConfig outboundAuthConfig = createLocalAndOutboundAuthenticationConfig();
    outboundAuthConfig.setEnableAuthorization(true);
    String script = getConditionalAuthScript(filename);
    AuthenticationScriptConfig config = new AuthenticationScriptConfig();
    config.setContent(script);
    config.setEnabled(true);
    outboundAuthConfig.setAuthenticationScriptConfig(config);
    serviceProvider.setLocalAndOutBoundAuthenticationConfig(outboundAuthConfig);
    applicationManagementServiceClient.updateApplicationData(serviceProvider);
}
Also used : LocalAndOutboundAuthenticationConfig(org.wso2.carbon.identity.application.common.model.xsd.LocalAndOutboundAuthenticationConfig) AuthenticationScriptConfig(org.wso2.carbon.identity.application.common.model.script.xsd.AuthenticationScriptConfig)

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