From ade002f111ca0721cbc65dcb2f2298d7d9628b33 Mon Sep 17 00:00:00 2001 From: Daniel Fiala Date: Tue, 20 Aug 2024 14:37:17 +0200 Subject: [PATCH] feat(rest): added rest extension with user info handler --- extensions/pom.xml | 1 + extensions/rest/deployment/pom.xml | 59 ++++++++++++++ .../deployment/ExternalAuthBuildSteps.java | 25 ++++++ extensions/rest/pom.xml | 20 +++++ extensions/rest/runtime/pom.xml | 80 +++++++++++++++++++ .../rest/endpoint/client/UserInfoHandler.java | 55 +++++++++++++ .../src/main/resources/META-INF/beans.xml | 0 .../resources/META-INF/quarkus-extension.yaml | 9 +++ pom.xml | 19 ++++- 9 files changed, 264 insertions(+), 4 deletions(-) create mode 100644 extensions/rest/deployment/pom.xml create mode 100644 extensions/rest/deployment/src/main/java/dev/cloudeko/zenei/extension/rest/deployment/ExternalAuthBuildSteps.java create mode 100644 extensions/rest/pom.xml create mode 100644 extensions/rest/runtime/pom.xml create mode 100644 extensions/rest/runtime/src/main/java/dev/cloudeko/zenei/extension/rest/endpoint/client/UserInfoHandler.java create mode 100644 extensions/rest/runtime/src/main/resources/META-INF/beans.xml create mode 100644 extensions/rest/runtime/src/main/resources/META-INF/quarkus-extension.yaml diff --git a/extensions/pom.xml b/extensions/pom.xml index 455bbf4..ffedcf0 100644 --- a/extensions/pom.xml +++ b/extensions/pom.xml @@ -17,6 +17,7 @@ external-authentication core + rest jdbc-panache diff --git a/extensions/rest/deployment/pom.xml b/extensions/rest/deployment/pom.xml new file mode 100644 index 0000000..88da614 --- /dev/null +++ b/extensions/rest/deployment/pom.xml @@ -0,0 +1,59 @@ + + + 4.0.0 + + dev.cloudeko + rest-extension-parent + 0.0.1 + + + Zenei - Extensions - Rest - Deployment + rest-extension-deployment + + + + dev.cloudeko + core-extension-deployment + + + + io.quarkus + quarkus-rest-jackson-deployment + + + io.quarkus + quarkus-rest-client-reactive-jackson-deployment + + + + dev.cloudeko + rest-extension + + + + io.quarkus + quarkus-junit5-internal + test + + + + + + maven-compiler-plugin + + ${maven.compiler.source} + ${maven.compiler.target} + + + io.quarkus + quarkus-extension-processor + ${quarkus.version} + + + + + + + diff --git a/extensions/rest/deployment/src/main/java/dev/cloudeko/zenei/extension/rest/deployment/ExternalAuthBuildSteps.java b/extensions/rest/deployment/src/main/java/dev/cloudeko/zenei/extension/rest/deployment/ExternalAuthBuildSteps.java new file mode 100644 index 0000000..84a7ed2 --- /dev/null +++ b/extensions/rest/deployment/src/main/java/dev/cloudeko/zenei/extension/rest/deployment/ExternalAuthBuildSteps.java @@ -0,0 +1,25 @@ +package dev.cloudeko.zenei.extension.rest.deployment; + +import io.quarkus.deployment.annotations.BuildStep; +import io.quarkus.deployment.builditem.FeatureBuildItem; +import io.quarkus.vertx.http.deployment.NonApplicationRootPathBuildItem; +import io.quarkus.vertx.http.deployment.RouteBuildItem; + +public class ExternalAuthBuildSteps { + + private static final String FEATURE = "core"; + + @BuildStep + public FeatureBuildItem feature() { + return new FeatureBuildItem(FEATURE); + } + + @BuildStep + public RouteBuildItem route(NonApplicationRootPathBuildItem nonApplicationRootPathBuildItem) { + return nonApplicationRootPathBuildItem.routeBuilder() + .route("custom-endpoint") + .handler(new MyCustomHandler()) + .displayOnNotFoundPage() + .build(); + } +} diff --git a/extensions/rest/pom.xml b/extensions/rest/pom.xml new file mode 100644 index 0000000..afa4da2 --- /dev/null +++ b/extensions/rest/pom.xml @@ -0,0 +1,20 @@ + + + 4.0.0 + + dev.cloudeko + extensions + 0.0.1 + + + Zenei - Extensions - Rest - Parent + rest-extension-parent + pom + + + deployment + runtime + + diff --git a/extensions/rest/runtime/pom.xml b/extensions/rest/runtime/pom.xml new file mode 100644 index 0000000..9b68612 --- /dev/null +++ b/extensions/rest/runtime/pom.xml @@ -0,0 +1,80 @@ + + + 4.0.0 + + dev.cloudeko + rest-extension-parent + 0.0.1 + + + Zenei - Extensions - Rest - Runtime + rest-extension + + + + dev.cloudeko + core-extension + + + + io.quarkus + quarkus-rest-jackson + + + io.quarkus + quarkus-rest-client-reactive-jackson + + + org.projectlombok + lombok + ${org.projectlombok.version} + provided + + + + + + + io.quarkus + quarkus-extension-maven-plugin + ${quarkus.version} + + + compile + + extension-descriptor + + + ${project.groupId}:${project.artifactId}-deployment:${project.version} + + + + + + + maven-compiler-plugin + + ${maven.compiler.source} + ${maven.compiler.target} + + -parameters + + + + org.projectlombok + lombok + ${org.projectlombok.version} + + + io.quarkus + quarkus-extension-processor + ${quarkus.version} + + + + + + + diff --git a/extensions/rest/runtime/src/main/java/dev/cloudeko/zenei/extension/rest/endpoint/client/UserInfoHandler.java b/extensions/rest/runtime/src/main/java/dev/cloudeko/zenei/extension/rest/endpoint/client/UserInfoHandler.java new file mode 100644 index 0000000..a0f75b7 --- /dev/null +++ b/extensions/rest/runtime/src/main/java/dev/cloudeko/zenei/extension/rest/endpoint/client/UserInfoHandler.java @@ -0,0 +1,55 @@ +package dev.cloudeko.zenei.extension.rest.endpoint.client; + +import com.fasterxml.jackson.databind.ObjectMapper; +import dev.cloudeko.zenei.extension.core.feature.FindUserByIdentifier; +import dev.cloudeko.zenei.extension.core.model.user.User; +import io.quarkus.arc.Arc; +import io.quarkus.security.identity.SecurityIdentity; +import io.quarkus.vertx.http.runtime.security.QuarkusHttpUser; +import io.vertx.core.Handler; +import io.vertx.core.http.HttpMethod; +import io.vertx.core.http.HttpServerRequest; +import io.vertx.core.http.HttpServerResponse; +import io.vertx.ext.web.RoutingContext; +import jakarta.ws.rs.core.MediaType; + +/** + * Handler that serves the user information of the authenticated user. + */ +public class UserInfoHandler implements Handler { + + private static final String ALLOWED_METHODS = "GET, HEAD, OPTIONS"; + + private final ObjectMapper objectMapper = new ObjectMapper(); + private volatile FindUserByIdentifier findUserByIdentifier; + + @Override + public void handle(RoutingContext routingContext) { + HttpServerRequest req = routingContext.request(); + HttpServerResponse resp = routingContext.response(); + + QuarkusHttpUser quarkusUser = (QuarkusHttpUser) routingContext.user(); + + if (req.method().equals(HttpMethod.OPTIONS)) { + resp.headers().set("Allow", ALLOWED_METHODS); + routingContext.next(); + } else { + resp.headers().set("Content-Type", MediaType.APPLICATION_JSON + ";charset=UTF-8"); + SecurityIdentity securityIdentity = quarkusUser.getSecurityIdentity(); + User user = findUserByIdentifier(securityIdentity.getPrincipal().getName()); + try { + resp.end(objectMapper.writeValueAsString(user)); + } catch (Exception e) { + resp.setStatusCode(500).end(); + } + } + } + + public User findUserByIdentifier(String identifier) { + if (this.findUserByIdentifier == null) { + this.findUserByIdentifier = Arc.container().instance(FindUserByIdentifier.class).get(); + } + + return this.findUserByIdentifier.handle(identifier); + } +} diff --git a/extensions/rest/runtime/src/main/resources/META-INF/beans.xml b/extensions/rest/runtime/src/main/resources/META-INF/beans.xml new file mode 100644 index 0000000..e69de29 diff --git a/extensions/rest/runtime/src/main/resources/META-INF/quarkus-extension.yaml b/extensions/rest/runtime/src/main/resources/META-INF/quarkus-extension.yaml new file mode 100644 index 0000000..cedb67d --- /dev/null +++ b/extensions/rest/runtime/src/main/resources/META-INF/quarkus-extension.yaml @@ -0,0 +1,9 @@ +name: Zenei Quarkus Rest Extension +#description: Do something useful. +metadata: +# keywords: +# - "key-auth" +# guide: ... # To create and publish this guide, see https://github.com/quarkiverse/quarkiverse/wiki#documenting-your-extension +# categories: +# - "miscellaneous" +# status: "preview" diff --git a/pom.xml b/pom.xml index 873dc21..f2a0659 100644 --- a/pom.xml +++ b/pom.xml @@ -50,6 +50,17 @@ + + dev.cloudeko + external-authentication-extension + ${project.version} + + + dev.cloudeko + external-authentication-extension-deployment + ${project.version} + + dev.cloudeko core-extension @@ -63,23 +74,23 @@ dev.cloudeko - jdbc-panache-extension + rest-extension ${project.version} dev.cloudeko - jdbc-panache-extension-deployment + rest-extension-deployment ${project.version} dev.cloudeko - external-authentication-extension + jdbc-panache-extension ${project.version} dev.cloudeko - external-authentication-extension-deployment + jdbc-panache-extension-deployment ${project.version}