Search in sources :

Example 11 with ResourceRepresentation

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

the class AbstractResourceServerTest method addResource.

protected ResourceRepresentation addResource(String resourceName, String owner, boolean ownerManagedAccess, String... scopeNames) throws Exception {
    ClientResource client = getClient(getRealm());
    AuthorizationResource authorization = client.authorization();
    ResourceRepresentation resource = new ResourceRepresentation(resourceName);
    if (owner != null) {
        resource.setOwner(new ResourceOwnerRepresentation(owner));
    }
    resource.setOwnerManagedAccess(ownerManagedAccess);
    resource.addScope(scopeNames);
    Response response = authorization.resources().create(resource);
    ResourceRepresentation temp = response.readEntity(ResourceRepresentation.class);
    resource.setId(temp.getId());
    response.close();
    return resource;
}
Also used : AuthorizationResponse(org.keycloak.representations.idm.authorization.AuthorizationResponse) Response(javax.ws.rs.core.Response) ClientResource(org.keycloak.admin.client.resource.ClientResource) ResourceOwnerRepresentation(org.keycloak.representations.idm.authorization.ResourceOwnerRepresentation) AuthorizationResource(org.keycloak.admin.client.resource.AuthorizationResource) ResourceRepresentation(org.keycloak.representations.idm.authorization.ResourceRepresentation)

Example 12 with ResourceRepresentation

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

the class ResourceManagementWithAuthzClientTest method testUpdateUri.

@Test
public void testUpdateUri() {
    doCreateResource(new ResourceRepresentation("/api/v1/*", Collections.emptySet(), "/api/v1/*", null));
    AuthzClient authzClient = getAuthzClient();
    List<ResourceRepresentation> resources = authzClient.protection().resource().findByMatchingUri("/api/v1/servers");
    assertNotNull(resources);
    assertEquals(1, resources.size());
    assertEquals("/api/v1/*", resources.get(0).getUri());
    resources.get(0).getUris().clear();
    resources.get(0).getUris().add("/api/v2/*");
    authzClient.protection().resource().update(resources.get(0));
    resources = authzClient.protection().resource().findByMatchingUri("/api/v1/servers");
    assertNotNull(resources);
    assertEquals(0, resources.size());
    resources = authzClient.protection().resource().findByMatchingUri("/api/v2");
    assertNotNull(resources);
    assertEquals(1, resources.size());
    assertEquals("/api/v2/*", resources.get(0).getUri());
}
Also used : AuthzClient(org.keycloak.authorization.client.AuthzClient) ResourceRepresentation(org.keycloak.representations.idm.authorization.ResourceRepresentation) Test(org.junit.Test)

Example 13 with ResourceRepresentation

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

the class ResourceManagementWithAuthzClientTest method toResourceRepresentation.

private ResourceRepresentation toResourceRepresentation(ResourceRepresentation newResource) {
    ResourceRepresentation resource = new ResourceRepresentation();
    resource.setId(newResource.getId());
    resource.setName(newResource.getName());
    resource.setIconUri(newResource.getIconUri());
    if (newResource.getUris() != null && !newResource.getUris().isEmpty()) {
        resource.setUris(newResource.getUris());
    } else {
        resource.setUri(newResource.getUri());
    }
    resource.setType(newResource.getType());
    if (newResource.getOwner() != null) {
        resource.setOwner(newResource.getOwner().getId());
    }
    resource.setScopes(newResource.getScopes().stream().map(scopeRepresentation -> {
        ScopeRepresentation scope = new ScopeRepresentation();
        scope.setName(scopeRepresentation.getName());
        scope.setIconUri(scopeRepresentation.getIconUri());
        return scope;
    }).collect(Collectors.toSet()));
    resource.setAttributes(newResource.getAttributes());
    return resource;
}
Also used : ScopeRepresentation(org.keycloak.representations.idm.authorization.ScopeRepresentation) ResourceRepresentation(org.keycloak.representations.idm.authorization.ResourceRepresentation)

Example 14 with ResourceRepresentation

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

the class ResourceManagementWithAuthzClientTest method testFindDeep.

@Test
public void testFindDeep() {
    ResourceRepresentation resource1 = new ResourceRepresentation("/*", new HashSet<>());
    resource1.addScope("a", "b", "c");
    resource1.setType("type");
    Map<String, List<String>> attributes = new HashMap<>();
    attributes.put("a", Arrays.asList("a"));
    attributes.put("b", Arrays.asList("b"));
    attributes.put("c", Arrays.asList("c"));
    resource1.setAttributes(attributes);
    resource1.setIconUri("icon");
    resource1.setUris(new HashSet<>(Arrays.asList("/a", "/b", "/c")));
    ResourceRepresentation resource = doCreateResource(resource1);
    AuthzClient authzClient = getAuthzClient();
    List<ResourceRepresentation> representations = authzClient.protection().resource().find(resource.getId(), null, null, null, null, null, false, true, null, null);
    assertEquals(1, representations.size());
    assertEquals(resource.getId(), representations.get(0).getId());
    assertEquals(resource.getName(), representations.get(0).getName());
    assertEquals(resource.getIconUri(), representations.get(0).getIconUri());
    assertThat(resource.getUris(), Matchers.containsInAnyOrder(representations.get(0).getUris().toArray()));
    assertThat(resource.getAttributes().entrySet(), Matchers.containsInAnyOrder(representations.get(0).getAttributes().entrySet().toArray()));
}
Also used : AuthzClient(org.keycloak.authorization.client.AuthzClient) HashMap(java.util.HashMap) List(java.util.List) ResourceRepresentation(org.keycloak.representations.idm.authorization.ResourceRepresentation) Test(org.junit.Test)

Example 15 with ResourceRepresentation

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

the class PolicyEnforcerTest method createResource.

private ResourceRepresentation createResource(ClientResource clientResource, String name, String uri, String... scopes) {
    ResourceRepresentation representation = new ResourceRepresentation();
    representation.setName(name);
    representation.setUri(uri);
    representation.setScopes(Arrays.asList(scopes).stream().map(ScopeRepresentation::new).collect(Collectors.toSet()));
    javax.ws.rs.core.Response response = clientResource.authorization().resources().create(representation);
    representation.setId(response.readEntity(ResourceRepresentation.class).getId());
    response.close();
    return representation;
}
Also used : ScopeRepresentation(org.keycloak.representations.idm.authorization.ScopeRepresentation) ResourceRepresentation(org.keycloak.representations.idm.authorization.ResourceRepresentation)

Aggregations

ResourceRepresentation (org.keycloak.representations.idm.authorization.ResourceRepresentation)154 Test (org.junit.Test)96 AuthorizationResource (org.keycloak.admin.client.resource.AuthorizationResource)49 AuthorizationRequest (org.keycloak.representations.idm.authorization.AuthorizationRequest)45 AuthzClient (org.keycloak.authorization.client.AuthzClient)44 AuthorizationResponse (org.keycloak.representations.idm.authorization.AuthorizationResponse)39 ClientResource (org.keycloak.admin.client.resource.ClientResource)38 Response (javax.ws.rs.core.Response)36 HttpResponseException (org.keycloak.authorization.client.util.HttpResponseException)35 PermissionResponse (org.keycloak.representations.idm.authorization.PermissionResponse)33 ResourcePermissionRepresentation (org.keycloak.representations.idm.authorization.ResourcePermissionRepresentation)33 Permission (org.keycloak.representations.idm.authorization.Permission)28 ScopeRepresentation (org.keycloak.representations.idm.authorization.ScopeRepresentation)26 JSPolicyRepresentation (org.keycloak.representations.idm.authorization.JSPolicyRepresentation)23 OAuthClient (org.keycloak.testsuite.util.OAuthClient)23 PermissionRequest (org.keycloak.representations.idm.authorization.PermissionRequest)22 AccessToken (org.keycloak.representations.AccessToken)19 ArrayList (java.util.ArrayList)18 List (java.util.List)18 TokenIntrospectionResponse (org.keycloak.authorization.client.representation.TokenIntrospectionResponse)18