Skip to content

Commit

Permalink
feat(rest): added rest extension with user info handler
Browse files Browse the repository at this point in the history
  • Loading branch information
zZHorizonZz committed Aug 20, 2024
1 parent e8515e7 commit ade002f
Show file tree
Hide file tree
Showing 9 changed files with 264 additions and 4 deletions.
1 change: 1 addition & 0 deletions extensions/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
<modules>
<module>external-authentication</module>
<module>core</module>
<module>rest</module>
<module>jdbc-panache</module>
</modules>

Expand Down
59 changes: 59 additions & 0 deletions extensions/rest/deployment/pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd"
xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>dev.cloudeko</groupId>
<artifactId>rest-extension-parent</artifactId>
<version>0.0.1</version>
</parent>

<name>Zenei - Extensions - Rest - Deployment</name>
<artifactId>rest-extension-deployment</artifactId>

<dependencies>
<dependency>
<groupId>dev.cloudeko</groupId>
<artifactId>core-extension-deployment</artifactId>
</dependency>

<dependency>
<groupId>io.quarkus</groupId>
<artifactId>quarkus-rest-jackson-deployment</artifactId>
</dependency>
<dependency>
<groupId>io.quarkus</groupId>
<artifactId>quarkus-rest-client-reactive-jackson-deployment</artifactId>
</dependency>

<dependency>
<groupId>dev.cloudeko</groupId>
<artifactId>rest-extension</artifactId>
</dependency>

<dependency>
<groupId>io.quarkus</groupId>
<artifactId>quarkus-junit5-internal</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<source>${maven.compiler.source}</source>
<target>${maven.compiler.target}</target>
<annotationProcessorPaths>
<path>
<groupId>io.quarkus</groupId>
<artifactId>quarkus-extension-processor</artifactId>
<version>${quarkus.version}</version>
</path>
</annotationProcessorPaths>
</configuration>
</plugin>
</plugins>
</build>
</project>
Original file line number Diff line number Diff line change
@@ -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();
}
}
20 changes: 20 additions & 0 deletions extensions/rest/pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd"
xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>dev.cloudeko</groupId>
<artifactId>extensions</artifactId>
<version>0.0.1</version>
</parent>

<name>Zenei - Extensions - Rest - Parent</name>
<artifactId>rest-extension-parent</artifactId>
<packaging>pom</packaging>

<modules>
<module>deployment</module>
<module>runtime</module>
</modules>
</project>
80 changes: 80 additions & 0 deletions extensions/rest/runtime/pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd"
xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>dev.cloudeko</groupId>
<artifactId>rest-extension-parent</artifactId>
<version>0.0.1</version>
</parent>

<name>Zenei - Extensions - Rest - Runtime</name>
<artifactId>rest-extension</artifactId>

<dependencies>
<dependency>
<groupId>dev.cloudeko</groupId>
<artifactId>core-extension</artifactId>
</dependency>

<dependency>
<groupId>io.quarkus</groupId>
<artifactId>quarkus-rest-jackson</artifactId>
</dependency>
<dependency>
<groupId>io.quarkus</groupId>
<artifactId>quarkus-rest-client-reactive-jackson</artifactId>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>${org.projectlombok.version}</version>
<scope>provided</scope>
</dependency>
</dependencies>

<build>
<plugins>
<plugin>
<groupId>io.quarkus</groupId>
<artifactId>quarkus-extension-maven-plugin</artifactId>
<version>${quarkus.version}</version>
<executions>
<execution>
<phase>compile</phase>
<goals>
<goal>extension-descriptor</goal>
</goals>
<configuration>
<deployment>${project.groupId}:${project.artifactId}-deployment:${project.version}
</deployment>
</configuration>
</execution>
</executions>
</plugin>
<plugin>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<source>${maven.compiler.source}</source>
<target>${maven.compiler.target}</target>
<compilerArgs>
<arg>-parameters</arg>
</compilerArgs>
<annotationProcessorPaths>
<path>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>${org.projectlombok.version}</version>
</path>
<path>
<groupId>io.quarkus</groupId>
<artifactId>quarkus-extension-processor</artifactId>
<version>${quarkus.version}</version>
</path>
</annotationProcessorPaths>
</configuration>
</plugin>
</plugins>
</build>
</project>
Original file line number Diff line number Diff line change
@@ -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<RoutingContext> {

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);
}
}
Empty file.
Original file line number Diff line number Diff line change
@@ -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"
19 changes: 15 additions & 4 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,17 @@
</dependency>

<!-- Import Zenei Dependencies -->
<dependency>
<groupId>dev.cloudeko</groupId>
<artifactId>external-authentication-extension</artifactId>
<version>${project.version}</version>
</dependency>
<dependency>
<groupId>dev.cloudeko</groupId>
<artifactId>external-authentication-extension-deployment</artifactId>
<version>${project.version}</version>
</dependency>

<dependency>
<groupId>dev.cloudeko</groupId>
<artifactId>core-extension</artifactId>
Expand All @@ -63,23 +74,23 @@

<dependency>
<groupId>dev.cloudeko</groupId>
<artifactId>jdbc-panache-extension</artifactId>
<artifactId>rest-extension</artifactId>
<version>${project.version}</version>
</dependency>
<dependency>
<groupId>dev.cloudeko</groupId>
<artifactId>jdbc-panache-extension-deployment</artifactId>
<artifactId>rest-extension-deployment</artifactId>
<version>${project.version}</version>
</dependency>

<dependency>
<groupId>dev.cloudeko</groupId>
<artifactId>external-authentication-extension</artifactId>
<artifactId>jdbc-panache-extension</artifactId>
<version>${project.version}</version>
</dependency>
<dependency>
<groupId>dev.cloudeko</groupId>
<artifactId>external-authentication-extension-deployment</artifactId>
<artifactId>jdbc-panache-extension-deployment</artifactId>
<version>${project.version}</version>
</dependency>

Expand Down

0 comments on commit ade002f

Please sign in to comment.