Skip to content

Commit 893fd6a

Browse files
vietjcode-brazier
authored andcommitted
Revert RoutingContext#user() returning UserContext to User.
Motivation: RoutingContext#user() method returns the io.vertx.ext.auth.User interface in Vert.x 4.x, its return type has been change to UserContext instead (in addition of other changes), as UserContext is the preferred API for interacting with the user and the User interface actually represents the User. However, there is nothing wrong with having the user method returns the io.vertx.ext.auth.User interface, since the User interface is still the API for accessing user informations. The UserContext API can be returned from a userContext method instead. This avoids breaking the contract of the user method between 4.x and 5.0 Changes: Introduce a new userContext method returning the UserContext and change the return type of the user method to return the User type.
1 parent c78b48b commit 893fd6a

38 files changed

+125
-131
lines changed

vertx-web-api-service/src/main/java/io/vertx/ext/web/api/service/impl/OpenAPIRouterHandlerImpl.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ public OpenAPIRouterHandlerImpl(EventBus eventBus, String address, DeliveryOptio
5454
protected Future<JsonObject> transformRequest(ValidatedRequest request, RoutingContext routingContext,
5555
Operation operation) {
5656
JsonObject params = buildParametersObject(request);
57-
JsonObject userPrincipal = Optional.ofNullable(routingContext.user().get()).map(User::principal).orElse(null);
57+
JsonObject userPrincipal = Optional.ofNullable(routingContext.user()).map(User::principal).orElse(null);
5858

5959
ServiceRequest sr = new ServiceRequest(
6060
params,

vertx-web-api-service/src/main/java/io/vertx/ext/web/api/service/impl/RouteToEBServiceHandlerImpl.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,7 @@ public RouteToEBServiceHandlerImpl extraPayloadMapper(Function<RoutingContext, J
7373

7474
private JsonObject buildPayload(RoutingContext context) {
7575
JsonObject params = context.get("parsedParameters") != null ? ((RequestParameters)context.get("parsedParameters")).toJson() : null;
76-
User user = context.user().get();
76+
User user = context.user();
7777
return new JsonObject().put("context", new ServiceRequest(
7878
params,
7979
context.request().headers(),

vertx-web-api-service/src/test/java/io/vertx/ext/web/api/service/tests/RouteToEBServiceHandlerTest.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -181,7 +181,7 @@ public void authorizedUserTest(Vertx vertx, VertxTestContext testContext) {
181181
.handler(
182182
ValidationHandlerBuilder.create(schemaRepo).build()
183183
).handler(rc -> {
184-
((UserContextInternal) rc.user()).setUser(User.fromName("slinkydeveloper")); // Put user mock into context
184+
((UserContextInternal) rc.userContext()).setUser(User.fromName("slinkydeveloper")); // Put user mock into context
185185
rc.next();
186186
})
187187
.handler(

vertx-web-graphql/src/main/java/examples/GraphQLExamples.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -154,7 +154,7 @@ public void routingContextInDataFetchingEnvironment() {
154154

155155
RoutingContext routingContext = environment.getGraphQlContext().get(RoutingContext.class);
156156

157-
UserContext user = routingContext.user();
157+
UserContext user = routingContext.userContext();
158158

159159
Future<List<Link>> future = retrieveLinksPostedBy(user);
160160
return future.toCompletionStage();

vertx-web-openapi-router/src/test/java/io/vertx/router/test/e2e/RouterBuilderSecurityOptionalCallbackawareTest.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -60,10 +60,10 @@ void testBuilderWithAuthn(VertxTestContext testContext) {
6060
.onSuccess(self -> {
6161
self
6262
.getRoute("opA")
63-
.addHandler(ctx -> ctx.json(ctx.user().get().principal()));
63+
.addHandler(ctx -> ctx.json(ctx.user().principal()));
6464
self
6565
.getRoute("opB")
66-
.addHandler(ctx -> ctx.json(ctx.user().get().principal()));
66+
.addHandler(ctx -> ctx.json(ctx.user().principal()));
6767
}))
6868
// this test may seem useless but it proves that the chain auth properly sets up a chain when the a handler
6969
// can perform redirects (callback aware) and doesn't throw an exception at setup time.

vertx-web-openapi-router/src/test/java/io/vertx/router/test/e2e/RouterBuilderSecurityOptionalTest.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -46,8 +46,8 @@ void testBuilderWithAuthn(VertxTestContext testContext) {
4646

4747
rb.getRoute("pets")
4848
.addHandler(ctx -> {
49-
if (ctx.user().authenticated()) {
50-
ctx.json(ctx.user().get().principal());
49+
if (ctx.userContext().authenticated()) {
50+
ctx.json(ctx.user().principal());
5151
} else {
5252
ctx.json(null);
5353
}

vertx-web/src/main/asciidoc/index.adoc

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1209,7 +1209,7 @@ make sure your authentication handler is before your application handlers on tho
12091209

12101210
If the authentication handler has successfully authenticated the user it will inject a {@link io.vertx.ext.auth.User}
12111211
object into the {@link io.vertx.ext.web.UserContext} so it's available in your handlers from the routing context:
1212-
{@link io.vertx.ext.web.RoutingContext#user()}.
1212+
{@link io.vertx.ext.web.RoutingContext#userContext()}.
12131213

12141214
If you want your User object to be stored in the session so it's available between requests so you don't have to
12151215
authenticate on each request, then you should make sure you have a session handler before the authentication handler.

vertx-web/src/main/java/examples/WebExamples.java

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,6 @@
4242
import io.vertx.ext.web.sstore.SessionStore;
4343

4444
import java.util.List;
45-
import java.util.function.Function;
4645

4746
/**
4847
* These are the examples used in the documentation.
@@ -837,7 +836,7 @@ public void example38(Vertx vertx, AuthenticationProvider authProvider, Router r
837836
// This will require a login
838837

839838
// This will have the value true
840-
boolean isAuthenticated = ctx.user().authenticated();
839+
boolean isAuthenticated = ctx.userContext().authenticated();
841840

842841
});
843842
}
@@ -871,7 +870,7 @@ public void example39(Vertx vertx, AuthenticationProvider authProvider, Router r
871870
// This will require a login
872871

873872
// This will have the value true
874-
boolean isAuthenticated = ctx.user().authenticated();
873+
boolean isAuthenticated = ctx.userContext().authenticated();
875874

876875
});
877876

@@ -1268,8 +1267,8 @@ public void example52(Vertx vertx) {
12681267
public void example53(Vertx vertx) {
12691268

12701269
Handler<RoutingContext> handler = ctx -> {
1271-
String theSubject = ctx.user().get().principal().getString("sub");
1272-
String someKey = ctx.user().get().principal().getString("someKey");
1270+
String theSubject = ctx.user().principal().getString("sub");
1271+
String someKey = ctx.user().principal().getString("someKey");
12731272
};
12741273
}
12751274

@@ -1495,7 +1494,7 @@ public void example62(Vertx vertx, Router router) {
14951494
// at this moment your user object should contain the info
14961495
// from the Oauth2 response, since this is a protected resource
14971496
// as specified above in the handler config the user object is never null
1498-
User user = ctx.user().get();
1497+
User user = ctx.user();
14991498
// just dump it to the client for demo purposes
15001499
ctx.response().end(user.toString());
15011500
});
@@ -1947,7 +1946,7 @@ public void example89(Router router) {
19471946
.handler(ctx -> {
19481947
// if the user isn't admin, we ask the user to login again as admin
19491948
ctx
1950-
.user()
1949+
.userContext()
19511950
.loginHint("admin")
19521951
.impersonate();
19531952
});
@@ -1958,7 +1957,7 @@ public void example90(Router router) {
19581957
.route("/high/security/route/back/to/me")
19591958
.handler(ctx -> {
19601959
ctx
1961-
.user()
1960+
.userContext()
19621961
.restore();
19631962
});
19641963
}
@@ -1968,7 +1967,7 @@ public void example91(Router router) {
19681967
.route("/high/security/route/refresh/me")
19691968
.handler(ctx -> {
19701969
ctx
1971-
.user()
1970+
.userContext()
19721971
.refresh();
19731972
});
19741973
}

vertx-web/src/main/java/io/vertx/ext/web/RoutingContext.java

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
import io.vertx.core.internal.ContextInternal;
2424
import io.vertx.core.json.EncodeException;
2525
import io.vertx.core.json.Json;
26+
import io.vertx.ext.auth.User;
2627
import io.vertx.ext.web.impl.ParsableMIMEValue;
2728
import io.vertx.ext.web.impl.Utils;
2829

@@ -223,7 +224,15 @@ public interface RoutingContext {
223224
* as perform authentication refreshes, logout and other operations.
224225
* @return the user context
225226
*/
226-
UserContext user();
227+
UserContext userContext();
228+
229+
/**
230+
* Get the authenticated user (if any). This will usually be injected by an auth handler if authentication if successful.
231+
* @return the user, or null if the current user is not authenticated.
232+
*/
233+
default @Nullable User user() {
234+
return userContext().get();
235+
}
227236

228237
/**
229238
* If the context is being routed to failure handlers after a failure has been triggered by calling

vertx-web/src/main/java/io/vertx/ext/web/handler/impl/AuthenticationHandlerImpl.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@ public void handle(RoutingContext ctx) {
6060
ctx.request().pause();
6161
}
6262

63-
final User user = ctx.user().get();
63+
final User user = ctx.user();
6464

6565
if (user != null) {
6666
if (mfa != null) {
@@ -85,7 +85,7 @@ public void handle(RoutingContext ctx) {
8585
// perform the authentication
8686
authenticate(ctx)
8787
.onSuccess(authenticated -> {
88-
((UserContextInternal) ctx.user())
88+
((UserContextInternal) ctx.userContext())
8989
.setUser(authenticated);
9090
Session session = ctx.session();
9191
if (session != null) {

0 commit comments

Comments
 (0)