Skip to content

Commit 04b9f86

Browse files
committed
Add API ID to API key identifier
1 parent dff646e commit 04b9f86

File tree

4 files changed

+12
-6
lines changed

4 files changed

+12
-6
lines changed

enforcer-parent/enforcer/src/main/java/org/wso2/choreo/connect/enforcer/security/jwt/APIKeyAuthenticator.java

+4-3
Original file line numberDiff line numberDiff line change
@@ -109,22 +109,23 @@ protected String retrieveTokenFromRequestCtx(RequestContext requestContext) thro
109109
APISecurityConstants.API_AUTH_INVALID_CREDENTIALS_MESSAGE);
110110
}
111111
String keyHash = APIKeyUtils.generateAPIKeyHash(apiKey);
112-
Object cachedJWT = CacheProvider.getGatewayAPIKeyJWTCache().getIfPresent(keyHash);
112+
String apiKeyId = keyHash + APIKeyConstants.API_KEY_ID_SEPARATOR + requestContext.getMatchedAPI().getUuid();
113+
Object cachedJWT = CacheProvider.getGatewayAPIKeyJWTCache().getIfPresent(apiKeyId);
113114
if (cachedJWT != null && !APIKeyUtils.isJWTExpired((String) cachedJWT)) {
114115
if (log.isDebugEnabled()) {
115116
log.debug("Token retrieved from the cache. Token: " + FilterUtils.getMaskedToken(keyHash));
116117
}
117118
return (String) cachedJWT;
118119
}
119120
// Exchange the API Key to a JWT token.
120-
Optional<String> jwt = APIKeyUtils.exchangeAPIKeyToJWT(keyHash);
121+
Optional<String> jwt = APIKeyUtils.exchangeAPIKeyToJWT(apiKeyId);
121122
if (jwt.isEmpty()) {
122123
throw new APISecurityException(APIConstants.StatusCodes.UNAUTHENTICATED.getCode(),
123124
APISecurityConstants.API_AUTH_INVALID_CREDENTIALS,
124125
APISecurityConstants.API_AUTH_INVALID_CREDENTIALS_MESSAGE);
125126
}
126127
// Cache the JWT token.
127-
CacheProvider.getGatewayAPIKeyJWTCache().put(keyHash, jwt.get());
128+
CacheProvider.getGatewayAPIKeyJWTCache().put(apiKeyId, jwt.get());
128129
return jwt.get();
129130
}
130131

enforcer-parent/enforcer/src/main/java/org/wso2/choreo/connect/enforcer/security/jwt/APIKeyConstants.java

+2
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,8 @@ public class APIKeyConstants {
2828

2929
public static final String API_KEY_JSON_KEY = "key";
3030

31+
public static final String API_KEY_ID_SEPARATOR = "#";
32+
3133
public static final String PAT_EXCHANGE_ENDPOINT = "/internal/pat";
3234
public static final String API_KEY_EXCHANGE_ENDPOINT = "/internal/apiKey/token";
3335
}

enforcer-parent/enforcer/src/main/java/org/wso2/choreo/connect/enforcer/security/jwt/APIKeyUtils.java

+3-3
Original file line numberDiff line numberDiff line change
@@ -144,10 +144,10 @@ public static Optional<String> exchangePATToJWT(String patHash) {
144144
/**
145145
* Exchange a given API key hash to a JWT token.
146146
*
147-
* @param apiKeyHash API Key Hash
147+
* @param apiKeyId API Key Hash + "#" + API ID.
148148
* @return JWT corresponding to given API Key.
149149
*/
150-
public static Optional<String> exchangeAPIKeyToJWT(String apiKeyHash) {
150+
public static Optional<String> exchangeAPIKeyToJWT(String apiKeyId) {
151151

152152
URL url = null;
153153
try {
@@ -162,7 +162,7 @@ public static Optional<String> exchangeAPIKeyToJWT(String apiKeyHash) {
162162
// Create a request to exchange API key to JWT.
163163
HttpPost exchangeRequest = new HttpPost(url.toURI());
164164
exchangeRequest.addHeader("Content-Type", ContentType.APPLICATION_JSON.toString());
165-
exchangeRequest.setEntity(new StringEntity(createKeyHashExchangeRequest(apiKeyHash)));
165+
exchangeRequest.setEntity(new StringEntity(createKeyHashExchangeRequest(apiKeyId)));
166166
try (CloseableHttpResponse response = httpClient.execute(exchangeRequest)) {
167167
if (response.getStatusLine().getStatusCode() == 200) {
168168
HttpEntity entity = response.getEntity();

enforcer-parent/enforcer/src/test/java/org/wso2/choreo/connect/enforcer/security/jwt/APIKeyAuthenticatorTest.java

+3
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,7 @@ public void retrieveTokenFromRequestCtxTest_invalidKey() {
7070
RequestContext.Builder requestContextBuilder = new RequestContext.Builder("/api-key");
7171
requestContextBuilder.matchedAPI(new APIConfig.Builder("Petstore")
7272
.basePath("/test")
73+
.uuid("6003a3b7-af0f-4fb3-853e-a6562b2345f2")
7374
.apiType("REST")
7475
.build());
7576
Map<String, String> headersMap = new HashMap<>();
@@ -100,6 +101,7 @@ public void retrieveTokenFromRequestCtxTest_cached_validKey() throws APISecurity
100101
RequestContext.Builder requestContextBuilder = new RequestContext.Builder("/api-key");
101102
requestContextBuilder.matchedAPI(new APIConfig.Builder("Petstore")
102103
.basePath("/test")
104+
.uuid("6003a3b7-af0f-4fb3-853e-a6562b2345f2")
103105
.apiType("REST")
104106
.build());
105107
Map<String, String> headersMap = new HashMap<>();
@@ -131,6 +133,7 @@ public void retrieveTokenFromRequestCtxTest_validKey() throws APISecurityExcepti
131133
RequestContext.Builder requestContextBuilder = new RequestContext.Builder("/api-key");
132134
requestContextBuilder.matchedAPI(new APIConfig.Builder("Petstore")
133135
.basePath("/test")
136+
.uuid("6003a3b7-af0f-4fb3-853e-a6562b2345f2")
134137
.apiType("REST")
135138
.build());
136139
Map<String, String> headersMap = new HashMap<>();

0 commit comments

Comments
 (0)