|
| 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