Skip to content

Commit

Permalink
feat: remove duplication on role security config
Browse files Browse the repository at this point in the history
  • Loading branch information
Ricardo Campos committed Apr 30, 2024
1 parent abcabfc commit 5c1e97a
Showing 1 changed file with 4 additions and 15 deletions.
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
package ca.bc.gov.backendstartapi.config;

import java.util.ArrayList;
import ca.bc.gov.backendstartapi.security.JwtSecurityUtil;
import java.util.Collection;
import java.util.List;
import java.util.Set;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
Expand Down Expand Up @@ -75,20 +76,8 @@ private Converter<Jwt, AbstractAuthenticationToken> converter() {
if (!jwt.getClaims().containsKey("cognito:groups")) {
return List.of();
}
Object clientRolesObj = jwt.getClaims().get("cognito:groups");
final List<String> realmAccess = new ArrayList<>();
if (clientRolesObj instanceof List<?> list) {
for (Object item : list) {
String role = String.valueOf(item);
// Removes Client Number
String clientNumber = role.substring(role.length() - 8);
if (clientNumber.replaceAll("[0-9]", "").isEmpty()) {
role = role.substring(0, role.length() - 9); // Removes dangling underscore
}
realmAccess.add(role);
}
}
return realmAccess.stream()
Set<String> roleSet = JwtSecurityUtil.getUserRolesFromJwt(jwt);
return roleSet.stream()
.map(roleName -> "ROLE_" + roleName)
.map(roleName -> (GrantedAuthority) new SimpleGrantedAuthority(roleName))
.toList();
Expand Down

0 comments on commit 5c1e97a

Please sign in to comment.