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;
}
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;
}
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");
}
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);
}
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);
}
Aggregations