Search in sources :

Example 26 with ScopeRepresentation

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

the class ResourceManagementTest method createResourceWithDefaultScopes.

private ResourceRepresentation createResourceWithDefaultScopes() {
    ResourceRepresentation resource = createResource();
    assertEquals(0, resource.getScopes().size());
    HashSet<ScopeRepresentation> scopes = new HashSet<>();
    scopes.add(createScope("Scope A", "").toRepresentation());
    scopes.add(createScope("Scope B", "").toRepresentation());
    scopes.add(createScope("Scope C", "").toRepresentation());
    resource.setScopes(scopes);
    return doUpdateResource(resource);
}
Also used : ScopeRepresentation(org.keycloak.representations.idm.authorization.ScopeRepresentation) ResourceRepresentation(org.keycloak.representations.idm.authorization.ResourceRepresentation) HashSet(java.util.HashSet)

Example 27 with ScopeRepresentation

use of org.keycloak.representations.idm.authorization.ScopeRepresentation 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 28 with ScopeRepresentation

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

the class GenericPolicyManagementTest method createScope.

private ResourceScopeResource createScope(String name) {
    ScopeRepresentation newScope = new ScopeRepresentation();
    newScope.setName(name);
    ResourceScopesResource scopes = getClientResource().authorization().scopes();
    try (Response response = scopes.create(newScope)) {
        assertEquals(Response.Status.CREATED.getStatusCode(), response.getStatus());
        ScopeRepresentation stored = response.readEntity(ScopeRepresentation.class);
        return scopes.scope(stored.getId());
    }
}
Also used : Response(javax.ws.rs.core.Response) ResourceScopesResource(org.keycloak.admin.client.resource.ResourceScopesResource) ScopeRepresentation(org.keycloak.representations.idm.authorization.ScopeRepresentation)

Example 29 with ScopeRepresentation

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

the class AbstractAuthorizationTest method createScope.

protected ResourceScopeResource createScope(String name, String iconUri) {
    ScopeRepresentation newScope = new ScopeRepresentation();
    newScope.setName(name);
    newScope.setIconUri(iconUri);
    ResourceScopesResource resources = getClientResource().authorization().scopes();
    try (Response response = resources.create(newScope)) {
        assertEquals(Response.Status.CREATED.getStatusCode(), response.getStatus());
        ScopeRepresentation stored = response.readEntity(ScopeRepresentation.class);
        return resources.scope(stored.getId());
    }
}
Also used : Response(javax.ws.rs.core.Response) ResourceScopesResource(org.keycloak.admin.client.resource.ResourceScopesResource) ScopeRepresentation(org.keycloak.representations.idm.authorization.ScopeRepresentation)

Example 30 with ScopeRepresentation

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

the class ScopesTable method toRepresentation.

public ScopeRepresentation toRepresentation(WebElement row) {
    ScopeRepresentation representation = null;
    List<WebElement> tds = row.findElements(tagName("td"));
    if (!(tds.isEmpty() || getTextFromElement(tds.get(1)).isEmpty())) {
        representation = new ScopeRepresentation();
        representation.setName(getTextFromElement(tds.get(1)));
    }
    return representation;
}
Also used : ScopeRepresentation(org.keycloak.representations.idm.authorization.ScopeRepresentation) WebElement(org.openqa.selenium.WebElement)

Aggregations

ScopeRepresentation (org.keycloak.representations.idm.authorization.ScopeRepresentation)48 ResourceRepresentation (org.keycloak.representations.idm.authorization.ResourceRepresentation)27 Test (org.junit.Test)18 ArrayList (java.util.ArrayList)14 List (java.util.List)12 Response (javax.ws.rs.core.Response)11 HashSet (java.util.HashSet)10 AuthorizationResource (org.keycloak.admin.client.resource.AuthorizationResource)10 HashMap (java.util.HashMap)8 Map (java.util.Map)8 Set (java.util.Set)8 AuthzClient (org.keycloak.authorization.client.AuthzClient)8 Arrays (java.util.Arrays)7 AuthorizationRequest (org.keycloak.representations.idm.authorization.AuthorizationRequest)7 PolicyRepresentation (org.keycloak.representations.idm.authorization.PolicyRepresentation)7 Collection (java.util.Collection)6 Collectors (java.util.stream.Collectors)6 ResourceScopesResource (org.keycloak.admin.client.resource.ResourceScopesResource)5 HttpResponseException (org.keycloak.authorization.client.util.HttpResponseException)5 WebElement (org.openqa.selenium.WebElement)5