Skip to content

Extract groups from OAuth JWT token #11430

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

Merged
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 @@ -2,6 +2,7 @@

import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.datatype.jsr310.JavaTimeModule;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.security.authentication.BadCredentialsException;
Expand All @@ -19,6 +20,7 @@ public static Collection<String> extractClientRoles(final Map<String, Object> cl
try {
// Convert the map to a JSON string
ObjectMapper objectMapper = new ObjectMapper();
objectMapper.registerModule(new JavaTimeModule());
String jsonString = objectMapper.writeValueAsString(claims);

JsonNode rolesCursor = new ObjectMapper().readTree(jsonString);
Expand Down Expand Up @@ -51,6 +53,9 @@ public static Collection<String> extractClientRoles(final JsonNode claims, final
}

}
if (rolesCursor.isTextual()) {
rolesCursor = new ObjectMapper().readTree(rolesCursor.asText());
}
return StreamSupport.stream(rolesCursor.spliterator(), false)
.map(JsonNode::asText)
.collect(Collectors.toSet());
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
package org.cbioportal.application.security.util;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.node.ObjectNode;
import org.junit.jupiter.api.Test;
import org.junit.runner.RunWith;
import org.mockito.junit.MockitoJUnitRunner;


import java.time.Instant;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import static org.junit.jupiter.api.Assertions.*;

@RunWith(MockitoJUnitRunner.class)
class ClaimRoleExtractorUtilTest {

@Test
void shouldExtractRolesFromSerializedClaims() {
String claims = """
{
"roles": ["role1", "role2"]
}
""";
String jwtRolesPath = "roles";
var result = ClaimRoleExtractorUtil.extractClientRoles(claims, jwtRolesPath);
assertNotNull(result);
assertEquals(2, result.size());
assertTrue(result.contains("role1"));
assertTrue(result.contains("role2"));
}

@Test
void shouldExtractRolesFromJsonNodeClaims() {
ObjectMapper objectMapper = new ObjectMapper();
String roleString = """
["role1","role2","role3"]
""";
ObjectNode root = objectMapper.createObjectNode();
root.put("roles", roleString);
String jwtRolesPath = "roles";

var result = ClaimRoleExtractorUtil.extractClientRoles(root, jwtRolesPath);

assertNotNull(result);
assertEquals(3, result.size());
assertTrue(result.contains("role1"));
assertTrue(result.contains("role2"));
assertTrue(result.contains("role3"));
}

@Test
void shouldExtractRolesFromString() {
ObjectMapper objectMapper = new ObjectMapper();
String groupString = """
["GROUP_A","GROUP_B","GROUP_C"]
""";
ObjectNode root = objectMapper.createObjectNode();
root.put("groups", groupString);
String jwtRolesPath = "groups";

var result = ClaimRoleExtractorUtil.extractClientRoles(root, jwtRolesPath);

assertNotNull(result);
assertEquals(3, result.size());
assertTrue(result.contains("GROUP_A"));
assertTrue(result.contains("GROUP_B"));
assertTrue(result.contains("GROUP_C"));
}

@Test
void shouldExtractRolesFormIdToken() {

Map<String, Object> claims = new HashMap<>();
claims.put("aud", "my-app-client-id");
claims.put("exp", Instant.now());
claims.put("iat", Instant.now());
claims.put("groups", List.of("GROUP_X", "GROUP_Y"));

String jwtRolesPath = "groups";
var result = ClaimRoleExtractorUtil.extractClientRoles(claims, jwtRolesPath);
assertNotNull(result);
assertEquals(2, result.size());
assertTrue(result.contains("GROUP_X"));
assertTrue(result.contains("GROUP_Y"));
}

}