Search in sources :

Example 16 with RolePolicyRepresentation

use of org.keycloak.representations.idm.authorization.RolePolicyRepresentation in project keycloak by keycloak.

the class RolePolicyManagementTest method assertRepresentation.

private void assertRepresentation(RolePolicyRepresentation representation, RolePolicyResource permission) {
    RolePolicyRepresentation actual = permission.toRepresentation();
    assertRepresentation(representation, actual, () -> permission.resources(), () -> Collections.emptyList(), () -> permission.associatedPolicies());
    assertEquals(representation.getRoles().size(), actual.getRoles().size());
    ClientRepresentation clientRep = getClient().toRepresentation();
    assertEquals(0, actual.getRoles().stream().filter(actualDefinition -> !representation.getRoles().stream().filter(roleDefinition -> (getRoleName(actualDefinition.getId()).equals(roleDefinition.getId()) || (clientRep.getClientId() + "/" + getRoleName(actualDefinition.getId())).equals(roleDefinition.getId())) && actualDefinition.isRequired() == roleDefinition.isRequired()).findFirst().isPresent()).count());
}
Also used : RolePolicyRepresentation(org.keycloak.representations.idm.authorization.RolePolicyRepresentation) ClientRepresentation(org.keycloak.representations.idm.ClientRepresentation)

Example 17 with RolePolicyRepresentation

use of org.keycloak.representations.idm.authorization.RolePolicyRepresentation in project keycloak by keycloak.

the class RolePolicyManagementTest method assertCreated.

private void assertCreated(AuthorizationResource authorization, RolePolicyRepresentation representation) {
    RolePoliciesResource permissions = authorization.policies().role();
    try (Response response = permissions.create(representation)) {
        RolePolicyRepresentation created = response.readEntity(RolePolicyRepresentation.class);
        RolePolicyResource permission = permissions.findById(created.getId());
        assertRepresentation(representation, permission);
    }
}
Also used : Response(javax.ws.rs.core.Response) RolePolicyRepresentation(org.keycloak.representations.idm.authorization.RolePolicyRepresentation) RolePoliciesResource(org.keycloak.admin.client.resource.RolePoliciesResource) RolePolicyResource(org.keycloak.admin.client.resource.RolePolicyResource)

Example 18 with RolePolicyRepresentation

use of org.keycloak.representations.idm.authorization.RolePolicyRepresentation in project keycloak by keycloak.

the class RolePolicyManagementTest method testDelete.

@Test
public void testDelete() {
    AuthorizationResource authorization = getClient().authorization();
    RolePolicyRepresentation representation = new RolePolicyRepresentation();
    representation.setName("Test Delete Permission");
    representation.addRole("Role A", false);
    RolePoliciesResource policies = authorization.policies().role();
    try (Response response = policies.create(representation)) {
        RolePolicyRepresentation created = response.readEntity(RolePolicyRepresentation.class);
        policies.findById(created.getId()).remove();
        RolePolicyResource removed = policies.findById(created.getId());
        try {
            removed.toRepresentation();
            fail("Permission not removed");
        } catch (NotFoundException ignore) {
        }
    }
}
Also used : Response(javax.ws.rs.core.Response) RolePolicyRepresentation(org.keycloak.representations.idm.authorization.RolePolicyRepresentation) RolePoliciesResource(org.keycloak.admin.client.resource.RolePoliciesResource) RolePolicyResource(org.keycloak.admin.client.resource.RolePolicyResource) NotFoundException(javax.ws.rs.NotFoundException) AuthorizationResource(org.keycloak.admin.client.resource.AuthorizationResource) Test(org.junit.Test)

Example 19 with RolePolicyRepresentation

use of org.keycloak.representations.idm.authorization.RolePolicyRepresentation in project keycloak by keycloak.

the class Policies method update.

public void update(String name, AbstractPolicyRepresentation representation) {
    for (WebElement row : policies().rows()) {
        PolicyRepresentation actual = policies().toRepresentation(row);
        if (actual.getName().equalsIgnoreCase(name)) {
            clickLink(row.findElements(tagName("a")).get(0));
            String type = representation.getType();
            if ("role".equals(type)) {
                rolePolicy.form().populate((RolePolicyRepresentation) representation, true);
            } else if ("user".equals(type)) {
                userPolicy.form().populate((UserPolicyRepresentation) representation, true);
            } else if ("aggregate".equals(type)) {
                aggregatePolicy.form().populate((AggregatePolicyRepresentation) representation, true);
            } else if ("js".equals(type)) {
                jsPolicy.form().populate((JSPolicyRepresentation) representation, true);
            } else if ("time".equals(type)) {
                timePolicy.form().populate((TimePolicyRepresentation) representation, true);
            } else if ("client".equals(type)) {
                clientPolicy.form().populate((ClientPolicyRepresentation) representation, true);
            } else if ("group".equals(type)) {
                groupPolicy.form().populate((GroupPolicyRepresentation) representation, true);
            }
            return;
        }
    }
}
Also used : RolePolicyRepresentation(org.keycloak.representations.idm.authorization.RolePolicyRepresentation) AbstractPolicyRepresentation(org.keycloak.representations.idm.authorization.AbstractPolicyRepresentation) GroupPolicyRepresentation(org.keycloak.representations.idm.authorization.GroupPolicyRepresentation) TimePolicyRepresentation(org.keycloak.representations.idm.authorization.TimePolicyRepresentation) PolicyRepresentation(org.keycloak.representations.idm.authorization.PolicyRepresentation) UserPolicyRepresentation(org.keycloak.representations.idm.authorization.UserPolicyRepresentation) AggregatePolicyRepresentation(org.keycloak.representations.idm.authorization.AggregatePolicyRepresentation) JSPolicyRepresentation(org.keycloak.representations.idm.authorization.JSPolicyRepresentation) ClientPolicyRepresentation(org.keycloak.representations.idm.authorization.ClientPolicyRepresentation) ClientPolicyRepresentation(org.keycloak.representations.idm.authorization.ClientPolicyRepresentation) UserPolicyRepresentation(org.keycloak.representations.idm.authorization.UserPolicyRepresentation) JSPolicyRepresentation(org.keycloak.representations.idm.authorization.JSPolicyRepresentation) WebElement(org.openqa.selenium.WebElement)

Example 20 with RolePolicyRepresentation

use of org.keycloak.representations.idm.authorization.RolePolicyRepresentation in project keycloak by keycloak.

the class AbstractServletAuthzAdapterTest method testRequiredRole.

@Test
public void testRequiredRole() throws Exception {
    performTests(() -> {
        login("jdoe", "jdoe");
        navigateToUserPremiumPage();
        assertWasNotDenied();
        RolesResource rolesResource = getClientResource(RESOURCE_SERVER_ID).roles();
        rolesResource.create(new RoleRepresentation("required-role", "", false));
        RolePolicyRepresentation policy = new RolePolicyRepresentation();
        policy.setName("Required Role Policy");
        policy.addRole("user_premium", false);
        policy.addRole(RESOURCE_SERVER_ID + "/required-role", false);
        RolePoliciesResource rolePolicy = getAuthorizationResource().policies().role();
        rolePolicy.create(policy);
        policy = rolePolicy.findByName(policy.getName());
        updatePermissionPolicies("Premium Resource Permission", policy.getName());
        login("jdoe", "jdoe");
        navigateToUserPremiumPage();
        assertWasNotDenied();
        policy.getRoles().clear();
        policy.addRole("user_premium", false);
        policy.addRole(RESOURCE_SERVER_ID + "/required-role", true);
        rolePolicy.findById(policy.getId()).update(policy);
        login("jdoe", "jdoe");
        navigateToUserPremiumPage();
        assertWasDenied();
        UsersResource users = realmsResouce().realm(REALM_NAME).users();
        UserRepresentation user = users.search("jdoe").get(0);
        RoleScopeResource roleScopeResource = users.get(user.getId()).roles().clientLevel(getClientResource(RESOURCE_SERVER_ID).toRepresentation().getId());
        RoleRepresentation requiredRole = rolesResource.get("required-role").toRepresentation();
        roleScopeResource.add(Arrays.asList(requiredRole));
        login("jdoe", "jdoe");
        navigateToUserPremiumPage();
        assertWasNotDenied();
        policy.getRoles().clear();
        policy.addRole("user_premium", false);
        policy.addRole(RESOURCE_SERVER_ID + "/required-role", false);
        rolePolicy.findById(policy.getId()).update(policy);
        login("jdoe", "jdoe");
        navigateToUserPremiumPage();
        assertWasNotDenied();
        roleScopeResource.remove(Arrays.asList(requiredRole));
        login("jdoe", "jdoe");
        navigateToUserPremiumPage();
        assertWasNotDenied();
    });
}
Also used : RoleRepresentation(org.keycloak.representations.idm.RoleRepresentation) RolePolicyRepresentation(org.keycloak.representations.idm.authorization.RolePolicyRepresentation) RoleScopeResource(org.keycloak.admin.client.resource.RoleScopeResource) UsersResource(org.keycloak.admin.client.resource.UsersResource) RolePoliciesResource(org.keycloak.admin.client.resource.RolePoliciesResource) RolesResource(org.keycloak.admin.client.resource.RolesResource) UserRepresentation(org.keycloak.representations.idm.UserRepresentation) Test(org.junit.Test)

Aggregations

RolePolicyRepresentation (org.keycloak.representations.idm.authorization.RolePolicyRepresentation)30 Test (org.junit.Test)16 AuthorizationResource (org.keycloak.admin.client.resource.AuthorizationResource)8 RolePoliciesResource (org.keycloak.admin.client.resource.RolePoliciesResource)8 UserPolicyRepresentation (org.keycloak.representations.idm.authorization.UserPolicyRepresentation)7 RoleRepresentation (org.keycloak.representations.idm.RoleRepresentation)6 RolesResource (org.keycloak.admin.client.resource.RolesResource)5 JSPolicyRepresentation (org.keycloak.representations.idm.authorization.JSPolicyRepresentation)5 RolePolicyResource (org.keycloak.admin.client.resource.RolePolicyResource)4 AggregatePolicyRepresentation (org.keycloak.representations.idm.authorization.AggregatePolicyRepresentation)4 ClientPolicyRepresentation (org.keycloak.representations.idm.authorization.ClientPolicyRepresentation)4 GroupPolicyRepresentation (org.keycloak.representations.idm.authorization.GroupPolicyRepresentation)4 Response (javax.ws.rs.core.Response)3 Before (org.junit.Before)3 PoliciesResource (org.keycloak.admin.client.resource.PoliciesResource)3 Policy (org.keycloak.authorization.model.Policy)3 AbstractPolicyRepresentation (org.keycloak.representations.idm.authorization.AbstractPolicyRepresentation)3 AggregatePolicy (org.keycloak.testsuite.console.page.clients.authorization.policy.AggregatePolicy)3 RolePolicy (org.keycloak.testsuite.console.page.clients.authorization.policy.RolePolicy)3 ResourcesResource (org.keycloak.admin.client.resource.ResourcesResource)2