From dc0fda3323adf6c25f04727f6cfea3fe88ea8d11 Mon Sep 17 00:00:00 2001 From: Harshit-kohli Date: Tue, 26 Nov 2024 13:27:03 -0800 Subject: [PATCH] keycloak changes --- .../bcgov/keycloak/rest/SoamRestUtils.java | 63 ++++++------------- .../keycloak/tenant/rest/TenantRestUtils.java | 48 ++++---------- 2 files changed, 32 insertions(+), 79 deletions(-) diff --git a/docker/keycloak/extensions-24/services/src/main/java/com/github/bcgov/keycloak/rest/SoamRestUtils.java b/docker/keycloak/extensions-24/services/src/main/java/com/github/bcgov/keycloak/rest/SoamRestUtils.java index 914166a..3f4bcca 100644 --- a/docker/keycloak/extensions-24/services/src/main/java/com/github/bcgov/keycloak/rest/SoamRestUtils.java +++ b/docker/keycloak/extensions-24/services/src/main/java/com/github/bcgov/keycloak/rest/SoamRestUtils.java @@ -6,13 +6,15 @@ import com.github.bcgov.keycloak.model.SoamServicesCard; import org.jboss.logging.Logger; import org.jboss.logging.MDC; +import org.springframework.http.HttpEntity; import org.springframework.http.HttpHeaders; import org.springframework.http.HttpMethod; import org.springframework.http.MediaType; import org.springframework.util.LinkedMultiValueMap; import org.springframework.util.MultiValueMap; import org.springframework.web.client.HttpClientErrorException; -import org.springframework.web.client.RestClient; +import org.springframework.web.reactive.function.BodyInserters; +import reactor.core.publisher.Mono; import java.util.Collections; import java.util.UUID; @@ -26,47 +28,22 @@ public class SoamRestUtils { private static Logger logger = Logger.getLogger(SoamRestUtils.class); - private static SoamRestUtils soamRestUtilsInstance; - private static ApplicationProperties props; - public SoamRestUtils() { - props = new ApplicationProperties(); - } - - public static SoamRestUtils getInstance() { - if (soamRestUtilsInstance == null) { - soamRestUtilsInstance = new SoamRestUtils(); - } - return soamRestUtilsInstance; - } + private RestWebClient restWebClient; - public String getToken() { - logger.debug("Calling get token method"); - RestClient restClient = RestClient.create(); - return restClient - .post() - .uri(props.getTokenURL()) - .headers( - headers -> { - headers.setBasicAuth(props.getClientID(), props.getClientSecret()); - headers.set("Content-Type", "application/json"); - }) - .retrieve() - .body(String.class); + public SoamRestUtils() { + this.restWebClient = new RestWebClient(); + props = new ApplicationProperties(); } public void performLogin(String identifierType, String identifierValue, String userID, SoamServicesCard servicesCard) { String url = props.getSoamApiURL() + "/login"; final String correlationID = logAndGetCorrelationID(identifierValue, url, HttpMethod.POST.toString()); - - RestClient restClient = RestClient.create(); - HttpHeaders headers = new HttpHeaders(); headers.setContentType(MediaType.APPLICATION_FORM_URLENCODED); headers.add("correlationID", correlationID); - headers.add("Authorization", "Bearer " + getToken()); - MultiValueMap map = new LinkedMultiValueMap<>(); + MultiValueMap map = new LinkedMultiValueMap(); map.add("identifierType", identifierType); map.add("identifierValue", identifierValue); map.add("userID", userID); @@ -84,15 +61,16 @@ public void performLogin(String identifierType, String identifierValue, String u map.add("userDisplayName", servicesCard.getUserDisplayName()); } + HttpEntity> request = new HttpEntity>(map, headers); + try { - restClient.post() + this.restWebClient.webClient().post() .uri(url) - .headers( - heads -> heads.addAll(headers)) - .contentType(MediaType.APPLICATION_FORM_URLENCODED) - .body(map) + .headers(httpHeadersOnWebClientBeingBuilt -> httpHeadersOnWebClientBeingBuilt.addAll( headers )) + .body(BodyInserters.fromFormData(map)) .retrieve() - .toBodilessEntity(); + .bodyToMono(SoamLoginEntity.class) + .block(); } catch (final HttpClientErrorException e) { throw new RuntimeException("Could not complete login call: " + e.getMessage()); } @@ -101,19 +79,16 @@ public void performLogin(String identifierType, String identifierValue, String u public SoamLoginEntity getSoamLoginEntity(String identifierType, String identifierValue) { String url = props.getSoamApiURL() + "/" + identifierType + "/" + identifierValue; final String correlationID = logAndGetCorrelationID(identifierValue, url, HttpMethod.GET.toString()); - - RestClient restClient = RestClient.create(); HttpHeaders headers = new HttpHeaders(); headers.setAccept(Collections.singletonList(MediaType.APPLICATION_JSON)); headers.add("correlationID", correlationID); - headers.add("Authorization", "Bearer " + getToken()); try { - return restClient.get() + return this.restWebClient.webClient().get() .uri(url) - .headers( - heads -> heads.addAll(headers)) + .headers(httpHeadersOnWebClientBeingBuilt -> httpHeadersOnWebClientBeingBuilt.addAll( headers )) .retrieve() - .body(SoamLoginEntity.class); + .bodyToMono(SoamLoginEntity.class) + .block(); } catch (final HttpClientErrorException e) { throw new RuntimeException("Could not complete getSoamLoginEntity call: " + e.getMessage()); } diff --git a/docker/keycloak/extensions-24/services/src/main/java/com/github/bcgov/keycloak/tenant/rest/TenantRestUtils.java b/docker/keycloak/extensions-24/services/src/main/java/com/github/bcgov/keycloak/tenant/rest/TenantRestUtils.java index fadb57a..0a9053f 100644 --- a/docker/keycloak/extensions-24/services/src/main/java/com/github/bcgov/keycloak/tenant/rest/TenantRestUtils.java +++ b/docker/keycloak/extensions-24/services/src/main/java/com/github/bcgov/keycloak/tenant/rest/TenantRestUtils.java @@ -2,6 +2,7 @@ import com.github.bcgov.keycloak.common.properties.ApplicationProperties; +import com.github.bcgov.keycloak.rest.RestWebClient; import com.github.bcgov.keycloak.tenant.model.TenantAccess; import org.jboss.logging.Logger; import org.jboss.logging.MDC; @@ -9,10 +10,9 @@ import org.springframework.http.HttpMethod; import org.springframework.http.MediaType; import org.springframework.web.client.HttpClientErrorException; -import org.springframework.web.client.RestClient; - -import java.util.*; +import java.util.Collections; +import java.util.UUID; /** * This class is used for REST calls @@ -23,51 +23,29 @@ public class TenantRestUtils { private static Logger logger = Logger.getLogger(TenantRestUtils.class); - private static TenantRestUtils tenantRestUtilsInstance; - private static ApplicationProperties props; + private RestWebClient restWebClient; + public TenantRestUtils() { + this.restWebClient = new RestWebClient(); props = new ApplicationProperties(); } - public static TenantRestUtils getInstance() { - if (tenantRestUtilsInstance == null) { - tenantRestUtilsInstance = new TenantRestUtils(); - } - return tenantRestUtilsInstance; - } - public String getToken() { - logger.debug("Calling get token method"); - RestClient restClient = RestClient.create(); - return restClient - .post() - .uri(props.getTokenURL()) - .headers( - headers -> { - headers.setBasicAuth(props.getClientID(), props.getClientSecret()); - headers.set("Content-Type", "application/json"); - }) - .retrieve() - .body(String.class); - } - public TenantAccess checkForValidTenant(String clientID, String tenantID) { - String url = props.getSoamApiURL() + "/" + clientID + "/" + tenantID; + String url = props.getSoamApiURL() + "/tenant?clientID=" + clientID + "&tenantID=" + tenantID; final String correlationID = logAndGetCorrelationID(tenantID, url, HttpMethod.GET.toString()); - - RestClient restClient = RestClient.create(); HttpHeaders headers = new HttpHeaders(); headers.setAccept(Collections.singletonList(MediaType.APPLICATION_JSON)); headers.add("correlationID", correlationID); - headers.add("Authorization", "Bearer " + getToken()); + try { - return restClient.get() + return this.restWebClient.webClient().get() .uri(url) - .headers( - heads -> heads.addAll(headers)) + .headers(httpHeadersOnWebClientBeingBuilt -> httpHeadersOnWebClientBeingBuilt.addAll( headers )) .retrieve() - .body(TenantAccess.class); + .bodyToMono(TenantAccess.class) + .block(); } catch (final HttpClientErrorException e) { throw new RuntimeException("Could not complete checkForValidTenant call: " + e.getMessage()); } @@ -83,4 +61,4 @@ private String logAndGetCorrelationID(String tenantID, String url, String httpMe MDC.clear(); return correlationID; } -} \ No newline at end of file +}