Search in sources :

Example 31 with PolicyRepresentation

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

the class GenericPolicyManagementTest method testCreate.

@Test
public void testCreate() {
    PolicyRepresentation newPolicy = createTestingPolicy().toRepresentation();
    assertEquals("Test Generic Policy", newPolicy.getName());
    assertEquals("scope", newPolicy.getType());
    assertEquals(Logic.POSITIVE, newPolicy.getLogic());
    assertEquals(DecisionStrategy.UNANIMOUS, newPolicy.getDecisionStrategy());
    assertEquals("configuration for A", newPolicy.getConfig().get("configA"));
    assertEquals("configuration for B", newPolicy.getConfig().get("configB"));
    assertEquals("configuration for C", newPolicy.getConfig().get("configC"));
    List<PolicyRepresentation> policies = getClientResource().authorization().policies().policies();
    assertEquals(6, policies.size());
    assertAssociatedPolicy("Test Associated A", newPolicy);
    assertAssociatedPolicy("Test Associated B", newPolicy);
    assertAssociatedPolicy("Test Associated C", newPolicy);
    assertAssociatedResource("Test Resource A", newPolicy);
    assertAssociatedResource("Test Resource B", newPolicy);
    assertAssociatedResource("Test Resource C", newPolicy);
    assertAssociatedScope("Test Scope A", newPolicy);
    assertAssociatedScope("Test Scope B", newPolicy);
    assertAssociatedScope("Test Scope C", newPolicy);
}
Also used : PolicyRepresentation(org.keycloak.representations.idm.authorization.PolicyRepresentation) Test(org.junit.Test)

Example 32 with PolicyRepresentation

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

the class GenericPolicyManagementTest method testQueryPolicyAllFields.

@Test
public void testQueryPolicyAllFields() {
    AuthorizationResource authorization = getClientResource().authorization();
    authorization.resources().create(new ResourceRepresentation("Resource A"));
    ResourcePermissionRepresentation permission = new ResourcePermissionRepresentation();
    permission.setName("Permission A");
    permission.addResource("Resource A");
    authorization.permissions().resource().create(permission);
    List<PolicyRepresentation> policies = authorization.policies().policies(null, "Permission A", null, null, null, true, null, "*", -1, -1);
    assertEquals(1, policies.size());
    assertEquals(1, policies.get(0).getResourcesData().size());
    policies = authorization.policies().policies(null, "Permission A", null, null, null, true, null, null, -1, -1);
    assertEquals(1, policies.size());
    assertNull(policies.get(0).getResourcesData());
}
Also used : PolicyRepresentation(org.keycloak.representations.idm.authorization.PolicyRepresentation) AuthorizationResource(org.keycloak.admin.client.resource.AuthorizationResource) ResourceRepresentation(org.keycloak.representations.idm.authorization.ResourceRepresentation) ResourcePermissionRepresentation(org.keycloak.representations.idm.authorization.ResourcePermissionRepresentation) Test(org.junit.Test)

Example 33 with PolicyRepresentation

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

the class GenericPolicyManagementTest method testUpdate.

@Test
public void testUpdate() {
    PolicyResource policyResource = createTestingPolicy();
    PolicyRepresentation policy = policyResource.toRepresentation();
    policy.setName("changed");
    policy.setLogic(Logic.NEGATIVE);
    policy.setDecisionStrategy(DecisionStrategy.AFFIRMATIVE);
    policy.getConfig().put("configA", "changed configuration for A");
    policy.getConfig().remove("configB");
    policy.getConfig().put("configC", "changed configuration for C");
    policyResource.update(policy);
    policy = policyResource.toRepresentation();
    assertEquals("changed", policy.getName());
    assertEquals(Logic.NEGATIVE, policy.getLogic());
    assertEquals(DecisionStrategy.AFFIRMATIVE, policy.getDecisionStrategy());
    assertEquals("changed configuration for A", policy.getConfig().get("configA"));
    assertNull(policy.getConfig().get("configB"));
    assertEquals("changed configuration for C", policy.getConfig().get("configC"));
    Map<String, String> config = policy.getConfig();
    config.put("applyPolicies", buildConfigOption(findPolicyByName("Test Associated C").getId()));
    config.put("resources", buildConfigOption(findResourceByName("Test Resource B").getId()));
    config.put("scopes", buildConfigOption(findScopeByName("Test Scope A").getId()));
    policyResource.update(policy);
    policy = policyResource.toRepresentation();
    config = policy.getConfig();
    assertAssociatedPolicy("Test Associated C", policy);
    List<PolicyRepresentation> associatedPolicies = getClientResource().authorization().policies().policy(policy.getId()).associatedPolicies();
    assertFalse(associatedPolicies.stream().filter(associated -> associated.getId().equals(findPolicyByName("Test Associated A").getId())).findFirst().isPresent());
    assertFalse(associatedPolicies.stream().filter(associated -> associated.getId().equals(findPolicyByName("Test Associated B").getId())).findFirst().isPresent());
    assertAssociatedResource("Test Resource B", policy);
    List<ResourceRepresentation> resources = policyResource.resources();
    assertFalse(resources.contains(findResourceByName("Test Resource A")));
    assertFalse(resources.contains(findResourceByName("Test Resource C")));
    assertAssociatedScope("Test Scope A", policy);
    List<ScopeRepresentation> scopes = getClientResource().authorization().policies().policy(policy.getId()).scopes();
    assertFalse(scopes.contains(findScopeByName("Test Scope B").getId()));
    assertFalse(scopes.contains(findScopeByName("Test Scope C").getId()));
}
Also used : PolicyRepresentation(org.keycloak.representations.idm.authorization.PolicyRepresentation) ResourceRepresentation(org.keycloak.representations.idm.authorization.ResourceRepresentation) Arrays(java.util.Arrays) ResourceResource(org.keycloak.admin.client.resource.ResourceResource) AuthorizationResource(org.keycloak.admin.client.resource.AuthorizationResource) HashMap(java.util.HashMap) Function(java.util.function.Function) PolicyRepresentation(org.keycloak.representations.idm.authorization.PolicyRepresentation) ArrayList(java.util.ArrayList) ResourcesResource(org.keycloak.admin.client.resource.ResourcesResource) Logic(org.keycloak.representations.idm.authorization.Logic) Map(java.util.Map) PolicyResource(org.keycloak.admin.client.resource.PolicyResource) ResourceScopeResource(org.keycloak.admin.client.resource.ResourceScopeResource) ResourcePermissionRepresentation(org.keycloak.representations.idm.authorization.ResourcePermissionRepresentation) ScopeRepresentation(org.keycloak.representations.idm.authorization.ScopeRepresentation) ResourceScopesResource(org.keycloak.admin.client.resource.ResourceScopesResource) Assert.assertNotNull(org.junit.Assert.assertNotNull) PolicyProviderRepresentation(org.keycloak.representations.idm.authorization.PolicyProviderRepresentation) Set(java.util.Set) Assert.assertTrue(org.junit.Assert.assertTrue) Test(org.junit.Test) DecisionStrategy(org.keycloak.representations.idm.authorization.DecisionStrategy) Collectors(java.util.stream.Collectors) PoliciesResource(org.keycloak.admin.client.resource.PoliciesResource) List(java.util.List) Assert.assertNull(org.junit.Assert.assertNull) Response(javax.ws.rs.core.Response) Assert.assertFalse(org.junit.Assert.assertFalse) Assert.assertEquals(org.junit.Assert.assertEquals) PolicyResource(org.keycloak.admin.client.resource.PolicyResource) ScopeRepresentation(org.keycloak.representations.idm.authorization.ScopeRepresentation) ResourceRepresentation(org.keycloak.representations.idm.authorization.ResourceRepresentation) Test(org.junit.Test)

Example 34 with PolicyRepresentation

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

the class GenericPolicyManagementTest method testQueryPolicyByIdAllFields.

@Test
public void testQueryPolicyByIdAllFields() {
    PolicyResource policy = createTestingPolicy();
    PolicyRepresentation representation = policy.toRepresentation("*");
    Set<ResourceRepresentation> resources = representation.getResourcesData();
    assertEquals(3, resources.size());
    representation = policy.toRepresentation();
    assertNull(representation.getResourcesData());
}
Also used : PolicyRepresentation(org.keycloak.representations.idm.authorization.PolicyRepresentation) PolicyResource(org.keycloak.admin.client.resource.PolicyResource) ResourceRepresentation(org.keycloak.representations.idm.authorization.ResourceRepresentation) Test(org.junit.Test)

Example 35 with PolicyRepresentation

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

the class RolePolicyManagementTest method testGenericConfig.

@Test
public void testGenericConfig() {
    AuthorizationResource authorization = getClient().authorization();
    RolePolicyRepresentation representation = new RolePolicyRepresentation();
    representation.setName("Test Generic Config  Permission");
    representation.addRole("Role A", false);
    RolePoliciesResource policies = authorization.policies().role();
    try (Response response = policies.create(representation)) {
        RolePolicyRepresentation created = response.readEntity(RolePolicyRepresentation.class);
        PolicyResource policy = authorization.policies().policy(created.getId());
        PolicyRepresentation genericConfig = policy.toRepresentation();
        assertNotNull(genericConfig.getConfig());
        assertNotNull(genericConfig.getConfig().get("roles"));
        RoleRepresentation role = getRealm().roles().get("Role A").toRepresentation();
        assertTrue(genericConfig.getConfig().get("roles").contains(role.getId()));
    }
}
Also used : Response(javax.ws.rs.core.Response) RolePolicyRepresentation(org.keycloak.representations.idm.authorization.RolePolicyRepresentation) PolicyRepresentation(org.keycloak.representations.idm.authorization.PolicyRepresentation) RoleRepresentation(org.keycloak.representations.idm.RoleRepresentation) RolePolicyRepresentation(org.keycloak.representations.idm.authorization.RolePolicyRepresentation) RolePolicyResource(org.keycloak.admin.client.resource.RolePolicyResource) PolicyResource(org.keycloak.admin.client.resource.PolicyResource) RolePoliciesResource(org.keycloak.admin.client.resource.RolePoliciesResource) AuthorizationResource(org.keycloak.admin.client.resource.AuthorizationResource) Test(org.junit.Test)

Aggregations

PolicyRepresentation (org.keycloak.representations.idm.authorization.PolicyRepresentation)58 Test (org.junit.Test)26 HashMap (java.util.HashMap)20 AuthorizationResource (org.keycloak.admin.client.resource.AuthorizationResource)19 ResourceRepresentation (org.keycloak.representations.idm.authorization.ResourceRepresentation)18 Response (javax.ws.rs.core.Response)15 List (java.util.List)12 Map (java.util.Map)12 IOException (java.io.IOException)10 ArrayList (java.util.ArrayList)10 Collectors (java.util.stream.Collectors)10 RoleRepresentation (org.keycloak.representations.idm.RoleRepresentation)10 AbstractPolicyRepresentation (org.keycloak.representations.idm.authorization.AbstractPolicyRepresentation)10 ScopeRepresentation (org.keycloak.representations.idm.authorization.ScopeRepresentation)10 UserPolicyRepresentation (org.keycloak.representations.idm.authorization.UserPolicyRepresentation)10 WebElement (org.openqa.selenium.WebElement)10 Set (java.util.Set)9 Policy (org.keycloak.authorization.model.Policy)9 ClientRepresentation (org.keycloak.representations.idm.ClientRepresentation)9 UserRepresentation (org.keycloak.representations.idm.UserRepresentation)8