-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add GraalJS support for conditional authentication functions.
* Introduce GraalJS proxy objects. * Add wrapper factory for GraalJS proxy objects.
- Loading branch information
1 parent
e14acfa
commit 04ba628
Showing
7 changed files
with
275 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
35 changes: 35 additions & 0 deletions
35
...org/wso2/carbon/identity/conditional/auth/functions/user/model/JsGraalWrapperFactory.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
68 changes: 68 additions & 0 deletions
68
...so2/carbon/identity/conditional/auth/functions/user/model/graaljs/JsGraalApplication.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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. | ||
} | ||
} |
70 changes: 70 additions & 0 deletions
70
.../wso2/carbon/identity/conditional/auth/functions/user/model/graaljs/JsGraalUserAgent.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} | ||
|
||
} |
83 changes: 83 additions & 0 deletions
83
...so2/carbon/identity/conditional/auth/functions/user/model/graaljs/JsGraalUserSession.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters