From a71f1725a7e9387d611439f3fb439b02ff3b35bb Mon Sep 17 00:00:00 2001 From: Mauro Mugnaini Date: Mon, 10 Jul 2023 15:46:48 +0200 Subject: [PATCH] The library now uses with `2.4.0-SNAPSHOT` version of the BOM and safe set `isExternalCall` to `true` with reflection to be compatible with both old and new `gxJRS` APIs --- pom.xml | 7 +----- .../keycloak/DefaultKeycloakClient.java | 22 ++++++++++++++++++- 2 files changed, 22 insertions(+), 7 deletions(-) diff --git a/pom.xml b/pom.xml index fff615b..e8e052f 100644 --- a/pom.xml +++ b/pom.xml @@ -20,7 +20,7 @@ org.gcube.distribution gcube-bom - 3.0.0-SNAPSHOT + 2.4.0-SNAPSHOT pom import @@ -60,11 +60,6 @@ gxJRS - - - - - org.slf4j slf4j-log4j12 diff --git a/src/main/java/org/gcube/common/keycloak/DefaultKeycloakClient.java b/src/main/java/org/gcube/common/keycloak/DefaultKeycloakClient.java index da67800..13c4b95 100644 --- a/src/main/java/org/gcube/common/keycloak/DefaultKeycloakClient.java +++ b/src/main/java/org/gcube/common/keycloak/DefaultKeycloakClient.java @@ -13,6 +13,8 @@ import static org.gcube.common.keycloak.model.OIDCConstants.UMA_TOKEN_GRANT_TYPE import java.io.IOException; import java.io.UnsupportedEncodingException; +import java.lang.reflect.InvocationTargetException; +import java.lang.reflect.Method; import java.net.MalformedURLException; import java.net.URL; import java.net.URLEncoder; @@ -280,11 +282,13 @@ public class DefaultKeycloakClient implements KeycloakClient { .flatMap(p -> p.getValue().stream().map(v -> p.getKey() + "=" + v)) .reduce((p1, p2) -> p1 + "&" + p2).orElse(""); - logger.trace("query string is {}", queryString); + logger.trace("Query string is {}", queryString); request = GXHTTPStringRequest.newRequest(tokenURL.toString()) .header("Content-Type", "application/x-www-form-urlencoded").withBody(queryString); + safeSetAsExternalCallForOldAPI(request); + logger.trace("Adding provided headers: {}", headers); for (String headerName : headers.keySet()) { request.header(headerName, headers.get(headerName)); @@ -408,6 +412,7 @@ public class DefaultKeycloakClient implements KeycloakClient { request = GXHTTPStringRequest.newRequest(tokenURL.toString()).header("Content-Type", "application/x-www-form-urlencoded").withBody(queryString); + safeSetAsExternalCallForOldAPI(request); } catch (Exception e) { throw new KeycloakClientException("Cannot construct the request object correctly", e); } @@ -471,6 +476,8 @@ public class DefaultKeycloakClient implements KeycloakClient { request = GXHTTPStringRequest.newRequest(introspectionURL.toString()).header("Content-Type", "application/x-www-form-urlencoded").withBody(queryString); + safeSetAsExternalCallForOldAPI(request); + request = request.header("Authorization", constructBasicAuthenticationHeader(clientId, clientSecret)); } catch (Exception e) { throw new KeycloakClientException("Cannot construct the request object correctly", e); @@ -511,4 +518,17 @@ public class DefaultKeycloakClient implements KeycloakClient { return introspectAccessToken(introspectionURL, clientId, clientSecret, accessTokenJWTString).getActive(); } + protected void safeSetAsExternalCallForOldAPI(GXHTTPStringRequest request) { + try { + logger.trace("Looking for the 'isExternalCall' method in the 'GXHTTPStringRequest' class"); + Method isExetnalCallMethod = request.getClass().getMethod("isExternalCall", boolean.class); + logger.trace("Method found, is the old gxJRS API. Invoking it with 'true' argument"); + isExetnalCallMethod.invoke(request, true); + } catch (NoSuchMethodException e) { + logger.trace("Method not found, is the new gxJRS API"); + } catch (SecurityException | IllegalAccessException | IllegalArgumentException | InvocationTargetException e) { + logger.warn("Cannot invoke 'isExternalCall' method via reflection on 'GXHTTPStringRequest' class", e); + } + } + }