use of org.keycloak.authorization.store.StoreFactory in project keycloak by keycloak.
the class ScopeService method find.
@Path("/search")
@GET
@Produces(MediaType.APPLICATION_JSON)
@NoCache
public Response find(@QueryParam("name") String name) {
this.auth.realm().requireViewAuthorization();
StoreFactory storeFactory = authorization.getStoreFactory();
if (name == null) {
return Response.status(Status.BAD_REQUEST).build();
}
Scope model = storeFactory.getScopeStore().findByName(name, this.resourceServer.getId());
if (model == null) {
return Response.status(Status.NO_CONTENT).build();
}
return Response.ok(toRepresentation(model)).build();
}
use of org.keycloak.authorization.store.StoreFactory in project keycloak by keycloak.
the class ScopeService method getResources.
@Path("{id}/resources")
@GET
@NoCache
@Produces(MediaType.APPLICATION_JSON)
public Response getResources(@PathParam("id") String id) {
this.auth.realm().requireViewAuthorization();
StoreFactory storeFactory = this.authorization.getStoreFactory();
Scope model = storeFactory.getScopeStore().findById(id, resourceServer.getId());
if (model == null) {
return Response.status(Status.NOT_FOUND).build();
}
return Response.ok(storeFactory.getResourceStore().findByScope(Arrays.asList(model.getId()), resourceServer.getId()).stream().map(resource -> {
ResourceRepresentation representation = new ResourceRepresentation();
representation.setId(resource.getId());
representation.setName(resource.getName());
return representation;
}).collect(Collectors.toList())).build();
}
use of org.keycloak.authorization.store.StoreFactory in project keycloak by keycloak.
the class PolicyService method findAll.
@GET
@Produces(MediaType.APPLICATION_JSON)
@NoCache
public Response findAll(@QueryParam("policyId") String id, @QueryParam("name") String name, @QueryParam("type") String type, @QueryParam("resource") String resource, @QueryParam("scope") String scope, @QueryParam("permission") Boolean permission, @QueryParam("owner") String owner, @QueryParam("fields") String fields, @QueryParam("first") Integer firstResult, @QueryParam("max") Integer maxResult) {
if (auth != null) {
this.auth.realm().requireViewAuthorization();
}
Map<Policy.FilterOption, String[]> search = new EnumMap<>(Policy.FilterOption.class);
if (id != null && !"".equals(id.trim())) {
search.put(Policy.FilterOption.ID, new String[] { id });
}
if (name != null && !"".equals(name.trim())) {
search.put(Policy.FilterOption.NAME, new String[] { name });
}
if (type != null && !"".equals(type.trim())) {
search.put(Policy.FilterOption.TYPE, new String[] { type });
}
if (owner != null && !"".equals(owner.trim())) {
search.put(Policy.FilterOption.OWNER, new String[] { owner });
}
StoreFactory storeFactory = authorization.getStoreFactory();
if (resource != null && !"".equals(resource.trim())) {
ResourceStore resourceStore = storeFactory.getResourceStore();
Resource resourceModel = resourceStore.findById(resource, resourceServer.getId());
if (resourceModel == null) {
Map<Resource.FilterOption, String[]> resourceFilters = new EnumMap<>(Resource.FilterOption.class);
resourceFilters.put(Resource.FilterOption.NAME, new String[] { resource });
if (owner != null) {
resourceFilters.put(Resource.FilterOption.OWNER, new String[] { owner });
}
Set<String> resources = resourceStore.findByResourceServer(resourceFilters, resourceServer.getId(), -1, 1).stream().map(Resource::getId).collect(Collectors.toSet());
if (resources.isEmpty()) {
return Response.noContent().build();
}
search.put(Policy.FilterOption.RESOURCE_ID, resources.toArray(new String[resources.size()]));
} else {
search.put(Policy.FilterOption.RESOURCE_ID, new String[] { resourceModel.getId() });
}
}
if (scope != null && !"".equals(scope.trim())) {
ScopeStore scopeStore = storeFactory.getScopeStore();
Scope scopeModel = scopeStore.findById(scope, resourceServer.getId());
if (scopeModel == null) {
Map<Scope.FilterOption, String[]> scopeFilters = new EnumMap<>(Scope.FilterOption.class);
scopeFilters.put(Scope.FilterOption.NAME, new String[] { scope });
Set<String> scopes = scopeStore.findByResourceServer(scopeFilters, resourceServer.getId(), -1, 1).stream().map(Scope::getId).collect(Collectors.toSet());
if (scopes.isEmpty()) {
return Response.noContent().build();
}
search.put(Policy.FilterOption.SCOPE_ID, scopes.toArray(new String[scopes.size()]));
} else {
search.put(Policy.FilterOption.SCOPE_ID, new String[] { scopeModel.getId() });
}
}
if (permission != null) {
search.put(Policy.FilterOption.PERMISSION, new String[] { permission.toString() });
}
return Response.ok(doSearch(firstResult, maxResult, fields, search)).build();
}
use of org.keycloak.authorization.store.StoreFactory in project keycloak by keycloak.
the class ResourceSetService method find.
@Path("/search")
@GET
@NoCache
@Produces("application/json")
public Response find(@QueryParam("name") String name) {
this.auth.realm().requireViewAuthorization();
StoreFactory storeFactory = authorization.getStoreFactory();
if (name == null) {
return Response.status(Status.BAD_REQUEST).build();
}
Resource model = storeFactory.getResourceStore().findByName(name, this.resourceServer.getId());
if (model == null) {
return Response.status(Status.NO_CONTENT).build();
}
return Response.ok(toRepresentation(model, this.resourceServer.getId(), authorization)).build();
}
use of org.keycloak.authorization.store.StoreFactory in project keycloak by keycloak.
the class PolicyEvaluationTest method testCheckUserGroups.
public static void testCheckUserGroups(KeycloakSession session) {
session.getContext().setRealm(session.realms().getRealmByName("authz-test"));
AuthorizationProvider authorization = session.getProvider(AuthorizationProvider.class);
ClientModel clientModel = session.clients().getClientByClientId(session.getContext().getRealm(), "resource-server-test");
StoreFactory storeFactory = authorization.getStoreFactory();
ResourceServer resourceServer = storeFactory.getResourceServerStore().findByClient(clientModel);
JSPolicyRepresentation policyRepresentation = new JSPolicyRepresentation();
policyRepresentation.setName("testCheckUserGroups");
StringBuilder builder = new StringBuilder();
builder.append("var realm = $evaluation.getRealm();");
builder.append("var groups = realm.getUserGroups('jdoe');");
builder.append("if (groups.size() == 2 && groups.contains('/Group A/Group B') && groups.contains('/Group A/Group D')) { $evaluation.grant(); }");
policyRepresentation.setCode(builder.toString());
Policy policy = storeFactory.getPolicyStore().create(policyRepresentation, resourceServer);
PolicyProvider provider = authorization.getProvider(policy.getType());
DefaultEvaluation evaluation = createEvaluation(session, authorization, resourceServer, policy);
provider.evaluate(evaluation);
Assert.assertEquals(Effect.PERMIT, evaluation.getEffect());
}
Aggregations