use of org.keycloak.representations.idm.authorization.PermissionTicketToken in project keycloak by keycloak.
the class PermissionManagementTest method assertPersistence.
private void assertPersistence(PermissionResponse response, ResourceRepresentation resource, String... scopeNames) throws Exception {
String ticket = response.getTicket();
assertNotNull(ticket);
int expectedPermissions = scopeNames.length > 0 ? scopeNames.length : 1;
List<PermissionTicketRepresentation> tickets = getAuthzClient().protection().permission().findByResource(resource.getId());
assertEquals(expectedPermissions, tickets.size());
PermissionTicketToken token = new JWSInput(ticket).readJsonContent(PermissionTicketToken.class);
List<Permission> tokenPermissions = token.getPermissions();
assertNotNull(tokenPermissions);
assertEquals(expectedPermissions, scopeNames.length > 0 ? scopeNames.length : tokenPermissions.size());
Iterator<Permission> permissionIterator = tokenPermissions.iterator();
while (permissionIterator.hasNext()) {
Permission resourcePermission = permissionIterator.next();
long count = tickets.stream().filter(representation -> representation.getResource().equals(resourcePermission.getResourceId())).count();
if (count == (scopeNames.length > 0 ? scopeNames.length : 1)) {
permissionIterator.remove();
}
}
assertTrue(tokenPermissions.isEmpty());
ArrayList<PermissionTicketRepresentation> expectedTickets = new ArrayList<>(tickets);
Iterator<PermissionTicketRepresentation> ticketIterator = expectedTickets.iterator();
while (ticketIterator.hasNext()) {
PermissionTicketRepresentation ticketRep = ticketIterator.next();
assertFalse(ticketRep.isGranted());
if (ticketRep.getScope() != null) {
ScopeRepresentation scope = getClient(getRealm()).authorization().scopes().scope(ticketRep.getScope()).toRepresentation();
if (Arrays.asList(scopeNames).contains(scope.getName())) {
ticketIterator.remove();
}
} else if (ticketRep.getResource().equals(resource.getId())) {
ticketIterator.remove();
}
}
assertTrue(expectedTickets.isEmpty());
}
Aggregations