Skip to content

Commit 7acb163

Browse files
committed
Add Component ID to API key identifier
1 parent dff646e commit 7acb163

File tree

4 files changed

+17
-7
lines changed

4 files changed

+17
-7
lines changed

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

+8-3
Original file line numberDiff line numberDiff line change
@@ -109,22 +109,27 @@ 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 componentId = requestContext.getMatchedAPI().getChoreoComponentInfo().getComponentID();
113+
if (componentId == null) {
114+
componentId = "";
115+
}
116+
String apiKeyId = keyHash + APIKeyConstants.API_KEY_ID_SEPARATOR + componentId;
117+
Object cachedJWT = CacheProvider.getGatewayAPIKeyJWTCache().getIfPresent(apiKeyId);
113118
if (cachedJWT != null && !APIKeyUtils.isJWTExpired((String) cachedJWT)) {
114119
if (log.isDebugEnabled()) {
115120
log.debug("Token retrieved from the cache. Token: " + FilterUtils.getMaskedToken(keyHash));
116121
}
117122
return (String) cachedJWT;
118123
}
119124
// Exchange the API Key to a JWT token.
120-
Optional<String> jwt = APIKeyUtils.exchangeAPIKeyToJWT(keyHash);
125+
Optional<String> jwt = APIKeyUtils.exchangeAPIKeyToJWT(apiKeyId);
121126
if (jwt.isEmpty()) {
122127
throw new APISecurityException(APIConstants.StatusCodes.UNAUTHENTICATED.getCode(),
123128
APISecurityConstants.API_AUTH_INVALID_CREDENTIALS,
124129
APISecurityConstants.API_AUTH_INVALID_CREDENTIALS_MESSAGE);
125130
}
126131
// Cache the JWT token.
127-
CacheProvider.getGatewayAPIKeyJWTCache().put(keyHash, jwt.get());
132+
CacheProvider.getGatewayAPIKeyJWTCache().put(apiKeyId, jwt.get());
128133
return jwt.get();
129134
}
130135

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

+4-4
Original file line numberDiff line numberDiff line change
@@ -142,12 +142,12 @@ public static Optional<String> exchangePATToJWT(String patHash) {
142142
}
143143

144144
/**
145-
* Exchange a given API key hash to a JWT token.
145+
* Exchange a given API key ID to a JWT token.
146146
*
147-
* @param apiKeyHash API Key Hash
147+
* @param apiKeyId API Key Hash + "#" + Target component 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)