Skip to content

Commit

Permalink
Add new functional interfaces for modified functions
Browse files Browse the repository at this point in the history
  • Loading branch information
shanggeeth committed Feb 29, 2024
1 parent 43a6b9a commit 001e009
Show file tree
Hide file tree
Showing 17 changed files with 179 additions and 33 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -165,8 +165,6 @@
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 @@ -173,8 +173,6 @@
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 @@ -19,7 +19,6 @@

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

import org.graalvm.polyglot.HostAccess;
import org.wso2.carbon.identity.application.authentication.framework.config.model.graph.js.JsServletRequest;

import java.util.Map;
Expand All @@ -37,7 +36,6 @@ public interface GetCookieFunction {
* @param params value mandatory and properties optional
* @return cookieValue
*/
@HostAccess.Export
String getCookieValue(JsServletRequest request, Object... params);

}
Original file line number Diff line number Diff line change
Expand Up @@ -128,8 +128,6 @@
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 @@ -106,7 +106,6 @@
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 @@ -114,7 +114,6 @@
org.wso2.carbon.identity.application.authentication.framework.exception; version="${carbon.identity.package.import.version.range}",
org.wso2.carbon.identity.application.authentication.framework.model; 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.authentication.framework.config.model; version="${carbon.identity.package.import.version.range}",
org.wso2.carbon.identity.application.authentication.framework.context; version="${carbon.identity.package.import.version.range}",
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
/*
* Copyright (c) 2024, WSO2 LLC. (http://www.wso2.com).
*
* WSO2 LLC. licenses this file to you under the Apache License,
* Version 2.0 (the "License"); you may not use this file except
* in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/

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

import java.util.Map;

@FunctionalInterface
public interface GetUserWithClaimValuesV2Function {

JsAuthenticatedUser getUniqueUserWithClaimValues(Map<String, String> claimMap,
JsAuthenticationContext authenticationContext,
String... parameters) throws FrameworkException;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
/*
* Copyright (c) 2024, WSO2 LLC. (http://www.wso2.com).
*
* WSO2 LLC. licenses this file to you under the Apache License,
* Version 2.0 (the "License"); you may not use this file except
* in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/

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

import org.graalvm.polyglot.HostAccess;
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.exception.FrameworkException;

import java.util.Map;

/**
* Implementation of GetUserWithClaimValuesV2Function. This will return a user given a set of claim values.
*/
public class GetUserWithClaimValuesV2FunctionImpl implements GetUserWithClaimValuesV2Function {

@Override
@HostAccess.Export
public JsAuthenticatedUser getUniqueUserWithClaimValues(Map<String, String> claimMap,
JsAuthenticationContext authenticationContext,
String... parameters) throws FrameworkException {

return new UserStoreFunctions().getUniqueUserWithClaimValues(claimMap, authenticationContext, parameters);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.graalvm.polyglot.HostAccess;
import org.wso2.carbon.CarbonConstants;
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;
Expand All @@ -46,11 +45,15 @@ public class UserStoreFunctions implements GetUserWithClaimValues {

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

@HostAccess.Export
public JsAuthenticatedUser getUniqueUserWithClaimValues(Map<String, String> claimMap, Object... parameters)
throws FrameworkException {
return getUniqueUserWithClaimValuesInternal(claimMap, parameters);
}

private JsAuthenticatedUser getUniqueUserWithClaimValuesInternal(Map<String, String> claimMap, Object... parameters)
throws FrameworkException {

JsAuthenticationContext authenticationContext = null;
JsAuthenticationContext authenticationContext = null;
String tenantDomain = null;
String profile = "default";
if (claimMap == null || parameters == null) {
Expand All @@ -63,15 +66,15 @@ public JsAuthenticatedUser getUniqueUserWithClaimValues(Map<String, String> clai
if (parameters.length == 2) {
if ( parameters[0] instanceof JsAuthenticationContext) {
authenticationContext = (JsAuthenticationContext) parameters[0];
tenantDomain = authenticationContext.getWrapped().getTenantDomain();
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.getWrapped().getTenantDomain();
tenantDomain = authenticationContext.getContext().getTenantDomain();
}
if (tenantDomain != null) {
int tenantId = IdentityTenantUtil.getTenantId(tenantDomain);
Expand Down Expand Up @@ -121,6 +124,10 @@ public JsAuthenticatedUser getUniqueUserWithClaimValues(Map<String, String> clai
}
authenticatedUser.setUserName(username);
authenticatedUser.setTenantDomain(tenantDomain);
if (authenticationContext != null) {
return (JsAuthenticatedUser) JsWrapperFactoryProvider.getInstance().getWrapperFactory().
createJsAuthenticatedUser(authenticatedUser);
}
return (JsAuthenticatedUser) JsWrapperFactoryProvider.getInstance().getWrapperFactory().
createJsAuthenticatedUser(authenticationContext.getWrapped(), authenticatedUser);
} else {
Expand All @@ -136,4 +143,5 @@ public JsAuthenticatedUser getUniqueUserWithClaimValues(Map<String, String> clai
}
return null;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,7 @@
import org.osgi.service.component.annotations.ReferenceCardinality;
import org.osgi.service.component.annotations.ReferencePolicy;
import org.wso2.carbon.identity.application.authentication.framework.JsFunctionRegistry;
import org.wso2.carbon.identity.conditional.auth.functions.user.store.GetUserWithClaimValues;
import org.wso2.carbon.identity.conditional.auth.functions.user.store.UserStoreFunctions;
import org.wso2.carbon.identity.conditional.auth.functions.user.store.GetUserWithClaimValuesV2FunctionImpl;
import org.wso2.carbon.user.core.service.RealmService;

/**
Expand All @@ -48,12 +47,12 @@ public class UserStoreFunctionsServiceComponent {
protected void activate(ComponentContext ctxt) {

try {
UserStoreFunctions storeFunctions = new UserStoreFunctions();
GetUserWithClaimValuesV2FunctionImpl storeFunctions = new GetUserWithClaimValuesV2FunctionImpl();
JsFunctionRegistry jsFunctionRegistry = UserStoreFunctionsServiceHolder.getInstance()
.getJsFunctionRegistry();

jsFunctionRegistry.register(JsFunctionRegistry.Subsystem.SEQUENCE_HANDLER, "getUniqueUserWithClaimValues",
(GetUserWithClaimValues) storeFunctions::getUniqueUserWithClaimValues);
new GetUserWithClaimValuesV2FunctionImpl());
} catch (Throwable e) {
LOG.error("Error occurred during conditional authentication user functions bundle activation. ", e);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -165,8 +165,6 @@
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.js.nashorn;
version="${carbon.identity.package.import.version.range}",
org.wso2.carbon.identity.application.authentication.framework.config.model.graph.js.openjdk.*;
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.user;

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

import java.util.List;

Expand All @@ -35,5 +35,5 @@ public interface GetAuthenticatedApplicationsFunction {
* @param context context object.
* @return List of already authenticated applications of the given session.
*/
List<JsApplication> getAuthenticatedApplications(JsAuthenticationContext context);
List<Application> getAuthenticatedApplications(JsAuthenticationContext context);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
/*
* Copyright (c) 2024, WSO2 LLC. (http://www.wso2.com).
*
* WSO2 LLC. licenses this file to you under the Apache License,
* Version 2.0 (the "License"); you may not use this file except
* in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/

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

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

import java.util.List;

/**
* Function definition for retrieving authenticated applications.
*/
@FunctionalInterface
public interface GetAuthenticatedApplicationsV2Function {

/**
* Retrieve the already authenticated applications for a given session.
*
* @param context context object.
* @return List of already authenticated applications of the given session.
*/
List<JsApplication> getAuthenticatedApplications(JsAuthenticationContext context);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
/*
* Copyright (c) 2024, WSO2 LLC. (http://www.wso2.com).
*
* WSO2 LLC. licenses this file to you under the Apache License,
* Version 2.0 (the "License"); you may not use this file except
* in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/

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

import org.graalvm.polyglot.HostAccess;
import org.wso2.carbon.identity.application.authentication.framework.config.model.graph.js.JsAuthenticationContext;
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.List;
import java.util.stream.Collectors;

/**
* Function for retrieving javascript authenticated applications for a given session.
*/
public class GetAuthenticatedApplicationsV2FunctionImpl implements GetAuthenticatedApplicationsV2Function {

@Override
@HostAccess.Export
public List<JsApplication> getAuthenticatedApplications(JsAuthenticationContext context) {

return new GetAuthenticatedAppsFuncImp().getAuthenticatedApplications(context)
.stream()
.map(app -> JsWrapperFactoryProvider.getInstance().getWrapperFactory().createJsApplication(app))
.collect(Collectors.toList());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
import org.wso2.carbon.identity.application.authentication.framework.config.model.graph.js.JsAuthenticationContext;
import org.wso2.carbon.identity.application.authentication.framework.exception.session.mgt.SessionManagementException;
import org.wso2.carbon.identity.application.authentication.framework.model.UserSession;
import org.wso2.carbon.identity.application.authentication.framework.model.Application;
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;
Expand All @@ -47,17 +48,15 @@ public class GetAuthenticatedAppsFuncImp implements GetAuthenticatedApplications
*/
@Override
@HostAccess.Export
public List<JsApplication> getAuthenticatedApplications(JsAuthenticationContext context) {
public List<Application> getAuthenticatedApplications(JsAuthenticationContext context) {

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

return userSession.getApplications().stream()
.map(app -> JsWrapperFactoryProvider.getInstance().getWrapperFactory().createJsApplication(app))
.collect(Collectors.toList());
return userSession.getApplications();
}
} 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,6 @@

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

import org.graalvm.polyglot.HostAccess;
import org.wso2.carbon.identity.application.authentication.framework.config.model.graph.js.JsAuthenticatedUser;

/**
Expand All @@ -38,6 +37,5 @@ public interface HasRoleFunction {
* @param roleName Role to be checked
* @return <code>true</code> if the user has the given role. <code>false</code> for any other case.
*/
@HostAccess.Export
boolean hasRole(JsAuthenticatedUser user, String roleName);
}
Loading

0 comments on commit 001e009

Please sign in to comment.