Search in sources :

Example 1 with Permission

use of org.keycloak.services.resources.account.resources.AbstractResourceService.Permission in project keycloak by keycloak.

the class ResourcesRestServiceTest method testGetPermissions.

@Test
public void testGetPermissions() throws Exception {
    Resource resource = getMyResources().get(0);
    List<Permission> shares = doGet("/" + resource.getId() + "/permissions", new TypeReference<List<Permission>>() {
    });
    assertEquals(1, shares.size());
    Permission firstShare = shares.get(0);
    List<Permission> permissions = new ArrayList<>();
    assertTrue(userNames.contains(firstShare.getUsername()));
    assertEquals(2, firstShare.getScopes().size());
    List<String> users = new ArrayList<>(userNames);
    users.remove(firstShare.getUsername());
    for (String userName : users) {
        Permission permission = new Permission();
        permission.setUsername(userName);
        permission.addScope("Scope D");
        permissions.add(permission);
    }
    SimpleHttp.doPut(getAccountUrl("resources/" + resource.getId() + "/permissions"), httpClient).auth(tokenUtil.getToken()).json(permissions).asResponse();
    shares = doGet("/" + resource.getId() + "/permissions", new TypeReference<List<Permission>>() {
    });
    assertEquals(3, shares.size());
    for (Permission user : shares) {
        assertTrue(userNames.contains(user.getUsername()));
        if (firstShare.getUsername().equals(user.getUsername())) {
            assertEquals(2, user.getScopes().size());
        } else {
            assertEquals(1, user.getScopes().size());
        }
    }
}
Also used : Resource(org.keycloak.services.resources.account.resources.AbstractResourceService.Resource) AuthorizationResource(org.keycloak.admin.client.resource.AuthorizationResource) ClientsResource(org.keycloak.admin.client.resource.ClientsResource) ClientResource(org.keycloak.admin.client.resource.ClientResource) Permission(org.keycloak.services.resources.account.resources.AbstractResourceService.Permission) ArrayList(java.util.ArrayList) ArrayList(java.util.ArrayList) LinkedList(java.util.LinkedList) List(java.util.List) TypeReference(com.fasterxml.jackson.core.type.TypeReference) Test(org.junit.Test)

Example 2 with Permission

use of org.keycloak.services.resources.account.resources.AbstractResourceService.Permission in project keycloak by keycloak.

the class ResourcesRestServiceTest method testGetPermissionRequests.

@Test
public void testGetPermissionRequests() {
    Resource resource = getMyResources().get(0);
    List<Permission> requests = doGet("/" + resource.getId() + "/permissions/requests", new TypeReference<List<Permission>>() {
    });
    assertTrue(requests.isEmpty());
    for (String userName : userNames) {
        List<String> scopes = new ArrayList<>();
        if ("bob".equals(userName)) {
            scopes.add("Scope D");
        } else if ("alice".equals(userName)) {
            scopes.add("Scope C");
        } else if ("jdoe".equals(userName)) {
            scopes.add("Scope C");
            scopes.add("Scope D");
        }
        for (String scope : scopes) {
            PermissionTicketRepresentation ticket = new PermissionTicketRepresentation();
            ticket.setGranted(false);
            ticket.setOwner("test-user@localhost");
            ticket.setRequesterName(userName);
            ticket.setResource(resource.getId());
            ticket.setScopeName(scope);
            authzClient.protection("test-user@localhost", "password").permission().create(ticket);
        }
    }
    requests = doGet("/" + resource.getId() + "/permissions/requests", new TypeReference<List<Permission>>() {
    });
    assertEquals(3, requests.size());
    Iterator<Permission> iterator = requests.iterator();
    while (iterator.hasNext()) {
        Permission permission = iterator.next();
        String username = permission.getUsername();
        List<String> scopes = permission.getScopes();
        if ("bob".equals(username)) {
            assertEquals(1, scopes.size());
            assertTrue(scopes.contains("Scope D"));
            iterator.remove();
        } else if ("alice".equals(username)) {
            assertEquals(1, scopes.size());
            assertTrue(scopes.contains("Scope C"));
            iterator.remove();
        } else if ("jdoe".equals(username)) {
            assertEquals(2, scopes.size());
            assertTrue(scopes.contains("Scope C"));
            assertTrue(scopes.contains("Scope D"));
            iterator.remove();
        }
    }
    assertTrue(requests.isEmpty());
}
Also used : Resource(org.keycloak.services.resources.account.resources.AbstractResourceService.Resource) AuthorizationResource(org.keycloak.admin.client.resource.AuthorizationResource) ClientsResource(org.keycloak.admin.client.resource.ClientsResource) ClientResource(org.keycloak.admin.client.resource.ClientResource) Permission(org.keycloak.services.resources.account.resources.AbstractResourceService.Permission) ArrayList(java.util.ArrayList) PermissionTicketRepresentation(org.keycloak.representations.idm.authorization.PermissionTicketRepresentation) ArrayList(java.util.ArrayList) LinkedList(java.util.LinkedList) List(java.util.List) TypeReference(com.fasterxml.jackson.core.type.TypeReference) Test(org.junit.Test)

Example 3 with Permission

use of org.keycloak.services.resources.account.resources.AbstractResourceService.Permission in project keycloak by keycloak.

the class ResourcesRestServiceTest method testShareResource.

@Test
public void testShareResource() throws Exception {
    List<String> users = new LinkedList<>(Arrays.asList("jdoe", "alice"));
    List<Permission> permissions = new ArrayList<>();
    AbstractResourceService.ResourcePermission sharedResource = null;
    for (String user : users) {
        sharedResource = getSharedWithMe(user).get(0);
        assertNotNull(sharedResource);
        assertEquals(2, sharedResource.getScopes().size());
    }
    permissions.add(new Permission(users.get(0), "Scope C", "Scope D"));
    permissions.add(new Permission(users.get(users.size() - 1), "Scope A", "Scope B", "Scope C", "Scope D"));
    String resourceId = sharedResource.getId();
    SimpleHttp.Response response = SimpleHttp.doPut(getAccountUrl("resources/" + resourceId + "/permissions"), httpClient).auth(tokenUtil.getToken()).json(permissions).asResponse();
    assertEquals(Response.Status.NO_CONTENT.getStatusCode(), response.getStatus());
    for (String user : users) {
        sharedResource = getSharedWithMe(user).stream().filter(resource1 -> resource1.getId().equals(resourceId)).findAny().orElse(null);
        assertNotNull(sharedResource);
        if (user.equals(users.get(users.size() - 1))) {
            assertEquals(4, sharedResource.getScopes().size());
        } else {
            assertEquals(2, sharedResource.getScopes().size());
        }
    }
}
Also used : ResourceRepresentation(org.keycloak.representations.idm.authorization.ResourceRepresentation) Arrays(java.util.Arrays) BeforeClass(org.junit.BeforeClass) Profile(org.keycloak.common.Profile) Resource(org.keycloak.services.resources.account.resources.AbstractResourceService.Resource) AuthorizationResource(org.keycloak.admin.client.resource.AuthorizationResource) HashMap(java.util.HashMap) AuthzClient(org.keycloak.authorization.client.AuthzClient) ArrayList(java.util.ArrayList) Assert.assertThat(org.junit.Assert.assertThat) Configuration(org.keycloak.authorization.client.Configuration) SimpleHttp(org.keycloak.broker.provider.util.SimpleHttp) ClientsResource(org.keycloak.admin.client.resource.ClientsResource) AccessToken(org.keycloak.representations.AccessToken) UserBuilder(org.keycloak.testsuite.util.UserBuilder) Map(java.util.Map) Assert.fail(org.junit.Assert.fail) ScopeRepresentation(org.keycloak.representations.idm.authorization.ScopeRepresentation) TypeReference(com.fasterxml.jackson.core.type.TypeReference) LinkedList(java.util.LinkedList) ClientResource(org.keycloak.admin.client.resource.ClientResource) ProfileAssume(org.keycloak.testsuite.ProfileAssume) AbstractResourceService(org.keycloak.services.resources.account.resources.AbstractResourceService) TokenUtil(org.keycloak.testsuite.util.TokenUtil) OAuth2ErrorRepresentation(org.keycloak.representations.idm.OAuth2ErrorRepresentation) JWSInput(org.keycloak.jose.jws.JWSInput) UserRepresentation(org.keycloak.representations.idm.UserRepresentation) Iterator(java.util.Iterator) Assert.assertNotNull(org.junit.Assert.assertNotNull) KeycloakUriBuilder(org.keycloak.common.util.KeycloakUriBuilder) Set(java.util.Set) Assert.assertTrue(org.junit.Assert.assertTrue) Test(org.junit.Test) IOException(java.io.IOException) RealmRepresentation(org.keycloak.representations.idm.RealmRepresentation) ClientRepresentation(org.keycloak.representations.idm.ClientRepresentation) PermissionTicketRepresentation(org.keycloak.representations.idm.authorization.PermissionTicketRepresentation) JsonSerialization(org.keycloak.util.JsonSerialization) Consumer(java.util.function.Consumer) List(java.util.List) AccountRoles(org.keycloak.models.AccountRoles) Assert.assertNull(org.junit.Assert.assertNull) Response(javax.ws.rs.core.Response) Permission(org.keycloak.services.resources.account.resources.AbstractResourceService.Permission) ClientBuilder(org.keycloak.testsuite.util.ClientBuilder) Collections(java.util.Collections) Assert.assertEquals(org.junit.Assert.assertEquals) SimpleHttp(org.keycloak.broker.provider.util.SimpleHttp) AbstractResourceService(org.keycloak.services.resources.account.resources.AbstractResourceService) Permission(org.keycloak.services.resources.account.resources.AbstractResourceService.Permission) ArrayList(java.util.ArrayList) LinkedList(java.util.LinkedList) Test(org.junit.Test)

Example 4 with Permission

use of org.keycloak.services.resources.account.resources.AbstractResourceService.Permission in project keycloak by keycloak.

the class ResourcesRestServiceTest method assertSharedWithOthersResponse.

private void assertSharedWithOthersResponse(List<AbstractResourceService.ResourcePermission> resources) {
    for (AbstractResourceService.ResourcePermission resource : resources) {
        String uri = resource.getUri();
        int id = Integer.parseInt(uri.substring(uri.lastIndexOf('/') + 1));
        assertNotNull(resource.getId());
        assertEquals("Resource " + id, resource.getName());
        assertEquals("Display Name " + id, resource.getDisplayName());
        assertEquals("Icon Uri " + id, resource.getIconUri());
        assertEquals("my-resource-server", resource.getClient().getClientId());
        assertEquals("My Resource Server", resource.getClient().getName());
        assertEquals("http://resourceserver.com", resource.getClient().getBaseUrl());
        assertEquals(1, resource.getPermissions().size());
        Permission user = resource.getPermissions().iterator().next();
        assertTrue(userNames.contains(user.getUsername()));
        assertEquals(2, user.getScopes().size());
    }
}
Also used : AbstractResourceService(org.keycloak.services.resources.account.resources.AbstractResourceService) Permission(org.keycloak.services.resources.account.resources.AbstractResourceService.Permission)

Example 5 with Permission

use of org.keycloak.services.resources.account.resources.AbstractResourceService.Permission in project keycloak by keycloak.

the class ResourcesRestServiceTest method testRevokePermission.

@Test
public void testRevokePermission() throws Exception {
    List<String> users = Arrays.asList("jdoe", "alice");
    List<Permission> permissions = new ArrayList<>();
    AbstractResourceService.ResourcePermission sharedResource = null;
    for (String user : users) {
        sharedResource = getSharedWithMe(user).get(0);
        assertNotNull(sharedResource);
        assertEquals(2, sharedResource.getScopes().size());
    }
    permissions.add(new Permission(users.get(0), "Scope C"));
    permissions.add(new Permission(users.get(users.size() - 1), "Scope B", "Scope D"));
    String resourceId = sharedResource.getId();
    SimpleHttp.Response response = SimpleHttp.doPut(getAccountUrl("resources/" + resourceId + "/permissions"), httpClient).auth(tokenUtil.getToken()).json(permissions).asResponse();
    assertEquals(Response.Status.NO_CONTENT.getStatusCode(), response.getStatus());
    for (String user : users) {
        sharedResource = getSharedWithMe(user).stream().filter(resource1 -> resource1.getId().equals(resourceId)).findAny().orElse(null);
        assertNotNull(sharedResource);
        if (user.equals(users.get(users.size() - 1))) {
            assertEquals(2, sharedResource.getScopes().size());
        } else {
            assertEquals(1, sharedResource.getScopes().size());
        }
    }
}
Also used : ResourceRepresentation(org.keycloak.representations.idm.authorization.ResourceRepresentation) Arrays(java.util.Arrays) BeforeClass(org.junit.BeforeClass) Profile(org.keycloak.common.Profile) Resource(org.keycloak.services.resources.account.resources.AbstractResourceService.Resource) AuthorizationResource(org.keycloak.admin.client.resource.AuthorizationResource) HashMap(java.util.HashMap) AuthzClient(org.keycloak.authorization.client.AuthzClient) ArrayList(java.util.ArrayList) Assert.assertThat(org.junit.Assert.assertThat) Configuration(org.keycloak.authorization.client.Configuration) SimpleHttp(org.keycloak.broker.provider.util.SimpleHttp) ClientsResource(org.keycloak.admin.client.resource.ClientsResource) AccessToken(org.keycloak.representations.AccessToken) UserBuilder(org.keycloak.testsuite.util.UserBuilder) Map(java.util.Map) Assert.fail(org.junit.Assert.fail) ScopeRepresentation(org.keycloak.representations.idm.authorization.ScopeRepresentation) TypeReference(com.fasterxml.jackson.core.type.TypeReference) LinkedList(java.util.LinkedList) ClientResource(org.keycloak.admin.client.resource.ClientResource) ProfileAssume(org.keycloak.testsuite.ProfileAssume) AbstractResourceService(org.keycloak.services.resources.account.resources.AbstractResourceService) TokenUtil(org.keycloak.testsuite.util.TokenUtil) OAuth2ErrorRepresentation(org.keycloak.representations.idm.OAuth2ErrorRepresentation) JWSInput(org.keycloak.jose.jws.JWSInput) UserRepresentation(org.keycloak.representations.idm.UserRepresentation) Iterator(java.util.Iterator) Assert.assertNotNull(org.junit.Assert.assertNotNull) KeycloakUriBuilder(org.keycloak.common.util.KeycloakUriBuilder) Set(java.util.Set) Assert.assertTrue(org.junit.Assert.assertTrue) Test(org.junit.Test) IOException(java.io.IOException) RealmRepresentation(org.keycloak.representations.idm.RealmRepresentation) ClientRepresentation(org.keycloak.representations.idm.ClientRepresentation) PermissionTicketRepresentation(org.keycloak.representations.idm.authorization.PermissionTicketRepresentation) JsonSerialization(org.keycloak.util.JsonSerialization) Consumer(java.util.function.Consumer) List(java.util.List) AccountRoles(org.keycloak.models.AccountRoles) Assert.assertNull(org.junit.Assert.assertNull) Response(javax.ws.rs.core.Response) Permission(org.keycloak.services.resources.account.resources.AbstractResourceService.Permission) ClientBuilder(org.keycloak.testsuite.util.ClientBuilder) Collections(java.util.Collections) Assert.assertEquals(org.junit.Assert.assertEquals) SimpleHttp(org.keycloak.broker.provider.util.SimpleHttp) AbstractResourceService(org.keycloak.services.resources.account.resources.AbstractResourceService) Permission(org.keycloak.services.resources.account.resources.AbstractResourceService.Permission) ArrayList(java.util.ArrayList) Test(org.junit.Test)

Aggregations

Permission (org.keycloak.services.resources.account.resources.AbstractResourceService.Permission)6 TypeReference (com.fasterxml.jackson.core.type.TypeReference)5 ArrayList (java.util.ArrayList)5 LinkedList (java.util.LinkedList)5 List (java.util.List)5 Test (org.junit.Test)5 AuthorizationResource (org.keycloak.admin.client.resource.AuthorizationResource)5 ClientResource (org.keycloak.admin.client.resource.ClientResource)5 ClientsResource (org.keycloak.admin.client.resource.ClientsResource)5 Resource (org.keycloak.services.resources.account.resources.AbstractResourceService.Resource)5 PermissionTicketRepresentation (org.keycloak.representations.idm.authorization.PermissionTicketRepresentation)4 AbstractResourceService (org.keycloak.services.resources.account.resources.AbstractResourceService)4 IOException (java.io.IOException)3 Arrays (java.util.Arrays)3 Collections (java.util.Collections)3 HashMap (java.util.HashMap)3 Iterator (java.util.Iterator)3 Map (java.util.Map)3 Set (java.util.Set)3 Consumer (java.util.function.Consumer)3