Search in sources :

Example 66 with Resource

use of org.keycloak.authorization.model.Resource in project keycloak by keycloak.

the class UserManagedPermissionServiceTest method testRemovePoliciesOnResourceDelete.

private static void testRemovePoliciesOnResourceDelete(KeycloakSession session) {
    RealmModel realm = session.realms().getRealmByName("authz-test");
    ClientModel client = realm.getClientByClientId("resource-server-test");
    AuthorizationProvider provider = session.getProvider(AuthorizationProvider.class);
    UserModel user = session.users().getUserByUsername(realm, "marta");
    Map<Policy.FilterOption, String[]> filters = new HashMap<>();
    filters.put(Policy.FilterOption.TYPE, new String[] { "uma" });
    filters.put(OWNER, new String[] { user.getId() });
    List<Policy> policies = provider.getStoreFactory().getPolicyStore().findByResourceServer(filters, client.getId(), -1, -1);
    assertEquals(1, policies.size());
    Policy policy = policies.get(0);
    assertFalse(policy.getResources().isEmpty());
    Resource resource = policy.getResources().iterator().next();
    assertEquals("Resource A", resource.getName());
    provider.getStoreFactory().getResourceStore().delete(resource.getId());
    filters = new HashMap<>();
    filters.put(OWNER, new String[] { user.getId() });
    policies = provider.getStoreFactory().getPolicyStore().findByResourceServer(filters, client.getId(), -1, -1);
    assertTrue(policies.isEmpty());
}
Also used : RealmModel(org.keycloak.models.RealmModel) UserModel(org.keycloak.models.UserModel) Policy(org.keycloak.authorization.model.Policy) ClientModel(org.keycloak.models.ClientModel) HashMap(java.util.HashMap) AuthorizationProvider(org.keycloak.authorization.AuthorizationProvider) AuthorizationResource(org.keycloak.authorization.client.resource.AuthorizationResource) ProtectionResource(org.keycloak.authorization.client.resource.ProtectionResource) PolicyResource(org.keycloak.authorization.client.resource.PolicyResource) Resource(org.keycloak.authorization.model.Resource)

Example 67 with Resource

use of org.keycloak.authorization.model.Resource in project keycloak by keycloak.

the class UserManagedPermissionServiceTest method testRemovePoliciesOnClientDelete.

private static void testRemovePoliciesOnClientDelete(KeycloakSession session) {
    RealmModel realm = session.realms().getRealmByName("authz-test");
    ClientModel client = realm.getClientByClientId("resource-server-test");
    AuthorizationProvider provider = session.getProvider(AuthorizationProvider.class);
    UserModel user = session.users().getUserByUsername(realm, "marta");
    Map<Policy.FilterOption, String[]> filters = new HashMap<>();
    filters.put(Policy.FilterOption.TYPE, new String[] { "uma" });
    filters.put(OWNER, new String[] { user.getId() });
    List<Policy> policies = provider.getStoreFactory().getPolicyStore().findByResourceServer(filters, client.getId(), -1, -1);
    assertEquals(1, policies.size());
    Policy policy = policies.get(0);
    assertFalse(policy.getResources().isEmpty());
    Resource resource = policy.getResources().iterator().next();
    assertEquals("Resource A", resource.getName());
    realm.removeClient(realm.getClientByClientId("client-remove").getId());
    filters = new HashMap<>();
    filters.put(OWNER, new String[] { user.getId() });
    policies = provider.getStoreFactory().getPolicyStore().findByResourceServer(filters, client.getId(), -1, -1);
    assertTrue(policies.isEmpty());
}
Also used : RealmModel(org.keycloak.models.RealmModel) UserModel(org.keycloak.models.UserModel) Policy(org.keycloak.authorization.model.Policy) ClientModel(org.keycloak.models.ClientModel) HashMap(java.util.HashMap) AuthorizationProvider(org.keycloak.authorization.AuthorizationProvider) AuthorizationResource(org.keycloak.authorization.client.resource.AuthorizationResource) ProtectionResource(org.keycloak.authorization.client.resource.ProtectionResource) PolicyResource(org.keycloak.authorization.client.resource.PolicyResource) Resource(org.keycloak.authorization.model.Resource)

Example 68 with Resource

use of org.keycloak.authorization.model.Resource in project keycloak by keycloak.

the class UserManagedPermissionServiceTest method testRemovePoliciesOnGroupDelete.

private static void testRemovePoliciesOnGroupDelete(KeycloakSession session) {
    RealmModel realm = session.realms().getRealmByName("authz-test");
    ClientModel client = realm.getClientByClientId("resource-server-test");
    AuthorizationProvider provider = session.getProvider(AuthorizationProvider.class);
    UserModel user = session.users().getUserByUsername(realm, "marta");
    Map<Policy.FilterOption, String[]> filters = new HashMap<>();
    filters.put(Policy.FilterOption.TYPE, new String[] { "uma" });
    filters.put(OWNER, new String[] { user.getId() });
    List<Policy> policies = provider.getStoreFactory().getPolicyStore().findByResourceServer(filters, client.getId(), -1, -1);
    assertEquals(1, policies.size());
    Policy policy = policies.get(0);
    assertFalse(policy.getResources().isEmpty());
    Resource resource = policy.getResources().iterator().next();
    assertEquals("Resource A", resource.getName());
    realm.removeGroup(realm.searchForGroupByNameStream("group_remove", -1, -1).findAny().get());
    filters = new HashMap<>();
    filters.put(OWNER, new String[] { user.getId() });
    policies = provider.getStoreFactory().getPolicyStore().findByResourceServer(filters, client.getId(), -1, -1);
    assertTrue(policies.isEmpty());
}
Also used : RealmModel(org.keycloak.models.RealmModel) UserModel(org.keycloak.models.UserModel) Policy(org.keycloak.authorization.model.Policy) ClientModel(org.keycloak.models.ClientModel) HashMap(java.util.HashMap) AuthorizationProvider(org.keycloak.authorization.AuthorizationProvider) AuthorizationResource(org.keycloak.authorization.client.resource.AuthorizationResource) ProtectionResource(org.keycloak.authorization.client.resource.ProtectionResource) PolicyResource(org.keycloak.authorization.client.resource.PolicyResource) Resource(org.keycloak.authorization.model.Resource)

Example 69 with Resource

use of org.keycloak.authorization.model.Resource in project keycloak by keycloak.

the class MapPermissionTicketStore method findGrantedResources.

@Override
public List<Resource> findGrantedResources(String requester, String name, int first, int max) {
    DefaultModelCriteria<PermissionTicket> mcb = criteria();
    mcb = mcb.compare(SearchableFields.REQUESTER, Operator.EQ, requester).compare(SearchableFields.GRANTED_TIMESTAMP, Operator.EXISTS);
    Function<MapPermissionTicketEntity, Resource> ticketResourceMapper;
    ResourceStore resourceStore = authorizationProvider.getStoreFactory().getResourceStore();
    if (name != null) {
        ticketResourceMapper = ticket -> {
            Map<Resource.FilterOption, String[]> filterOptionMap = new EnumMap<>(Resource.FilterOption.class);
            filterOptionMap.put(Resource.FilterOption.ID, new String[] { ticket.getResourceId() });
            filterOptionMap.put(Resource.FilterOption.NAME, new String[] { name });
            List<Resource> resource = resourceStore.findByResourceServer(filterOptionMap, ticket.getResourceServerId(), -1, 1);
            return resource.isEmpty() ? null : resource.get(0);
        };
    } else {
        ticketResourceMapper = ticket -> resourceStore.findById(ticket.getResourceId(), ticket.getResourceServerId());
    }
    return paginatedStream(tx.read(withCriteria(mcb).orderBy(SearchableFields.RESOURCE_ID, ASCENDING)).filter(distinctByKey(MapPermissionTicketEntity::getResourceId)).map(ticketResourceMapper).filter(Objects::nonNull), first, max).collect(Collectors.toList());
}
Also used : PermissionTicket(org.keycloak.authorization.model.PermissionTicket) Resource(org.keycloak.authorization.model.Resource) Objects(java.util.Objects) ResourceStore(org.keycloak.authorization.store.ResourceStore) MapPermissionTicketEntity(org.keycloak.models.map.authorization.entity.MapPermissionTicketEntity) EnumMap(java.util.EnumMap)

Example 70 with Resource

use of org.keycloak.authorization.model.Resource in project keycloak by keycloak.

the class GroupPermissions method resource.

@Override
public Resource resource(GroupModel group) {
    ResourceServer server = root.realmResourceServer();
    if (server == null)
        return null;
    Resource resource = resourceStore.findByName(getGroupResourceName(group), server.getId());
    if (resource == null)
        return null;
    return resource;
}
Also used : Resource(org.keycloak.authorization.model.Resource) ResourceServer(org.keycloak.authorization.model.ResourceServer)

Aggregations

Resource (org.keycloak.authorization.model.Resource)87 ResourceServer (org.keycloak.authorization.model.ResourceServer)51 Policy (org.keycloak.authorization.model.Policy)45 Scope (org.keycloak.authorization.model.Scope)44 AuthorizationProvider (org.keycloak.authorization.AuthorizationProvider)27 ResourceStore (org.keycloak.authorization.store.ResourceStore)27 StoreFactory (org.keycloak.authorization.store.StoreFactory)26 ArrayList (java.util.ArrayList)22 ClientModel (org.keycloak.models.ClientModel)22 List (java.util.List)20 HashSet (java.util.HashSet)19 Map (java.util.Map)19 UserModel (org.keycloak.models.UserModel)18 RealmModel (org.keycloak.models.RealmModel)16 HashMap (java.util.HashMap)15 Set (java.util.Set)15 EnumMap (java.util.EnumMap)14 Collectors (java.util.stream.Collectors)14 Path (javax.ws.rs.Path)13 PolicyStore (org.keycloak.authorization.store.PolicyStore)13