Search in sources :

Example 1 with AuthHandler

use of io.vertx.ext.web.handler.AuthHandler in project hono by eclipse.

the class HonoAuthHandlerImpl method authorize.

@Override
public void authorize(User user, Handler<AsyncResult<Void>> handler) {
    int requiredcount = authorities.size();
    if (requiredcount > 0) {
        if (user == null) {
            handler.handle(Future.failedFuture(FORBIDDEN));
            return;
        }
        AtomicInteger count = new AtomicInteger();
        AtomicBoolean sentFailure = new AtomicBoolean();
        Handler<AsyncResult<Boolean>> authHandler = res -> {
            if (res.succeeded()) {
                if (res.result()) {
                    if (count.incrementAndGet() == requiredcount) {
                        // Has all required authorities
                        handler.handle(Future.succeededFuture());
                    }
                } else {
                    if (sentFailure.compareAndSet(false, true)) {
                        handler.handle(Future.failedFuture(FORBIDDEN));
                    }
                }
            } else {
                handler.handle(Future.failedFuture(res.cause()));
            }
        };
        for (String authority : authorities) {
            if (!sentFailure.get()) {
                user.isAuthorized(authority, authHandler);
            }
        }
    } else {
        // No auth required
        handler.handle(Future.succeededFuture());
    }
}
Also used : AuthHandler(io.vertx.ext.web.handler.AuthHandler) HttpServerRequest(io.vertx.core.http.HttpServerRequest) Session(io.vertx.ext.web.Session) HttpHeaders(io.vertx.core.http.HttpHeaders) Set(java.util.Set) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) HttpStatusException(io.vertx.ext.web.handler.impl.HttpStatusException) RoutingContext(io.vertx.ext.web.RoutingContext) Future(io.vertx.core.Future) AuthProvider(io.vertx.ext.auth.AuthProvider) HashSet(java.util.HashSet) Base64(java.util.Base64) User(io.vertx.ext.auth.User) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) HttpMethod(io.vertx.core.http.HttpMethod) JsonObject(io.vertx.core.json.JsonObject) AsyncResult(io.vertx.core.AsyncResult) Handler(io.vertx.core.Handler) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) AsyncResult(io.vertx.core.AsyncResult)

Aggregations

AsyncResult (io.vertx.core.AsyncResult)1 Future (io.vertx.core.Future)1 Handler (io.vertx.core.Handler)1 HttpHeaders (io.vertx.core.http.HttpHeaders)1 HttpMethod (io.vertx.core.http.HttpMethod)1 HttpServerRequest (io.vertx.core.http.HttpServerRequest)1 JsonObject (io.vertx.core.json.JsonObject)1 AuthProvider (io.vertx.ext.auth.AuthProvider)1 User (io.vertx.ext.auth.User)1 RoutingContext (io.vertx.ext.web.RoutingContext)1 Session (io.vertx.ext.web.Session)1 AuthHandler (io.vertx.ext.web.handler.AuthHandler)1 HttpStatusException (io.vertx.ext.web.handler.impl.HttpStatusException)1 Base64 (java.util.Base64)1 HashSet (java.util.HashSet)1 Set (java.util.Set)1 AtomicBoolean (java.util.concurrent.atomic.AtomicBoolean)1 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)1