Skip to content

Commit

Permalink
Ensure that token based auth uses the same User object layout
Browse files Browse the repository at this point in the history
Signed-off-by: Paulo Lopes <pmlopes@gmail.com>
  • Loading branch information
pmlopes committed Oct 8, 2020
1 parent f4c635b commit 4e07c1f
Show file tree
Hide file tree
Showing 4 changed files with 10 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,8 @@

import io.vertx.core.Vertx;
import io.vertx.core.http.HttpMethod;
import io.vertx.core.json.JsonObject;
import io.vertx.ext.auth.authentication.AuthenticationProvider;
import io.vertx.ext.auth.authentication.UsernamePasswordCredentials;
import io.vertx.ext.auth.authorization.Authorization;
import io.vertx.ext.auth.authorization.AuthorizationProvider;
import io.vertx.ext.auth.authorization.PermissionBasedAuthorization;
Expand Down Expand Up @@ -77,7 +77,7 @@ protected void testAuthorization(String username, boolean fail, Authorization au
router.route().handler(rc -> {
// we need to be logged in
if (rc.user() == null) {
JsonObject authInfo = new JsonObject().put("username", username).put("password", "delicious:sausages");
UsernamePasswordCredentials authInfo = new UsernamePasswordCredentials(username, "delicious:sausages");
authNProvider.authenticate(authInfo, res -> {
if (res.succeeded()) {
rc.setUser(res.result());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
import io.vertx.core.json.JsonArray;
import io.vertx.core.json.JsonObject;
import io.vertx.ext.auth.authentication.AuthenticationProvider;
import io.vertx.ext.auth.authentication.UsernamePasswordCredentials;
import io.vertx.ext.auth.properties.PropertyFileAuthentication;
import io.vertx.ext.auth.properties.PropertyFileAuthorization;
import io.vertx.ext.bridge.BridgeEventType;
Expand Down Expand Up @@ -1093,7 +1094,7 @@ private void addLoginHandler(Router router, AuthenticationProvider authProvider)
router.route("/eventbus/*").handler(rc -> {
// we need to be logged in
if (rc.user() == null) {
JsonObject authInfo = new JsonObject().put("username", "tim").put("password", "delicious:sausages");
UsernamePasswordCredentials authInfo = new UsernamePasswordCredentials("tim", "delicious:sausages");
HttpServerRequest request = rc.request();
request.pause();
authProvider.authenticate(authInfo, res -> {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ public void testLogin() throws Exception {

Handler<RoutingContext> handler = rc -> {
assertNotNull(rc.user());
assertEquals("paulo", rc.user().principal().getString("sub"));
assertEquals("paulo", rc.user().attributes().getJsonObject("accessToken").getString("sub"));
rc.response().end("Welcome to the protected resource!");
};

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ public void testJWTAuthenticationNoAuthorization() throws Exception {

router.route("/protected/page1").handler(rc -> {
assertNotNull(rc.user());
assertEquals("paulo", rc.user().principal().getString("sub"));
assertEquals("paulo", rc.user().attributes().getJsonObject("accessToken").getString("sub"));
rc.response().end("Welcome");
});

Expand All @@ -67,7 +67,7 @@ public void testJWTAuthenticationWithAuthorization1() throws Exception {

router.route("/protected/page1").handler(rc -> {
assertNotNull(rc.user());
assertEquals("paulo", rc.user().principal().getString("sub"));
assertEquals("paulo", rc.user().attributes().getJsonObject("accessToken").getString("sub"));
rc.response().end("Welcome");
});

Expand All @@ -94,7 +94,7 @@ public void testJWTAuthenticationWithAuthorization2() throws Exception {

router.route("/protected/page1").handler(rc -> {
assertNotNull(rc.user());
assertEquals("paulo", rc.user().principal().getString("sub"));
assertEquals("paulo", rc.user().attributes().getJsonObject("accessToken").getString("sub"));
rc.response().end("Welcome");
});

Expand Down Expand Up @@ -123,7 +123,7 @@ public void testJWTAuthenticationWithAuthorization3() throws Exception {

router.route("/protected/page1").handler(rc -> {
assertNotNull(rc.user());
assertEquals("paulo", rc.user().principal().getString("sub"));
assertEquals("paulo", rc.user().attributes().getJsonObject("accessToken").getString("sub"));
rc.response().end("Welcome");
});

Expand Down Expand Up @@ -152,7 +152,7 @@ public void testJWTAuthenticationWithAuthorization4() throws Exception {

router.route("/protected/page1").handler(rc -> {
assertNotNull(rc.user());
assertEquals("paulo", rc.user().principal().getString("sub"));
assertEquals("paulo", rc.user().attributes().getJsonObject("accessToken").getString("sub"));
rc.response().end("Welcome");
});

Expand Down

0 comments on commit 4e07c1f

Please # to comment.