Skip to content

Commit 1ffb655

Browse files
committed
Upgrade to Vert.x 5
1 parent b48be76 commit 1ffb655

File tree

6 files changed

+28
-33
lines changed

6 files changed

+28
-33
lines changed

README.adoc

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -59,10 +59,10 @@ include::src/main/java/howto/oauth_oidc/MainVerticle.java[]
5959

6060
<1> We will read the secrets as environment variables
6161
<2> In order to use a handlebars we first need to create an engine
62-
<3> To simplify the development of the web components we use a Router to route all HTTP requests to organize our code in a reusable way.
62+
<3> To simplify the development of the web components we use a `Router` to route all HTTP requests to organize our code in a reusable way.
6363
<4> Entry point to the application, this will render a custom template.
6464
<5> The protected resource (not really protected yet)
65-
<6> We now configure the oauth2 handler, it will setup the callback handler (as defined in GitHub Application panel)
65+
<6> We now configure the OAuth2 handler, it will set up the callback handler (as defined in GitHub Application panel)
6666
<7> For this resource we require that users have the authority to retrieve the user emails
6767
<8> Start up the server
6868

@@ -95,7 +95,7 @@ All this is taken care for you by the handler.
9595
Before the `User` object is handled to you, if your handler was configured with `authorities` they will be first checked.
9696
If they are not present then the whole process is aborted with an `Authorization (403)` error.
9797

98-
However you might want to assert for other granted authorities, in this case you would add a intermediate handler such as:
98+
However, you might want to assert for other granted authorities, in this case you would add an intermediate handler such as:
9999

100100
[source,java]
101101
----
@@ -157,7 +157,7 @@ This can be achieved with the stock handlers, so our server file would be:
157157
include::src/main/java/howto/oauth_oidc/MainPersistentVerticle.java[tags=persistent]
158158
----
159159

160-
<1> A session handler using in memory storage will now be able to keep track of active users and you will not need to re-login on each request.
160+
<1> A session handler using in memory storage will now be able to keep track of active users, and you will not need to re-login on each request.
161161

162162

163163
=== Why persistence is important?

pom.xml

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,9 @@
1010

1111
<properties>
1212
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
13-
<vertx.version>4.1.0</vertx.version>
13+
<vertx.version>5.0.0.CR2</vertx.version>
1414
<main.verticle>howto.oauth_oidc.MainVerticle</main.verticle>
15-
<launcher.class>io.vertx.core.Launcher</launcher.class>
15+
<launcher.class>io.vertx.launcher.application.VertxApplication</launcher.class>
1616
</properties>
1717

1818
<dependencyManagement>
@@ -28,6 +28,10 @@
2828
</dependencyManagement>
2929

3030
<dependencies>
31+
<dependency>
32+
<groupId>io.vertx</groupId>
33+
<artifactId>vertx-launcher-application</artifactId>
34+
</dependency>
3135
<dependency>
3236
<groupId>io.vertx</groupId>
3337
<artifactId>vertx-web-client</artifactId>
@@ -89,9 +93,8 @@
8993
<artifactId>exec-maven-plugin</artifactId>
9094
<version>3.5.0</version>
9195
<configuration>
92-
<mainClass>io.vertx.core.Launcher</mainClass>
96+
<mainClass>${launcher.class}</mainClass>
9397
<arguments>
94-
<argument>run</argument>
9598
<argument>${main.verticle}</argument>
9699
</arguments>
97100
</configuration>

src/main/java/howto/oauth_oidc/KeycloakDiscoverVerticle.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ public class KeycloakDiscoverVerticle extends AbstractVerticle {
1515
@Override
1616
public void start(Promise<Void> startPromise) {
1717
OAuth2Options options = new OAuth2Options()
18-
.setClientID(CLIENT_ID)
18+
.setClientId(CLIENT_ID)
1919
.setClientSecret(CLIENT_SECRET)
2020
.setTenant("vertx-test") // <1>
2121
.setSite("https://your.keycloak.instance/auth/realms/{tenant}"); // <2>

src/main/java/howto/oauth_oidc/MainPersistentVerticle.java

Lines changed: 8 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
package howto.oauth_oidc;
22

3-
import io.vertx.core.AbstractVerticle;
4-
import io.vertx.core.Promise;
3+
import io.vertx.core.Future;
4+
import io.vertx.core.VerticleBase;
55
import io.vertx.ext.auth.authentication.TokenCredentials;
66
import io.vertx.ext.auth.oauth2.OAuth2Auth;
77
import io.vertx.ext.auth.oauth2.providers.GithubAuth;
@@ -13,7 +13,7 @@
1313
import io.vertx.ext.web.sstore.LocalSessionStore;
1414
import io.vertx.ext.web.templ.handlebars.HandlebarsTemplateEngine;
1515

16-
public class MainPersistentVerticle extends AbstractVerticle {
16+
public class MainPersistentVerticle extends VerticleBase {
1717

1818
private static final String CLIENT_ID =
1919
System.getenv("GITHUB_CLIENT_ID");
@@ -22,7 +22,7 @@ public class MainPersistentVerticle extends AbstractVerticle {
2222

2323
// tag::persistent[]
2424
@Override
25-
public void start(Promise<Void> startPromise) {
25+
public Future<?> start() {
2626

2727
HandlebarsTemplateEngine engine =
2828
HandlebarsTemplateEngine.create(vertx);
@@ -59,7 +59,7 @@ public void start(Promise<Void> startPromise) {
5959
.withScope("user:email"))
6060
.handler(ctx -> {
6161
authProvider
62-
.userInfo(ctx.user())
62+
.userInfo(ctx.user().get())
6363
.onFailure(err -> {
6464
ctx.session().destroy();
6565
ctx.fail(err);
@@ -68,7 +68,7 @@ public void start(Promise<Void> startPromise) {
6868
// fetch the user emails from the github API
6969
WebClient.create(ctx.vertx())
7070
.getAbs("https://api.github.com/user/emails")
71-
.authentication(new TokenCredentials(ctx.user().<String>get("access_token"))) // <2>
71+
.authentication(new TokenCredentials(ctx.user().get().<String>get("access_token"))) // <2>
7272
.as(BodyCodec.jsonArray())
7373
.send()
7474
.onFailure(err -> {
@@ -91,13 +91,9 @@ public void start(Promise<Void> startPromise) {
9191
});
9292
});
9393

94-
vertx.createHttpServer()
94+
return vertx.createHttpServer()
9595
.requestHandler(router)
9696
.listen(Integer.getInteger("port", 8080))
97-
.onSuccess(server -> {
98-
System.out.println(
99-
"HTTP server started on port: " + server.actualPort());
100-
startPromise.complete();
101-
}).onFailure(startPromise::fail);
97+
.onSuccess(server -> System.out.println("HTTP server started on port: " + server.actualPort()));
10298
}
10399
}

src/main/java/howto/oauth_oidc/MainVerticle.java

Lines changed: 6 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,22 @@
11
package howto.oauth_oidc;
22

3-
import io.vertx.core.AbstractVerticle;
4-
import io.vertx.core.Promise;
3+
import io.vertx.core.Future;
4+
import io.vertx.core.VerticleBase;
55
import io.vertx.ext.auth.oauth2.OAuth2Auth;
66
import io.vertx.ext.auth.oauth2.providers.GithubAuth;
77
import io.vertx.ext.web.Router;
88
import io.vertx.ext.web.handler.OAuth2AuthHandler;
99
import io.vertx.ext.web.templ.handlebars.HandlebarsTemplateEngine;
1010

11-
public class MainVerticle extends AbstractVerticle {
11+
public class MainVerticle extends VerticleBase {
1212

1313
private static final String CLIENT_ID =
1414
System.getenv("GITHUB_CLIENT_ID");
1515
private static final String CLIENT_SECRET =
1616
System.getenv("GITHUB_CLIENT_SECRET"); // <1>
1717

1818
@Override
19-
public void start(Promise<Void> startPromise) {
19+
public Future<?> start() {
2020

2121
HandlebarsTemplateEngine engine =
2222
HandlebarsTemplateEngine.create(vertx); // <2>
@@ -49,13 +49,9 @@ public void start(Promise<Void> startPromise) {
4949
.end("Hello protected!");
5050
});
5151

52-
vertx.createHttpServer() // <8>
52+
return vertx.createHttpServer() // <8>
5353
.requestHandler(router)
5454
.listen(Integer.getInteger("port", 8080))
55-
.onSuccess(server -> {
56-
System.out.println(
57-
"HTTP server started on port: " + server.actualPort());
58-
startPromise.complete();
59-
}).onFailure(startPromise::fail);
55+
.onSuccess(server -> System.out.println("HTTP server started on port: " + server.actualPort()));
6056
}
6157
}

src/main/java/howto/oauth_oidc/ProtectedProfileHandler.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ class ProtectedProfileHandler implements Handler<RoutingContext> {
2121
@Override
2222
public void handle(RoutingContext ctx) {
2323
authProvider
24-
.userInfo(ctx.user()) // <1>
24+
.userInfo(ctx.user().get()) // <1>
2525
.onFailure(err -> {
2626
ctx.session().destroy();
2727
ctx.fail(err);
@@ -30,7 +30,7 @@ public void handle(RoutingContext ctx) {
3030
// fetch the user emails from the github API
3131
WebClient.create(ctx.vertx())
3232
.getAbs("https://api.github.com/user/emails")
33-
.authentication(new TokenCredentials(ctx.user().<String>get("access_token"))) // <2>
33+
.authentication(new TokenCredentials(ctx.user().get().<String>get("access_token"))) // <2>
3434
.as(BodyCodec.jsonArray())
3535
.send()
3636
.onFailure(err -> {

0 commit comments

Comments
 (0)