[ 
https://issues.apache.org/jira/browse/PROTON-1718?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel&focusedCommentId=16316780#comment-16316780
 ] 

Robbie Gemmell commented on PROTON-1718:
----------------------------------------

I have put in a change via PROTON-1736. It adds ability to set a listener on 
the Sasl object, which is notified as frames arrive when the transport is 
processed and can then be used to do related processing, such as selecting a 
mechanism, responding to challenges, etc.

A 0.25.0 snapshot with the change is available in the snapshots repo 
(https://repository.apache.org/content/repositories/snapshots/) if you want to 
try it, or you can build the current master locally.

I put together this use of it within the reactor to show how it could be used 
there:
{code}
import java.io.IOException;
import java.nio.charset.StandardCharsets;
import java.util.Arrays;

import org.apache.qpid.proton.Proton;
import org.apache.qpid.proton.engine.BaseHandler;
import org.apache.qpid.proton.engine.Connection;
import org.apache.qpid.proton.engine.Event;
import org.apache.qpid.proton.engine.Sasl;
import org.apache.qpid.proton.engine.SaslListener;
import org.apache.qpid.proton.engine.Transport;
import org.apache.qpid.proton.reactor.Handshaker;
import org.apache.qpid.proton.reactor.Reactor;
import org.apache.qpid.proton.reactor.ReactorOptions;

public class ReactorSASL extends BaseHandler {
    private final String host;
    private final int port;

    public static void main(String[] args) throws IOException {
        ReactorOptions options = new ReactorOptions();
        options.setEnableSaslByDefault(false);

        Reactor reactor = Proton.reactor(options, new ReactorSASL("localhost", 
5672));
        reactor.run();
    }

    private ReactorSASL(String host, int port) {
        this.host = host;
        this.port = port;
    }

    @Override
    public void onReactorInit(Event event) {
        event.getReactor().connectionToHost(host, port, new 
ReactorSASLHandler());
    }

    private static class ReactorSASLHandler extends BaseHandler {
        private ReactorSASLHandler() {
            // ..basic handshake handling..
            add(new Handshaker());
        }

        @Override
        public void onConnectionBound(Event e) {
            Transport t = e.getTransport();

            Sasl s = t.sasl();
            s.client();
            s.setListener(new SaslHandling());
        }

        @Override
        public void onConnectionInit(Event event) {
            Connection conn = event.getConnection();

            conn.open();
        }

        @Override
        public void onConnectionRemoteOpen(Event event) {
            Connection conn = event.getConnection();

            conn.close();
        }
    }

    private static class SaslHandling implements SaslListener {

        @Override
        public void onSaslMechanisms(Sasl s, Transport t) {
            // ...inspect remote offered mechs, select one and set any initial 
response..
            System.out.println("### mechs:" + 
Arrays.toString(s.getRemoteMechanisms()));

            s.setMechanisms("CUSTOM_MECH");
            byte[] init = "initBytes".getBytes(StandardCharsets.UTF_8);
            s.send(init, 0, init.length);
        }

        @Override
        public void onSaslChallenge(Sasl s, Transport t) {
            // ...process challenge, send response...
            byte[] challenge = new byte[s.pending()];
            s.recv(challenge, 0, challenge.length);

            System.out.println("### challenged: " + new String(challenge, 
StandardCharsets.UTF_8));

            byte[] response = "responseBytes".getBytes(StandardCharsets.UTF_8);
            s.send(response, 0, response.length);
        }

        @Override
        public void onSaslOutcome(Sasl s, Transport t) {
            System.out.println("### outcome:" + s.getOutcome());
        }

        @Override
        public void onSaslInit(Sasl s, Transport t) { }

        @Override
        public void onSaslResponse(Sasl s, Transport t) { }
    }
}
{code}

> (Proton-J) Custom Sasl
> ----------------------
>
>                 Key: PROTON-1718
>                 URL: https://issues.apache.org/jira/browse/PROTON-1718
>             Project: Qpid Proton
>          Issue Type: Improvement
>          Components: proton-j
>    Affects Versions: proton-j-0.24.0
>            Reporter: Tim Taylor
>              Labels: features
>
> I would like to be able to provide a custom SASL implementation for Proton-j 
> to use instead of being forced to use the default SaslImpl.java 
> implementation.
> Ideally, code like below would be possible
> private class CustomSasl implements org.apache.qpid.proton.engine.Sasl
> {
> ...
> }
> ...
> ...
> //transport.sasl(...) saves the provided sasl implementation and uses it 
> internally
> Sasl sasl = transport.sasl(new CustomSasl());
> Do you currently have a workaround that would allow me to use Proton-J this 
> way?



--
This message was sent by Atlassian JIRA
(v6.4.14#64029)

---------------------------------------------------------------------
To unsubscribe, e-mail: dev-unsubscr...@qpid.apache.org
For additional commands, e-mail: dev-h...@qpid.apache.org

Reply via email to