Implement access token context encoding framework#8
Conversation
closes #37118 Signed-off-by: mposolda <mposolda@gmail.com>
1 similar comment
|
Claude finished @ShashankFC's task in 2m 7s —— View job Code Review CompleteTasks
SummaryThis PR implements a new token context encoding framework that encodes metadata about access tokens (session type, token type, grant type) directly into the token ID using a compact 6-character prefix format. The implementation follows Keycloak's SPI pattern well and integrates cleanly with the existing OAuth2 grant type system. Critical Issues1. Bug: Incorrect null check in AccessTokenContext constructor
The constructor checks Objects.requireNonNull(grantType, "Null grantType not allowed");
Objects.requireNonNull(grantType, "Null rawTokenId not allowed"); // BUG: should be rawTokenIdThis is a copy-paste error. The second line should be: Objects.requireNonNull(rawTokenId, "Null rawTokenId not allowed");2. Typo: "aucienceClients" → "audienceClients"
Variable name has a typo that was fixed in this PR: final ClientModel[] requestedAudienceClients = clientSessionCtx.getAttribute(Constants.REQUESTED_AUDIENCE_CLIENTS, ClientModel[].class);Good fix! This improves code readability. High Priority Issues3. Missing package visibility modifier on fields
Two fields have package-private visibility (no modifier) while others are Map<String, String> grantsByShortcuts;
Map<String, String> grantsToShortcuts;These should either be:
The test at 4. Inconsistent error handling patterns
The
Consider whether this is the desired behavior. Should malformed tokens also return UNKNOWN types for more graceful degradation? Document the rationale. Medium Priority Issues5. Magic number in length check
The hardcoded private static final int ENCODED_CONTEXT_LENGTH = 6; // 2 chars each for sessionType, tokenType, grantTypeThis improves maintainability and documents the format structure. 6. Concurrent modification risk during refresh
The String grantShortcut = grantsToShortcuts.get(grantType);
if (grantShortcut == null) {
OAuth2GrantTypeFactory factory = ...;
if (factory != null) {
String shortcut = factory.getShortcut();
grantsByShortcuts.put(shortcut, grantType); // Could happen multiple times
grantsToShortcuts.put(grantType, shortcut);
}
grantShortcut = grantsToShortcuts.get(grantType);
}Consider using 7. Test coverage gaps
The test class only covers:
Missing test coverage for:
Low Priority Issues8. Documentation: Backward compatibility not explicitly documentedThe code handles old tokens (without encoded context) by returning UNKNOWN types, but this backward compatibility strategy isn't documented in the class or interface Javadoc. Add a comment explaining how old tokens are handled. 9. Potential collision with token IDs containing colons
The encoding format uses int indexOf = encodedTokenId.indexOf(':');If the
Actually, looking at line 117, the format is Positive Observations✅ Well-structured SPI implementation - Follows Keycloak's provider pattern correctly with proper factory, provider, and SPI classes. ✅ Good separation of concerns - The encoding logic is cleanly separated from token generation logic. ✅ Efficient encoding - The 6-character prefix is compact and doesn't significantly increase token size. ✅ Dynamic grant type support - The factory can discover new grant types at runtime, making the system extensible. ✅ Proper service registration - Both META-INF service files are correctly updated. ✅ Grant type propagation - The PR properly adds ✅ Consistent shortcut implementation - All grant type factories now implement the Recommendations
|
Test 8nn---nReplicated from ai-code-review-evaluation/keycloak-greptile#8