Search in sources :

Example 1 with PolicyResource

use of org.keycloak.authorization.client.resource.PolicyResource in project keycloak by keycloak.

the class UserManagedPermissionServiceTest method testPermissionWithoutScopes.

@Test
public void testPermissionWithoutScopes() {
    ResourceRepresentation resource = new ResourceRepresentation();
    resource.setName(UUID.randomUUID().toString());
    resource.setOwner("marta");
    resource.setOwnerManagedAccess(true);
    resource.addScope("Scope A", "Scope B", "Scope C");
    ProtectionResource protection = getAuthzClient().protection();
    resource = protection.resource().create(resource);
    UmaPermissionRepresentation permission = new UmaPermissionRepresentation();
    permission.setName("Custom User-Managed Policy");
    permission.addRole("role_a");
    PolicyResource policy = getAuthzClient().protection("marta", "password").policy(resource.getId());
    permission = policy.create(permission);
    assertEquals(3, permission.getScopes().size());
    assertTrue(Arrays.asList("Scope A", "Scope B", "Scope C").containsAll(permission.getScopes()));
    permission = policy.findById(permission.getId());
    assertTrue(Arrays.asList("Scope A", "Scope B", "Scope C").containsAll(permission.getScopes()));
    assertEquals(3, permission.getScopes().size());
    permission.removeScope("Scope B");
    policy.update(permission);
    permission = policy.findById(permission.getId());
    assertEquals(2, permission.getScopes().size());
    assertTrue(Arrays.asList("Scope A", "Scope C").containsAll(permission.getScopes()));
}
Also used : ProtectionResource(org.keycloak.authorization.client.resource.ProtectionResource) PolicyResource(org.keycloak.authorization.client.resource.PolicyResource) UmaPermissionRepresentation(org.keycloak.representations.idm.authorization.UmaPermissionRepresentation) ResourceRepresentation(org.keycloak.representations.idm.authorization.ResourceRepresentation) Test(org.junit.Test)

Example 2 with PolicyResource

use of org.keycloak.authorization.client.resource.PolicyResource in project keycloak by keycloak.

the class UserManagedPermissionServiceTest method testFindPermission.

@Test
public void testFindPermission() {
    ResourceRepresentation resource = new ResourceRepresentation();
    resource.setName(UUID.randomUUID().toString());
    resource.setOwner("marta");
    resource.setOwnerManagedAccess(true);
    resource.addScope("Scope A", "Scope B", "Scope C");
    ProtectionResource protection = getAuthzClient().protection();
    resource = protection.resource().create(resource);
    PolicyResource policy = getAuthzClient().protection("marta", "password").policy(resource.getId());
    for (int i = 0; i < 10; i++) {
        UmaPermissionRepresentation permission = new UmaPermissionRepresentation();
        permission.setName("Custom User-Managed Policy " + i);
        permission.addRole("role_a");
        policy.create(permission);
    }
    assertEquals(10, policy.find(null, null, null, null).size());
    List<UmaPermissionRepresentation> byId = policy.find("Custom User-Managed Policy 8", null, null, null);
    assertEquals(1, byId.size());
    assertEquals(byId.get(0).getId(), policy.findById(byId.get(0).getId()).getId());
    assertEquals(10, policy.find(null, "Scope A", null, null).size());
    assertEquals(5, policy.find(null, null, -1, 5).size());
    assertEquals(2, policy.find(null, null, -1, 2).size());
}
Also used : ProtectionResource(org.keycloak.authorization.client.resource.ProtectionResource) PolicyResource(org.keycloak.authorization.client.resource.PolicyResource) UmaPermissionRepresentation(org.keycloak.representations.idm.authorization.UmaPermissionRepresentation) ResourceRepresentation(org.keycloak.representations.idm.authorization.ResourceRepresentation) Test(org.junit.Test)

Aggregations

Test (org.junit.Test)2 PolicyResource (org.keycloak.authorization.client.resource.PolicyResource)2 ProtectionResource (org.keycloak.authorization.client.resource.ProtectionResource)2 ResourceRepresentation (org.keycloak.representations.idm.authorization.ResourceRepresentation)2 UmaPermissionRepresentation (org.keycloak.representations.idm.authorization.UmaPermissionRepresentation)2