Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: handle default claims when no specific role claim #106

Merged
merged 1 commit into from
Jan 24, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,10 @@
@AllArgsConstructor
public class GrantedAuthorityConverter implements Converter<Jwt, Collection<GrantedAuthority>> {

public static final String REALM_ACCESS_ROLE = "roles";
public static final String REALM_ACCESS = "realm_access";
private final Map<String, List<SimpleGrantedAuthority>> roles;
ApplicationConfig applicationConfig;
private ApplicationConfig applicationConfig;

public GrantedAuthorityConverter(ApplicationConfig applicationConfig) {
this.applicationConfig = applicationConfig;
Expand All @@ -30,8 +32,7 @@ public GrantedAuthorityConverter(ApplicationConfig applicationConfig) {
@SuppressWarnings("unchecked")
@Override
public Collection<GrantedAuthority> convert(@NonNull Jwt jwt) {
Map<String, Object> claims = jwt.getClaims();
List<String> userRoles = (List<String>) claims.get(applicationConfig.getRoleClaim());
List<String> userRoles = getUserRoles(jwt);

if(userRoles == null) {
return new ArrayList<>();
Expand All @@ -49,6 +50,10 @@ public Collection<GrantedAuthority> convert(@NonNull Jwt jwt) {

private void fillGrantedRoles(List<String> configRoles, AuthorityRoleEnum authorityRole) {

if(configRoles == null) {
return;
}

for (String configRole : configRoles ) {
if(configRole == null || configRole.isBlank()) {
return;
Expand All @@ -63,5 +68,16 @@ private void fillGrantedRoles(List<String> configRoles, AuthorityRoleEnum author
});
}
}

@SuppressWarnings("unchecked")
private List<String> getUserRoles(Jwt jwt) {
Map<String, Object> claims = jwt.getClaims();

if(applicationConfig.getRoleClaim() == null || applicationConfig.getRoleClaim().isBlank()) {
Map<String, Object> realmAccess = jwt.getClaim(REALM_ACCESS);
return (List<String>) realmAccess.get(REALM_ACCESS_ROLE);
}
return (List<String>) claims.get(applicationConfig.getRoleClaim());
}
}

Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.Arguments;
import org.junit.jupiter.params.provider.MethodSource;
import org.junit.jupiter.params.provider.NullAndEmptySource;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.authority.SimpleGrantedAuthority;
import org.springframework.security.oauth2.jwt.Jwt;
Expand Down Expand Up @@ -40,6 +41,7 @@ void testConverter01() {
applicationConfig.setRoleAdmin(List.of(""));
applicationConfig.setRoleInternalUser(List.of());
applicationConfig.setRoleWebClient(List.of(""));
applicationConfig.setRoleClaim("role-claim");
List<String> nullList = new ArrayList<>();
nullList.add(null);
applicationConfig.setRoleRespondent(nullList);
Expand All @@ -53,9 +55,42 @@ void testConverter01() {
assertThat(authorities).isEmpty();
}

@ParameterizedTest
@NullAndEmptySource
@DisplayName("Given a JWT, when converting to roles using no specific role-claim, use default claims")
void testConverter01(String roleClaim) {
// given
applicationConfig.setRoleClaim(roleClaim);
applicationConfig.setRoleAdmin(JWT_ROLE_ADMIN);
applicationConfig.setRoleWebClient(List.of(""));
applicationConfig.setRoleInternalUser(JWT_ROLE_INTERNAL_USER);
converter = new GrantedAuthorityConverter(applicationConfig);
List<String> tokenRoles = List.of("internal_user", "adm");

Map<String, Object> jwtHeaders = new HashMap<>();
jwtHeaders.put("header", "headerValue");

Map<String, Object> claims = new HashMap<>();
Map<String, List<String>> realmRoles = new HashMap<>();
realmRoles.put(GrantedAuthorityConverter.REALM_ACCESS_ROLE, tokenRoles);
claims.put(GrantedAuthorityConverter.REALM_ACCESS, realmRoles);
Jwt jwt = new Jwt("user-id", Instant.now(), Instant.MAX, jwtHeaders, claims);

// when
Collection<GrantedAuthority> authorities = converter.convert(jwt);

// then
assertThat(authorities)
.hasSize(2)
.containsExactlyInAnyOrder(
new SimpleGrantedAuthority(AuthorityRoleEnum.INTERNAL_USER.securityRole()),
new SimpleGrantedAuthority(AuthorityRoleEnum.ADMIN.securityRole()));
}

@Test
@DisplayName("Given a JWT, when converting roles, then convert only JWT roles matching roles in role properties")
void testConverter02() {
applicationConfig.setRoleClaim("role-claim");
applicationConfig.setRoleAdmin(JWT_ROLE_ADMIN);
applicationConfig.setRoleInternalUser(JWT_ROLE_INTERNAL_USER);
applicationConfig.setRoleWebClient(List.of("webclient", "adm"));
Expand All @@ -78,6 +113,7 @@ void testConverter02() {
void testConverter03() {
String dummyRole = "dummyRole";
String dummyRole2 = "dummyRole2";
applicationConfig.setRoleClaim("role-claim");
applicationConfig.setRoleAdmin(List.of(dummyRole));
applicationConfig.setRoleInternalUser(List.of(dummyRole2));
applicationConfig.setRoleWebClient(List.of(dummyRole));
Expand All @@ -100,6 +136,7 @@ void testConverter03() {
@MethodSource("provideJWTRoleWithAppRoleAssociated")
@DisplayName("Given a JWT, when converting roles, then assure each JWT role is converted to equivalent app role")
void testConverter04(List<String> jwtRoles, AuthorityRoleEnum appRole) {
applicationConfig.setRoleClaim("role-claim");
applicationConfig.setRoleAdmin(JWT_ROLE_ADMIN);
applicationConfig.setRoleInternalUser(JWT_ROLE_INTERNAL_USER);
applicationConfig.setRoleWebClient(JWT_ROLE_WEBCLIENT);
Expand Down
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@
<module>platine-management-api</module>
</modules>
<properties>
<revision>2.8.2</revision>
<revision>2.8.3</revision>
<changelist></changelist>
<java.version>21</java.version>
<maven.compiler.source>21</maven.compiler.source>
Expand Down
Loading