Skip to content

Commit

Permalink
feat(be:FSADT1-1575): Passed groups to legacy
Browse files Browse the repository at this point in the history
  • Loading branch information
mamartinezmejia committed Nov 26, 2024
1 parent 7551593 commit 5263735
Show file tree
Hide file tree
Showing 5 changed files with 46 additions and 12 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -58,16 +58,14 @@ public Mono<ClientDetailsDto> getClientDetailsByIncorporationNumber(
public Mono<ForestClientDetailsDto> getClientDetailsByClientNumber(
@PathVariable String clientNumber,
JwtAuthenticationToken principal
) {
) {
log.info("Requesting client details for client number {} from the client service. {}",
clientNumber,
JwtPrincipalUtil.getUserId(principal)
);
return clientService.getClientDetailsByClientNumber(
clientNumber,
JwtPrincipalUtil.getUserId(principal),
JwtPrincipalUtil.getBusinessId(principal),
JwtPrincipalUtil.getProvider(principal));
JwtPrincipalUtil.getGroups(principal));
}

@GetMapping("/search")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,8 @@ public Flux<ForestClientDto> searchLegacy(
}

public Mono<ForestClientDetailsDto> searchByClientNumber(
String clientNumber
String clientNumber,
List<String> groups
) {
log.info("Searching for client number {} in legacy", clientNumber);

Expand All @@ -101,6 +102,7 @@ public Mono<ForestClientDetailsDto> searchByClientNumber(
builder
.path("/api/search/clientNumber")
.queryParam("clientNumber", clientNumber)
.queryParam("groups", groups)
.build(Map.of())
)
.exchangeToMono(response -> response.bodyToMono(ForestClientDetailsDto.class))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -145,14 +145,12 @@ public Mono<ClientDetailsDto> getClientDetailsByIncorporationNumber(

public Mono<ForestClientDetailsDto> getClientDetailsByClientNumber(
String clientNumber,
String userId,
String businessId,
String provider
List<String> groups
) {
log.info("Loading details for {} {} {} {}", clientNumber, userId, businessId, provider);
log.info("Loading details for {} for user role {}", clientNumber, groups.toString());

return legacyService
.searchByClientNumber(clientNumber)
.searchByClientNumber(clientNumber, groups)
.flatMap(forestClientDetailsDto -> {
String corpRegnNmbr = forestClientDetailsDto.corpRegnNmbr();

Expand Down
33 changes: 33 additions & 0 deletions backend/src/main/java/ca/bc/gov/app/util/JwtPrincipalUtil.java
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
package ca.bc.gov.app.util;

import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.stream.Stream;
import lombok.NoArgsConstructor;
import org.apache.commons.lang3.StringUtils;
Expand Down Expand Up @@ -379,5 +382,35 @@ private static String getLastNameValue(Map<String, Object> claims) {
private static String getNameValue(Map<String, Object> claims) {
return processName(claims).get("fullName");
}

/**
* Retrieves a list of groups from the given JwtPrincipal.
*
* This method extracts the token attributes from the provided {@link JwtPrincipal}, then looks for the key "cognito:groups"
* in the token attributes. If the value associated with this key is a {@link List}, the method filters the elements to only
* include non-null values of type {@link String}. The resulting list of strings is returned.
*
* @param jwtPrincipal The {@link JwtPrincipal} containing the token attributes. It must have the "cognito:groups" key.
* If the key does not exist or the value is not a list of strings, an empty list is returned.
* @return A list of group names, or an empty list if the key is missing or the value is not a list of strings.
*/
public static List<String> getGroups(JwtAuthenticationToken jwtPrincipal) {
if (jwtPrincipal == null || jwtPrincipal.getTokenAttributes() == null) {
return Collections.emptyList();
}

Map<String, Object> tokenAttributes = jwtPrincipal.getTokenAttributes();
Object groups = tokenAttributes.get("cognito:groups");

if (groups instanceof List) {
return ((List<?>) groups).stream()
.filter(Objects::nonNull)
.filter(String.class::isInstance)
.map(String.class::cast)
.toList();
}

return Collections.emptyList();
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -77,9 +77,12 @@ public Flux<ForestClientDto> findByIdAndLastName(

@GetMapping("/clientNumber")
public Mono<ForestClientDto> findByClientNumber(
@RequestParam String clientNumber
@RequestParam String clientNumber,
@RequestParam List<String> groups
) {
log.info("Receiving request to search by ID {} and Last Name", clientNumber);
log.info("Receiving request to search by ID {} and groups {}",
clientNumber,
groups);
return service.findByClientNumber(clientNumber);
}

Expand Down

0 comments on commit 5263735

Please sign in to comment.