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());
}
}
Aggregations