Skip to content

Commit

Permalink
Add GraalJS support for conditional authentication functions.
Browse files Browse the repository at this point in the history
* Introduce GraalJS proxy objects.
* Add wrapper factory for GraalJS proxy objects.
  • Loading branch information
shanggeeth committed Jan 9, 2024
1 parent e14acfa commit 04ba628
Show file tree
Hide file tree
Showing 7 changed files with 275 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,11 @@
<artifactId>nashorn-core</artifactId>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.graalvm.sdk</groupId>
<artifactId>graal-sdk</artifactId>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.wso2.orbit.com.nimbusds</groupId>
<artifactId>nimbus-jose-jwt</artifactId>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
/*
* Copyright (c) 2023, 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.model;

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

/**
* Factory to create a Javascript Object Wrappers for GraalJS execution.
* Since Nashorn is deprecated in JDK 11 and onwards. We are introducing GraalJS engine.
*/
public class JsGraalWrapperFactory implements JsWrapperBaseFactory {

@Override
public JsUserSession createJsUserSession(UserSession userSession) {

return new JsGraalUserSession(userSession);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@

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

import org.wso2.carbon.identity.application.authentication.framework.config.model.graph.JsBaseGraphBuilderFactory;
import org.wso2.carbon.identity.application.authentication.framework.config.model.graph.JsGraphBuilderFactory;
import org.wso2.carbon.identity.application.authentication.framework.config.model.graph.openjdk.nashorn.JsOpenJdkNashornGraphBuilderFactory;
import org.wso2.carbon.identity.application.authentication.framework.util.FrameworkUtils;

Expand All @@ -32,8 +34,11 @@ public class JsWrapperFactoryProvider {

private JsWrapperFactoryProvider() {

if (FrameworkUtils.createJsGraphBuilderFactoryFromConfig() instanceof JsOpenJdkNashornGraphBuilderFactory) {
JsBaseGraphBuilderFactory jsGraphBuilderFactory = FrameworkUtils.createJsGraphBuilderFactoryFromConfig();
if (jsGraphBuilderFactory instanceof JsOpenJdkNashornGraphBuilderFactory) {
jsWrapperBaseFactory = new JsOpenJdkNashornWrapperFactory();
} else if (jsGraphBuilderFactory instanceof JsGraalWrapperFactory) {
jsWrapperBaseFactory = new JsGraalWrapperFactory();
} else {
jsWrapperBaseFactory = new JsWrapperFactory();
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
/*
* Copyright (c) 2023, 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.model.graaljs;

import org.graalvm.polyglot.Value;
import org.graalvm.polyglot.proxy.ProxyArray;
import org.graalvm.polyglot.proxy.ProxyObject;
import org.wso2.carbon.identity.application.authentication.framework.model.Application;
import org.wso2.carbon.identity.conditional.auth.functions.user.model.JsApplication;

/**
* Javascript wrapper for Java level Application.
* This provides controlled access to UserSession object via provided javascript native syntax.
* Also, it prevents writing an arbitrary values to the respective fields, keeping consistency on runtime
* AuthenticatedUser.
*
* @see Application
*/
public class JsGraalApplication extends JsApplication implements ProxyObject {

public JsGraalApplication(Application wrappedApplication) {

super(wrappedApplication);
}

@Override
public Object getMemberKeys() {

return ProxyArray.fromArray("subject", "appName", "appId");
}

@Override
public Object getMember(String name) {

switch (name) {
case "subject":
return getWrapped().getSubject();
case "appName":
return getWrapped().getAppName();
case "appId":
return getWrapped().getAppId();
default:
return super.getMember(name);
}
}

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

// read-only object.
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
/*
* Copyright (c) 2023, 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.model.graaljs;

import org.graalvm.polyglot.Value;
import org.graalvm.polyglot.proxy.ProxyObject;
import org.wso2.carbon.identity.conditional.auth.functions.user.model.JsUserAgent;
import org.wso2.carbon.identity.core.model.UserAgent;

/**
* Javascript wrapper for Java level UserAgent.
* This provides controlled access to UserSession object via provided javascript native syntax.
* Also, it prevents writing an arbitrary values to the respective fields, keeping consistency on runtime
* AuthenticatedUser.
*
* @see UserAgent
*/
public class JsGraalUserAgent extends JsUserAgent implements ProxyObject {

public JsGraalUserAgent(UserAgent wrappedUserAgent) {

super(wrappedUserAgent);
}

@Override
public Object getMemberKeys() {

return new String[]{"rawString", "browser", "platform", "device"};
}

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

// read-only object.
}

@Override
public Object getMember(String name) {

switch (name) {
case "rawString":
return getWrapped().getRawString();
case "browser":
return getWrapped().getBrowser();
case "platform":
return getWrapped().getPlatform();
case "device":
return getWrapped().getDevice();
default:
return super.getMember(name);
}
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
/*
* Copyright (c) 2023, 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.model.graaljs;

import org.graalvm.polyglot.Value;
import org.graalvm.polyglot.proxy.ProxyObject;
import org.wso2.carbon.identity.application.authentication.framework.model.UserSession;
import org.wso2.carbon.identity.conditional.auth.functions.user.model.JsUserSession;
import org.wso2.carbon.identity.conditional.auth.functions.user.model.nashorn.JsNashornApplication;
import org.wso2.carbon.identity.conditional.auth.functions.user.model.nashorn.JsNashornUserAgent;
import org.wso2.carbon.identity.core.model.UserAgent;

import java.util.stream.Collectors;

/**
* Javascript wrapper for Java level UserSession.
* This provides controlled access to UserSession object via provided javascript native syntax.
* Also it prevents writing an arbitrary values to the respective fields, keeping consistency on runtime
* AuthenticatedUser.
*
* @see UserSession
*/
public class JsGraalUserSession extends JsUserSession implements ProxyObject {

private final UserAgent userAgent;

public JsGraalUserSession(UserSession wrappedUserSession) {

super(wrappedUserSession);
userAgent = new UserAgent(wrappedUserSession.getUserAgent());
}

@Override
public Object getMemberKeys() {

return new String[]{"id", "createdTimestamp", "lastAccessTime", "tenantDomain", "user", "application",
"userAgent"};
}

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

}

@Override
public Object getMember(String name) {

switch (name) {
case "userAgent":
return new JsNashornUserAgent(userAgent);
case "ip":
return getWrapped().getIp();
case "loginTime":
return getWrapped().getLoginTime();
case "lastAccessTime":
return getWrapped().getLastAccessTime();
case "id":
return getWrapped().getSessionId();
case "applications":
return getWrapped().getApplications().stream().map(JsNashornApplication::new)
.collect(Collectors.toList());
default:
return super.getMember(name);
}
}

}
9 changes: 8 additions & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -289,6 +289,12 @@
<version>${nashorn.core.version}</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.graalvm.sdk</groupId>
<artifactId>graal-sdk</artifactId>
<version>${graalvm.version}</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>javax.ws.rs</groupId>
<artifactId>jsr311-api</artifactId>
Expand Down Expand Up @@ -483,7 +489,7 @@
<carbon.kernel.version>4.9.17</carbon.kernel.version>
<carbon.kernel.package.import.version.range>[4.6.0, 5.0.0)</carbon.kernel.package.import.version.range>
<carbon.user.package.import.version.range>[1.0.1, 2.0.0)</carbon.user.package.import.version.range>
<carbon.identity.framework.version>5.25.509</carbon.identity.framework.version>
<carbon.identity.framework.version>5.25.643-SNAPSHOT</carbon.identity.framework.version>
<identity.organization.management.core.version>1.0.89</identity.organization.management.core.version>
<carbon.identity.framework.testutils.version>5.20.447</carbon.identity.framework.testutils.version>
<carbon.identity.package.import.version.range>[5.14.0, 7.0.0)</carbon.identity.package.import.version.range>
Expand Down Expand Up @@ -533,6 +539,7 @@
<org.osgi.framework.imp.pkg.version.range>[1.9.0,2.0.0)</org.osgi.framework.imp.pkg.version.range>
<xml.apis.version>1.4.01</xml.apis.version>
<nashorn.core.version>15.3</nashorn.core.version>
<graalvm.version>20.2.0</graalvm.version>
<sonar.coverage.exclusions>
**/*Exception.java,
**/*Constants*.java,
Expand Down

0 comments on commit 04ba628

Please sign in to comment.