Skip to content

Commit e7ca5a9

Browse files
committed
add cosolidate simplejwt
1 parent c01c21f commit e7ca5a9

3 files changed

Lines changed: 144 additions & 0 deletions

File tree

src/SUMMARY.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
- [GenAI Client](./design/genai-client.md)
1111
- [CCAC Token Exchange](./design/ccac-exchange.md)
1212
- [Client Simple Pool](./design/client-simplepool.md)
13+
- [Remove AbsSimJwt](./design/remove-simplejwtverify.md)
1314
- [Cross-Cutting-Concerns](./cross-cutting-concerns.md)
1415
- [Light-4j](./concern/light-4j.md)
1516
- [Http Handler](./concern/http-handler.md)
@@ -124,6 +125,7 @@
124125
- [Gemini Client](./concern/light-genai-4j/gemini-client.md)
125126
- [Bedrock Client](./concern/light-genai-4j/bedrock-client.md)
126127
- [Genai WebSocket Handler](./concern/light-genai-4j/genai-websocket-handler.md)
128+
- [Code Executor](./concern/light-genai-4j/code-executor.md)
127129
- [Example](./example.md)
128130
- [Light-websocket-4j](./example/light-websocket-4j.md)
129131
- [llmchat-server](./example/light-websocket-4j/llmchat-server.md)
Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
1+
# Code Executor Module
2+
3+
The `code-executor` module in `light-genai-4j` provides a robust environment for executing code snippets generated by LLMs. It leverages [GraalVM Polyglot](https://www.graalvm.org/latest/reference-manual/polyglot/) to execute code in various languages (JavaScript, Python, Ruby, R, etc.) securely within the Java application.
4+
5+
## Overview
6+
7+
This module is designed to integrate seamlessly with `langchain4j` agents that require tool execution capabilities. By using GraalVM, it avoids the need for external execution environments or heavy Docker containers for many use cases, while still providing a degree of isolation and high performance.
8+
9+
## Dependencies
10+
11+
To use the code executor, add the following dependency to your `pom.xml`:
12+
13+
```xml
14+
<dependency>
15+
<groupId>com.networknt</groupId>
16+
<artifactId>code-executor</artifactId>
17+
<version>${version.light-genai-4j}</version>
18+
</dependency>
19+
```
20+
21+
You also need to ensure you have the necessary GraalVM dependencies. The module by default includes support for **JavaScript**.
22+
23+
```xml
24+
<dependency>
25+
<groupId>org.graalvm.polyglot</groupId>
26+
<artifactId>js</artifactId>
27+
<version>${version.graalvm}</version>
28+
<type>pom</type>
29+
</dependency>
30+
```
31+
32+
If you need to execute other languages (e.g., Python), you must add the corresponding GraalVM language dependency:
33+
34+
```xml
35+
<!-- For Python support -->
36+
<dependency>
37+
<groupId>org.graalvm.polyglot</groupId>
38+
<artifactId>python</artifactId>
39+
<version>${version.graalvm}</version>
40+
<type>pom</type>
41+
</dependency>
42+
```
43+
44+
## Usage
45+
46+
The core component is the `CodeExecutionEngine`. You can instantiate a `GraalVmJavaScriptExecutionEngine` (or other language-specific engines provided by LangChain4j) to execute code.
47+
48+
### Example: Executing JavaScript
49+
50+
```java
51+
import dev.langchain4j.code.CodeExecutionEngine;
52+
import dev.langchain4j.code.graalvm.GraalVmJavaScriptExecutionEngine;
53+
54+
public class CodeExecutorExample {
55+
public static void main(String[] args) {
56+
CodeExecutionEngine engine = new GraalVmJavaScriptExecutionEngine();
57+
String code = "var x = 10; var y = 20; x + y;";
58+
String result = engine.execute(code);
59+
System.out.println("Result: " + result); // Output: 30
60+
}
61+
}
62+
```
63+
64+
### Integration with LangChain4j Agents
65+
66+
This module effectively provides the implementation for LangChain4j's `CodeExecutionTool`. You can register this tool with your agent to allow it to write and execute code to solve complex problems.
67+
68+
```java
69+
// Example setup with an agent (conceptual)
70+
CodeExecutionEngine engine = new GraalVmJavaScriptExecutionEngine();
71+
ToolSpecification codeExecutionTool = ToolSpecification.builder()
72+
.name("execute_javascript")
73+
.description("Executes JavaScript code and returns the result")
74+
.addArgument("code", JsonSchemaProperty.STRING, "The JavaScript code to execute")
75+
.build();
76+
77+
// ... register tool with your ChatModel or Agent ...
78+
```
79+
80+
## Configuration
81+
82+
The GraalVM execution engine can be configured to restrict access to host resources (file system, network, etc.) for security. By default, LangChain4j's implementation provides a reasonable level of sandboxing, but you should review the [GraalVM Security Guide](https://www.graalvm.org/latest/security-guide/) for production deployments, especially if executing untrusted code.
Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
# Consolidating AbstractJwtVerifyHandler and AbstractSimpleJwtVerifyHandler
2+
3+
## Introduction
4+
5+
The `light-4j` framework previously had two abstract base classes for JWT verification: `AbstractJwtVerifyHandler` and `AbstractSimpleJwtVerifyHandler`.
6+
7+
- `AbstractJwtVerifyHandler`: Used by `JwtVerifyHandler` (in `light-rest-4j`) and `HybridJwtVerifyHandler` (in `light-hybrid-4j`). It included logic for verifying OAuth 2.0 scopes against an OpenAPI specification.
8+
- `AbstractSimpleJwtVerifyHandler`: Used by `SimpleJwtVerifyHandler` (in `light-rest-4j`). It provided basic JWT verification (signature, expiration) but skipped all scope verification logic.
9+
10+
This design document outlines the consolidation of these two classes into a single `AbstractJwtVerifyHandler` to reduce code duplication and simplify maintenance.
11+
12+
## Motivation
13+
14+
The two abstract classes shared approximately 85% of their code, including token extraction, JWT signature verification using `JwtVerifier`, and audit info population. The primary difference was the presence of scope verification logic in `AbstractJwtVerifyHandler`.
15+
16+
Maintaining two separate hierarchies for largely identical logic increased the risk of inconsistencies (e.g., bug fixes applied to one but missed in the other) and added unnecessary complexity.
17+
18+
## Changes
19+
20+
### 1. AbstractJwtVerifyHandler Updates
21+
22+
The `AbstractJwtVerifyHandler` in `light-4j/unified-security` has been updated to support scopeless verification, effectively merging the behavior of `AbstractSimpleJwtVerifyHandler`.
23+
24+
- **`getSpecScopes()` is no longer abstract**: It now has a default implementation that returns `null`.
25+
```java
26+
public List<String> getSpecScopes(HttpServerExchange exchange, Map<String, Object> auditInfo) throws Exception {
27+
return null; // Default: no scope verification (simple JWT behavior)
28+
}
29+
```
30+
- **Conditional Scope Verification**: The `handleJwt` method now checks if `getSpecScopes()` returns `null`. If it does, the scope verification logic (checking secondary scopes and matching against spec scopes) is skipped.
31+
- **Enriched Audit Info**: `AbstractJwtVerifyHandler` extracts additional claims like `email`, `host`, and `role` into the audit info map. By using this class for simple JWT verification, these claims are now populated for `SimpleJwtVerifyHandler` as well, improving audit capabilities.
32+
33+
### 2. Removal of AbstractSimpleJwtVerifyHandler
34+
35+
The `AbstractSimpleJwtVerifyHandler` class in `light-4j/unified-security` has been deleted.
36+
37+
### 3. SimpleJwtVerifyHandler Update
38+
39+
The `SimpleJwtVerifyHandler` in `light-rest-4j/openapi-security` has been updated to extend `AbstractJwtVerifyHandler` directly.
40+
41+
- It inherits the default `getSpecScopes()` implementation (returning `null`), which triggers the scopeless verification path in the base class.
42+
- It implements the required `isSkipAuth()` method, same as before.
43+
44+
### 4. UnifiedSecurityHandler Update
45+
46+
The `UnifiedSecurityHandler` in `light-4j/unified-security` has been updated to cast the "sjwt" (Simple JWT) handler to `AbstractJwtVerifyHandler` instead of the removed `AbstractSimpleJwtVerifyHandler`.
47+
48+
## Impact
49+
50+
### Simplified Hierarchy
51+
There is now a single abstract base class (`AbstractJwtVerifyHandler`) for all JWT verification handlers in the framework.
52+
53+
### Backward Compatibility
54+
- **Functionality**: Existing handlers (`JwtVerifyHandler`, `SimpleJwtVerifyHandler`, `HybridJwtVerifyHandler`) continue to work as before.
55+
- **Configuration**: No changes to `security.yml` or `openapi-security.yml` are required.
56+
- **Behavior**: `SimpleJwtVerifyHandler` now extracts more comprehensive audit information (e.g., user email, host, role) if present in the token, which is a beneficial side effect.
57+
58+
## Conclusion
59+
60+
This refactoring simplifies the codebase, removes duplication, and ensures consistent JWT handling across different modules while maintaining full backward compatibility.

0 commit comments

Comments
 (0)