Search in sources :

Example 6 with UsernamePasswordCredentials

use of io.vertx.ext.auth.authentication.UsernamePasswordCredentials in project vertx-web by vert-x3.

the class FormLoginHandlerImpl method authenticate.

@Override
public void authenticate(RoutingContext context, Handler<AsyncResult<User>> handler) {
    HttpServerRequest req = context.request();
    if (req.method() != HttpMethod.POST) {
        // Must be a POST
        handler.handle(Future.failedFuture(BAD_METHOD));
    } else {
        if (!((RoutingContextInternal) context).seenHandler(RoutingContextInternal.BODY_HANDLER)) {
            handler.handle(Future.failedFuture("BodyHandler is required to process POST requests"));
        } else {
            MultiMap params = req.formAttributes();
            String username = params.get(usernameParam);
            String password = params.get(passwordParam);
            if (username == null || password == null) {
                handler.handle(Future.failedFuture(BAD_REQUEST));
            } else {
                authProvider.authenticate(new UsernamePasswordCredentials(username, password), authn -> {
                    if (authn.failed()) {
                        handler.handle(Future.failedFuture(new HttpException(401, authn.cause())));
                    } else {
                        handler.handle(authn);
                    }
                });
            }
        }
    }
}
Also used : MultiMap(io.vertx.core.MultiMap) HttpServerRequest(io.vertx.core.http.HttpServerRequest) HttpException(io.vertx.ext.web.handler.HttpException) UsernamePasswordCredentials(io.vertx.ext.auth.authentication.UsernamePasswordCredentials)

Aggregations

UsernamePasswordCredentials (io.vertx.ext.auth.authentication.UsernamePasswordCredentials)6 HttpServerRequest (io.vertx.core.http.HttpServerRequest)2 HttpException (io.vertx.ext.web.handler.HttpException)2 MultiMap (io.vertx.core.MultiMap)1 User (io.vertx.ext.auth.User)1 AuthenticationProvider (io.vertx.ext.auth.authentication.AuthenticationProvider)1 AuthorizationProvider (io.vertx.ext.auth.authorization.AuthorizationProvider)1 LocalSessionStore (io.vertx.ext.web.sstore.LocalSessionStore)1 SessionStore (io.vertx.ext.web.sstore.SessionStore)1 Test (org.junit.Test)1