Search in sources :

Example 11 with PolicyRepresentation

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

the class GenericPolicyManagementTest method assertAssociatedScope.

private void assertAssociatedScope(String scopeName, PolicyRepresentation policy) {
    ScopeRepresentation scope = findScopeByName(scopeName);
    scope = getClientResource().authorization().scopes().scope(scope.getId()).toRepresentation();
    assertNotNull(scope);
    List<ScopeRepresentation> scopes = getClientResource().authorization().policies().policy(policy.getId()).scopes();
    assertTrue(scopes.stream().map((Function<ScopeRepresentation, String>) rep -> rep.getId()).collect(Collectors.toList()).contains(scope.getId()));
    List<PolicyRepresentation> permissions = getClientResource().authorization().scopes().scope(scope.getId()).permissions();
    assertEquals(1, permissions.size());
    assertTrue(permissions.stream().map(PolicyRepresentation::getId).collect(Collectors.toList()).contains(policy.getId()));
}
Also used : PolicyRepresentation(org.keycloak.representations.idm.authorization.PolicyRepresentation) Function(java.util.function.Function) ScopeRepresentation(org.keycloak.representations.idm.authorization.ScopeRepresentation)

Example 12 with PolicyRepresentation

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

the class GenericPolicyManagementTest method assertAssociatedPolicy.

private void assertAssociatedPolicy(String associatedPolicyName, PolicyRepresentation dependentPolicy) {
    PolicyRepresentation associatedPolicy = findPolicyByName(associatedPolicyName);
    PoliciesResource policies = getClientResource().authorization().policies();
    associatedPolicy = policies.policy(associatedPolicy.getId()).toRepresentation();
    assertNotNull(associatedPolicy);
    PolicyRepresentation finalAssociatedPolicy = associatedPolicy;
    PolicyResource policyResource = policies.policy(dependentPolicy.getId());
    List<PolicyRepresentation> associatedPolicies = policyResource.associatedPolicies();
    assertTrue(associatedPolicies.stream().filter(associated -> associated.getId().equals(finalAssociatedPolicy.getId())).findFirst().isPresent());
    List<PolicyRepresentation> dependentPolicies = policies.policy(associatedPolicy.getId()).dependentPolicies();
    assertEquals(1, dependentPolicies.size());
    assertEquals(dependentPolicy.getId(), dependentPolicies.get(0).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) PoliciesResource(org.keycloak.admin.client.resource.PoliciesResource)

Example 13 with PolicyRepresentation

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

the class GenericPolicyManagementTest method assertAssociatedResource.

private void assertAssociatedResource(String resourceName, PolicyRepresentation policy) {
    ResourceRepresentation resource = findResourceByName(resourceName);
    assertNotNull(resource);
    List<ResourceRepresentation> resources = getClientResource().authorization().policies().policy(policy.getId()).resources();
    assertTrue(resources.contains(resource));
    List<PolicyRepresentation> policies = getClientResource().authorization().resources().resource(resource.getId()).permissions();
    assertEquals(1, policies.size());
    assertTrue(policies.stream().map(PolicyRepresentation::getId).collect(Collectors.toList()).contains(policy.getId()));
}
Also used : PolicyRepresentation(org.keycloak.representations.idm.authorization.PolicyRepresentation) ResourceRepresentation(org.keycloak.representations.idm.authorization.ResourceRepresentation)

Example 14 with PolicyRepresentation

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

the class GenericPolicyManagementTest method createPolicy.

private PolicyResource createPolicy(String name, Map<String, String> config) {
    PolicyRepresentation newPolicy = new PolicyRepresentation();
    newPolicy.setName(name);
    newPolicy.setType("scope");
    newPolicy.setConfig(config);
    PoliciesResource policies = getClientResource().authorization().policies();
    try (Response response = policies.create(newPolicy)) {
        assertEquals(Response.Status.CREATED.getStatusCode(), response.getStatus());
        PolicyRepresentation stored = response.readEntity(PolicyRepresentation.class);
        return policies.policy(stored.getId());
    }
}
Also used : PolicyRepresentation(org.keycloak.representations.idm.authorization.PolicyRepresentation) Response(javax.ws.rs.core.Response) PoliciesResource(org.keycloak.admin.client.resource.PoliciesResource)

Example 15 with PolicyRepresentation

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

the class AbstractBaseServletAuthzAdapterTest method updatePermissionPolicies.

protected void updatePermissionPolicies(String permissionName, String... policyNames) {
    for (PolicyRepresentation policy : getAuthorizationResource().policies().policies()) {
        if (permissionName.equalsIgnoreCase(policy.getName())) {
            StringBuilder policies = new StringBuilder("[");
            for (String policyName : policyNames) {
                if (policies.length() > 1) {
                    policies.append(",");
                }
                policies.append("\"").append(policyName).append("\"");
            }
            policies.append("]");
            policy.getConfig().put("applyPolicies", policies.toString());
            getAuthorizationResource().policies().policy(policy.getId()).update(policy);
        }
    }
}
Also used : PolicyRepresentation(org.keycloak.representations.idm.authorization.PolicyRepresentation) UserPolicyRepresentation(org.keycloak.representations.idm.authorization.UserPolicyRepresentation)

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