Skip to content

Commit

Permalink
Fix bugs related to bound host functions
Browse files Browse the repository at this point in the history
* GraalJS cannot convert proxy objects to native JAVA objects
* Error when executing function ReferenceError: getCookieValue is not defined
* Unsupported operation identifier 'addCookie'
* Async processes are not getting executed
  • Loading branch information
shanggeeth committed Jan 12, 2024
1 parent 04ba628 commit b0cbb7c
Show file tree
Hide file tree
Showing 54 changed files with 131 additions and 113 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -165,6 +165,8 @@
version="${carbon.identity.package.import.version.range}",
org.wso2.carbon.identity.application.authentication.framework.config.model.graph.js;
version="${carbon.identity.package.import.version.range}",
org.wso2.carbon.identity.application.authentication.framework.config.model.graph.js.base;
version="${carbon.identity.package.import.version.range}",
org.wso2.carbon.identity.application.authentication.framework.config.model.graph;
version="${carbon.identity.package.import.version.range}",
org.wso2.carbon.identity.application.common.model; version="${carbon.identity.package.import.version.range}",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@
import java.net.SocketTimeoutException;
import java.net.URL;
import java.util.Collections;
import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.atomic.AtomicInteger;

Expand All @@ -63,11 +64,13 @@ public class CallAnalyticsFunctionImpl extends AbstractAnalyticsFunction impleme
public void callAnalytics(Map<String, String> metadata,
Map<String, Object> payloadData, Map<String, Object> eventHandlers) {

Map<String, String> metadataMap = new HashMap<>(metadata);
Map<String, Object> payloadDataMap = new HashMap<>(payloadData);
AsyncProcess asyncProcess = new AsyncProcess((authenticationContext, asyncReturn) -> {

String appName = metadata.get(PARAM_APP_NAME);
String inputStream = metadata.get(PARAM_INPUT_STREAM);
String receiverUrl = metadata.get(PARAM_EP_URL);
String appName = metadataMap.get(PARAM_APP_NAME);
String inputStream = metadataMap.get(PARAM_INPUT_STREAM);
String receiverUrl = metadataMap.get(PARAM_EP_URL);
String targetPath;
try {
if (appName != null && inputStream != null) {
Expand All @@ -92,7 +95,7 @@ public void callAnalytics(Map<String, String> metadata,

JSONObject jsonObject = new JSONObject();
JSONObject event = new JSONObject();
for (Map.Entry<String, Object> dataElements : payloadData.entrySet()) {
for (Map.Entry<String, Object> dataElements : payloadDataMap.entrySet()) {
event.put(dataElements.getKey(), dataElements.getValue());
}
jsonObject.put("event", event);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@

package org.wso2.carbon.identity.conditional.auth.functions.analytics;

import org.wso2.carbon.identity.application.authentication.framework.config.model.graph.js.JsAuthenticationContext;
import org.wso2.carbon.identity.application.authentication.framework.config.model.graph.js.base.JsBaseAuthenticationContext;

import java.util.Map;

Expand All @@ -35,5 +35,5 @@ public interface PublishToAnalyticsFunction {
* @param payloadData payload data.
*/
void publishToAnalytics(Map<String, String> metadata, Map<String, Object> payloadData,
JsAuthenticationContext context);
JsBaseAuthenticationContext context);
}
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.nio.client.CloseableHttpAsyncClient;
import org.json.simple.JSONObject;
import org.wso2.carbon.identity.application.authentication.framework.config.model.graph.js.JsAuthenticationContext;
import org.wso2.carbon.identity.application.authentication.framework.config.model.graph.js.base.JsBaseAuthenticationContext;
import org.wso2.carbon.identity.application.authentication.framework.exception.FrameworkException;
import org.wso2.carbon.identity.conditional.auth.functions.common.utils.CommonUtils;
import org.wso2.carbon.identity.event.IdentityEventException;
Expand All @@ -49,7 +49,7 @@ public class PublishToAnalyticsFunctionImpl extends AbstractAnalyticsFunction im

@Override
public void publishToAnalytics(Map<String, String> metadata, Map<String, Object> payloadData,
JsAuthenticationContext context) {
JsBaseAuthenticationContext context) {

String appName = metadata.get(PARAM_APP_NAME);
String inputStream = metadata.get(PARAM_INPUT_STREAM);
Expand All @@ -64,7 +64,7 @@ public void publishToAnalytics(Map<String, String> metadata, Map<String, Object>
LOG.error("Target path cannot be found.");
return;
}
String tenantDomain = context.getContext().getTenantDomain();
String tenantDomain = context.getWrapped().getTenantDomain();
String targetHostUrl = CommonUtils.getConnectorConfig(AnalyticsEngineConfigImpl.RECEIVER, tenantDomain);
if (targetHostUrl == null) {
LOG.error("Target host cannot be found.");
Expand Down Expand Up @@ -105,11 +105,11 @@ public void completed(final HttpResponse response) {
if (responseCode == 200) {
if (LOG.isDebugEnabled()) {
LOG.debug("Successfully published data to the analytics for session data key: " +
context.getContext().getContextIdentifier());
context.getWrapped().getContextIdentifier());
}
} else {
LOG.error("Error while publishing data to analytics engine for session data key: " +
context.getContext().getContextIdentifier() + ". Request completed successfully. " +
context.getWrapped().getContextIdentifier() + ". Request completed successfully. " +
"But response code was not 200");
}
}
Expand All @@ -118,25 +118,25 @@ public void completed(final HttpResponse response) {
public void failed(final Exception ex) {

LOG.error("Error while publishing data to analytics engine for session data key: " +
context.getContext().getContextIdentifier() + ". Request failed with: " + ex);
context.getWrapped().getContextIdentifier() + ". Request failed with: " + ex);
}

@Override
public void cancelled() {

LOG.error("Error while publishing data to analytics engine for session data key: " +
context.getContext().getContextIdentifier() + ". Request canceled.");
context.getWrapped().getContextIdentifier() + ". Request canceled.");
}
});
}

} catch (IOException e) {
LOG.error("Error while calling analytics engine for tenant: " + context.getContext().getTenantDomain(), e);
LOG.error("Error while calling analytics engine for tenant: " + context.getWrapped().getTenantDomain(), e);
} catch (IdentityEventException e) {
LOG.error("Error while preparing authentication information for tenant: " + context.getContext()
LOG.error("Error while preparing authentication information for tenant: " + context.getWrapped()
.getTenantDomain(), e);
} catch (FrameworkException e) {
LOG.error("Error while building client to invoke analytics engine for tenant: " + context.getContext()
LOG.error("Error while building client to invoke analytics engine for tenant: " + context.getWrapped()
.getTenantDomain(), e);
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,7 @@
import java.util.Base64;
import java.util.Collections;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.atomic.AtomicInteger;
Expand Down Expand Up @@ -125,10 +126,12 @@ public CallChoreoFunctionImpl() {
public void callChoreo(Map<String, String> connectionMetaData, Map<String, Object> payloadData,
Map<String, Object> eventHandlers) {

Map<String, String> connectionMetaDataMap = new HashMap<>(connectionMetaData);
Map<String, Object> payloadDataMap = new HashMap<>(payloadData);
AsyncProcess asyncProcess = new AsyncProcess((authenticationContext, asyncReturn) -> {
LOG.info("Starting the callChoreo function for session data key: " +
authenticationContext.getContextIdentifier());
String epUrl = connectionMetaData.get(URL_VARIABLE_NAME);
String epUrl = connectionMetaDataMap.get(URL_VARIABLE_NAME);
try {
if (!isValidChoreoDomain(epUrl)) {
LOG.error("Provided Url does not contain a configured choreo domain. Invalid Url: " + epUrl);
Expand All @@ -138,7 +141,7 @@ public void callChoreo(Map<String, String> connectionMetaData, Map<String, Objec

String tenantDomain = authenticationContext.getTenantDomain();
AccessTokenRequestHelper accessTokenRequestHelper = new AccessTokenRequestHelper(
connectionMetaData, asyncReturn, authenticationContext, payloadData);
connectionMetaDataMap, asyncReturn, authenticationContext, payloadDataMap);
String accessToken = choreoAccessTokenCache.getValueFromCache(accessTokenRequestHelper.getConsumerKey(),
tenantDomain);
if (StringUtils.isNotEmpty(accessToken) && !isTokenExpired(accessToken)) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,7 @@ public CallElasticFunctionImpl() {
@Override
public void callElastic(Map<String, String> params, Map<String, Object> eventHandlers) {

Map<String, String> paramsMap = new HashMap<>(params);
AsyncProcess asyncProcess = new AsyncProcess((authenticationContext, asyncReturn) -> {

try {
Expand All @@ -78,13 +79,13 @@ public void callElastic(Map<String, String> params, Map<String, Object> eventHan
throw new FrameworkException("Elasticsearch host cannot be found.");
}

HttpPost request = new HttpPost(elasticConfigProvider.getElasticSearchUrl(targetHostUrl,params));
HttpPost request = new HttpPost(elasticConfigProvider.getElasticSearchUrl(targetHostUrl,paramsMap));

request.setHeader(ACCEPT, TYPE_APPLICATION_JSON);
request.setHeader(CONTENT_TYPE, TYPE_APPLICATION_JSON);
handleAuthentication(request, authenticationContext.getTenantDomain());

String query = elasticConfigProvider.getQuery(params);
String query = elasticConfigProvider.getQuery(paramsMap);
request.setEntity(new StringEntity(query, StandardCharsets.UTF_8));

String[] targetHostUrls = targetHostUrl.split(";");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -173,6 +173,8 @@
version="${carbon.identity.package.import.version.range}",
org.wso2.carbon.identity.application.authentication.framework.config.model.graph.js;
version="${carbon.identity.package.import.version.range}",
org.wso2.carbon.identity.application.authentication.framework.config.model.graph.js.base;
version="${carbon.identity.package.import.version.range}",
org.wso2.carbon.identity.application.authentication.framework.config.model.graph;
version="${carbon.identity.package.import.version.range}",
org.wso2.carbon.identity.core.util; version="${carbon.identity.package.import.version.range}",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,8 @@
import org.wso2.carbon.core.util.CryptoException;
import org.wso2.carbon.core.util.CryptoUtil;
import org.wso2.carbon.core.util.SignatureUtil;
import org.wso2.carbon.identity.application.authentication.framework.config.model.graph.js.JsServletRequest;
import org.wso2.carbon.identity.application.authentication.framework.config.model.graph.js.JsServletResponse;
import org.wso2.carbon.identity.application.authentication.framework.config.model.graph.js.base.JsBaseServletRequest;
import org.wso2.carbon.identity.application.authentication.framework.config.model.graph.js.base.JsBaseServletResponse;
import org.wso2.carbon.identity.application.authentication.framework.util.FrameworkConstants;
import org.wso2.carbon.identity.conditional.auth.functions.http.util.HTTPConstants;

Expand All @@ -51,7 +51,7 @@ public class CookieFunctionImpl implements SetCookieFunction, GetCookieFunction
private static final String ENABLE_ADAPTIVE_SCRIPT_COOKIE_LEGACY_MODE = "enableAdaptiveScriptCookieLegacyMode";

@Override
public void setCookie(JsServletResponse response, String name, Object... params) {
public void setCookie(JsBaseServletResponse response, String name, Object... params) {

Map<String, Object> properties = null;
if (params.length == 0 || params.length > 2) {
Expand Down Expand Up @@ -117,11 +117,11 @@ public void setCookie(JsServletResponse response, String name, Object... params)
Optional.ofNullable((sameSite != null) ? SameSiteCookie.valueOf(sameSite) : null)
.ifPresent(cookie::setSameSite);
}
response.addCookie(cookie);
response.getWrapped().getWrapped().addCookie(cookie);
}

@Override
public String getCookieValue(JsServletRequest request, Object... params) {
public String getCookieValue(JsBaseServletRequest request, Object... params) {

Map<String, Object> properties = null;
if (params.length == 0 || params.length > 2) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@

package org.wso2.carbon.identity.conditional.auth.functions.http;

import org.wso2.carbon.identity.application.authentication.framework.config.model.graph.js.JsServletRequest;
import org.wso2.carbon.identity.application.authentication.framework.config.model.graph.js.base.JsBaseServletRequest;

import java.util.Map;

Expand All @@ -36,6 +36,6 @@ public interface GetCookieFunction {
* @param params value mandatory and properties optional
* @return cookieValue
*/
String getCookieValue(JsServletRequest request, Object... params);
String getCookieValue(JsBaseServletRequest request, Object... params);

}
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@

package org.wso2.carbon.identity.conditional.auth.functions.http;

import org.wso2.carbon.identity.application.authentication.framework.config.model.graph.js.JsServletResponse;
import org.wso2.carbon.identity.application.authentication.framework.config.model.graph.js.base.JsBaseServletResponse;

import java.util.Map;

Expand All @@ -37,5 +37,5 @@ public interface SetCookieFunction {
* @param params value of the cookie mandatory and optional parameter of cookie with two additional parameters
* encrypt and sign.
*/
void setCookie(JsServletResponse response, String name, Object... params);
void setCookie(JsBaseServletResponse response, String name, Object... params);
}
Original file line number Diff line number Diff line change
Expand Up @@ -128,6 +128,8 @@
version="${carbon.identity.package.import.version.range}",
org.wso2.carbon.identity.application.authentication.framework.config.model.graph.js;
version="${carbon.identity.package.import.version.range}",
org.wso2.carbon.identity.application.authentication.framework.config.model.graph.js.base;
version="${carbon.identity.package.import.version.range}",
org.wso2.carbon.identity.event; version="${carbon.identity.package.import.version.range}",
org.wso2.carbon.identity.event.event; version="${carbon.identity.package.import.version.range}",
org.wso2.carbon.identity.event.services; version="${carbon.identity.package.import.version.range}",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@

package org.wso2.carbon.identity.conditional.auth.functions.notification;

import org.wso2.carbon.identity.application.authentication.framework.config.model.graph.js.JsAuthenticatedUser;
import org.wso2.carbon.identity.application.authentication.framework.config.model.graph.js.base.JsBaseAuthenticatedUser;

import java.util.Map;

Expand All @@ -33,6 +33,6 @@ public interface SendEmailFunction {
* @return <code>true</code> if the email is successfully queued to be sent. <code>false</code> If the mail
* couldn't be queued due to any error.
*/
boolean sendMail(JsAuthenticatedUser user, String templateId, Map<String, String> paramMap);
boolean sendMail(JsBaseAuthenticatedUser user, String templateId, Map<String, String> paramMap);

}
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.wso2.carbon.identity.application.authentication.framework.config.model.graph.js.JsAuthenticatedUser;
import org.wso2.carbon.identity.application.authentication.framework.config.model.graph.js.base.JsBaseAuthenticatedUser;
import org.wso2.carbon.identity.conditional.auth.functions.notification.internal.NotificationFunctionServiceHolder;
import org.wso2.carbon.identity.event.IdentityEventConstants;
import org.wso2.carbon.identity.event.IdentityEventException;
Expand All @@ -40,7 +40,7 @@ public class SendEmailFunctionImpl implements SendEmailFunction {
private static final Log LOG = LogFactory.getLog(SendEmailFunctionImpl.class);

@Override
public boolean sendMail(JsAuthenticatedUser user, String templateId, Map<String, String> paramMap) {
public boolean sendMail(JsBaseAuthenticatedUser user, String templateId, Map<String, String> paramMap) {

String eventName = IdentityEventConstants.Event.TRIGGER_NOTIFICATION;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,7 @@
org.osgi.service.component,
org.wso2.carbon.identity.application.authentication.framework,
org.wso2.carbon.identity.application.authentication.framework.config.model.graph.js,
org.wso2.carbon.identity.application.authentication.framework.config.model.graph.js.base;
org.wso2.carbon.identity.application.authentication.framework.context,
org.wso2.carbon.identity.application.authentication.framework.exception,
org.wso2.carbon.identity.application.authentication.framework.model,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
*/
package org.wso2.carbon.identity.conditional.auth.functions.session.function;

import org.wso2.carbon.identity.application.authentication.framework.config.model.graph.js.JsAuthenticationContext;
import org.wso2.carbon.identity.application.authentication.framework.config.model.graph.js.base.JsBaseAuthenticationContext;

import java.util.Map;

Expand All @@ -35,5 +35,5 @@ public interface ExecuteActionFunction {
* @param map parameter map
* @return boolean value indicating the success or failure state of process
*/
boolean execute(JsAuthenticationContext context, Map<String, String> map);
boolean execute(JsBaseAuthenticationContext context, Map<String, String> map);
}
Loading

0 comments on commit b0cbb7c

Please sign in to comment.