Skip to content

Commit 94d9149

Browse files
authored
Merge pull request #3597 from mevan-karu/pat_impl_choreo
Add support to send PAT JWT to backend
2 parents e46b688 + 313d6c1 commit 94d9149

File tree

2 files changed

+7
-6
lines changed

2 files changed

+7
-6
lines changed

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

+2-3
Original file line numberDiff line numberDiff line change
@@ -97,10 +97,10 @@ public static String generateAPIKeyHash(String apiKey) {
9797
/**
9898
* This function exchanges a given API key to an JWT token.
9999
*
100-
* @param pat PAT
100+
* @param keyHash Key Hash
101101
* @return JWT corresponding to given PAT.
102102
*/
103-
public static Optional<String> exchangePATToJWT(String pat) {
103+
public static Optional<String> exchangePATToJWT(String keyHash) {
104104

105105
URL url = null;
106106
try {
@@ -115,7 +115,6 @@ public static Optional<String> exchangePATToJWT(String pat) {
115115
// Create a request to exchange API key to JWT.
116116
HttpPost exchangeRequest = new HttpPost(url.toURI());
117117
exchangeRequest.addHeader("Content-Type", ContentType.APPLICATION_JSON.toString());
118-
String keyHash = generateAPIKeyHash(pat);
119118
exchangeRequest.setEntity(new StringEntity(createPATExchangeRequest(keyHash)));
120119
try (CloseableHttpResponse response = httpClient.execute(exchangeRequest)) {
121120
if (response.getStatusLine().getStatusCode() == 200) {

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

+5-3
Original file line numberDiff line numberDiff line change
@@ -193,7 +193,7 @@ public AuthenticationContext authenticate(RequestContext requestContext) throws
193193
}
194194
// Handle PAT logic
195195
if (isPATEnabled && token.startsWith(APIKeyConstants.PAT_PREFIX)) {
196-
token = exchangeJWTForPAT(token);
196+
token = exchangeJWTForPAT(requestContext, token);
197197
}
198198
String context = requestContext.getMatchedAPI().getBasePath();
199199
String name = requestContext.getMatchedAPI().getName();
@@ -806,7 +806,7 @@ private String getJWTTokenIdentifier(SignedJWTInfo signedJWTInfo) {
806806
return signedJWTInfo.getSignedJWT().getSignature().toString();
807807
}
808808

809-
private String exchangeJWTForPAT(String pat) throws APISecurityException {
809+
private String exchangeJWTForPAT(RequestContext requestContext, String pat) throws APISecurityException {
810810
if (!APIKeyUtils.isValidAPIKey(pat)) {
811811
throw new APISecurityException(APIConstants.StatusCodes.UNAUTHENTICATED.getCode(),
812812
APISecurityConstants.API_AUTH_INVALID_CREDENTIALS,
@@ -820,13 +820,15 @@ private String exchangeJWTForPAT(String pat) throws APISecurityException {
820820
}
821821
return (String) cachedJWT;
822822
}
823-
Optional<String> jwt = APIKeyUtils.exchangePATToJWT(pat);
823+
Optional<String> jwt = APIKeyUtils.exchangePATToJWT(keyHash);
824824
if (jwt.isEmpty()) {
825825
throw new APISecurityException(APIConstants.StatusCodes.UNAUTHENTICATED.getCode(),
826826
APISecurityConstants.API_AUTH_INVALID_CREDENTIALS,
827827
APISecurityConstants.API_AUTH_INVALID_CREDENTIALS_MESSAGE);
828828
}
829829
CacheProvider.getGatewayAPIKeyJWTCache().put(keyHash, jwt.get());
830+
// Add jwt to x-forwarded-authorization header.
831+
requestContext.addOrModifyHeaders("x-forwarded-authorization", jwt.get());
830832
return jwt.get();
831833
}
832834

0 commit comments

Comments
 (0)