Search in sources :

Example 1 with AuthenticationGraph

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

the class UIBasedConfigurationLoader method getSequenceConfig.

@Override
public SequenceConfig getSequenceConfig(AuthenticationContext context, Map<String, String[]> parameterMap, ServiceProvider serviceProvider) throws FrameworkException {
    String tenantDomain = context.getTenantDomain();
    AuthenticationStep[] authenticationSteps = null;
    LocalAndOutboundAuthenticationConfig localAndOutboundAuthenticationConfig = serviceProvider.getLocalAndOutBoundAuthenticationConfig();
    if (localAndOutboundAuthenticationConfig.getAuthenticationSteps() != null && localAndOutboundAuthenticationConfig.getAuthenticationSteps().length > 0) {
        // Use the default steps when there are no chains configured.
        authenticationSteps = localAndOutboundAuthenticationConfig.getAuthenticationSteps();
    }
    SequenceConfig sequenceConfig = getSequence(serviceProvider, tenantDomain, authenticationSteps);
    // Use script based evaluation if script is present.
    if (isAuthenticationScriptBasedSequence(localAndOutboundAuthenticationConfig)) {
        // Clear the sequenceConfig step map, so that it will be re-populated by Dynamic execution
        Map<Integer, StepConfig> originalStepConfigMap = new HashMap<>(sequenceConfig.getStepMap());
        Map<Integer, StepConfig> stepConfigMapCopy = new HashMap<>();
        originalStepConfigMap.forEach((k, v) -> stepConfigMapCopy.put(k, new StepConfig(v)));
        sequenceConfig.getStepMap().clear();
        JsGraphBuilderFactory jsGraphBuilderFactory = FrameworkServiceDataHolder.getInstance().getJsGraphBuilderFactory();
        JsGraphBuilder jsGraphBuilder = jsGraphBuilderFactory.createBuilder(context, stepConfigMapCopy);
        context.setServiceProviderName(serviceProvider.getApplicationName());
        AuthenticationGraph graph = jsGraphBuilder.createWith(localAndOutboundAuthenticationConfig.getAuthenticationScriptConfig().getContent()).build();
        graph.setEnabled(localAndOutboundAuthenticationConfig.getAuthenticationScriptConfig().isEnabled());
        sequenceConfig.setAuthenticationGraph(graph);
        graph.setStepMap(originalStepConfigMap);
    }
    return sequenceConfig;
}
Also used : JsGraphBuilderFactory(org.wso2.carbon.identity.application.authentication.framework.config.model.graph.JsGraphBuilderFactory) LocalAndOutboundAuthenticationConfig(org.wso2.carbon.identity.application.common.model.LocalAndOutboundAuthenticationConfig) HashMap(java.util.HashMap) AuthenticationStep(org.wso2.carbon.identity.application.common.model.AuthenticationStep) StepConfig(org.wso2.carbon.identity.application.authentication.framework.config.model.StepConfig) JsGraphBuilder(org.wso2.carbon.identity.application.authentication.framework.config.model.graph.JsGraphBuilder) SequenceConfig(org.wso2.carbon.identity.application.authentication.framework.config.model.SequenceConfig) AuthenticationGraph(org.wso2.carbon.identity.application.authentication.framework.config.model.graph.AuthenticationGraph)

Example 2 with AuthenticationGraph

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

the class JsGraphBuilder method executeStepInAsyncEvent.

/**
 * Adds the step given by step ID tp the authentication graph.
 *
 * @param params params
 */
@SuppressWarnings("unchecked")
public void executeStepInAsyncEvent(int stepId, Object... params) {
    AuthenticationContext context = contextForJs.get();
    AuthGraphNode currentNode = dynamicallyBuiltBaseNode.get();
    if (log.isDebugEnabled()) {
        log.debug("Execute Step on async event. Step ID : " + stepId);
    }
    AuthenticationGraph graph = context.getSequenceConfig().getAuthenticationGraph();
    if (graph == null) {
        log.error("The graph happens to be null on the sequence config. Can not execute step : " + stepId);
        return;
    }
    StepConfig stepConfig = graph.getStepMap().get(stepId);
    if (stepConfig == null) {
        if (log.isDebugEnabled()) {
            log.debug("The stepConfig of the step ID : " + stepId + " is null");
        }
        return;
    }
    // Inorder to keep original stepConfig as a backup in AuthenticationGraph.
    StepConfig clonedStepConfig = new StepConfig(stepConfig);
    StepConfig stepConfigFromContext = null;
    if (MapUtils.isNotEmpty(context.getSequenceConfig().getStepMap())) {
        stepConfigFromContext = context.getSequenceConfig().getStepMap().values().stream().filter(contextStepConfig -> (stepConfig.getOrder() == contextStepConfig.getOrder())).findFirst().orElse(null);
    }
    clonedStepConfig.applyStateChangesToNewObjectFromContextStepMap(stepConfigFromContext);
    if (log.isDebugEnabled()) {
        log.debug("Found step for the Step ID : " + stepId + ", Step Config " + clonedStepConfig);
    }
    StepConfigGraphNode newNode = wrap(clonedStepConfig);
    if (currentNode == null) {
        if (log.isDebugEnabled()) {
            log.debug("Setting a new node at the first time. Node : " + newNode.getName());
        }
        dynamicallyBuiltBaseNode.set(newNode);
    } else {
        attachToLeaf(currentNode, newNode);
    }
    if (params.length > 0) {
        // if there is only one param, it is assumed to be the event listeners
        if (params[params.length - 1] instanceof Map) {
            attachEventListeners((Map<String, Object>) params[params.length - 1], newNode);
        } else {
            log.error("Invalid argument and hence ignored. Last argument should be a Map of event listeners.");
        }
    }
    if (params.length == 2) {
        // There is an argument with options present
        if (params[0] instanceof Map) {
            Map<String, Object> options = (Map<String, Object>) params[0];
            handleOptions(options, clonedStepConfig);
        }
    }
}
Also used : StringUtils(org.apache.commons.lang.StringUtils) Bindings(javax.script.Bindings) AuthenticationContext(org.wso2.carbon.identity.application.authentication.framework.context.AuthenticationContext) FrameworkConstants(org.wso2.carbon.identity.application.authentication.framework.util.FrameworkConstants) HashMap(java.util.HashMap) Function(java.util.function.Function) HashSet(java.util.HashSet) AuthenticationDecisionEvaluator(org.wso2.carbon.identity.application.authentication.framework.AuthenticationDecisionEvaluator) JSObject(jdk.nashorn.api.scripting.JSObject) Map(java.util.Map) JsFunctionRegistry(org.wso2.carbon.identity.application.authentication.framework.JsFunctionRegistry) BiConsumer(java.util.function.BiConsumer) ScriptException(javax.script.ScriptException) FrameworkServiceComponent(org.wso2.carbon.identity.application.authentication.framework.internal.FrameworkServiceComponent) Compilable(javax.script.Compilable) MapUtils(org.apache.commons.collections.MapUtils) StepConfig(org.wso2.carbon.identity.application.authentication.framework.config.model.StepConfig) ApplicationAuthenticatorService(org.wso2.carbon.identity.application.common.ApplicationAuthenticatorService) AsyncProcess(org.wso2.carbon.identity.application.authentication.framework.AsyncProcess) Set(java.util.Set) AuthenticatorConfig(org.wso2.carbon.identity.application.authentication.framework.config.model.AuthenticatorConfig) UUID(java.util.UUID) FunctionLibrary(org.wso2.carbon.identity.functions.library.mgt.model.FunctionLibrary) FederatedAuthenticatorConfig(org.wso2.carbon.identity.application.common.model.FederatedAuthenticatorConfig) Collectors(java.util.stream.Collectors) ScriptContext(javax.script.ScriptContext) Serializable(java.io.Serializable) FunctionLibraryManagementService(org.wso2.carbon.identity.functions.library.mgt.FunctionLibraryManagementService) List(java.util.List) Invocable(javax.script.Invocable) FrameworkServiceDataHolder(org.wso2.carbon.identity.application.authentication.framework.internal.FrameworkServiceDataHolder) CarbonContext(org.wso2.carbon.context.CarbonContext) CompiledScript(javax.script.CompiledScript) ScriptEngine(javax.script.ScriptEngine) Log(org.apache.commons.logging.Log) FunctionLibraryManagementException(org.wso2.carbon.identity.functions.library.mgt.exception.FunctionLibraryManagementException) ScriptObjectMirror(jdk.nashorn.api.scripting.ScriptObjectMirror) LogFactory(org.apache.commons.logging.LogFactory) JsAuthenticationContext(org.wso2.carbon.identity.application.authentication.framework.config.model.graph.js.JsAuthenticationContext) FrameworkUtils(org.wso2.carbon.identity.application.authentication.framework.util.FrameworkUtils) LocalAuthenticatorConfig(org.wso2.carbon.identity.application.common.model.LocalAuthenticatorConfig) Collections(java.util.Collections) AuthenticationContext(org.wso2.carbon.identity.application.authentication.framework.context.AuthenticationContext) JsAuthenticationContext(org.wso2.carbon.identity.application.authentication.framework.config.model.graph.js.JsAuthenticationContext) StepConfig(org.wso2.carbon.identity.application.authentication.framework.config.model.StepConfig) JSObject(jdk.nashorn.api.scripting.JSObject) HashMap(java.util.HashMap) Map(java.util.Map)

Example 3 with AuthenticationGraph

use of org.wso2.carbon.identity.application.authentication.framework.config.model.graph.AuthenticationGraph 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 4 with AuthenticationGraph

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

the class JsGraphBuilderTest method testCreateDirectJava.

@Test
public void testCreateDirectJava() throws Exception {
    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.executeStep(1);
    jsGraphBuilder.executeStep(2);
    AuthenticationGraph graph = jsGraphBuilder.build();
    assertNotNull(graph.getStartNode());
    assertTrue(graph.getStartNode() instanceof StepConfigGraphNode);
    StepConfigGraphNode firstStep = (StepConfigGraphNode) graph.getStartNode();
    assertNotNull(firstStep.getNext());
    assertTrue(firstStep.getNext() instanceof StepConfigGraphNode);
}
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 5 with AuthenticationGraph

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

the class AbstractApplicationAuthenticatorTest method testRetryAuthenticationEnabled.

@Test
public void testRetryAuthenticationEnabled(AuthenticationContext context) {
    when(context.getSequenceConfig()).thenReturn(sequenceConfig);
    when(context.getCurrentAuthenticator()).thenReturn("TestAuthenticator");
    Map<String, String> authParams = new HashMap<>();
    authParams.put(AbstractApplicationAuthenticator.ENABLE_RETRY_FROM_AUTHENTICATOR, "true");
    when(context.getAuthenticatorParams("TestAuthenticator")).thenReturn(authParams);
    AuthenticationGraph graph = new AuthenticationGraph();
    graph.setEnabled(true);
    when(sequenceConfig.getAuthenticationGraph()).thenReturn(graph);
    assertTrue(abstractApplicationAuthenticator.retryAuthenticationEnabled(context));
}
Also used : HashMap(java.util.HashMap) AuthenticationGraph(org.wso2.carbon.identity.application.authentication.framework.config.model.graph.AuthenticationGraph) Matchers.anyString(org.mockito.Matchers.anyString) Test(org.testng.annotations.Test) BeforeTest(org.testng.annotations.BeforeTest) PrepareForTest(org.powermock.core.classloader.annotations.PrepareForTest)

Aggregations

HashMap (java.util.HashMap)7 StepConfig (org.wso2.carbon.identity.application.authentication.framework.config.model.StepConfig)6 AuthenticationGraph (org.wso2.carbon.identity.application.authentication.framework.config.model.graph.AuthenticationGraph)5 BeforeTest (org.testng.annotations.BeforeTest)4 Test (org.testng.annotations.Test)4 SequenceConfig (org.wso2.carbon.identity.application.authentication.framework.config.model.SequenceConfig)4 AuthenticationContext (org.wso2.carbon.identity.application.authentication.framework.context.AuthenticationContext)4 AfterTest (org.testng.annotations.AfterTest)3 AbstractFrameworkTest (org.wso2.carbon.identity.application.authentication.framework.AbstractFrameworkTest)3 ServiceProvider (org.wso2.carbon.identity.application.common.model.ServiceProvider)3 Serializable (java.io.Serializable)1 Collections (java.util.Collections)1 HashSet (java.util.HashSet)1 List (java.util.List)1 Map (java.util.Map)1 Set (java.util.Set)1 UUID (java.util.UUID)1 BiConsumer (java.util.function.BiConsumer)1 Function (java.util.function.Function)1 Collectors (java.util.stream.Collectors)1