Skip to content

Commit

Permalink
[KEYCLOAK-3135] - Some more tests and making policy type rest api mor…
Browse files Browse the repository at this point in the history
…e generic
  • Loading branch information
pedroigor committed Apr 12, 2017
1 parent 8e64bc3 commit d60dcb4
Show file tree
Hide file tree
Showing 14 changed files with 662 additions and 482 deletions.
Expand Up @@ -11,6 +11,7 @@
import org.keycloak.authorization.policy.provider.PolicyProviderFactory;
import org.keycloak.models.KeycloakSession;
import org.keycloak.models.KeycloakSessionFactory;
import org.keycloak.representations.idm.authorization.AbstractPolicyRepresentation;
import org.keycloak.representations.idm.authorization.ResourcePermissionRepresentation;

/**
Expand All @@ -37,28 +38,32 @@ public PolicyProvider create(AuthorizationProvider authorization) {

@Override
public PolicyProviderAdminService getAdminResource(ResourceServer resourceServer, AuthorizationProvider authorization) {
return new PolicyProviderAdminService<ResourcePermissionRepresentation>() {
return new PolicyProviderAdminService() {
@Override
public void onCreate(Policy policy, ResourcePermissionRepresentation representation) {
public void onCreate(Policy policy, AbstractPolicyRepresentation representation) {
updateResourceType(policy, representation);
}

@Override
public void onUpdate(Policy policy, ResourcePermissionRepresentation representation) {
public void onUpdate(Policy policy, AbstractPolicyRepresentation representation) {
updateResourceType(policy, representation);
}

private void updateResourceType(Policy policy, ResourcePermissionRepresentation representation) {
//TODO: remove this check once we migrate to new API
private void updateResourceType(Policy policy, AbstractPolicyRepresentation representation) {
if (representation != null) {
Map<String, String> config = policy.getConfig();
//TODO: remove this check once we migrate to new API
if (ResourcePermissionRepresentation.class.equals(representation.getClass())) {
ResourcePermissionRepresentation resourcePermission = ResourcePermissionRepresentation.class.cast(representation);
Map<String, String> config = policy.getConfig();

config.compute("defaultResourceType", (key, value) -> {
String resourceType = representation.getResourceType();
return resourceType != null ? representation.getResourceType() : null;
});
config.compute("defaultResourceType", (key, value) -> {
String resourceType = resourcePermission.getResourceType();
return resourceType != null ? resourcePermission.getResourceType() : null;
});

policy.setConfig(config);
policy.setConfig(config);

}
}
}

Expand All @@ -68,7 +73,7 @@ public void onRemove(Policy policy) {
}

@Override
public Class<ResourcePermissionRepresentation> getRepresentationType() {
public Class<? extends AbstractPolicyRepresentation> getRepresentationType() {
return ResourcePermissionRepresentation.class;
}

Expand Down
Expand Up @@ -35,22 +35,7 @@ public PolicyProvider create(AuthorizationProvider authorization) {

@Override
public PolicyProviderAdminService getAdminResource(ResourceServer resourceServer, AuthorizationProvider authorization) {
return new PolicyProviderAdminService<ScopePermissionRepresentation>() {
@Override
public void onCreate(Policy policy, ScopePermissionRepresentation representation) {

}

@Override
public void onUpdate(Policy policy, ScopePermissionRepresentation representation) {

}

@Override
public void onRemove(Policy policy) {

}

return new PolicyProviderAdminService() {
@Override
public Class<ScopePermissionRepresentation> getRepresentationType() {
return ScopePermissionRepresentation.class;
Expand Down
Expand Up @@ -88,7 +88,7 @@ public Set<String> getPolicies() {
return policies;
}

public void addPolicies(String... id) {
public void addPolicy(String... id) {
if (this.policies == null) {
this.policies = new HashSet<>();
}
Expand Down
Expand Up @@ -25,4 +25,7 @@ public interface PermissionsResource {

@Path("resource")
ResourcePermissionsResource resource();

@Path("scope")
ScopePermissionsResource scope();
}
@@ -0,0 +1,75 @@
/*
* Copyright 2016 Red Hat, Inc. and/or its affiliates
* and other contributors as indicated by the @author tags.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.keycloak.admin.client.resource;

import java.util.List;

import javax.ws.rs.Consumes;
import javax.ws.rs.DELETE;
import javax.ws.rs.GET;
import javax.ws.rs.PUT;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;
import javax.ws.rs.core.MediaType;

import org.jboss.resteasy.annotations.cache.NoCache;
import org.keycloak.representations.idm.authorization.PolicyRepresentation;
import org.keycloak.representations.idm.authorization.ResourceRepresentation;
import org.keycloak.representations.idm.authorization.ScopePermissionRepresentation;
import org.keycloak.representations.idm.authorization.ScopeRepresentation;

/**
* @author <a href="mailto:psilva@redhat.com">Pedro Igor</a>
*/
public interface ScopePermissionResource {

@GET
@Produces(MediaType.APPLICATION_JSON)
@NoCache
ScopePermissionRepresentation toRepresentation();

@PUT
@Consumes(MediaType.APPLICATION_JSON)
void update(ScopePermissionRepresentation representation);

@DELETE
void remove();

@Path("/associatedPolicies")
@GET
@Produces(MediaType.APPLICATION_JSON)
@NoCache
List<PolicyRepresentation> associatedPolicies();

@Path("/dependentPolicies")
@GET
@Produces(MediaType.APPLICATION_JSON)
@NoCache
List<PolicyRepresentation> dependentPolicies();

@Path("/resources")
@GET
@Produces("application/json")
@NoCache
List<ResourceRepresentation> resources();

@Path("/scopes")
@GET
@Produces("application/json")
@NoCache
List<ScopeRepresentation> scopes();
}
@@ -0,0 +1,41 @@
/*
* Copyright 2016 Red Hat, Inc. and/or its affiliates
* and other contributors as indicated by the @author tags.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.keycloak.admin.client.resource;

import javax.ws.rs.Consumes;
import javax.ws.rs.POST;
import javax.ws.rs.Path;
import javax.ws.rs.PathParam;
import javax.ws.rs.Produces;
import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.Response;

import org.keycloak.representations.idm.authorization.ScopePermissionRepresentation;

/**
* @author <a href="mailto:psilva@redhat.com">Pedro Igor</a>
*/
public interface ScopePermissionsResource {

@POST
@Consumes(MediaType.APPLICATION_JSON)
@Produces(MediaType.APPLICATION_JSON)
Response create(ScopePermissionRepresentation representation);

@Path("{id}")
ScopePermissionResource findById(@PathParam("id") String id);
}
Expand Up @@ -26,13 +26,19 @@
*/
public interface PolicyProviderAdminService<R extends AbstractPolicyRepresentation> {

void onCreate(Policy policy, R representation);
default void onCreate(Policy policy, R representation) {

void onUpdate(Policy policy, R representation);
}

default void onUpdate(Policy policy, R representation) {

}

default void onRemove(Policy policy) {

void onRemove(Policy policy);
}

default AbstractPolicyRepresentation toRepresentation(Policy policy) {
default R toRepresentation(Policy policy) {
return null;
}

Expand Down

0 comments on commit d60dcb4

Please sign in to comment.