use of org.keycloak.services.resources.account.resources.AbstractResourceService.Resource in project keycloak by keycloak.
the class ResourcesRestServiceTest method testGetPermissions.
@Test
public void testGetPermissions() throws Exception {
Resource resource = getMyResources().get(0);
List<Permission> shares = doGet("/" + resource.getId() + "/permissions", new TypeReference<List<Permission>>() {
});
assertEquals(1, shares.size());
Permission firstShare = shares.get(0);
List<Permission> permissions = new ArrayList<>();
assertTrue(userNames.contains(firstShare.getUsername()));
assertEquals(2, firstShare.getScopes().size());
List<String> users = new ArrayList<>(userNames);
users.remove(firstShare.getUsername());
for (String userName : users) {
Permission permission = new Permission();
permission.setUsername(userName);
permission.addScope("Scope D");
permissions.add(permission);
}
SimpleHttp.doPut(getAccountUrl("resources/" + resource.getId() + "/permissions"), httpClient).auth(tokenUtil.getToken()).json(permissions).asResponse();
shares = doGet("/" + resource.getId() + "/permissions", new TypeReference<List<Permission>>() {
});
assertEquals(3, shares.size());
for (Permission user : shares) {
assertTrue(userNames.contains(user.getUsername()));
if (firstShare.getUsername().equals(user.getUsername())) {
assertEquals(2, user.getScopes().size());
} else {
assertEquals(1, user.getScopes().size());
}
}
}
use of org.keycloak.services.resources.account.resources.AbstractResourceService.Resource in project keycloak by keycloak.
the class ResourcesRestServiceTest method assertMyResourcesResponse.
private void assertMyResourcesResponse(List<Resource> resources) {
for (Resource resource : resources) {
String uri = resource.getUri();
int id = Integer.parseInt(uri.substring(uri.lastIndexOf('/') + 1));
assertNotNull(resource.getId());
assertEquals("Resource " + id, resource.getName());
assertEquals("Display Name " + id, resource.getDisplayName());
assertEquals("Icon Uri " + id, resource.getIconUri());
assertEquals("my-resource-server", resource.getClient().getClientId());
assertEquals("My Resource Server", resource.getClient().getName());
assertEquals("http://resourceserver.com", resource.getClient().getBaseUrl());
}
}
use of org.keycloak.services.resources.account.resources.AbstractResourceService.Resource in project keycloak by keycloak.
the class ResourcesRestServiceTest method testGetPermissionRequests.
@Test
public void testGetPermissionRequests() {
Resource resource = getMyResources().get(0);
List<Permission> requests = doGet("/" + resource.getId() + "/permissions/requests", new TypeReference<List<Permission>>() {
});
assertTrue(requests.isEmpty());
for (String userName : userNames) {
List<String> scopes = new ArrayList<>();
if ("bob".equals(userName)) {
scopes.add("Scope D");
} else if ("alice".equals(userName)) {
scopes.add("Scope C");
} else if ("jdoe".equals(userName)) {
scopes.add("Scope C");
scopes.add("Scope D");
}
for (String scope : scopes) {
PermissionTicketRepresentation ticket = new PermissionTicketRepresentation();
ticket.setGranted(false);
ticket.setOwner("test-user@localhost");
ticket.setRequesterName(userName);
ticket.setResource(resource.getId());
ticket.setScopeName(scope);
authzClient.protection("test-user@localhost", "password").permission().create(ticket);
}
}
requests = doGet("/" + resource.getId() + "/permissions/requests", new TypeReference<List<Permission>>() {
});
assertEquals(3, requests.size());
Iterator<Permission> iterator = requests.iterator();
while (iterator.hasNext()) {
Permission permission = iterator.next();
String username = permission.getUsername();
List<String> scopes = permission.getScopes();
if ("bob".equals(username)) {
assertEquals(1, scopes.size());
assertTrue(scopes.contains("Scope D"));
iterator.remove();
} else if ("alice".equals(username)) {
assertEquals(1, scopes.size());
assertTrue(scopes.contains("Scope C"));
iterator.remove();
} else if ("jdoe".equals(username)) {
assertEquals(2, scopes.size());
assertTrue(scopes.contains("Scope C"));
assertTrue(scopes.contains("Scope D"));
iterator.remove();
}
}
assertTrue(requests.isEmpty());
}
use of org.keycloak.services.resources.account.resources.AbstractResourceService.Resource in project keycloak by keycloak.
the class ResourcesRestServiceTest method testGetResource.
@Test
public void testGetResource() {
Resource resource = doGet("/" + getMyResources().get(0).getId(), Resource.class);
String uri = resource.getUri();
int id = Integer.parseInt(uri.substring(uri.lastIndexOf('/') + 1));
assertNotNull(resource.getId());
assertEquals("Resource " + id, resource.getName());
assertEquals("Display Name " + id, resource.getDisplayName());
assertEquals("Icon Uri " + id, resource.getIconUri());
assertEquals("my-resource-server", resource.getClient().getClientId());
assertEquals("My Resource Server", resource.getClient().getName());
assertEquals("http://resourceserver.com", resource.getClient().getBaseUrl());
assertEquals(4, resource.getScopes().size());
OAuth2ErrorRepresentation response = doGet("/invalid_resource", OAuth2ErrorRepresentation.class);
assertEquals("resource_not_found", response.getError());
response = doGet("/" + getMyResources().get(0).getId(), authzClient.obtainAccessToken("jdoe", "password").getToken(), OAuth2ErrorRepresentation.class);
assertEquals("invalid_resource", response.getError());
}
use of org.keycloak.services.resources.account.resources.AbstractResourceService.Resource in project keycloak by keycloak.
the class ResourcesRestServiceTest method testApprovePermissionRequest.
@Test
public void testApprovePermissionRequest() throws IOException {
Resource resource = getMyResources().get(0);
List<Permission> requests = doGet("/" + resource.getId() + "/permissions/requests", new TypeReference<List<Permission>>() {
});
assertTrue(requests.isEmpty());
for (String userName : userNames) {
List<String> scopes = new ArrayList<>();
if ("bob".equals(userName)) {
scopes.add("Scope D");
} else if ("alice".equals(userName)) {
scopes.add("Scope C");
} else if ("jdoe".equals(userName)) {
scopes.add("Scope C");
scopes.add("Scope D");
}
for (String scope : scopes) {
PermissionTicketRepresentation ticket = new PermissionTicketRepresentation();
ticket.setGranted(false);
ticket.setOwner("test-user@localhost");
ticket.setRequesterName(userName);
ticket.setResource(resource.getId());
ticket.setScopeName(scope);
authzClient.protection("test-user@localhost", "password").permission().create(ticket);
}
}
requests = doGet("/" + resource.getId() + "/permissions/requests", new TypeReference<List<Permission>>() {
});
assertEquals(3, requests.size());
Iterator<Permission> iterator = requests.iterator();
while (iterator.hasNext()) {
Permission permission = iterator.next();
String username = permission.getUsername();
List<String> scopes = permission.getScopes();
if ("bob".equals(username)) {
scopes.clear();
} else if ("jdoe".equals(username)) {
scopes.remove("Scope C");
}
}
SimpleHttp.doPut(getAccountUrl("resources/" + resource.getId() + "/permissions"), httpClient).auth(tokenUtil.getToken()).json(requests).asResponse();
requests = doGet("/" + resource.getId() + "/permissions/requests", new TypeReference<List<Permission>>() {
});
assertTrue(requests.isEmpty());
for (String user : Arrays.asList("alice", "jdoe")) {
AbstractResourceService.ResourcePermission sharedResource = getSharedWithMe(user).stream().filter(resource1 -> resource1.getId().equals(resource.getId())).findAny().orElse(null);
assertNotNull(sharedResource);
Set<ScopeRepresentation> scopes = sharedResource.getScopes();
if ("alice".equals(user)) {
assertEquals(1, scopes.size());
assertTrue(scopes.stream().anyMatch(scope -> "Scope C".equals(scope.getName())));
} else if ("jdoe".equals(user)) {
assertEquals(1, scopes.size());
assertTrue(scopes.stream().anyMatch(scope -> "Scope D".equals(scope.getName())));
}
}
}
Aggregations