package io.vertx.ext.auth.impl;
import io.vertx.core.AsyncResult;
import io.vertx.core.Future;
import io.vertx.core.Handler;
import io.vertx.core.json.JsonObject;
import io.vertx.ext.auth.AuthProvider;
import io.vertx.ext.auth.ChainAuth;
import io.vertx.ext.auth.User;
import java.util.ArrayList;
import java.util.List;
public class ChainAuthImpl implements ChainAuth {
private final List<AuthProvider> providers = new ArrayList<>();
@Override
public ChainAuth append(AuthProvider other) {
providers.add(other);
return this;
}
@Override
public boolean remove(AuthProvider other) {
return providers.remove(other);
}
@Override
public void clear() {
providers.clear();
}
@Override
public void authenticate(final JsonObject authInfo, final Handler<AsyncResult<User>> resultHandler) {
iterate(0, authInfo, resultHandler);
}
private void iterate(final int idx, final JsonObject authInfo, final Handler<AsyncResult<User>> resultHandler) {
if (idx >= providers.size()) {
resultHandler.handle(Future.failedFuture("No more providers in the auth chain."));
return;
}
providers.get(idx).authenticate(authInfo, res -> {
if (res.succeeded()) {
resultHandler.handle(res);
} else {
iterate(idx + 1, authInfo, resultHandler);
}
});
}
}