Skip to content

Commit

Permalink
KEYCLOAK-7967 Remove injection of UriInfo
Browse files Browse the repository at this point in the history
  • Loading branch information
stianst authored and mposolda committed Aug 1, 2018
1 parent a2afe7c commit ae47b7f
Show file tree
Hide file tree
Showing 61 changed files with 453 additions and 627 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@
import org.keycloak.authorization.store.StoreFactory;
import org.keycloak.events.admin.OperationType;
import org.keycloak.events.admin.ResourceType;
import org.keycloak.models.KeycloakSession;
import org.keycloak.models.utils.ModelToRepresentation;
import org.keycloak.models.utils.RepresentationToModel;
import org.keycloak.representations.idm.authorization.AbstractPolicyRepresentation;
Expand All @@ -55,6 +56,8 @@
*/
public class PolicyResourceService {

@Context
private KeycloakSession session;
private final Policy policy;
protected final ResourceServer resourceServer;
protected final AuthorizationProvider authorization;
Expand All @@ -73,7 +76,7 @@ public PolicyResourceService(Policy policy, ResourceServer resourceServer, Autho
@Consumes("application/json")
@Produces("application/json")
@NoCache
public Response update(@Context UriInfo uriInfo, String payload) {
public Response update(String payload) {
if (auth != null) {
this.auth.realm().requireManageAuthorization();
}
Expand All @@ -89,13 +92,13 @@ public Response update(@Context UriInfo uriInfo, String payload) {
RepresentationToModel.toModel(representation, authorization, policy);


audit(uriInfo, representation, OperationType.UPDATE);
audit(representation, OperationType.UPDATE);

return Response.status(Status.CREATED).build();
}

@DELETE
public Response delete(@Context UriInfo uriInfo) {
public Response delete() {
if (auth != null) {
this.auth.realm().requireManageAuthorization();
}
Expand All @@ -113,7 +116,7 @@ public Response delete(@Context UriInfo uriInfo) {
policyStore.delete(policy.getId());

if (authorization.getRealm().isAdminEventsEnabled()) {
audit(uriInfo, toRepresentation(policy, authorization), OperationType.DELETE);
audit(toRepresentation(policy, authorization), OperationType.DELETE);
}

return Response.noContent().build();
Expand Down Expand Up @@ -255,9 +258,9 @@ protected Policy getPolicy() {
return policy;
}

private void audit(@Context UriInfo uriInfo, AbstractPolicyRepresentation policy, OperationType operation) {
private void audit(AbstractPolicyRepresentation policy, OperationType operation) {
if (authorization.getRealm().isAdminEventsEnabled()) {
adminEvent.operation(operation).resourcePath(uriInfo).representation(policy).success();
adminEvent.operation(operation).resourcePath(session.getContext().getUri()).representation(policy).success();
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@
import org.keycloak.events.admin.OperationType;
import org.keycloak.events.admin.ResourceType;
import org.keycloak.models.Constants;
import org.keycloak.models.KeycloakSession;
import org.keycloak.models.utils.ModelToRepresentation;
import org.keycloak.representations.idm.authorization.AbstractPolicyRepresentation;
import org.keycloak.representations.idm.authorization.PolicyProviderRepresentation;
Expand All @@ -71,6 +72,8 @@
*/
public class PolicyService {

@Context
private KeycloakSession session;
protected final ResourceServer resourceServer;
protected final AuthorizationProvider authorization;
protected final AdminPermissionEvaluator auth;
Expand Down Expand Up @@ -108,7 +111,7 @@ protected Object doCreatePolicyResource(Policy policy) {
@Consumes(MediaType.APPLICATION_JSON)
@Produces(MediaType.APPLICATION_JSON)
@NoCache
public Response create(@Context UriInfo uriInfo, String payload) {
public Response create(String payload) {
if (auth != null) {
this.auth.realm().requireManageAuthorization();
}
Expand All @@ -118,7 +121,7 @@ public Response create(@Context UriInfo uriInfo, String payload) {

representation.setId(policy.getId());

audit(uriInfo, representation, representation.getId(), OperationType.CREATE);
audit(representation, representation.getId(), OperationType.CREATE);

return Response.status(Status.CREATED).entity(representation).build();
}
Expand Down Expand Up @@ -325,12 +328,12 @@ private void findAssociatedPolicies(Policy policy, List<Policy> policies) {
});
}

private void audit(@Context UriInfo uriInfo, AbstractPolicyRepresentation resource, String id, OperationType operation) {
private void audit(AbstractPolicyRepresentation resource, String id, OperationType operation) {
if (authorization.getRealm().isAdminEventsEnabled()) {
if (id != null) {
adminEvent.operation(operation).resourcePath(uriInfo, id).representation(resource).success();
adminEvent.operation(operation).resourcePath(session.getContext().getUri(), id).representation(resource).success();
} else {
adminEvent.operation(operation).resourcePath(uriInfo).representation(resource).success();
adminEvent.operation(operation).resourcePath(session.getContext().getUri()).representation(resource).success();
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,6 @@
import javax.ws.rs.PUT;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;
import javax.ws.rs.core.Context;
import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.Response;
import javax.ws.rs.core.UriInfo;
Expand Down Expand Up @@ -68,9 +67,6 @@ public class ResourceServerService {
private ResourceServer resourceServer;
private final ClientModel client;

@Context
private UriInfo uriInfo;

public ResourceServerService(AuthorizationProvider authorization, ResourceServer resourceServer, ClientModel client, AdminPermissionEvaluator auth, AdminEventBuilder adminEvent) {
this.authorization = authorization;
this.session = authorization.getKeycloakSession();
Expand All @@ -96,7 +92,7 @@ public ResourceServer create(boolean newClient) {
this.resourceServer = this.authorization.getStoreFactory().getResourceServerStore().create(this.client.getId());
createDefaultRoles(serviceAccount);
createDefaultPermission(createDefaultResource(), createDefaultPolicy());
audit(OperationType.CREATE, uriInfo, newClient);
audit(OperationType.CREATE, session.getContext().getUri(), newClient);

return resourceServer;
}
Expand All @@ -108,14 +104,14 @@ public Response update(ResourceServerRepresentation server) {
this.auth.realm().requireManageAuthorization();
this.resourceServer.setAllowRemoteResourceManagement(server.isAllowRemoteResourceManagement());
this.resourceServer.setPolicyEnforcementMode(server.getPolicyEnforcementMode());
audit(OperationType.UPDATE, uriInfo, false);
audit(OperationType.UPDATE, session.getContext().getUri(), false);
return Response.noContent().build();
}

public void delete() {
this.auth.realm().requireManageAuthorization();
authorization.getStoreFactory().getResourceServerStore().delete(resourceServer.getId());
audit(OperationType.DELETE, uriInfo, false);
audit(OperationType.DELETE, session.getContext().getUri(), false);
}

@GET
Expand All @@ -136,21 +132,21 @@ public Response exportSettings() {
@Path("/import")
@POST
@Consumes(MediaType.APPLICATION_JSON)
public Response importSettings(@Context final UriInfo uriInfo, ResourceServerRepresentation rep) {
public Response importSettings(ResourceServerRepresentation rep) {
this.auth.realm().requireManageAuthorization();

rep.setClientId(client.getId());

RepresentationToModel.toModel(rep, authorization);

audit(OperationType.UPDATE, uriInfo, false);
audit(OperationType.UPDATE, session.getContext().getUri(), false);

return Response.noContent().build();
}

@Path("/resource")
public ResourceSetService getResourceSetResource() {
ResourceSetService resource = new ResourceSetService(this.resourceServer, this.authorization, this.auth, adminEvent);
ResourceSetService resource = new ResourceSetService(this.session, this.resourceServer, this.authorization, this.auth, adminEvent);

ResteasyProviderFactory.getInstance().injectProperties(resource);

Expand All @@ -159,7 +155,7 @@ public ResourceSetService getResourceSetResource() {

@Path("/scope")
public ScopeService getScopeResource() {
ScopeService resource = new ScopeService(this.resourceServer, this.authorization, this.auth, adminEvent);
ScopeService resource = new ScopeService(this.session, this.resourceServer, this.authorization, this.auth, adminEvent);

ResteasyProviderFactory.getInstance().injectProperties(resource);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@
import org.keycloak.events.admin.ResourceType;
import org.keycloak.models.ClientModel;
import org.keycloak.models.Constants;
import org.keycloak.models.KeycloakSession;
import org.keycloak.models.RealmModel;
import org.keycloak.models.UserModel;
import org.keycloak.representations.idm.authorization.PolicyRepresentation;
Expand All @@ -79,9 +80,11 @@ public class ResourceSetService {
private final AuthorizationProvider authorization;
private final AdminPermissionEvaluator auth;
private final AdminEventBuilder adminEvent;
private KeycloakSession session;
private ResourceServer resourceServer;

public ResourceSetService(ResourceServer resourceServer, AuthorizationProvider authorization, AdminPermissionEvaluator auth, AdminEventBuilder adminEvent) {
public ResourceSetService(KeycloakSession session, ResourceServer resourceServer, AuthorizationProvider authorization, AdminPermissionEvaluator auth, AdminEventBuilder adminEvent) {
this.session = session;
this.resourceServer = resourceServer;
this.authorization = authorization;
this.auth = auth;
Expand All @@ -92,14 +95,14 @@ public ResourceSetService(ResourceServer resourceServer, AuthorizationProvider a
@NoCache
@Consumes("application/json")
@Produces("application/json")
public Response create(@Context UriInfo uriInfo, ResourceRepresentation resource) {
public Response createPost(ResourceRepresentation resource) {
if (resource == null) {
return Response.status(Status.BAD_REQUEST).build();
}

ResourceRepresentation newResource = create(resource);

audit(uriInfo, resource, resource.getId(), OperationType.CREATE);
audit(resource, resource.getId(), OperationType.CREATE);

return Response.status(Status.CREATED).entity(newResource).build();
}
Expand Down Expand Up @@ -134,7 +137,7 @@ public ResourceRepresentation create(ResourceRepresentation resource) {
@PUT
@Consumes("application/json")
@Produces("application/json")
public Response update(@Context UriInfo uriInfo, @PathParam("id") String id, ResourceRepresentation resource) {
public Response update(@PathParam("id") String id, ResourceRepresentation resource) {
requireManage();
resource.setId(id);
StoreFactory storeFactory = this.authorization.getStoreFactory();
Expand All @@ -147,14 +150,14 @@ public Response update(@Context UriInfo uriInfo, @PathParam("id") String id, Res

toModel(resource, resourceServer, authorization);

audit(uriInfo, resource, OperationType.UPDATE);
audit(resource, OperationType.UPDATE);

return Response.noContent().build();
}

@Path("{id}")
@DELETE
public Response delete(@Context UriInfo uriInfo, @PathParam("id") String id) {
public Response delete(@PathParam("id") String id) {
requireManage();
StoreFactory storeFactory = authorization.getStoreFactory();
Resource resource = storeFactory.getResourceStore().findById(id, resourceServer.getId());
Expand All @@ -166,7 +169,7 @@ public Response delete(@Context UriInfo uriInfo, @PathParam("id") String id) {
storeFactory.getResourceStore().delete(id);

if (authorization.getRealm().isAdminEventsEnabled()) {
audit(uriInfo, toRepresentation(resource, resourceServer, authorization), OperationType.DELETE);
audit(toRepresentation(resource, resourceServer, authorization), OperationType.DELETE);
}

return Response.noContent().build();
Expand Down Expand Up @@ -465,16 +468,16 @@ private void requireManage() {
}
}

private void audit(@Context UriInfo uriInfo, ResourceRepresentation resource, OperationType operation) {
audit(uriInfo, resource, null, operation);
private void audit(ResourceRepresentation resource, OperationType operation) {
audit(resource, null, operation);
}

public void audit(@Context UriInfo uriInfo, ResourceRepresentation resource, String id, OperationType operation) {
public void audit(ResourceRepresentation resource, String id, OperationType operation) {
if (authorization.getRealm().isAdminEventsEnabled()) {
if (id != null) {
adminEvent.operation(operation).resourcePath(uriInfo, id).representation(resource).success();
adminEvent.operation(operation).resourcePath(session.getContext().getUri(), id).representation(resource).success();
} else {
adminEvent.operation(operation).resourcePath(uriInfo).representation(resource).success();
adminEvent.operation(operation).resourcePath(session.getContext().getUri()).representation(resource).success();
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
import org.keycloak.events.admin.OperationType;
import org.keycloak.events.admin.ResourceType;
import org.keycloak.models.Constants;
import org.keycloak.models.KeycloakSession;
import org.keycloak.representations.idm.authorization.PolicyRepresentation;
import org.keycloak.representations.idm.authorization.ResourceRepresentation;
import org.keycloak.representations.idm.authorization.ScopeRepresentation;
Expand Down Expand Up @@ -67,9 +68,11 @@ public class ScopeService {
private final AuthorizationProvider authorization;
private final AdminPermissionEvaluator auth;
private final AdminEventBuilder adminEvent;
private KeycloakSession session;
private ResourceServer resourceServer;

public ScopeService(ResourceServer resourceServer, AuthorizationProvider authorization, AdminPermissionEvaluator auth, AdminEventBuilder adminEvent) {
public ScopeService(KeycloakSession session, ResourceServer resourceServer, AuthorizationProvider authorization, AdminPermissionEvaluator auth, AdminEventBuilder adminEvent) {
this.session = session;
this.resourceServer = resourceServer;
this.authorization = authorization;
this.auth = auth;
Expand All @@ -80,13 +83,13 @@ public ScopeService(ResourceServer resourceServer, AuthorizationProvider authori
@NoCache
@Consumes(MediaType.APPLICATION_JSON)
@Produces(MediaType.APPLICATION_JSON)
public Response create(@Context UriInfo uriInfo, ScopeRepresentation scope) {
public Response create(ScopeRepresentation scope) {
this.auth.realm().requireManageAuthorization();
Scope model = toModel(scope, this.resourceServer, authorization);

scope.setId(model.getId());

audit(uriInfo, scope, scope.getId(), OperationType.CREATE);
audit(scope, scope.getId(), OperationType.CREATE);

return Response.status(Status.CREATED).entity(scope).build();
}
Expand All @@ -95,7 +98,7 @@ public Response create(@Context UriInfo uriInfo, ScopeRepresentation scope) {
@PUT
@Consumes(MediaType.APPLICATION_JSON)
@Produces(MediaType.APPLICATION_JSON)
public Response update(@Context UriInfo uriInfo, @PathParam("id") String id, ScopeRepresentation scope) {
public Response update(@PathParam("id") String id, ScopeRepresentation scope) {
this.auth.realm().requireManageAuthorization();
scope.setId(id);
StoreFactory storeFactory = authorization.getStoreFactory();
Expand All @@ -107,14 +110,14 @@ public Response update(@Context UriInfo uriInfo, @PathParam("id") String id, Sco

toModel(scope, resourceServer, authorization);

audit(uriInfo, scope, OperationType.UPDATE);
audit(scope, OperationType.UPDATE);

return Response.noContent().build();
}

@Path("{id}")
@DELETE
public Response delete(@Context UriInfo uriInfo, @PathParam("id") String id) {
public Response delete(@PathParam("id") String id) {
this.auth.realm().requireManageAuthorization();
StoreFactory storeFactory = authorization.getStoreFactory();
List<Resource> resources = storeFactory.getResourceStore().findByScope(Arrays.asList(id), resourceServer.getId());
Expand Down Expand Up @@ -143,7 +146,7 @@ public Response delete(@Context UriInfo uriInfo, @PathParam("id") String id) {
storeFactory.getScopeStore().delete(id);

if (authorization.getRealm().isAdminEventsEnabled()) {
audit(uriInfo, toRepresentation(scope), OperationType.DELETE);
audit(toRepresentation(scope), OperationType.DELETE);
}

return Response.noContent().build();
Expand Down Expand Up @@ -260,16 +263,16 @@ public Response findAll(@QueryParam("scopeId") String id,
.build();
}

private void audit(@Context UriInfo uriInfo, ScopeRepresentation resource, OperationType operation) {
audit(uriInfo, resource, null, operation);
private void audit(ScopeRepresentation resource, OperationType operation) {
audit(resource, null, operation);
}

private void audit(@Context UriInfo uriInfo, ScopeRepresentation resource, String id, OperationType operation) {
private void audit(ScopeRepresentation resource, String id, OperationType operation) {
if (authorization.getRealm().isAdminEventsEnabled()) {
if (id != null) {
adminEvent.operation(operation).resourcePath(uriInfo, id).representation(resource).success();
adminEvent.operation(operation).resourcePath(session.getContext().getUri(), id).representation(resource).success();
} else {
adminEvent.operation(operation).resourcePath(uriInfo).representation(resource).success();
adminEvent.operation(operation).resourcePath(session.getContext().getUri()).representation(resource).success();
}
}
}
Expand Down
Loading

0 comments on commit ae47b7f

Please sign in to comment.