Skip to content

Commit

Permalink
keycloak changes
Browse files Browse the repository at this point in the history
  • Loading branch information
Harry0589 committed Nov 26, 2024
1 parent 1717855 commit dc0fda3
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 79 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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<String, String> map = new LinkedMultiValueMap<>();
MultiValueMap<String, String> map = new LinkedMultiValueMap<String, String>();
map.add("identifierType", identifierType);
map.add("identifierValue", identifierValue);
map.add("userID", userID);
Expand All @@ -84,15 +61,16 @@ public void performLogin(String identifierType, String identifierValue, String u
map.add("userDisplayName", servicesCard.getUserDisplayName());
}

HttpEntity<MultiValueMap<String, String>> request = new HttpEntity<MultiValueMap<String, String>>(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());
}
Expand All @@ -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());
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,17 @@


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;
import org.springframework.http.HttpHeaders;
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
Expand All @@ -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());
}
Expand All @@ -83,4 +61,4 @@ private String logAndGetCorrelationID(String tenantID, String url, String httpMe
MDC.clear();
return correlationID;
}
}
}

0 comments on commit dc0fda3

Please sign in to comment.