[KEYCLOAK-2262] - New SPIs to extend both Realm and Admin RESTful APIs

This commit is contained in:
Pedro Igor 2016-04-05 12:18:53 -03:00 committed by Stian Thorgersen
parent 91cbfbd37f
commit e876a3c988
9 changed files with 334 additions and 0 deletions

View file

@ -0,0 +1,41 @@
/*
* JBoss, Home of Professional Open Source.
* Copyright 2016 Red Hat, Inc., and individual 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.services.resource;
import org.keycloak.provider.Provider;
/**
* <p>A {@link RealmResourceProvider} creates JAX-RS <emphasis>sub-resource</emphasis> instances for paths relative
* to Realm's RESTful API that could not be resolved by the server.
*
* @author <a href="mailto:psilva@redhat.com">Pedro Igor</a>
*/
public interface RealmResourceProvider extends Provider {
/**
* <p>Returns a JAX-RS resource instance that maps to the given <code>path</code>.
*
* <p>If the given <code>path</code> could not be resolved to a sub-resource, this method must return null to give a chance to other providers
* to resolve their sub-resources.
*
* @param path the sub-resource's path
* @return a JAX-RS sub-resource instance that maps to the given path or null if the path could not be resolved to a sub-resource.
*/
Object getResource(String path);
}

View file

@ -0,0 +1,41 @@
/*
* JBoss, Home of Professional Open Source.
* Copyright 2016 Red Hat, Inc., and individual 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.services.resource;
import org.keycloak.models.KeycloakSession;
import org.keycloak.models.RealmModel;
import org.keycloak.provider.ProviderFactory;
import org.keycloak.provider.ServerInfoAwareProviderFactory;
/**
* <p>A factory that creates {@link RealmResourceProvider} instances.
*
* @author <a href="mailto:psilva@redhat.com">Pedro Igor</a>
*/
public interface RealmResourceProviderFactory extends ProviderFactory<RealmResourceProvider>, ServerInfoAwareProviderFactory {
/**
* Creates a {@link RealmResourceProvider}.
*
* @param realm the {@link RealmModel} associated with the current request
* @param keycloakSession the {@link KeycloakSession} associated with the current request
* @return a {@link RealmResourceProvider} instance
*/
RealmResourceProvider create(RealmModel realm, KeycloakSession keycloakSession);
}

View file

@ -0,0 +1,54 @@
/*
* JBoss, Home of Professional Open Source.
* Copyright 2016 Red Hat, Inc., and individual 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.services.resource;
import org.keycloak.provider.Provider;
import org.keycloak.provider.ProviderFactory;
import org.keycloak.provider.Spi;
/**
* <p>A {@link Spi} to plug additional sub-resources to Realms' RESTful API.
*
* <p>Implementors can use this {@link Spi} to provide additional services to the mentioned API and extend Keycloak capabilities by
* creating JAX-RS sub-resources for paths not known by the server.
*
* @author <a href="mailto:psilva@redhat.com">Pedro Igor</a>
*/
public class RealmResourceSPI implements Spi {
@Override
public boolean isInternal() {
return true;
}
@Override
public String getName() {
return "realm-restapi-extension";
}
@Override
public Class<? extends Provider> getProviderClass() {
return RealmResourceProvider.class;
}
@Override
public Class<? extends ProviderFactory> getProviderFactoryClass() {
return RealmResourceProviderFactory.class;
}
}

View file

@ -0,0 +1,41 @@
/*
* JBoss, Home of Professional Open Source.
* Copyright 2016 Red Hat, Inc., and individual 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.services.resource.admin;
import org.keycloak.provider.Provider;
/**
* <p>A {@link RealmAdminResourceProvider} creates JAX-RS <emphasis>sub-resource</emphasis> instances for paths relative to
* Realm's Admin RESTful API that could not be resolved by the server.
*
* @author <a href="mailto:psilva@redhat.com">Pedro Igor</a>
*/
public interface RealmAdminResourceProvider extends Provider {
/**
* <p>Returns a JAX-RS resource instance that maps to the given <code>path</code>.
*
* <p>If the given <code>path</code> could not be resolved to a sub-resource, this method must return null to give a chance to other providers
* to resolve their sub-resources.
*
* @param path the sub-resource's path
* @return a JAX-RS sub-resource instance that maps to the given path or null if the path could not be resolved to a sub-resource.
*/
Object getResource(String path);
}

View file

@ -0,0 +1,41 @@
/*
* JBoss, Home of Professional Open Source.
* Copyright 2016 Red Hat, Inc., and individual 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.services.resource.admin;
import org.keycloak.models.KeycloakSession;
import org.keycloak.models.RealmModel;
import org.keycloak.provider.ProviderFactory;
import org.keycloak.provider.ServerInfoAwareProviderFactory;
/**
* <p>A factory that creates {@link RealmAdminResourceProvider} instances.
*
* @author <a href="mailto:psilva@redhat.com">Pedro Igor</a>
*/
public interface RealmAdminResourceProviderFactory extends ProviderFactory<RealmAdminResourceProvider>, ServerInfoAwareProviderFactory {
/**
* Creates a {@link RealmAdminResourceProvider}.
*
* @param realm the {@link RealmModel} associated with the current request
* @param keycloakSession the {@link KeycloakSession} associated with the current request
* @return a {@link RealmAdminResourceProvider} instance
*/
RealmAdminResourceProvider create(RealmModel realm, KeycloakSession keycloakSession);
}

View file

@ -0,0 +1,54 @@
/*
* JBoss, Home of Professional Open Source.
* Copyright 2016 Red Hat, Inc., and individual 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.services.resource.admin;
import org.keycloak.provider.Provider;
import org.keycloak.provider.ProviderFactory;
import org.keycloak.provider.Spi;
/**
* <p>A {@link Spi} to plug additional sub-resources to Realms' Administration RESTful API.
*
* <p>Implementors can use this {@link Spi} to provide additional services to the mentioned API and extend Keycloak capabilities by
* creating JAX-RS sub-resources for paths not known by the server.
*
* @author <a href="mailto:psilva@redhat.com">Pedro Igor</a>
*/
public class RealmAdminResourceSPI implements Spi {
@Override
public boolean isInternal() {
return true;
}
@Override
public String getName() {
return "realm-admin-restapi-extension";
}
@Override
public Class<? extends Provider> getProviderClass() {
return RealmAdminResourceProvider.class;
}
@Override
public Class<? extends ProviderFactory> getProviderFactoryClass() {
return RealmAdminResourceProviderFactory.class;
}
}

View file

@ -30,6 +30,8 @@ org.keycloak.exportimport.ExportSpi
org.keycloak.exportimport.ImportSpi
org.keycloak.timer.TimerSpi
org.keycloak.services.managers.BruteForceProtectorSpi
org.keycloak.services.resource.RealmResourceSPI
org.keycloak.services.resource.admin.RealmAdminResourceSPI
org.keycloak.protocol.ClientInstallationSpi
org.keycloak.protocol.LoginProtocolSpi
org.keycloak.protocol.ProtocolMapperSpi

View file

@ -28,9 +28,12 @@ import org.keycloak.models.KeycloakSession;
import org.keycloak.models.RealmModel;
import org.keycloak.protocol.LoginProtocol;
import org.keycloak.protocol.LoginProtocolFactory;
import org.keycloak.provider.ProviderFactory;
import org.keycloak.services.ServicesLogger;
import org.keycloak.services.clientregistration.ClientRegistrationService;
import org.keycloak.services.managers.RealmManager;
import org.keycloak.services.resource.RealmResourceProvider;
import org.keycloak.services.resource.RealmResourceProviderFactory;
import org.keycloak.services.util.CacheControlUtil;
import org.keycloak.services.util.ResolveRelative;
import org.keycloak.wellknown.WellKnownProvider;
@ -42,6 +45,7 @@ import javax.ws.rs.Produces;
import javax.ws.rs.core.*;
import javax.ws.rs.core.Response.ResponseBuilder;
import java.net.URI;
import java.util.List;
/**
* @author <a href="mailto:bill@burkecentral.com">Bill Burke</a>
@ -236,4 +240,30 @@ public class RealmsResource {
return Cors.add(request, responseBuilder).allowedOrigins("*").build();
}
/**
* A JAX-RS sub-resource locator that uses the {@link org.keycloak.services.resource.RealmResourceSPI} to resolve sub-resources instances given an <code>unknownPath</code>.
*
* @param unknownPath a path that is unknown to the server
* @return a JAX-RS sub-resource instance that maps to the given <code>unknownPath</code>. Otherwise null is returned.
*/
@Path("{realm}/{unknow_path}")
public Object resolveUnknowPath(@PathParam("realm") String realmName, @PathParam("unknow_path") String unknownPath) {
List<ProviderFactory> factory = this.session.getKeycloakSessionFactory().getProviderFactories(RealmResourceProvider.class);
if (factory != null) {
RealmModel realm = init(realmName);
for (ProviderFactory providerFactory : factory) {
RealmResourceProviderFactory realmFactory = (RealmResourceProviderFactory) providerFactory;
RealmResourceProvider resourceProvider = realmFactory.create(realm, this.session);
Object resource = resourceProvider.getResource(unknownPath);
if (resource != null) {
return resource;
}
}
}
return null;
}
}

View file

@ -64,6 +64,10 @@ import org.keycloak.services.managers.ResourceAdminManager;
import org.keycloak.services.ServicesLogger;
import org.keycloak.services.managers.UsersSyncManager;
import org.keycloak.services.ErrorResponse;
import org.keycloak.services.resource.RealmResourceProvider;
import org.keycloak.services.resource.RealmResourceProviderFactory;
import org.keycloak.services.resource.admin.RealmAdminResourceProvider;
import org.keycloak.services.resource.admin.RealmAdminResourceProviderFactory;
import org.keycloak.timer.TimerProvider;
import javax.ws.rs.Consumes;
@ -790,4 +794,30 @@ public class RealmAdminResource {
}
}
/**
* A JAX-RS sub-resource locator that uses the {@link org.keycloak.services.resource.admin.RealmAdminResourceSPI} to resolve
* sub-resources instances given an <code>unknownPath</code>.
*
* @param unknownPath a path that is unknown to the server
* @return a JAX-RS sub-resource instance that maps to the given <code>unknownPath</code>. Otherwise null is returned.
*/
@Path("{unknow_path}")
public Object resolveUnknowPath(@PathParam("unknow_path") String unknownPath) {
List<ProviderFactory> factory = this.session.getKeycloakSessionFactory().getProviderFactories(RealmAdminResourceProvider.class);
if (factory != null) {
for (ProviderFactory providerFactory : factory) {
RealmAdminResourceProviderFactory realmFactory = (RealmAdminResourceProviderFactory) providerFactory;
RealmAdminResourceProvider resourceProvider = realmFactory.create(realm, this.session);
Object resource = resourceProvider.getResource(unknownPath);
if (resource != null) {
return resource;
}
}
}
return null;
}
}