KEYCLOAK-2262 Refactor realm resource provider, remove admin resource provider and added example

This commit is contained in:
Stian Thorgersen 2016-04-06 09:43:15 +02:00
parent e876a3c988
commit 0c829cd605
14 changed files with 217 additions and 208 deletions

View file

@ -35,5 +35,6 @@
<module>event-store-mem</module>
<module>federation-provider</module>
<module>authenticator</module>
<module>rest</module>
</modules>
</project>

View file

@ -0,0 +1,16 @@
Example Realm REST Resource provider
====================================
To deploy copy target/hello-rest-example.jar to providers directory. Alternatively you can deploy as a module by running:
$KEYCLOAK_HOME/bin/jboss-cli.sh --command="module add --name=org.keycloak.examples.hello-rest-example --resources=target/hello-rest-example.jar --dependencies=org.keycloak.keycloak-core,org.keycloak.keycloak-server-spi,javax.ws.rs.api"
Then registering the provider by editing keycloak-server.json and adding the module to the providers field:
"providers": [
....
"module:org.keycloak.examples.hello-rest-example"
],
Then start (or restart) the server. Once started open http://localhost:8080/realms/master/hello and you should see the message _Hello master_.
You can also invoke the endpoint for other realms by replacing `master` with the realm name in the above url.

53
examples/providers/rest/pom.xml Executable file
View file

@ -0,0 +1,53 @@
<!--
~ 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.
-->
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
<parent>
<artifactId>keycloak-examples-providers-parent</artifactId>
<groupId>org.keycloak</groupId>
<version>2.0.0.CR1-SNAPSHOT</version>
</parent>
<name>Authenticator Example</name>
<description/>
<modelVersion>4.0.0</modelVersion>
<artifactId>keycloak-examples-providers-rest</artifactId>
<packaging>jar</packaging>
<dependencies>
<dependency>
<groupId>org.keycloak</groupId>
<artifactId>keycloak-core</artifactId>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.keycloak</groupId>
<artifactId>keycloak-server-spi</artifactId>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.jboss.spec.javax.ws.rs</groupId>
<artifactId>jboss-jaxrs-api_2.0_spec</artifactId>
</dependency>
</dependencies>
<build>
<finalName>hello-rest-example</finalName>
</build>
</project>

View file

@ -0,0 +1,57 @@
/*
* 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.examples.rest;
import org.keycloak.models.KeycloakSession;
import org.keycloak.services.resource.RealmResourceProvider;
import javax.ws.rs.GET;
import javax.ws.rs.Produces;
import javax.ws.rs.core.MediaType;
/**
* @author <a href="mailto:sthorger@redhat.com">Stian Thorgersen</a>
*/
public class HelloResourceProvider implements RealmResourceProvider {
private KeycloakSession session;
public HelloResourceProvider(KeycloakSession session) {
this.session = session;
}
@Override
public Object getResource() {
return this;
}
@GET
@Produces(MediaType.TEXT_PLAIN)
public String get() {
String name = session.getContext().getRealm().getDisplayName();
if (name == null) {
name = session.getContext().getRealm().getName();
}
return "Hello " + name;
}
@Override
public void close() {
}
}

View file

@ -0,0 +1,55 @@
/*
* 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.examples.rest;
import org.keycloak.Config.Scope;
import org.keycloak.models.KeycloakSession;
import org.keycloak.models.KeycloakSessionFactory;
import org.keycloak.services.resource.RealmResourceProvider;
import org.keycloak.services.resource.RealmResourceProviderFactory;
/**
* @author <a href="mailto:sthorger@redhat.com">Stian Thorgersen</a>
*/
public class HelloResourceProviderFactory implements RealmResourceProviderFactory {
public static final String ID = "hello";
@Override
public String getId() {
return ID;
}
@Override
public RealmResourceProvider create(KeycloakSession session) {
return new HelloResourceProvider(session);
}
@Override
public void init(Scope config) {
}
@Override
public void postInit(KeycloakSessionFactory factory) {
}
@Override
public void close() {
}
}

View file

@ -0,0 +1,18 @@
#
# 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.
#
org.keycloak.examples.rest.HelloResourceProviderFactory

View file

@ -29,13 +29,10 @@ import org.keycloak.provider.Provider;
public interface RealmResourceProvider extends Provider {
/**
* <p>Returns a JAX-RS resource instance that maps to the given <code>path</code>.
* <p>Returns a JAX-RS resource instance.
*
* <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.
* @return a JAX-RS sub-resource instance
*/
Object getResource(String path);
Object getResource();
}

View file

@ -18,24 +18,13 @@
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 {
public interface RealmResourceProviderFactory extends ProviderFactory<RealmResourceProvider> {
/**
* 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

@ -1,41 +0,0 @@
/*
* 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

@ -1,41 +0,0 @@
/*
* 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

@ -1,54 +0,0 @@
/*
* 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

@ -31,7 +31,6 @@ 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

@ -17,7 +17,6 @@
package org.keycloak.services.resources;
import org.jboss.resteasy.spi.HttpRequest;
import org.jboss.resteasy.spi.NotFoundException;
import org.jboss.resteasy.spi.ResteasyProviderFactory;
import org.keycloak.common.ClientConnection;
import org.keycloak.common.util.KeycloakUriBuilder;
@ -28,24 +27,22 @@ 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;
import javax.ws.rs.GET;
import javax.ws.rs.NotFoundException;
import javax.ws.rs.Path;
import javax.ws.rs.PathParam;
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>
@ -243,27 +240,20 @@ public class RealmsResource {
/**
* 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.
* @param extension a path that could be to a REST extension
* @return a JAX-RS sub-resource instance for the REST extension if found. 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;
}
@Path("{realm}/{extension}")
public Object resolveRealmExtension(@PathParam("realm") String realmName, @PathParam("extension") String extension) {
RealmResourceProvider provider = session.getProvider(RealmResourceProvider.class, extension);
if (provider != null) {
init(realmName);
Object resource = provider.getResource();
if (resource != null) {
return resource;
}
}
return null;
throw new NotFoundException();
}
}

View file

@ -64,10 +64,6 @@ 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;
@ -794,30 +790,4 @@ 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;
}
}