Search in sources :

Example 1 with User

use of io.vertx.ext.auth.User 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)

Example 2 with User

use of io.vertx.ext.auth.User in project hono by eclipse.

the class HonoAuthHandlerImpl method handle.

@Override
public void handle(RoutingContext ctx) {
    if (handlePreflight(ctx)) {
        return;
    }
    User user = ctx.user();
    if (user != null) {
        // proceed to AuthZ
        authorizeUser(ctx, user);
        return;
    }
    // parse the request in order to extract the credentials object
    parseCredentials(ctx, res -> {
        if (res.failed()) {
            processException(ctx, res.cause());
            return;
        }
        // check if the user has been set
        User updatedUser = ctx.user();
        if (updatedUser != null) {
            Session session = ctx.session();
            if (session != null) {
                // the user has upgraded from unauthenticated to authenticated
                // session should be upgraded as recommended by owasp
                session.regenerateId();
            }
            // proceed to AuthZ
            authorizeUser(ctx, updatedUser);
            return;
        }
        // proceed to authN
        getAuthProvider(ctx).authenticate(res.result(), authN -> {
            if (authN.succeeded()) {
                User authenticated = authN.result();
                ctx.setUser(authenticated);
                Session session = ctx.session();
                if (session != null) {
                    // the user has upgraded from unauthenticated to authenticated
                    // session should be upgraded as recommended by owasp
                    session.regenerateId();
                }
                // proceed to AuthZ
                authorizeUser(ctx, authenticated);
            } else {
                String header = authenticateHeader(ctx);
                if (header != null) {
                    ctx.response().putHeader("WWW-Authenticate", header);
                }
                processException(ctx, authN.cause());
            }
        });
    });
}
Also used : User(io.vertx.ext.auth.User) Session(io.vertx.ext.web.Session)

Aggregations

User (io.vertx.ext.auth.User)2 Session (io.vertx.ext.web.Session)2 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 RoutingContext (io.vertx.ext.web.RoutingContext)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