use of org.keycloak.authorization.client.resource.ProtectionResource in project keycloak by keycloak.
the class UserManagedPermissionServiceTest method testCreate.
private void testCreate() {
ResourceRepresentation resource = new ResourceRepresentation();
resource.setName("Resource A");
resource.setOwnerManagedAccess(true);
resource.setOwner("marta");
resource.addScope("Scope A", "Scope B", "Scope C");
resource = getAuthzClient().protection().resource().create(resource);
UmaPermissionRepresentation newPermission = new UmaPermissionRepresentation();
newPermission.setName("Custom User-Managed Permission");
newPermission.setDescription("Users from specific roles are allowed to access");
newPermission.addScope("Scope A", "Scope B", "Scope C");
newPermission.addRole("role_a", "role_b", "role_c", "role_d");
newPermission.addGroup("/group_a", "/group_a/group_b", "/group_c");
newPermission.addClient("client-a", "resource-server-test");
if (Profile.isFeatureEnabled(Profile.Feature.UPLOAD_SCRIPTS)) {
newPermission.setCondition("$evaluation.grant()");
}
newPermission.addUser("kolo");
ProtectionResource protection = getAuthzClient().protection("marta", "password");
UmaPermissionRepresentation permission = protection.policy(resource.getId()).create(newPermission);
assertEquals(newPermission.getName(), permission.getName());
assertEquals(newPermission.getDescription(), permission.getDescription());
assertNotNull(permission.getScopes());
assertTrue(permission.getScopes().containsAll(newPermission.getScopes()));
assertNotNull(permission.getRoles());
assertTrue(permission.getRoles().containsAll(newPermission.getRoles()));
assertNotNull(permission.getGroups());
assertTrue(permission.getGroups().containsAll(newPermission.getGroups()));
assertNotNull(permission.getClients());
assertTrue(permission.getClients().containsAll(newPermission.getClients()));
assertEquals(newPermission.getCondition(), permission.getCondition());
assertNotNull(permission.getUsers());
assertTrue(permission.getUsers().containsAll(newPermission.getUsers()));
}
use of org.keycloak.authorization.client.resource.ProtectionResource in project keycloak by keycloak.
the class UserManagedPermissionServiceTest method testRemovePoliciesOnGroupDelete.
@Test
public void testRemovePoliciesOnGroupDelete() {
ResourceRepresentation resource = new ResourceRepresentation();
resource.setName("Resource A");
resource.setOwnerManagedAccess(true);
resource.setOwner("marta");
resource.addScope("Scope A", "Scope B", "Scope C");
resource = getAuthzClient().protection().resource().create(resource);
UmaPermissionRepresentation newPermission = new UmaPermissionRepresentation();
newPermission.setName("Custom User-Managed Permission");
newPermission.addGroup("/group_remove");
ProtectionResource protection = getAuthzClient().protection("marta", "password");
protection.policy(resource.getId()).create(newPermission);
getTestingClient().server().run((RunOnServer) UserManagedPermissionServiceTest::testRemovePoliciesOnGroupDelete);
}
use of org.keycloak.authorization.client.resource.ProtectionResource in project keycloak by keycloak.
the class AlbumService method deleteProtectedResource.
private void deleteProtectedResource(Album album) {
String uri = "/album/" + album.getName();
try {
ProtectionResource protection = getAuthzClient().protection();
List<ResourceRepresentation> search = protection.resource().findByUri(uri);
if (search.isEmpty()) {
throw new RuntimeException("Could not find protected resource with URI [" + uri + "]");
}
protection.resource().delete(search.get(0).getId());
} catch (RuntimeException e) {
throw new RuntimeException("Could not search protected resource.", e);
}
}
use of org.keycloak.authorization.client.resource.ProtectionResource in project keycloak by keycloak.
the class UserManagedPermissionServiceTest method testUserManagedPermission.
@Test
public void testUserManagedPermission() {
ResourceRepresentation resource = new ResourceRepresentation();
resource.setName("Resource A");
resource.setOwnerManagedAccess(true);
resource.setOwner("marta");
resource.addScope("Scope A", "Scope B", "Scope C");
resource = getAuthzClient().protection().resource().create(resource);
UmaPermissionRepresentation permission = new UmaPermissionRepresentation();
permission.setName("Custom User-Managed Permission");
permission.setDescription("Users from specific roles are allowed to access");
permission.addScope("Scope A");
permission.addRole("role_a");
ProtectionResource protection = getAuthzClient().protection("marta", "password");
permission = protection.policy(resource.getId()).create(permission);
AuthorizationResource authorization = getAuthzClient().authorization("kolo", "password");
AuthorizationRequest request = new AuthorizationRequest();
request.addPermission(resource.getId(), "Scope A");
AuthorizationResponse authzResponse = authorization.authorize(request);
assertNotNull(authzResponse);
permission.removeRole("role_a");
permission.addRole("role_b");
protection.policy(resource.getId()).update(permission);
try {
authorization.authorize(request);
fail("User should not have permission");
} catch (Exception e) {
assertTrue(AuthorizationDeniedException.class.isInstance(e));
}
try {
getAuthzClient().authorization("alice", "password").authorize(request);
fail("User should not have permission");
} catch (Exception e) {
assertTrue(AuthorizationDeniedException.class.isInstance(e));
}
permission.addRole("role_a");
protection.policy(resource.getId()).update(permission);
authzResponse = authorization.authorize(request);
assertNotNull(authzResponse);
protection.policy(resource.getId()).delete(permission.getId());
try {
authorization.authorize(request);
fail("User should not have permission");
} catch (Exception e) {
assertTrue(AuthorizationDeniedException.class.isInstance(e));
}
try {
getAuthzClient().protection("marta", "password").policy(resource.getId()).findById(permission.getId());
fail("Permission must not exist");
} catch (Exception e) {
assertEquals(404, HttpResponseException.class.cast(e.getCause()).getStatusCode());
}
// create a user based permission, where only selected users are allowed access to the resource.
permission = new UmaPermissionRepresentation();
permission.setName("Custom User-Managed Permission");
permission.setDescription("Specific users are allowed access to the resource");
permission.addScope("Scope A");
permission.addUser("alice");
protection.policy(resource.getId()).create(permission);
// alice should be able to access the resource with the updated permission.
authzResponse = getAuthzClient().authorization("alice", "password").authorize(request);
assertNotNull(authzResponse);
// kolo shouldn't be able to access the resource with the updated permission.
try {
authorization.authorize(request);
fail("User should not have permission to access the protected resource");
} catch (Exception e) {
assertTrue(AuthorizationDeniedException.class.isInstance(e));
}
}
use of org.keycloak.authorization.client.resource.ProtectionResource in project keycloak by keycloak.
the class UserManagedPermissionServiceTest method testOnlyResourcesWithOwnerManagedAccess.
@Test
public void testOnlyResourcesWithOwnerManagedAccess() {
ResourceRepresentation resource = new ResourceRepresentation();
resource.setName(UUID.randomUUID().toString());
resource.setOwner("marta");
resource.addScope("Scope A", "Scope B", "Scope C");
ProtectionResource protection = getAuthzClient().protection();
resource = protection.resource().create(resource);
try {
getAuthzClient().protection("marta", "password").policy(resource.getId()).create(new UmaPermissionRepresentation());
fail("Error expected");
} catch (Exception e) {
assertTrue(HttpResponseException.class.cast(e.getCause()).toString().contains("Only resources with owner managed accessed can have policies"));
}
}
Aggregations