Skip to content

Commit

Permalink
Fix bugs related to bound host functions
Browse files Browse the repository at this point in the history
* Fix GetUserWithClaimValues
* Fix getAuthenticatedApplications
* Add hasMember methods to Proxy objects
* Add support for JsGraalApplication
* Fix UserStoreFunctions to support GraalJs
  • Loading branch information
shanggeeth committed Jan 31, 2024
1 parent b0cbb7c commit 82fa5a6
Show file tree
Hide file tree
Showing 11 changed files with 71 additions and 27 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -19,13 +19,14 @@
package org.wso2.carbon.identity.conditional.auth.functions.user.store;

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.JsBaseAuthenticationContext;
import org.wso2.carbon.identity.application.authentication.framework.exception.FrameworkException;

import java.util.Map;

@FunctionalInterface
public interface GetUserWithClaimValues {

JsAuthenticatedUser getUniqueUserWithClaimValues(Map<String, String> claimMap, Object... parameters) throws
FrameworkException;
JsAuthenticatedUser getUniqueUserWithClaimValues(Map<String, String> claimMap, JsBaseAuthenticationContext context,
String... parameters) throws FrameworkException;
}
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@
import org.wso2.carbon.identity.application.authentication.framework.config.model.graph.JsWrapperFactoryProvider;
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.JsAuthenticationContext;
import org.wso2.carbon.identity.application.authentication.framework.config.model.graph.js.base.JsBaseAuthenticationContext;
import org.wso2.carbon.identity.application.authentication.framework.context.AuthenticationContext;
import org.wso2.carbon.identity.application.authentication.framework.exception.FrameworkException;
import org.wso2.carbon.identity.application.authentication.framework.model.AuthenticatedUser;
import org.wso2.carbon.identity.conditional.auth.functions.user.store.internal.UserStoreFunctionsServiceHolder;
Expand All @@ -45,30 +47,22 @@ public class UserStoreFunctions implements GetUserWithClaimValues {

private static final Log LOG = LogFactory.getLog(UserStoreFunctions.class);

public JsAuthenticatedUser getUniqueUserWithClaimValues(Map<String, String> claimMap, Object... parameters)
public JsAuthenticatedUser getUniqueUserWithClaimValues(Map<String, String> claimMap,
JsBaseAuthenticationContext context, String... parameters)
throws FrameworkException {

JsAuthenticationContext authenticationContext = null;
String tenantDomain = null;
AuthenticationContext authenticationContext = context.getWrapped();
String tenantDomain = authenticationContext.getTenantDomain();
String profile = "default";
if (claimMap == null || parameters == null) {
if (LOG.isDebugEnabled()) {
LOG.debug("Passed parameter to getUniqueUserWithClaimValues method has null values");
}
return null;
}
if (parameters.length == 2) {
if ( parameters[0] instanceof JsAuthenticationContext) {
authenticationContext = (JsAuthenticationContext) parameters[0];
tenantDomain = authenticationContext.getContext().getTenantDomain();
}
if ( parameters[1] instanceof String) {
profile = (String) parameters[1];
}
}
if (parameters.length == 1 && parameters[0] instanceof JsAuthenticationContext) {
authenticationContext = (JsAuthenticationContext) parameters[0];
tenantDomain = authenticationContext.getContext().getTenantDomain();

if (parameters.length == 1) {
profile = parameters[0];
}

if (tenantDomain != null) {
Expand Down Expand Up @@ -119,12 +113,8 @@ public JsAuthenticatedUser getUniqueUserWithClaimValues(Map<String, String> clai
}
authenticatedUser.setUserName(username);
authenticatedUser.setTenantDomain(tenantDomain);
if (authenticationContext != null) {
return (JsAuthenticatedUser) JsWrapperFactoryProvider.getInstance().getWrapperFactory().
createJsAuthenticatedUser(authenticationContext.getContext(), authenticatedUser);
}
return (JsAuthenticatedUser) JsWrapperFactoryProvider.getInstance().getWrapperFactory().
createJsAuthenticatedUser(authenticatedUser);
createJsAuthenticatedUser(authenticationContext, authenticatedUser);
} else {
LOG.error("Cannot find the user realm for the given tenant: " + tenantId);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@

import org.wso2.carbon.identity.application.authentication.framework.config.model.graph.js.base.JsBaseAuthenticationContext;
import org.wso2.carbon.identity.application.authentication.framework.model.Application;
import org.wso2.carbon.identity.conditional.auth.functions.user.model.JsApplication;

import java.util.List;

Expand All @@ -35,5 +36,5 @@ public interface GetAuthenticatedApplicationsFunction {
* @param context context object.
* @return List of already authenticated applications of the given session.
*/
List<Application> getAuthenticatedApplications(JsBaseAuthenticationContext context);
List<JsApplication> getAuthenticatedApplications(JsBaseAuthenticationContext context);
}
Original file line number Diff line number Diff line change
Expand Up @@ -22,12 +22,14 @@
import org.apache.commons.logging.LogFactory;
import org.wso2.carbon.identity.application.authentication.framework.config.model.graph.js.base.JsBaseAuthenticationContext;
import org.wso2.carbon.identity.application.authentication.framework.exception.session.mgt.SessionManagementException;
import org.wso2.carbon.identity.application.authentication.framework.model.Application;
import org.wso2.carbon.identity.application.authentication.framework.model.UserSession;
import org.wso2.carbon.identity.conditional.auth.functions.user.internal.UserFunctionsServiceHolder;
import org.wso2.carbon.identity.conditional.auth.functions.user.model.JsApplication;
import org.wso2.carbon.identity.conditional.auth.functions.user.model.JsWrapperFactoryProvider;

import java.util.Collections;
import java.util.List;
import java.util.stream.Collectors;

/**
* Function for retrieving authenticated applications for a given session.
Expand All @@ -43,14 +45,17 @@ public class GetAuthenticatedAppsFuncImp implements GetAuthenticatedApplications
* @return List of already authenticated applications of the given session.
*/
@Override
public List<Application> getAuthenticatedApplications(JsBaseAuthenticationContext context) {
public List<JsApplication> getAuthenticatedApplications(JsBaseAuthenticationContext context) {

String sessionContextKey = context.getWrapped().getSessionIdentifier();
try {
if (sessionContextKey != null) {
UserSession userSession = UserFunctionsServiceHolder.getInstance().getUserSessionManagementService()
.getUserSessionBySessionId(sessionContextKey).get();
return userSession.getApplications();

return userSession.getApplications().stream()
.map(app -> JsWrapperFactoryProvider.getInstance().getWrapperFactory().createJsApplication(app))
.collect(Collectors.toList());
}
} catch (SessionManagementException e) {
log.debug("Error occurred while retrieving the user session.");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,9 @@

package org.wso2.carbon.identity.conditional.auth.functions.user.model;

import org.wso2.carbon.identity.application.authentication.framework.model.Application;
import org.wso2.carbon.identity.application.authentication.framework.model.UserSession;
import org.wso2.carbon.identity.conditional.auth.functions.user.model.graaljs.JsGraalApplication;
import org.wso2.carbon.identity.conditional.auth.functions.user.model.graaljs.JsGraalUserSession;

/**
Expand All @@ -32,4 +34,10 @@ public JsUserSession createJsUserSession(UserSession userSession) {

return new JsGraalUserSession(userSession);
}

@Override
public JsApplication createJsApplication(Application application) {

return new JsGraalApplication(application);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,9 @@

package org.wso2.carbon.identity.conditional.auth.functions.user.model;

import org.wso2.carbon.identity.application.authentication.framework.model.Application;
import org.wso2.carbon.identity.application.authentication.framework.model.UserSession;
import org.wso2.carbon.identity.conditional.auth.functions.user.model.openjdk.nashorn.JsOpenJdkNashornApplication;
import org.wso2.carbon.identity.conditional.auth.functions.user.model.openjdk.nashorn.JsOpenJdkNashornUserSession;

/**
Expand All @@ -32,4 +34,9 @@ public JsUserSession createJsUserSession(UserSession userSession) {

return new JsOpenJdkNashornUserSession(userSession);
}

@Override
public JsApplication createJsApplication(Application application) {
return new JsOpenJdkNashornApplication(application);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@

package org.wso2.carbon.identity.conditional.auth.functions.user.model;

import org.wso2.carbon.identity.application.authentication.framework.model.Application;
import org.wso2.carbon.identity.application.authentication.framework.model.UserSession;

/**
Expand All @@ -32,5 +33,10 @@ public interface JsWrapperBaseFactory {
*/
JsUserSession createJsUserSession(UserSession userSession);


/**
* Creates a JavaScript Proxy for Application.
* @param application - Represent Application Subject
* @return Proxy for Application
*/
JsApplication createJsApplication(Application application);
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,9 @@

package org.wso2.carbon.identity.conditional.auth.functions.user.model;

import org.wso2.carbon.identity.application.authentication.framework.model.Application;
import org.wso2.carbon.identity.application.authentication.framework.model.UserSession;
import org.wso2.carbon.identity.conditional.auth.functions.user.model.nashorn.JsNashornApplication;
import org.wso2.carbon.identity.conditional.auth.functions.user.model.nashorn.JsNashornUserSession;

/**
Expand All @@ -30,4 +32,8 @@ public class JsWrapperFactory implements JsWrapperBaseFactory {
public JsUserSession createJsUserSession(UserSession userSession) {
return new JsNashornUserSession(userSession);
}

public JsApplication createJsApplication(Application application) {
return new JsNashornApplication(application);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,12 @@ public Object getMemberKeys() {
return ProxyArray.fromArray("subject", "appName", "appId");
}

@Override
public boolean hasMember(String key) {

return "subject".equals(key) || "appName".equals(key) || "appId".equals(key);
}

@Override
public Object getMember(String name) {

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,12 @@ public Object getMemberKeys() {
return new String[]{"rawString", "browser", "platform", "device"};
}

@Override
public boolean hasMember(String key) {

return "rawString".equals(key) || "browser".equals(key) || "platform".equals(key) || "device".equals(key);
}

@Override
public void putMember(String key, Value value) {

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,14 @@ public Object getMemberKeys() {
"userAgent"};
}

@Override
public boolean hasMember(String key) {

return "id".equals(key) || "createdTimestamp".equals(key) || "lastAccessTime".equals(key) ||
"tenantDomain".equals(key) || "user".equals(key) || "application".equals(key) ||
"userAgent".equals(key);
}

@Override
public void putMember(String key, Value value) {

Expand Down

0 comments on commit 82fa5a6

Please sign in to comment.