use of org.wso2.carbon.identity.application.authentication.framework.config.model.graph.js.JsAuthenticationContext 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.js.JsAuthenticationContext in project carbon-identity-framework by wso2.
the class JsAuthenticationContextTest method testClaimAssignment.
@Test
public void testClaimAssignment() throws ScriptException {
ClaimMapping claimMapping1 = ClaimMapping.build("", "", "", false);
ClaimMapping claimMapping2 = ClaimMapping.build("Test.Remote.Claim.Url.2", "Test.Remote.Claim.Url.2", "", false);
AuthenticatedUser authenticatedUser = new AuthenticatedUser();
authenticatedUser.getUserAttributes().put(claimMapping1, "TestClaimVal1");
authenticatedUser.getUserAttributes().put(claimMapping2, "TestClaimVal2");
AuthenticationContext authenticationContext = new AuthenticationContext();
setupAuthContextWithStepData(authenticationContext, authenticatedUser);
JsAuthenticationContext jsAuthenticationContext = new JsAuthenticationContext(authenticationContext);
Bindings bindings = scriptEngine.getBindings(ScriptContext.GLOBAL_SCOPE);
bindings.put("context", jsAuthenticationContext);
Object result = scriptEngine.eval("context.steps[1].subject.remoteClaims['Test.Remote.Claim.Url.1']");
assertNull(result);
result = scriptEngine.eval("context.steps[1].subject.remoteClaims['Test.Remote.Claim.Url.2']");
assertEquals(result, "TestClaimVal2");
scriptEngine.eval("context.steps[1].subject.remoteClaims['Test.Remote.Claim.Url.2'] = 'Modified2'");
result = scriptEngine.eval("context.steps[1].subject.remoteClaims['Test.Remote.Claim.Url.2']");
assertEquals(result, "Modified2");
}
use of org.wso2.carbon.identity.application.authentication.framework.config.model.graph.js.JsAuthenticationContext in project carbon-identity-framework by wso2.
the class JsAuthenticationContextTest method testGetLastLoginFailedUserNullFromWrappedContext.
@Test
public void testGetLastLoginFailedUserNullFromWrappedContext() throws Exception {
AuthenticationContext authenticationContext = new AuthenticationContext();
authenticationContext.setProperty(FrameworkConstants.JSAttributes.JS_LAST_LOGIN_FAILED_USER, null);
JsAuthenticationContext jsAuthenticationContext = new JsAuthenticationContext(authenticationContext);
Bindings bindings = scriptEngine.getBindings(ScriptContext.GLOBAL_SCOPE);
bindings.put("context", jsAuthenticationContext);
Object result = scriptEngine.eval("context.lastLoginFailedUser");
assertNull(result);
}
use of org.wso2.carbon.identity.application.authentication.framework.config.model.graph.js.JsAuthenticationContext 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());
}
}
use of org.wso2.carbon.identity.application.authentication.framework.config.model.graph.js.JsAuthenticationContext in project carbon-identity-framework by wso2.
the class GraphBasedSequenceHandlerCustomFunctionsTest method testHandleDynamicBoolean.
public void testHandleDynamicBoolean() throws Exception {
PrivilegedCarbonContext.getThreadLocalCarbonContext().setTenantDomain(MultitenantConstants.SUPER_TENANT_DOMAIN_NAME);
PrivilegedCarbonContext.getThreadLocalCarbonContext().setTenantId(MultitenantConstants.SUPER_TENANT_ID);
JsFunctionRegistry jsFunctionRegistrar = new JsFunctionRegistryImpl();
FrameworkServiceDataHolder.getInstance().setJsFunctionRegistry(jsFunctionRegistrar);
jsFunctionRegistrar.register(JsFunctionRegistry.Subsystem.SEQUENCE_HANDLER, "fn1", (Function<JsAuthenticationContext, String>) GraphBasedSequenceHandlerCustomFunctionsTest::customFunction1);
jsFunctionRegistrar.register(JsFunctionRegistry.Subsystem.SEQUENCE_HANDLER, "getTrueFunction", (Function<JsAuthenticationContext, Boolean>) GraphBasedSequenceHandlerCustomFunctionsTest::customBoolean);
jsFunctionRegistrar.register(JsFunctionRegistry.Subsystem.SEQUENCE_HANDLER, "getTrueFunction2", (BiFunction<JsAuthenticationContext, String, Boolean>) GraphBasedSequenceHandlerCustomFunctionsTest::customBoolean2);
ServiceProvider sp1 = getTestServiceProvider("js-sp-dynamic-1.xml");
String script = "var onLoginRequest = function(context) {\n" + " var myBool = getTrueFunction2(context, 'a');\n" + " Log.info(\"My Bool Value \"+myBool);\n" + " if(myBool) {\n" + " Log.info(\"My Bool Is Selected \"+myBool);\n" + " executeStep(1, {\n" + " onSuccess : function(context) {\n" + " executeStep(3);\n" + " }\n" + " });\n" + " executeStep(2);\n" + " } else {\n" + " Log.info(\"My Bool Not Selected \"+myBool);\n" + " executeStep(1);\n" + " executeStep(3);\n" + " }\n" + "};";
sp1.getLocalAndOutBoundAuthenticationConfig().getAuthenticationScriptConfig().setContent(script);
AuthenticationContext context = processAndGetAuthenticationContext(new String[0], sp1);
List<AuthHistory> authHistories = context.getAuthenticationStepHistory();
assertNotNull(authHistories);
assertEquals(authHistories.size(), 3);
assertEquals(authHistories.get(0).getAuthenticatorName(), "BasicMockAuthenticator");
assertEquals(authHistories.get(1).getAuthenticatorName(), "FptMockAuthenticator");
assertEquals(authHistories.get(2).getAuthenticatorName(), "HwkMockAuthenticator");
}
Aggregations