Search in sources :

Example 1 with PathMatcher

use of org.keycloak.common.util.PathMatcher in project keycloak by keycloak.

the class ResourceSetService method find.

public Response find(@QueryParam("_id") String id, @QueryParam("name") String name, @QueryParam("uri") String uri, @QueryParam("owner") String owner, @QueryParam("type") String type, @QueryParam("scope") String scope, @QueryParam("matchingUri") Boolean matchingUri, @QueryParam("exactName") Boolean exactName, @QueryParam("deep") Boolean deep, @QueryParam("first") Integer firstResult, @QueryParam("max") Integer maxResult, BiFunction<Resource, Boolean, ?> toRepresentation) {
    requireView();
    StoreFactory storeFactory = authorization.getStoreFactory();
    if (deep == null) {
        deep = true;
    }
    Map<Resource.FilterOption, String[]> search = new EnumMap<>(Resource.FilterOption.class);
    if (id != null && !"".equals(id.trim())) {
        search.put(Resource.FilterOption.ID, new String[] { id });
    }
    if (name != null && !"".equals(name.trim())) {
        search.put(exactName != null && exactName ? Resource.FilterOption.EXACT_NAME : Resource.FilterOption.NAME, new String[] { name });
    }
    if (uri != null && !"".equals(uri.trim())) {
        search.put(Resource.FilterOption.URI, new String[] { uri });
    }
    if (owner != null && !"".equals(owner.trim())) {
        RealmModel realm = authorization.getKeycloakSession().getContext().getRealm();
        ClientModel clientModel = realm.getClientByClientId(owner);
        if (clientModel != null) {
            owner = clientModel.getId();
        } else {
            UserModel user = authorization.getKeycloakSession().users().getUserByUsername(realm, owner);
            if (user != null) {
                owner = user.getId();
            }
        }
        search.put(Resource.FilterOption.OWNER, new String[] { owner });
    }
    if (type != null && !"".equals(type.trim())) {
        search.put(Resource.FilterOption.TYPE, new String[] { type });
    }
    if (scope != null && !"".equals(scope.trim())) {
        Map<Scope.FilterOption, String[]> scopeFilter = new EnumMap<>(Scope.FilterOption.class);
        scopeFilter.put(Scope.FilterOption.NAME, new String[] { scope });
        List<Scope> scopes = authorization.getStoreFactory().getScopeStore().findByResourceServer(scopeFilter, resourceServer.getId(), -1, -1);
        if (scopes.isEmpty()) {
            return Response.ok(Collections.emptyList()).build();
        }
        search.put(Resource.FilterOption.SCOPE_ID, scopes.stream().map(Scope::getId).toArray(String[]::new));
    }
    List<Resource> resources = storeFactory.getResourceStore().findByResourceServer(search, this.resourceServer.getId(), firstResult != null ? firstResult : -1, maxResult != null ? maxResult : Constants.DEFAULT_MAX_RESULTS);
    if (matchingUri != null && matchingUri && resources.isEmpty()) {
        Map<Resource.FilterOption, String[]> attributes = new EnumMap<>(Resource.FilterOption.class);
        attributes.put(Resource.FilterOption.URI_NOT_NULL, new String[] { "true" });
        attributes.put(Resource.FilterOption.OWNER, new String[] { resourceServer.getId() });
        List<Resource> serverResources = storeFactory.getResourceStore().findByResourceServer(attributes, this.resourceServer.getId(), firstResult != null ? firstResult : -1, maxResult != null ? maxResult : -1);
        PathMatcher<Map.Entry<String, Resource>> pathMatcher = new PathMatcher<Map.Entry<String, Resource>>() {

            @Override
            protected String getPath(Map.Entry<String, Resource> entry) {
                return entry.getKey();
            }

            @Override
            protected Collection<Map.Entry<String, Resource>> getPaths() {
                Map<String, Resource> result = new HashMap<>();
                serverResources.forEach(resource -> resource.getUris().forEach(uri -> {
                    result.put(uri, resource);
                }));
                return result.entrySet();
            }
        };
        Map.Entry<String, Resource> matches = pathMatcher.matches(uri);
        if (matches != null) {
            resources = Collections.singletonList(matches.getValue());
        }
    }
    Boolean finalDeep = deep;
    return Response.ok(resources.stream().map(resource -> toRepresentation.apply(resource, finalDeep)).collect(Collectors.toList())).build();
}
Also used : ResourceRepresentation(org.keycloak.representations.idm.authorization.ResourceRepresentation) ResourceType(org.keycloak.events.admin.ResourceType) Produces(javax.ws.rs.Produces) BiFunction(java.util.function.BiFunction) Path(javax.ws.rs.Path) OAuthErrorException(org.keycloak.OAuthErrorException) QueryParam(javax.ws.rs.QueryParam) Consumes(javax.ws.rs.Consumes) ErrorResponseException(org.keycloak.services.ErrorResponseException) ModelToRepresentation.toRepresentation(org.keycloak.models.utils.ModelToRepresentation.toRepresentation) Map(java.util.Map) ResourceOwnerRepresentation(org.keycloak.representations.idm.authorization.ResourceOwnerRepresentation) AuthorizationProvider(org.keycloak.authorization.AuthorizationProvider) DELETE(javax.ws.rs.DELETE) RealmModel(org.keycloak.models.RealmModel) EnumMap(java.util.EnumMap) Collection(java.util.Collection) Set(java.util.Set) PolicyStore(org.keycloak.authorization.store.PolicyStore) ResourceStore(org.keycloak.authorization.store.ResourceStore) Collectors(java.util.stream.Collectors) List(java.util.List) Response(javax.ws.rs.core.Response) RepresentationToModel.toModel(org.keycloak.models.utils.RepresentationToModel.toModel) ClientModel(org.keycloak.models.ClientModel) OperationType(org.keycloak.events.admin.OperationType) PathParam(javax.ws.rs.PathParam) Scope(org.keycloak.authorization.model.Scope) GET(javax.ws.rs.GET) StoreFactory(org.keycloak.authorization.store.StoreFactory) Constants(org.keycloak.models.Constants) HashMap(java.util.HashMap) Function(java.util.function.Function) PolicyRepresentation(org.keycloak.representations.idm.authorization.PolicyRepresentation) ArrayList(java.util.ArrayList) HashSet(java.util.HashSet) UserModel(org.keycloak.models.UserModel) ScopeRepresentation(org.keycloak.representations.idm.authorization.ScopeRepresentation) Status(javax.ws.rs.core.Response.Status) PathMatcher(org.keycloak.common.util.PathMatcher) ResourceServer(org.keycloak.authorization.model.ResourceServer) POST(javax.ws.rs.POST) AdminPermissionEvaluator(org.keycloak.services.resources.admin.permissions.AdminPermissionEvaluator) KeycloakSession(org.keycloak.models.KeycloakSession) Policy(org.keycloak.authorization.model.Policy) NoCache(org.jboss.resteasy.annotations.cache.NoCache) PUT(javax.ws.rs.PUT) Collections(java.util.Collections) Resource(org.keycloak.authorization.model.Resource) AdminEventBuilder(org.keycloak.services.resources.admin.AdminEventBuilder) HashMap(java.util.HashMap) Resource(org.keycloak.authorization.model.Resource) StoreFactory(org.keycloak.authorization.store.StoreFactory) RealmModel(org.keycloak.models.RealmModel) UserModel(org.keycloak.models.UserModel) ClientModel(org.keycloak.models.ClientModel) PathMatcher(org.keycloak.common.util.PathMatcher) Scope(org.keycloak.authorization.model.Scope) EnumMap(java.util.EnumMap) Map(java.util.Map) EnumMap(java.util.EnumMap) HashMap(java.util.HashMap)

Aggregations

ArrayList (java.util.ArrayList)1 Collection (java.util.Collection)1 Collections (java.util.Collections)1 EnumMap (java.util.EnumMap)1 HashMap (java.util.HashMap)1 HashSet (java.util.HashSet)1 List (java.util.List)1 Map (java.util.Map)1 Set (java.util.Set)1 BiFunction (java.util.function.BiFunction)1 Function (java.util.function.Function)1 Collectors (java.util.stream.Collectors)1 Consumes (javax.ws.rs.Consumes)1 DELETE (javax.ws.rs.DELETE)1 GET (javax.ws.rs.GET)1 POST (javax.ws.rs.POST)1 PUT (javax.ws.rs.PUT)1 Path (javax.ws.rs.Path)1 PathParam (javax.ws.rs.PathParam)1 Produces (javax.ws.rs.Produces)1