Search code examples
javaauthenticationvert.xvertx-eventbus

How to fix "Internal Server Error" on using EventBus in custom AuthProvider


Using vert.x EventBus in a custom AuthProvider does give "Internal Server Error" on future completion.

Built a custom AuthProvider, which gets user data from EventBus. EventBus and user data are returned correctly, but after succeededFuture() the handler I only get "Internal Server Error". Do I mess up the handler and async mode of EventBus.send()?

package tld.domain.auth;

import io.vertx.core.AsyncResult;
import io.vertx.core.Future;
import io.vertx.core.Handler;
import io.vertx.core.Vertx;
import io.vertx.core.eventbus.EventBus;
import io.vertx.core.json.JsonObject;
import io.vertx.ext.auth.AuthProvider;
import io.vertx.ext.auth.User;

public class MyAuthProvider implements AuthProvider {

    private final EventBus eventBus;

    public MyAuthProvider(Vertx vertx) {
        this.eventBus = vertx.eventBus();
    }

    public static MyAuthProvider create(Vertx vertx) {
        return new MyAuthProvider(vertx);
    }

    @Override
    public void authenticate(JsonObject jsonObject, Handler<AsyncResult<User>> handler) {

        try {
            eventBus.send("hello", null, message -> {
                try {
                    if (true) {
                        handler.handle(Future.succeededFuture(new MyAuthUser()));
                    } else {
                        handler.handle(Future.failedFuture("failed by purpose"));
                    }
                } catch (Exception e) {
                    e.printStackTrace();
                }
            });
        } catch (Exception e) {
            e.printStackTrace();
        }

    }
}

package tld.domain.auth;

import io.vertx.core.AsyncResult;
import io.vertx.core.Future;
import io.vertx.core.Handler;
import io.vertx.core.json.JsonObject;
import io.vertx.ext.auth.AbstractUser;
import io.vertx.ext.auth.AuthProvider;

public class MyAuthUser extends AbstractUser {
    @Override
    protected void doIsPermitted(String s, Handler<AsyncResult<Boolean>> handler) {
        handler.handle(Future.succeededFuture(true));
    }

    @Override
    public JsonObject principal() {
        return null;
    }

    @Override
    public void setAuthProvider(AuthProvider authProvider) {

    }
}

And in the verticle

_router = Router.router(vertx);

MyAuthProvider authProvider = MyAuthProvider.create(vertx);

// router auth session storage
_router.route().handler(CookieHandler.create());
_router.route().handler(SessionHandler.create(LocalSessionStore.create(vertx)).setAuthProvider(authProvider));

// router basic auth
AuthHandler basicAuthHandler = BasicAuthHandler.create(authProvider);
_router.route().handler(basicAuthHandler);

_router.route().handler(BodyHandler.create());
_router.get("/hello").handler(this::getHelloHandler);

Removing the eventbus.send() makes the example working (getting the proper response). Adding the eventbus messes the result up only getting "Internal Server Error" instead of a response.

Is there an issue with handler and async eventbus.send(), or do I miss some other important vert.x concept?


Solution

  • I found the solution by swapping the BodyHandler and the AuthHandler registering order.

    // BodyHandler FIRST !!!
    _router.route().handler(BodyHandler.create());
    // AuthHandler SECOND
    _router.route().handler(_basicAuthHandler);
    // DO NOT CREATE BodyHandler AFTER AuthHandler
    //_router.route().handler(BodyHandler.create());