All Downloads are FREE. Search and download functionalities are using the official Maven repository.

io.vertx.ext.web.handler.impl.SimpleAuthenticationHandlerImpl Maven / Gradle / Ivy

There is a newer version: 5.0.0.CR1
Show newest version
package io.vertx.ext.web.handler.impl;

import io.vertx.core.AsyncResult;
import io.vertx.core.Future;
import io.vertx.core.Handler;
import io.vertx.ext.auth.User;
import io.vertx.ext.web.RoutingContext;
import io.vertx.ext.web.handler.HttpException;
import io.vertx.ext.web.handler.SimpleAuthenticationHandler;

import java.util.function.Function;

public class SimpleAuthenticationHandlerImpl extends AuthenticationHandlerImpl implements SimpleAuthenticationHandler {

  private Function> authn;

  public SimpleAuthenticationHandlerImpl() {
    super(new NOOPAuthenticationProvider());
  }

  @Override
  public void authenticate(RoutingContext ctx, Handler> handler) {
    if (authn != null) {
      authn.apply(ctx)
        .onFailure(err -> {
          if (err instanceof HttpException) {
            handler.handle(Future.failedFuture(err));
          } else {
            handler.handle(Future.failedFuture(new HttpException(401, err)));
          }
        })
        .onSuccess(user -> handler.handle(Future.succeededFuture(user)));
    } else {
      handler.handle(Future.failedFuture("No Authenticate function"));
    }
  }

  @Override
  public SimpleAuthenticationHandlerImpl authenticate(Function> authnFunction) {
    this.authn = authnFunction;
    return this;
  }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy