Search in sources :

Example 1 with ProtonSaslAuthenticator

use of io.vertx.proton.sasl.ProtonSaslAuthenticator in project vertx-proton by vert-x3.

the class ProtonServerImplTest method testCustomAuthenticatorHasInitCalled.

@Test(timeout = 20000)
public void testCustomAuthenticatorHasInitCalled(TestContext context) {
    Async initCalledAsync = context.async();
    ProtonServer.create(vertx).saslAuthenticatorFactory(new ProtonSaslAuthenticatorFactory() {

        @Override
        public ProtonSaslAuthenticator create() {
            return new ProtonSaslAuthenticator() {

                @Override
                public void init(NetSocket socket, ProtonConnection protonConnection, Transport transport) {
                    initCalledAsync.complete();
                }

                @Override
                public void process(Handler<Boolean> completionHandler) {
                    completionHandler.handle(false);
                }

                @Override
                public boolean succeeded() {
                    return false;
                }
            };
        }
    }).connectHandler(protonConnection -> {
    }).listen(server -> ProtonClient.create(vertx).connect("localhost", server.result().actualPort(), protonConnectionAsyncResult -> {
    }));
}
Also used : NetSocket(io.vertx.core.net.NetSocket) TestContext(io.vertx.ext.unit.TestContext) ProtonConnection(io.vertx.proton.ProtonConnection) Async(io.vertx.ext.unit.Async) ProtonSaslAuthenticator(io.vertx.proton.sasl.ProtonSaslAuthenticator) Vertx(io.vertx.core.Vertx) RunWith(org.junit.runner.RunWith) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) ProtonClient(io.vertx.proton.ProtonClient) Test(org.junit.Test) VertxUnitRunner(io.vertx.ext.unit.junit.VertxUnitRunner) Sasl(org.apache.qpid.proton.engine.Sasl) SaslOutcome(org.apache.qpid.proton.engine.Sasl.SaslOutcome) Context(io.vertx.core.Context) ProtonServer(io.vertx.proton.ProtonServer) StandardCharsets(java.nio.charset.StandardCharsets) Transport(org.apache.qpid.proton.engine.Transport) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) After(org.junit.After) Handler(io.vertx.core.Handler) ProtonSaslAuthenticatorFactory(io.vertx.proton.sasl.ProtonSaslAuthenticatorFactory) NetSocket(io.vertx.core.net.NetSocket) Before(org.junit.Before) ProtonSaslAuthenticatorFactory(io.vertx.proton.sasl.ProtonSaslAuthenticatorFactory) ProtonConnection(io.vertx.proton.ProtonConnection) Async(io.vertx.ext.unit.Async) Transport(org.apache.qpid.proton.engine.Transport) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) ProtonSaslAuthenticator(io.vertx.proton.sasl.ProtonSaslAuthenticator) Test(org.junit.Test)

Example 2 with ProtonSaslAuthenticator

use of io.vertx.proton.sasl.ProtonSaslAuthenticator in project vertx-proton by vert-x3.

the class ProtonServerImpl method connectHandler.

@Override
public ProtonServerImpl connectHandler(Handler<ProtonConnection> handler) {
    this.handler = handler;
    server.connectHandler(netSocket -> {
        String hostname = null;
        try {
            hostname = InetAddress.getLocalHost().getHostName();
        } catch (UnknownHostException e) {
        // ignore
        }
        final ProtonConnectionImpl connection = new ProtonConnectionImpl(vertx, hostname);
        if (advertiseAnonymousRelayCapability) {
            connection.setOfferedCapabilities(new Symbol[] { ProtonConnectionImpl.ANONYMOUS_RELAY });
        }
        final ProtonSaslAuthenticator authenticator = authenticatorFactory.create();
        ProtonTransportOptions transportOptions = new ProtonTransportOptions();
        transportOptions.setHeartbeat(this.options.getHeartbeat());
        transportOptions.setMaxFrameSize(this.options.getMaxFrameSize());
        connection.bindServer(netSocket, new ProtonSaslAuthenticator() {

            @Override
            public void init(NetSocket socket, ProtonConnection protonConnection, Transport transport) {
                authenticator.init(socket, protonConnection, transport);
            }

            @Override
            public void process(Handler<Boolean> completionHandler) {
                final Context context = Vertx.currentContext();
                authenticator.process(complete -> {
                    final Context callbackContext = vertx.getOrCreateContext();
                    if (context != callbackContext) {
                        throw new IllegalStateException("Callback was not made on the original context");
                    }
                    if (complete) {
                        // The authenticator completed, now check success, do required post processing
                        if (succeeded()) {
                            handler.handle(connection);
                            connection.flush();
                        } else {
                            // auth failed, flush any pending data and disconnect client
                            connection.flush();
                            connection.disconnect();
                        }
                    }
                    completionHandler.handle(complete);
                });
            }

            @Override
            public boolean succeeded() {
                return authenticator.succeeded();
            }
        }, transportOptions);
    });
    return this;
}
Also used : NetSocket(io.vertx.core.net.NetSocket) Context(io.vertx.core.Context) ProtonConnection(io.vertx.proton.ProtonConnection) ProtonServerOptions(io.vertx.proton.ProtonServerOptions) ProtonSaslAuthenticator(io.vertx.proton.sasl.ProtonSaslAuthenticator) Vertx(io.vertx.core.Vertx) Context(io.vertx.core.Context) Future(io.vertx.core.Future) ProtonServer(io.vertx.proton.ProtonServer) ProtonTransportOptions(io.vertx.proton.ProtonTransportOptions) UnknownHostException(java.net.UnknownHostException) Transport(org.apache.qpid.proton.engine.Transport) InetAddress(java.net.InetAddress) NetServer(io.vertx.core.net.NetServer) Symbol(org.apache.qpid.proton.amqp.Symbol) AsyncResult(io.vertx.core.AsyncResult) Handler(io.vertx.core.Handler) ProtonSaslAuthenticatorFactory(io.vertx.proton.sasl.ProtonSaslAuthenticatorFactory) NetSocket(io.vertx.core.net.NetSocket) UnknownHostException(java.net.UnknownHostException) ProtonSaslAuthenticator(io.vertx.proton.sasl.ProtonSaslAuthenticator) ProtonConnection(io.vertx.proton.ProtonConnection) ProtonTransportOptions(io.vertx.proton.ProtonTransportOptions) Transport(org.apache.qpid.proton.engine.Transport)

Aggregations

Context (io.vertx.core.Context)2 Handler (io.vertx.core.Handler)2 Vertx (io.vertx.core.Vertx)2 NetSocket (io.vertx.core.net.NetSocket)2 ProtonConnection (io.vertx.proton.ProtonConnection)2 ProtonServer (io.vertx.proton.ProtonServer)2 ProtonSaslAuthenticator (io.vertx.proton.sasl.ProtonSaslAuthenticator)2 ProtonSaslAuthenticatorFactory (io.vertx.proton.sasl.ProtonSaslAuthenticatorFactory)2 Transport (org.apache.qpid.proton.engine.Transport)2 AsyncResult (io.vertx.core.AsyncResult)1 Future (io.vertx.core.Future)1 NetServer (io.vertx.core.net.NetServer)1 Async (io.vertx.ext.unit.Async)1 TestContext (io.vertx.ext.unit.TestContext)1 VertxUnitRunner (io.vertx.ext.unit.junit.VertxUnitRunner)1 ProtonClient (io.vertx.proton.ProtonClient)1 ProtonServerOptions (io.vertx.proton.ProtonServerOptions)1 ProtonTransportOptions (io.vertx.proton.ProtonTransportOptions)1 InetAddress (java.net.InetAddress)1 UnknownHostException (java.net.UnknownHostException)1