Skip to content

fixes #2446 refactor the key logic in the token-limit handler #2447

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Feb 4, 2025
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -166,12 +166,24 @@ public void handleRequest(HttpServerExchange exchange) throws Exception {
return;
}

// construct the key based on grant_type and client_id or code.
if(grantType.equals(CLIENT_CREDENTIALS)) {
key = clientId + ":" + clientIpAddress + ":" + scope;
if(logger.isTraceEnabled()) logger.trace("client credentials key = {}", key);
} else if(grantType.equals(AUTHORIZATION_CODE)) {
String code = bodyMap.get(CODE);
key = clientId + ":" + code + ":" + clientIpAddress + ":" + scope;
if(logger.isTraceEnabled()) logger.trace("authorization code key = {}", key);
} else {
// other grant_type, ignore it.
if(logger.isTraceEnabled()) logger.trace("other grant type {}, ignore it", grantType);
}

// secondly, we need to identify if the ClientID is considered Legacy or not. If it is, bypass limit, cache and call next handler.
List<String> legacyClient = config.getLegacyClient();
if(legacyClient.contains(clientId)) {
if(logger.isTraceEnabled()) logger.trace("client {} is configured as Legacy, bypass the token limit.", clientId);
// check if cache key exists in cache manager, if exists return cached token
key = clientId + ":" + clientSecret + ":" + scope;
ResponseEntity<String> responseEntity = (ResponseEntity) cacheManager.get(CLIENT_TOKEN, key);
if (responseEntity != null) {
if(logger.isTraceEnabled()) logger.trace("legacy client cache key {} has token value, returning cached token.", key);
Expand All @@ -186,18 +198,6 @@ public void handleRequest(HttpServerExchange exchange) throws Exception {
return;
}

// construct the key based on grant_type and client_id or code.
if(grantType.equals(CLIENT_CREDENTIALS)) {
key = clientId + ":" + clientIpAddress;
if(logger.isTraceEnabled()) logger.trace("client credentials key = {}", key);
} else if(grantType.equals(AUTHORIZATION_CODE)) {
String code = bodyMap.get(CODE);
key = clientId + ":" + code + ":" + clientIpAddress;
if(logger.isTraceEnabled()) logger.trace("authorization code key = {}", key);
} else {
// other grant_type, ignore it.
if(logger.isTraceEnabled()) logger.trace("other grant type {}, ignore it", grantType);
}

if(key != null) {
// check if the key is in the cache manager.
Expand Down