Search in sources :

Example 1 with JsGraphBuilder

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

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

the class JsGraphBuilder method createWith.

/**
 * Creates the graph with the given Script and step map.
 *
 * @param script the Dynamic authentication script.
 */
public JsGraphBuilder createWith(String script) {
    try {
        currentBuilder.set(this);
        Bindings globalBindings = engine.getBindings(ScriptContext.GLOBAL_SCOPE);
        globalBindings.put(FrameworkConstants.JSAttributes.JS_FUNC_EXECUTE_STEP, (StepExecutor) this::executeStep);
        globalBindings.put(FrameworkConstants.JSAttributes.JS_FUNC_SEND_ERROR, (BiConsumer<String, Map>) this::sendError);
        globalBindings.put(FrameworkConstants.JSAttributes.JS_AUTH_FAILURE, (FailAuthenticationFunction) this::fail);
        globalBindings.put(FrameworkConstants.JSAttributes.JS_FUNC_SHOW_PROMPT, (PromptExecutor) this::addShowPrompt);
        globalBindings.put(FrameworkConstants.JSAttributes.JS_FUNC_LOAD_FUNC_LIB, (LoadExecutor) this::loadLocalLibrary);
        JsFunctionRegistry jsFunctionRegistrar = FrameworkServiceDataHolder.getInstance().getJsFunctionRegistry();
        if (jsFunctionRegistrar != null) {
            Map<String, Object> functionMap = jsFunctionRegistrar.getSubsystemFunctionsMap(JsFunctionRegistry.Subsystem.SEQUENCE_HANDLER);
            functionMap.forEach(globalBindings::put);
        }
        Invocable invocable = (Invocable) engine;
        engine.eval(FrameworkServiceDataHolder.getInstance().getCodeForRequireFunction());
        removeDefaultFunctions(engine);
        String identifier = UUID.randomUUID().toString();
        JSExecutionMonitorData scriptExecutionData;
        try {
            startScriptExecutionMonitor(identifier, authenticationContext);
            engine.eval(script);
            invocable.invokeFunction(FrameworkConstants.JSAttributes.JS_FUNC_ON_LOGIN_REQUEST, new JsAuthenticationContext(authenticationContext));
        } finally {
            scriptExecutionData = endScriptExecutionMonitor(identifier);
        }
        storeAuthScriptExecutionMonitorData(authenticationContext, scriptExecutionData);
        JsGraphBuilderFactory.persistCurrentContext(authenticationContext, engine);
    } catch (ScriptException e) {
        result.setBuildSuccessful(false);
        result.setErrorReason("Error in executing the Javascript. Nested exception is: " + e.getMessage());
        if (log.isDebugEnabled()) {
            log.debug("Error in executing the Javascript.", e);
        }
    } catch (NoSuchMethodException e) {
        result.setBuildSuccessful(false);
        result.setErrorReason("Error in executing the Javascript. " + FrameworkConstants.JSAttributes.JS_FUNC_ON_LOGIN_REQUEST + " function is not defined.");
        if (log.isDebugEnabled()) {
            log.debug("Error in executing the Javascript.", e);
        }
    } finally {
        clearCurrentBuilder();
    }
    return this;
}
Also used : JsFunctionRegistry(org.wso2.carbon.identity.application.authentication.framework.JsFunctionRegistry) Bindings(javax.script.Bindings) Invocable(javax.script.Invocable) ScriptException(javax.script.ScriptException) JsAuthenticationContext(org.wso2.carbon.identity.application.authentication.framework.config.model.graph.js.JsAuthenticationContext) JSObject(jdk.nashorn.api.scripting.JSObject) BiConsumer(java.util.function.BiConsumer)

Example 3 with JsGraphBuilder

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

the class JsGraphBuilderTest method testParamsOptions.

@Test(dataProvider = "filterParamsDataProvider", alwaysRun = true)
public void testParamsOptions(Map<String, Object> options, StepConfig stepConfig, String authenticatorName, String key, String value) throws Exception {
    ServiceProvider sp1 = getTestServiceProvider("js-sp-1.xml");
    AuthenticationContext context = getAuthenticationContext(sp1);
    Map<Integer, StepConfig> stepConfigMap = new HashMap<>();
    stepConfigMap.put(1, stepConfig);
    JsGraphBuilder jsGraphBuilder = jsGraphBuilderFactory.createBuilder(context, stepConfigMap);
    jsGraphBuilder.authenticatorParamsOptions(options, stepConfig);
    assertEquals(context.getAuthenticatorParams(authenticatorName).get(key), value, "Params are not set expected");
}
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 JsGraphBuilder

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

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

Aggregations

HashMap (java.util.HashMap)6 StepConfig (org.wso2.carbon.identity.application.authentication.framework.config.model.StepConfig)6 AfterTest (org.testng.annotations.AfterTest)5 BeforeTest (org.testng.annotations.BeforeTest)5 Test (org.testng.annotations.Test)5 AbstractFrameworkTest (org.wso2.carbon.identity.application.authentication.framework.AbstractFrameworkTest)5 AuthenticationContext (org.wso2.carbon.identity.application.authentication.framework.context.AuthenticationContext)5 ServiceProvider (org.wso2.carbon.identity.application.common.model.ServiceProvider)5 JsGraphBuilder (org.wso2.carbon.identity.application.authentication.framework.config.model.graph.JsGraphBuilder)4 JsGraphBuilderFactory (org.wso2.carbon.identity.application.authentication.framework.config.model.graph.JsGraphBuilderFactory)4 JsAuthenticationContext (org.wso2.carbon.identity.application.authentication.framework.config.model.graph.js.JsAuthenticationContext)4 SerializableJsFunction (org.wso2.carbon.identity.application.authentication.framework.config.model.graph.SerializableJsFunction)3 FrameworkServiceDataHolder (org.wso2.carbon.identity.application.authentication.framework.internal.FrameworkServiceDataHolder)3 EndStep (org.wso2.carbon.identity.application.authentication.framework.config.model.graph.EndStep)2 BiConsumer (java.util.function.BiConsumer)1 Bindings (javax.script.Bindings)1 Invocable (javax.script.Invocable)1 ScriptException (javax.script.ScriptException)1 JSObject (jdk.nashorn.api.scripting.JSObject)1 JsFunctionRegistry (org.wso2.carbon.identity.application.authentication.framework.JsFunctionRegistry)1