package io.vertx.proton.impl;
import io.vertx.core.Handler;
import io.vertx.core.net.NetSocket;
import io.vertx.proton.ProtonConnection;
import io.vertx.proton.sasl.ProtonSaslAuthenticator;
import org.apache.qpid.proton.engine.Sasl;
import org.apache.qpid.proton.engine.Sasl.SaslOutcome;
import org.apache.qpid.proton.engine.Transport;
import io.vertx.proton.sasl.impl.ProtonSaslAnonymousImpl;
public class ProtonSaslServerAuthenticatorImpl implements ProtonSaslAuthenticator {
private Sasl sasl;
private boolean succeeded;
@Override
public void init(NetSocket socket, ProtonConnection protonConnection, Transport transport) {
this.sasl = transport.sasl();
sasl.server();
sasl.allowSkip(false);
sasl.setMechanisms(ProtonSaslAnonymousImpl.MECH_NAME);
succeeded = false;
}
@Override
public void process(Handler<Boolean> completionHandler) {
if (sasl == null) {
throw new IllegalStateException("Init was not called with the associated transport");
}
boolean done = false;
String[] remoteMechanisms = sasl.getRemoteMechanisms();
if (remoteMechanisms.length > 0) {
String chosen = remoteMechanisms[0];
if (ProtonSaslAnonymousImpl.MECH_NAME.equals(chosen)) {
sasl.done(SaslOutcome.PN_SASL_OK);
succeeded = true;
} else {
sasl.done(SaslOutcome.PN_SASL_AUTH);
}
done = true;
}
completionHandler.handle(done);
}
@Override
public boolean succeeded() {
return succeeded;
}
}