Skip to content

Commit 0a830cf

Browse files
committed
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 7367748 commit 0a830cf

38 files changed

+125
-131
lines changed

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

+1-1
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

+1-1
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

+1-1
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

+1-1
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

+2-2
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

+2-2
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

+1-1
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

+8-9
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

+10-1
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

+2-2
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) {

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

+2-2
Original file line numberDiff line numberDiff line change
@@ -94,7 +94,7 @@ private Authorization computeAuthorizationIfNeeded(RoutingContext ctx) {
9494

9595
@Override
9696
public void handle(RoutingContext ctx) {
97-
final User user = ctx.user().get();
97+
final User user = ctx.user();
9898

9999
if (user == null) {
100100
ctx.fail(FORBIDDEN_CODE, FORBIDDEN_EXCEPTION);
@@ -141,7 +141,7 @@ public AuthorizationHandler variableConsumer(BiConsumer<RoutingContext, Authoriz
141141
* @param providers the providers iterator
142142
*/
143143
private void checkOrFetchAuthorizations(RoutingContext ctx, Authorization authorization, AuthorizationContext authorizationContext, Iterator<AuthorizationProvider> providers) {
144-
final User user = ctx.user().get();
144+
final User user = ctx.user();
145145
final SecurityAudit audit = ((RoutingContextInternal) ctx).securityAudit();
146146
audit.authorization(authorization);
147147
audit.user(user);

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

+3-3
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@ public Future<User> authenticate(RoutingContext ctx) {
6161
return Future.failedFuture(new HttpException(500, new IllegalStateException("No callback mounted!")));
6262
}
6363

64-
final User user = ctx.user().get();
64+
final User user = ctx.user();
6565

6666
if (user == null) {
6767
return Future.failedFuture(new HttpException(401));
@@ -145,7 +145,7 @@ private void mountRegister() {
145145
.method(HttpMethod.POST)
146146
.order(order - 1)
147147
.handler(ctx -> {
148-
final User user = ctx.user().get();
148+
final User user = ctx.user();
149149
if (user == null || user.get("username") == null) {
150150
ctx.fail(new VertxException("User object misses 'username' attribute", true));
151151
return;
@@ -169,7 +169,7 @@ private void mountVerify() {
169169
.method(HttpMethod.POST)
170170
.order(order - 1)
171171
.handler(ctx -> {
172-
final User user = ctx.user().get();
172+
final User user = ctx.user();
173173
if (user == null || user.get("username") == null) {
174174
ctx.fail(new VertxException("User object misses 'username' attribute", true));
175175
return;

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

+1-1
Original file line numberDiff line numberDiff line change
@@ -118,7 +118,7 @@ public JWTAuthHandler scopeDelimiter(String delimiter) {
118118
*/
119119
@Override
120120
public void postAuthentication(RoutingContext ctx) {
121-
final User user = ctx.user().get();
121+
final User user = ctx.user();
122122
if (user == null) {
123123
// bad state
124124
ctx.fail(403, new VertxException("no user in the context", true));

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

+2-2
Original file line numberDiff line numberDiff line change
@@ -337,7 +337,7 @@ public void postAuthentication(RoutingContext ctx) {
337337
final List<String> scopes = getScopesOrSearchMetadata(this.scopes, ctx);
338338

339339
if (scopes.size() > 0) {
340-
final User user = ctx.user().get();
340+
final User user = ctx.user();
341341
if (user == null) {
342342
// bad state
343343
ctx.fail(403, new VertxException("no user in the context", true));
@@ -506,7 +506,7 @@ private void mountCallback() {
506506
.andThen(op -> audit.audit(Marker.AUTHENTICATION, op.succeeded()))
507507
.onFailure(ctx::fail)
508508
.onSuccess(user -> {
509-
((UserContextInternal) ctx.user())
509+
((UserContextInternal) ctx.userContext())
510510
.setUser(user);
511511
String location = resource != null ? resource : "/";
512512
if (session != null) {

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

+2-2
Original file line numberDiff line numberDiff line change
@@ -181,7 +181,7 @@ private Future<Void> flush(RoutingContext context, boolean skipCrc, boolean igno
181181
Boolean storeUser = context.get(SESSION_STOREUSER_KEY);
182182
if (storeUser != null && storeUser) {
183183
// during the request the user might have been removed
184-
if (context.user().get() != null) {
184+
if (context.user() != null) {
185185
session.put(SESSION_USER_HOLDER_KEY, new UserHolder(context));
186186
}
187187
}
@@ -368,7 +368,7 @@ public Future<Void> setUser(RoutingContext context, User user) {
368368
if (!cookieless) {
369369
context.response().removeCookie(sessionCookieName, false);
370370
}
371-
((UserContextInternal) context.user())
371+
((UserContextInternal) context.userContext())
372372
.setUser(user);
373373
// signal we must store the user to link it to the session
374374
context.put(SESSION_STOREUSER_KEY, true);

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

+3-3
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@ public Future<User> authenticate(RoutingContext ctx) {
6161
return Future.failedFuture(new HttpException(500, new IllegalStateException("No callback mounted!")));
6262
}
6363

64-
final User user = ctx.user().get();
64+
final User user = ctx.user();
6565

6666
if (user == null) {
6767
return Future.failedFuture(new HttpException(401));
@@ -143,7 +143,7 @@ private void mountRegister() {
143143
.method(HttpMethod.POST)
144144
.order(order - 1)
145145
.handler(ctx -> {
146-
final User user = ctx.user().get();
146+
final User user = ctx.user();
147147
if (user == null || user.get("username") == null) {
148148
ctx.fail(new VertxException("User object misses 'username' attribute", true));
149149
return;
@@ -167,7 +167,7 @@ private void mountVerify() {
167167
.method(HttpMethod.POST)
168168
.order(order - 1)
169169
.handler(ctx -> {
170-
final User user = ctx.user().get();
170+
final User user = ctx.user();
171171

172172
if (user == null || user.get("username") == null) {
173173
ctx.fail(new VertxException("User object misses 'username' attribute", true));

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

+3-3
Original file line numberDiff line numberDiff line change
@@ -47,12 +47,12 @@ public UserHolder(RoutingContext context) {
4747
public synchronized void refresh(RoutingContext context) {
4848
if (this.context != null) {
4949
// this is a new object instance or already refreshed
50-
user = this.context.user().get();
50+
user = this.context.user();
5151
}
5252
// refresh the context
5353
this.context = context;
5454
if (user != null) {
55-
((UserContextInternal) this.context.user())
55+
((UserContextInternal) this.context.userContext())
5656
.setUser(user);
5757
}
5858
}
@@ -63,7 +63,7 @@ public void writeToBuffer(Buffer buffer) {
6363
final User user;
6464

6565
synchronized (this) {
66-
user = context != null ? context.user().get() : this.user;
66+
user = context != null ? context.user() : this.user;
6767
// clear the context as this holder is not in a request anymore
6868
context = null;
6969
}

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

+2-2
Original file line numberDiff line numberDiff line change
@@ -100,7 +100,7 @@ public Future<User> authenticate(RoutingContext ctx) {
100100
return Future.failedFuture(new HttpException(500, new IllegalStateException("No callback mounted!")));
101101
}
102102

103-
final User user = ctx.user().get();
103+
final User user = ctx.user();
104104

105105
if (user == null) {
106106
return Future.failedFuture(new HttpException(401));
@@ -301,7 +301,7 @@ private void mountResponse() {
301301
.onSuccess(user -> {
302302
audit.audit(Marker.AUTHENTICATION, true);
303303
// save the user into the context
304-
((UserContextInternal) ctx.user())
304+
((UserContextInternal) ctx.userContext())
305305
.setUser(user);
306306
// the user has upgraded from unauthenticated to authenticated
307307
// session should be upgraded as recommended by owasp

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

+1-1
Original file line numberDiff line numberDiff line change
@@ -126,6 +126,6 @@ public Session webSession() {
126126

127127
@Override
128128
public User webUser() {
129-
return routingContext.user().get();
129+
return routingContext.user();
130130
}
131131
}

vertx-web/src/main/java/io/vertx/ext/web/impl/RoutingContextDecorator.java

+2-3
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@
99
import io.vertx.core.http.HttpMethod;
1010
import io.vertx.core.http.HttpServerRequest;
1111
import io.vertx.core.http.HttpServerResponse;
12-
import io.vertx.ext.auth.User;
1312
import io.vertx.ext.auth.audit.SecurityAudit;
1413
import io.vertx.ext.web.*;
1514

@@ -195,8 +194,8 @@ public HttpServerResponse response() {
195194
}
196195

197196
@Override
198-
public UserContext user() {
199-
return decoratedContext.user();
197+
public UserContext userContext() {
198+
return decoratedContext.userContext();
200199
}
201200

202201
@Override

vertx-web/src/main/java/io/vertx/ext/web/impl/RoutingContextImpl.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -339,7 +339,7 @@ public boolean isSessionAccessed() {
339339
}
340340

341341
@Override
342-
public UserContext user() {
342+
public UserContext userContext() {
343343
if (identity == null) {
344344
identity = new UserContextImpl(this);
345345
}

vertx-web/src/main/java/io/vertx/ext/web/impl/RoutingContextWrapper.java

+2-2
Original file line numberDiff line numberDiff line change
@@ -178,8 +178,8 @@ public boolean isSessionAccessed() {
178178
}
179179

180180
@Override
181-
public UserContext user() {
182-
return inner.user();
181+
public UserContext userContext() {
182+
return inner.userContext();
183183
}
184184

185185
@Override

0 commit comments

Comments
 (0)