keycloak-aplcache

KEYCLOAK-2262 Refactor realm resource provider, remove admin

4/6/2016 4:43:15 AM

Details

diff --git a/examples/providers/pom.xml b/examples/providers/pom.xml
index cde1696..95a3e0c 100755
--- a/examples/providers/pom.xml
+++ b/examples/providers/pom.xml
@@ -35,5 +35,6 @@
         <module>event-store-mem</module>
         <module>federation-provider</module>
         <module>authenticator</module>
+        <module>rest</module>
     </modules>
 </project>
diff --git a/examples/providers/rest/pom.xml b/examples/providers/rest/pom.xml
new file mode 100755
index 0000000..9ab4dac
--- /dev/null
+++ b/examples/providers/rest/pom.xml
@@ -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>1.9.2.Final-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>
diff --git a/examples/providers/rest/README.md b/examples/providers/rest/README.md
new file mode 100644
index 0000000..5124f88
--- /dev/null
+++ b/examples/providers/rest/README.md
@@ -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.
\ No newline at end of file
diff --git a/examples/providers/rest/src/main/java/org/keycloak/examples/rest/HelloResourceProvider.java b/examples/providers/rest/src/main/java/org/keycloak/examples/rest/HelloResourceProvider.java
new file mode 100644
index 0000000..aebd677
--- /dev/null
+++ b/examples/providers/rest/src/main/java/org/keycloak/examples/rest/HelloResourceProvider.java
@@ -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() {
+    }
+
+}
diff --git a/examples/providers/rest/src/main/resources/META-INF/services/org.keycloak.services.resource.RealmResourceProviderFactory b/examples/providers/rest/src/main/resources/META-INF/services/org.keycloak.services.resource.RealmResourceProviderFactory
new file mode 100644
index 0000000..3d053e0
--- /dev/null
+++ b/examples/providers/rest/src/main/resources/META-INF/services/org.keycloak.services.resource.RealmResourceProviderFactory
@@ -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
\ No newline at end of file
diff --git a/server-spi/src/main/java/org/keycloak/services/resource/RealmResourceProvider.java b/server-spi/src/main/java/org/keycloak/services/resource/RealmResourceProvider.java
index f8f21e4..8f615c6 100644
--- a/server-spi/src/main/java/org/keycloak/services/resource/RealmResourceProvider.java
+++ b/server-spi/src/main/java/org/keycloak/services/resource/RealmResourceProvider.java
@@ -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();
+
 }
diff --git a/server-spi/src/main/java/org/keycloak/services/resource/RealmResourceProviderFactory.java b/server-spi/src/main/java/org/keycloak/services/resource/RealmResourceProviderFactory.java
index d970687..b39bc12 100644
--- a/server-spi/src/main/java/org/keycloak/services/resource/RealmResourceProviderFactory.java
+++ b/server-spi/src/main/java/org/keycloak/services/resource/RealmResourceProviderFactory.java
@@ -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);
 }
\ No newline at end of file
diff --git a/server-spi/src/main/resources/META-INF/services/org.keycloak.provider.Spi b/server-spi/src/main/resources/META-INF/services/org.keycloak.provider.Spi
index d263efd..657faaf 100755
--- a/server-spi/src/main/resources/META-INF/services/org.keycloak.provider.Spi
+++ b/server-spi/src/main/resources/META-INF/services/org.keycloak.provider.Spi
@@ -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
diff --git a/services/src/main/java/org/keycloak/services/resources/admin/RealmAdminResource.java b/services/src/main/java/org/keycloak/services/resources/admin/RealmAdminResource.java
index 5825d0d..b434cdf 100644
--- a/services/src/main/java/org/keycloak/services/resources/admin/RealmAdminResource.java
+++ b/services/src/main/java/org/keycloak/services/resources/admin/RealmAdminResource.java
@@ -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;
-    }
-
 }
diff --git a/services/src/main/java/org/keycloak/services/resources/RealmsResource.java b/services/src/main/java/org/keycloak/services/resources/RealmsResource.java
index 24b4450..1f8df07 100755
--- a/services/src/main/java/org/keycloak/services/resources/RealmsResource.java
+++ b/services/src/main/java/org/keycloak/services/resources/RealmsResource.java
@@ -17,10 +17,8 @@
 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;
 import org.keycloak.events.EventBuilder;
 import org.keycloak.models.ClientModel;
 import org.keycloak.models.Constants;
@@ -28,20 +26,23 @@ import org.keycloak.models.KeycloakSession;
 import org.keycloak.models.RealmModel;
 import org.keycloak.protocol.LoginProtocol;
 import org.keycloak.protocol.LoginProtocolFactory;
+import org.keycloak.protocol.oidc.OIDCLoginProtocol;
+import org.keycloak.protocol.oidc.OIDCLoginProtocolService;
 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.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.QueryParam;
 import javax.ws.rs.core.*;
 import javax.ws.rs.core.Response.ResponseBuilder;
-import java.net.URI;
 
 /**
  * @author <a href="mailto:bill@burkecentral.com">Bill Burke</a>
@@ -60,9 +61,6 @@ public class RealmsResource {
     @Context
     private HttpRequest request;
 
-    @Context
-    private UriInfo uriInfo;
-
     public static UriBuilder realmBaseUrl(UriInfo uriInfo) {
         UriBuilder baseUriBuilder = uriInfo.getBaseUriBuilder();
         return realmBaseUrl(baseUriBuilder);
@@ -106,49 +104,6 @@ public class RealmsResource {
         return endpoint;
     }
 
-    /**
-     * Returns a temporary redirect to the client url configured for the given {@code clientId} in the given {@code realmName}.
-     * <p>
-     * This allows a client to refer to other clients just by their client id in URLs, will then redirect users to the actual client url.
-     * The client url is derived according to the rules of the base url in the client configuration.
-     * </p>
-     *
-     * @param realmName
-     * @param clientId
-     * @return
-     * @since 2.0
-     */
-    @GET
-    @Path("{realm}/clients/{client_id}/redirect")
-    public Response getRedirect(final @PathParam("realm") String realmName, final @PathParam("client_id") String clientId) {
-
-        RealmModel realm = init(realmName);
-
-        if (realm == null) {
-            return null;
-        }
-
-        ClientModel client = realm.getClientByClientId(clientId);
-
-        if (client == null) {
-            return null;
-        }
-
-        if (client.getRootUrl() == null && client.getBaseUrl() == null) {
-            return null;
-        }
-
-
-        URI targetUri;
-        if (client.getRootUrl() != null && (client.getBaseUrl() == null || client.getBaseUrl().isEmpty())) {
-            targetUri = KeycloakUriBuilder.fromUri(client.getRootUrl()).build();
-        } else {
-            targetUri = KeycloakUriBuilder.fromUri(ResolveRelative.resolveRelativeUri(uriInfo.getRequestUri(), client.getRootUrl(), client.getBaseUrl())).build();
-        }
-
-        return Response.temporaryRedirect(targetUri).build();
-    }
-
     @Path("{realm}/login-actions")
     public LoginActionsService getLoginActionsService(final @PathParam("realm") String name) {
         RealmModel realm = init(name);
@@ -239,27 +194,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();
     }
 }