Search in sources :

Example 1 with DynamicDecisionNode

use of org.wso2.carbon.identity.application.authentication.framework.config.model.graph.DynamicDecisionNode in project carbon-identity-framework by wso2.

the class JsGraphBuilderTest method testCreateJavascript.

@Test
public void testCreateJavascript() throws Exception {
    String script = "var onLoginRequest = function(context) { executeStep(1, { onSuccess : function(context) {" + "executeStep(2);}})};";
    ServiceProvider sp1 = getTestServiceProvider("js-sp-1.xml");
    AuthenticationContext context = getAuthenticationContext(sp1);
    Map<Integer, StepConfig> stepConfigMap = new HashMap<>();
    stepConfigMap.put(1, new StepConfig());
    stepConfigMap.put(2, new StepConfig());
    JsGraphBuilder jsGraphBuilder = jsGraphBuilderFactory.createBuilder(context, stepConfigMap);
    jsGraphBuilder.createWith(script);
    AuthenticationGraph graph = jsGraphBuilder.build();
    assertNotNull(graph.getStartNode());
    assertTrue(graph.getStartNode() instanceof StepConfigGraphNode);
    StepConfigGraphNode firstStep = (StepConfigGraphNode) graph.getStartNode();
    assertNotNull(firstStep.getNext());
    assertTrue(firstStep.getNext() instanceof DynamicDecisionNode);
}
Also used : AuthenticationContext(org.wso2.carbon.identity.application.authentication.framework.context.AuthenticationContext) HashMap(java.util.HashMap) ServiceProvider(org.wso2.carbon.identity.application.common.model.ServiceProvider) StepConfig(org.wso2.carbon.identity.application.authentication.framework.config.model.StepConfig) Test(org.testng.annotations.Test) AbstractFrameworkTest(org.wso2.carbon.identity.application.authentication.framework.AbstractFrameworkTest) BeforeTest(org.testng.annotations.BeforeTest) AfterTest(org.testng.annotations.AfterTest)

Example 2 with DynamicDecisionNode

use of org.wso2.carbon.identity.application.authentication.framework.config.model.graph.DynamicDecisionNode in project carbon-identity-framework by wso2.

the class GraphBasedSequenceHandler method executeFunction.

private void executeFunction(String outcomeName, DynamicDecisionNode dynamicDecisionNode, AuthenticationContext context, Map<String, Object> data) {
    SerializableJsFunction fn = dynamicDecisionNode.getFunctionMap().get(outcomeName);
    FrameworkServiceDataHolder dataHolder = FrameworkServiceDataHolder.getInstance();
    JsGraphBuilderFactory jsGraphBuilderFactory = dataHolder.getJsGraphBuilderFactory();
    JsGraphBuilder jsGraphBuilder = jsGraphBuilderFactory.createBuilder(context, context.getSequenceConfig().getAuthenticationGraph().getStepMap(), dynamicDecisionNode);
    JsGraphBuilder.JsBasedEvaluator jsBasedEvaluator = jsGraphBuilder.new JsBasedEvaluator(fn);
    jsBasedEvaluator.evaluate(context, (jsConsumer) -> jsConsumer.call(null, new JsAuthenticationContext(context), new JsWritableParameters(data)));
    if (dynamicDecisionNode.getDefaultEdge() == null) {
        dynamicDecisionNode.setDefaultEdge(new EndStep());
    }
}
Also used : JsGraphBuilderFactory(org.wso2.carbon.identity.application.authentication.framework.config.model.graph.JsGraphBuilderFactory) JsWritableParameters(org.wso2.carbon.identity.application.authentication.framework.config.model.graph.js.JsWritableParameters) JsAuthenticationContext(org.wso2.carbon.identity.application.authentication.framework.config.model.graph.js.JsAuthenticationContext) JsGraphBuilder(org.wso2.carbon.identity.application.authentication.framework.config.model.graph.JsGraphBuilder) FrameworkServiceDataHolder(org.wso2.carbon.identity.application.authentication.framework.internal.FrameworkServiceDataHolder) EndStep(org.wso2.carbon.identity.application.authentication.framework.config.model.graph.EndStep) SerializableJsFunction(org.wso2.carbon.identity.application.authentication.framework.config.model.graph.SerializableJsFunction)

Example 3 with DynamicDecisionNode

use of org.wso2.carbon.identity.application.authentication.framework.config.model.graph.DynamicDecisionNode in project carbon-identity-framework by wso2.

the class GraphBasedSequenceHandler method handleAuthenticationStep.

private boolean handleAuthenticationStep(HttpServletRequest request, HttpServletResponse response, AuthenticationContext context, SequenceConfig sequenceConfig, StepConfigGraphNode stepConfigGraphNode) throws FrameworkException {
    StepConfig stepConfig = stepConfigGraphNode.getStepConfig();
    if (stepConfig == null) {
        throw new FrameworkException("StepConfig not found while handling the step. Service Provider : " + context.getServiceProviderName());
    }
    // if the current step is completed
    if (stepConfig.isCompleted()) {
        stepConfig.setCompleted(false);
        stepConfig.setRetrying(false);
        // if the request didn't fail during the step execution
        if (context.isRequestAuthenticated()) {
            if (log.isDebugEnabled()) {
                log.debug("Step " + stepConfig.getOrder() + " is completed. Going to get the next one.");
            }
        } else {
            if (log.isDebugEnabled()) {
                log.debug("Authentication has failed in the Step " + (context.getCurrentStep()));
            }
            // authentication
            if (stepConfig.isMultiOption() && !context.isPassiveAuthenticate()) {
                stepConfig.setRetrying(true);
                context.setRequestAuthenticated(true);
            } else {
                FrameworkUtils.resetAuthenticationContext(context);
            }
        }
        FrameworkUtils.resetAuthenticationContext(context);
    }
    // if the sequence is not completed, we have work to do.
    if (log.isDebugEnabled()) {
        log.debug("Starting Step: " + stepConfig.getOrder());
    }
    AuthenticatorFlowStatus flowStatus = (AuthenticatorFlowStatus) request.getAttribute(FrameworkConstants.RequestParams.FLOW_STATUS);
    int stepNumber = context.getCurrentStep();
    if (!context.isReturning()) {
        if (stepNumber <= 0) {
            stepNumber = 1;
        } else if (flowStatus != FAIL_COMPLETED) {
            stepNumber++;
        }
        context.setCurrentStep(stepNumber);
        context.getSequenceConfig().getStepMap().put(stepNumber, stepConfig);
    }
    FrameworkUtils.getStepHandler().handle(request, response, context);
    flowStatus = (AuthenticatorFlowStatus) request.getAttribute(FrameworkConstants.RequestParams.FLOW_STATUS);
    if (flowStatus != SUCCESS_COMPLETED && flowStatus != INCOMPLETE && !(FAIL_COMPLETED.equals(flowStatus) && context.isRetrying())) {
        stepConfig.setSubjectAttributeStep(false);
        stepConfig.setSubjectIdentifierStep(false);
    }
    if (flowStatus == FAIL_COMPLETED) {
        if (!(stepConfigGraphNode.getNext() instanceof DynamicDecisionNode)) {
            if (context.isRetrying()) {
                StepConfigGraphNode newNextNode = new StepConfigGraphNode(stepConfigGraphNode.getStepConfig());
                newNextNode.setNext(stepConfigGraphNode.getNext());
                AuthGraphNode parentNode = stepConfigGraphNode.getParent();
                if (parentNode == null) {
                    parentNode = sequenceConfig.getAuthenticationGraph().getStartNode();
                }
                newNextNode.setParent(parentNode);
                if (parentNode instanceof DynamicDecisionNode) {
                    ((DynamicDecisionNode) parentNode).setDefaultEdge(newNextNode);
                } else if (parentNode instanceof StepConfigGraphNode) {
                    ((StepConfigGraphNode) parentNode).setNext(newNextNode);
                }
                stepConfigGraphNode.setNext(newNextNode);
            } else {
                stepConfigGraphNode.setNext(new FailNode());
            }
        }
    }
    // if step is not completed, that means step wants to redirect to outside
    if (!stepConfig.isCompleted()) {
        if (log.isDebugEnabled()) {
            log.debug("Step is not complete yet. Redirecting to outside.");
        }
        return true;
    }
    if (context.isPassiveAuthenticate() && !context.isRequestAuthenticated()) {
        return true;
    }
    context.setReturning(false);
    return false;
}
Also used : DynamicDecisionNode(org.wso2.carbon.identity.application.authentication.framework.config.model.graph.DynamicDecisionNode) FailNode(org.wso2.carbon.identity.application.authentication.framework.config.model.graph.FailNode) FrameworkException(org.wso2.carbon.identity.application.authentication.framework.exception.FrameworkException) StepConfig(org.wso2.carbon.identity.application.authentication.framework.config.model.StepConfig) AuthenticatorFlowStatus(org.wso2.carbon.identity.application.authentication.framework.AuthenticatorFlowStatus) StepConfigGraphNode(org.wso2.carbon.identity.application.authentication.framework.config.model.graph.StepConfigGraphNode) AuthGraphNode(org.wso2.carbon.identity.application.authentication.framework.config.model.graph.AuthGraphNode)

Example 4 with DynamicDecisionNode

use of org.wso2.carbon.identity.application.authentication.framework.config.model.graph.DynamicDecisionNode in project carbon-identity-framework by wso2.

the class GraphBasedSequenceHandler method modifyCurrentNodeAsFirstStep.

private void modifyCurrentNodeAsFirstStep(AuthenticationContext context) {
    context.removeProperty(BACK_TO_FIRST_STEP);
    if (context.getProperty(PROP_CURRENT_NODE) != null) {
        // Identifier first should be the first step. Other steps will be determine dynamically.
        int size = context.getSequenceConfig().getStepMap().size();
        for (int i = 2; i <= size; i++) {
            context.getSequenceConfig().getStepMap().remove(i);
        }
        context.getSequenceConfig().setCompleted(false);
        context.setProperty(PROP_CURRENT_NODE, null);
        AuthGraphNode startNode = context.getSequenceConfig().getAuthenticationGraph().getStartNode();
        if (startNode instanceof StepConfigGraphNode) {
            ((StepConfigGraphNode) startNode).getStepConfig().setCompleted(false);
            ((StepConfigGraphNode) startNode).getStepConfig().setAuthenticatedAutenticator(null);
            ((StepConfigGraphNode) startNode).getStepConfig().setAuthenticatedUser(null);
            if (((StepConfigGraphNode) startNode).getNext() instanceof DynamicDecisionNode) {
                ((DynamicDecisionNode) ((StepConfigGraphNode) startNode).getNext()).setDefaultEdge(new EndStep());
            }
        }
        if (log.isDebugEnabled()) {
            log.debug("Modified current node a parent node which can restart authentication flow" + " from first step.");
        }
    }
}
Also used : DynamicDecisionNode(org.wso2.carbon.identity.application.authentication.framework.config.model.graph.DynamicDecisionNode) StepConfigGraphNode(org.wso2.carbon.identity.application.authentication.framework.config.model.graph.StepConfigGraphNode) EndStep(org.wso2.carbon.identity.application.authentication.framework.config.model.graph.EndStep) AuthGraphNode(org.wso2.carbon.identity.application.authentication.framework.config.model.graph.AuthGraphNode)

Example 5 with DynamicDecisionNode

use of org.wso2.carbon.identity.application.authentication.framework.config.model.graph.DynamicDecisionNode in project carbon-identity-framework by wso2.

the class GraphBasedSequenceHandler method executeFunction.

private void executeFunction(String outcomeName, DynamicDecisionNode dynamicDecisionNode, AuthenticationContext context) {
    SerializableJsFunction fn = dynamicDecisionNode.getFunctionMap().get(outcomeName);
    FrameworkServiceDataHolder dataHolder = FrameworkServiceDataHolder.getInstance();
    JsGraphBuilderFactory jsGraphBuilderFactory = dataHolder.getJsGraphBuilderFactory();
    JsGraphBuilder graphBuilder = jsGraphBuilderFactory.createBuilder(context, context.getSequenceConfig().getAuthenticationGraph().getStepMap(), dynamicDecisionNode);
    JsGraphBuilder.JsBasedEvaluator jsBasedEvaluator = graphBuilder.new JsBasedEvaluator(fn);
    jsBasedEvaluator.evaluate(context, (jsConsumer) -> jsConsumer.call(null, new JsAuthenticationContext(context)));
    if (dynamicDecisionNode.getDefaultEdge() == null) {
        dynamicDecisionNode.setDefaultEdge(new EndStep());
    }
}
Also used : JsGraphBuilderFactory(org.wso2.carbon.identity.application.authentication.framework.config.model.graph.JsGraphBuilderFactory) JsAuthenticationContext(org.wso2.carbon.identity.application.authentication.framework.config.model.graph.js.JsAuthenticationContext) JsGraphBuilder(org.wso2.carbon.identity.application.authentication.framework.config.model.graph.JsGraphBuilder) FrameworkServiceDataHolder(org.wso2.carbon.identity.application.authentication.framework.internal.FrameworkServiceDataHolder) EndStep(org.wso2.carbon.identity.application.authentication.framework.config.model.graph.EndStep) SerializableJsFunction(org.wso2.carbon.identity.application.authentication.framework.config.model.graph.SerializableJsFunction)

Aggregations

EndStep (org.wso2.carbon.identity.application.authentication.framework.config.model.graph.EndStep)4 AuthGraphNode (org.wso2.carbon.identity.application.authentication.framework.config.model.graph.AuthGraphNode)3 JsGraphBuilder (org.wso2.carbon.identity.application.authentication.framework.config.model.graph.JsGraphBuilder)3 JsGraphBuilderFactory (org.wso2.carbon.identity.application.authentication.framework.config.model.graph.JsGraphBuilderFactory)3 SerializableJsFunction (org.wso2.carbon.identity.application.authentication.framework.config.model.graph.SerializableJsFunction)3 JsAuthenticationContext (org.wso2.carbon.identity.application.authentication.framework.config.model.graph.js.JsAuthenticationContext)3 FrameworkServiceDataHolder (org.wso2.carbon.identity.application.authentication.framework.internal.FrameworkServiceDataHolder)3 AuthenticatorFlowStatus (org.wso2.carbon.identity.application.authentication.framework.AuthenticatorFlowStatus)2 StepConfig (org.wso2.carbon.identity.application.authentication.framework.config.model.StepConfig)2 DynamicDecisionNode (org.wso2.carbon.identity.application.authentication.framework.config.model.graph.DynamicDecisionNode)2 FailNode (org.wso2.carbon.identity.application.authentication.framework.config.model.graph.FailNode)2 StepConfigGraphNode (org.wso2.carbon.identity.application.authentication.framework.config.model.graph.StepConfigGraphNode)2 HashMap (java.util.HashMap)1 AfterTest (org.testng.annotations.AfterTest)1 BeforeTest (org.testng.annotations.BeforeTest)1 Test (org.testng.annotations.Test)1 AbstractFrameworkTest (org.wso2.carbon.identity.application.authentication.framework.AbstractFrameworkTest)1 JsWritableParameters (org.wso2.carbon.identity.application.authentication.framework.config.model.graph.js.JsWritableParameters)1 AuthenticationContext (org.wso2.carbon.identity.application.authentication.framework.context.AuthenticationContext)1 FrameworkException (org.wso2.carbon.identity.application.authentication.framework.exception.FrameworkException)1