File tree 4 files changed +12
-6
lines changed
enforcer-parent/enforcer/src
main/java/org/wso2/choreo/connect/enforcer/security/jwt
test/java/org/wso2/choreo/connect/enforcer/security/jwt
4 files changed +12
-6
lines changed Original file line number Diff line number Diff line change @@ -109,22 +109,23 @@ protected String retrieveTokenFromRequestCtx(RequestContext requestContext) thro
109
109
APISecurityConstants .API_AUTH_INVALID_CREDENTIALS_MESSAGE );
110
110
}
111
111
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 );
113
114
if (cachedJWT != null && !APIKeyUtils .isJWTExpired ((String ) cachedJWT )) {
114
115
if (log .isDebugEnabled ()) {
115
116
log .debug ("Token retrieved from the cache. Token: " + FilterUtils .getMaskedToken (keyHash ));
116
117
}
117
118
return (String ) cachedJWT ;
118
119
}
119
120
// Exchange the API Key to a JWT token.
120
- Optional <String > jwt = APIKeyUtils .exchangeAPIKeyToJWT (keyHash );
121
+ Optional <String > jwt = APIKeyUtils .exchangeAPIKeyToJWT (apiKeyId );
121
122
if (jwt .isEmpty ()) {
122
123
throw new APISecurityException (APIConstants .StatusCodes .UNAUTHENTICATED .getCode (),
123
124
APISecurityConstants .API_AUTH_INVALID_CREDENTIALS ,
124
125
APISecurityConstants .API_AUTH_INVALID_CREDENTIALS_MESSAGE );
125
126
}
126
127
// Cache the JWT token.
127
- CacheProvider .getGatewayAPIKeyJWTCache ().put (keyHash , jwt .get ());
128
+ CacheProvider .getGatewayAPIKeyJWTCache ().put (apiKeyId , jwt .get ());
128
129
return jwt .get ();
129
130
}
130
131
Original file line number Diff line number Diff line change @@ -28,6 +28,8 @@ public class APIKeyConstants {
28
28
29
29
public static final String API_KEY_JSON_KEY = "key" ;
30
30
31
+ public static final String API_KEY_ID_SEPARATOR = "#" ;
32
+
31
33
public static final String PAT_EXCHANGE_ENDPOINT = "/internal/pat" ;
32
34
public static final String API_KEY_EXCHANGE_ENDPOINT = "/internal/apiKey/token" ;
33
35
}
Original file line number Diff line number Diff line change @@ -144,10 +144,10 @@ public static Optional<String> exchangePATToJWT(String patHash) {
144
144
/**
145
145
* Exchange a given API key hash to a JWT token.
146
146
*
147
- * @param apiKeyHash API Key Hash
147
+ * @param apiKeyId API Key Hash + "#" + API ID.
148
148
* @return JWT corresponding to given API Key.
149
149
*/
150
- public static Optional <String > exchangeAPIKeyToJWT (String apiKeyHash ) {
150
+ public static Optional <String > exchangeAPIKeyToJWT (String apiKeyId ) {
151
151
152
152
URL url = null ;
153
153
try {
@@ -162,7 +162,7 @@ public static Optional<String> exchangeAPIKeyToJWT(String apiKeyHash) {
162
162
// Create a request to exchange API key to JWT.
163
163
HttpPost exchangeRequest = new HttpPost (url .toURI ());
164
164
exchangeRequest .addHeader ("Content-Type" , ContentType .APPLICATION_JSON .toString ());
165
- exchangeRequest .setEntity (new StringEntity (createKeyHashExchangeRequest (apiKeyHash )));
165
+ exchangeRequest .setEntity (new StringEntity (createKeyHashExchangeRequest (apiKeyId )));
166
166
try (CloseableHttpResponse response = httpClient .execute (exchangeRequest )) {
167
167
if (response .getStatusLine ().getStatusCode () == 200 ) {
168
168
HttpEntity entity = response .getEntity ();
Original file line number Diff line number Diff line change @@ -70,6 +70,7 @@ public void retrieveTokenFromRequestCtxTest_invalidKey() {
70
70
RequestContext .Builder requestContextBuilder = new RequestContext .Builder ("/api-key" );
71
71
requestContextBuilder .matchedAPI (new APIConfig .Builder ("Petstore" )
72
72
.basePath ("/test" )
73
+ .uuid ("6003a3b7-af0f-4fb3-853e-a6562b2345f2" )
73
74
.apiType ("REST" )
74
75
.build ());
75
76
Map <String , String > headersMap = new HashMap <>();
@@ -100,6 +101,7 @@ public void retrieveTokenFromRequestCtxTest_cached_validKey() throws APISecurity
100
101
RequestContext .Builder requestContextBuilder = new RequestContext .Builder ("/api-key" );
101
102
requestContextBuilder .matchedAPI (new APIConfig .Builder ("Petstore" )
102
103
.basePath ("/test" )
104
+ .uuid ("6003a3b7-af0f-4fb3-853e-a6562b2345f2" )
103
105
.apiType ("REST" )
104
106
.build ());
105
107
Map <String , String > headersMap = new HashMap <>();
@@ -131,6 +133,7 @@ public void retrieveTokenFromRequestCtxTest_validKey() throws APISecurityExcepti
131
133
RequestContext .Builder requestContextBuilder = new RequestContext .Builder ("/api-key" );
132
134
requestContextBuilder .matchedAPI (new APIConfig .Builder ("Petstore" )
133
135
.basePath ("/test" )
136
+ .uuid ("6003a3b7-af0f-4fb3-853e-a6562b2345f2" )
134
137
.apiType ("REST" )
135
138
.build ());
136
139
Map <String , String > headersMap = new HashMap <>();
You can’t perform that action at this time.
0 commit comments