Java Code Examples for javax.security.sasl.SaslClient#hasInitialResponse()

The following examples show how to use javax.security.sasl.SaslClient#hasInitialResponse() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: TSaslClientTransport.java    From incubator-retired-blur with Apache License 2.0 6 votes vote down vote up
/**
 * Performs the client side of the initial portion of the Thrift SASL
 * protocol. Generates and sends the initial response to the server, including
 * which mechanism this client wants to use.
 */
@Override
protected void handleSaslStartMessage() throws TTransportException, SaslException {
  SaslClient saslClient = getSaslClient();

  byte[] initialResponse = new byte[0];
  if (saslClient.hasInitialResponse())
    initialResponse = saslClient.evaluateChallenge(initialResponse);

  LOGGER.debug("Sending mechanism name {} and initial response of length {}", mechanism,
      initialResponse.length);

  byte[] mechanismBytes = mechanism.getBytes();
  sendSaslMessage(NegotiationStatus.START,
                  mechanismBytes);
  // Send initial response
  sendSaslMessage(saslClient.isComplete() ? NegotiationStatus.COMPLETE : NegotiationStatus.OK,
                  initialResponse);
  underlyingTransport.flush();
}
 
Example 2
Source File: TSaslClientTransport.java    From incubator-retired-blur with Apache License 2.0 6 votes vote down vote up
/**
 * Performs the client side of the initial portion of the Thrift SASL
 * protocol. Generates and sends the initial response to the server, including
 * which mechanism this client wants to use.
 */
@Override
protected void handleSaslStartMessage() throws TTransportException, SaslException {
  SaslClient saslClient = getSaslClient();

  byte[] initialResponse = new byte[0];
  if (saslClient.hasInitialResponse())
    initialResponse = saslClient.evaluateChallenge(initialResponse);

  LOGGER.debug("Sending mechanism name {} and initial response of length {}", mechanism,
      initialResponse.length);

  byte[] mechanismBytes = mechanism.getBytes();
  sendSaslMessage(NegotiationStatus.START,
                  mechanismBytes);
  // Send initial response
  sendSaslMessage(saslClient.isComplete() ? NegotiationStatus.COMPLETE : NegotiationStatus.OK,
                  initialResponse);
  underlyingTransport.flush();
}
 
Example 3
Source File: TSaslClientTransport.java    From galaxy-sdk-java with Apache License 2.0 6 votes vote down vote up
/**
 * Performs the client side of the initial portion of the Thrift SASL
 * protocol. Generates and sends the initial response to the server, including
 * which mechanism this client wants to use.
 */
@Override
protected void handleSaslStartMessage() throws TTransportException, SaslException {
  SaslClient saslClient = getSaslClient();

  byte[] initialResponse = new byte[0];
  if (saslClient.hasInitialResponse())
    initialResponse = saslClient.evaluateChallenge(initialResponse);

  LOGGER.debug("Sending mechanism name {} and initial response of length {}", mechanism,
      initialResponse.length);

  byte[] mechanismBytes = mechanism.getBytes();
  sendSaslMessage(NegotiationStatus.START,
                  mechanismBytes);
  // Send initial response
  sendSaslMessage(saslClient.isComplete() ? NegotiationStatus.COMPLETE : NegotiationStatus.OK,
                  initialResponse);
  underlyingTransport.flush();
}
 
Example 4
Source File: AbstractSaslAuthenticator.java    From mongodb-async-driver with Apache License 2.0 6 votes vote down vote up
/**
 * Starts to authenticate the user with the specified credentials.
 *
 * @param credentials
 *            The credentials to use to login to the database.
 * @param connection
 *            The connection to authenticate the user with.
 * @throws MongoDbAuthenticationException
 *             On a failure in the protocol to authenticate the user on the
 *             connection.
 */
public void startAuthentication(final Credential credentials,
        final Connection connection) throws MongoDbAuthenticationException {
    try {
        final SaslClient client = createSaslClient(credentials, connection);

        if (client != null) {
            byte[] payload = EMPTY_BYTES;
            if (client.hasInitialResponse()) {
                payload = client.evaluateChallenge(payload);
            }

            sendStart(payload, connection, new SaslResponseCallback(client,
                    connection, myResults));
        }
        else {
            throw new MongoDbAuthenticationException(
                    "Could not locate a SASL provider.");
        }
    }
    catch (final SaslException e) {
        throw new MongoDbAuthenticationException(e);
    }
}
 
Example 5
Source File: AuthenticationOutcomeListener.java    From Bats with Apache License 2.0 6 votes vote down vote up
public void initiate(final String mechanismName) {
  logger.trace("Initiating SASL exchange.");
  try {
    final ByteString responseData;
    final SaslClient saslClient = connection.getSaslClient();
    if (saslClient.hasInitialResponse()) {
      responseData = ByteString.copyFrom(evaluateChallenge(ugi, saslClient, new byte[0]));
    } else {
      responseData = ByteString.EMPTY;
    }
    client.send(new AuthenticationOutcomeListener<>(client, connection, saslRpcType, ugi, completionListener),
        connection,
        saslRpcType,
        SaslMessage.newBuilder()
            .setMechanism(mechanismName)
            .setStatus(SaslStatus.SASL_START)
            .setData(responseData)
            .build(),
        SaslMessage.class,
        true /* the connection will not be backed up at this point */);
    logger.trace("Initiated SASL exchange.");
  } catch (final Exception e) {
    completionListener.failed(RpcException.mapException(e));
  }
}
 
Example 6
Source File: SaslTestCase.java    From wildfly-core with GNU Lesser General Public License v2.1 5 votes vote down vote up
private void testSaslServerClient(SaslServer server, SaslClient client) throws SaslException {
    byte[] message = new byte[]{};
    if (client.hasInitialResponse()) message = client.evaluateChallenge(message);
    while(!server.isComplete() || !client.isComplete()) {
        if (!server.isComplete()) message = server.evaluateResponse(message);
        if (!client.isComplete()) message = client.evaluateChallenge(message);
    }
}
 
Example 7
Source File: NoQuoteParams.java    From jdk8u-dev-jdk with GNU General Public License v2.0 4 votes vote down vote up
public static void main(String[] args) throws Exception {

        Map<String, String> props = new TreeMap<String, String>();
        props.put(Sasl.QOP, "auth");

        // client
        SaslClient client = Sasl.createSaslClient(new String[]{ DIGEST_MD5 },
            "user1", "xmpp", "127.0.0.1", props, authCallbackHandler);
        if (client == null) {
            throw new Exception("Unable to find client implementation for: " +
                DIGEST_MD5);
        }

        byte[] response = client.hasInitialResponse()
            ? client.evaluateChallenge(EMPTY) : EMPTY;
        logger.info("initial: " + new String(response));

        // server
        byte[] challenge = null;
        SaslServer server = Sasl.createSaslServer(DIGEST_MD5, "xmpp",
          "127.0.0.1", props, authCallbackHandler);
        if (server == null) {
            throw new Exception("Unable to find server implementation for: " +
                DIGEST_MD5);
        }

        if (!client.isComplete() || !server.isComplete()) {
            challenge = server.evaluateResponse(response);

            logger.info("challenge: " + new String(challenge));

            if (challenge != null) {
                response = client.evaluateChallenge(challenge);
            }
        }

        String challengeString = new String(challenge, "UTF-8").toLowerCase();

        if (challengeString.indexOf("\"md5-sess\"") > 0 ||
            challengeString.indexOf("\"utf-8\"") > 0) {
            throw new Exception("The challenge string's charset and " +
                "algorithm values must not be enclosed within quotes");
        }

        client.dispose();
        server.dispose();
    }
 
Example 8
Source File: NoQuoteParams.java    From openjdk-8 with GNU General Public License v2.0 4 votes vote down vote up
public static void main(String[] args) throws Exception {

        Map<String, String> props = new TreeMap<String, String>();
        props.put(Sasl.QOP, "auth");

        // client
        SaslClient client = Sasl.createSaslClient(new String[]{ DIGEST_MD5 },
            "user1", "xmpp", "127.0.0.1", props, authCallbackHandler);
        if (client == null) {
            throw new Exception("Unable to find client implementation for: " +
                DIGEST_MD5);
        }

        byte[] response = client.hasInitialResponse()
            ? client.evaluateChallenge(EMPTY) : EMPTY;
        logger.info("initial: " + new String(response));

        // server
        byte[] challenge = null;
        SaslServer server = Sasl.createSaslServer(DIGEST_MD5, "xmpp",
          "127.0.0.1", props, authCallbackHandler);
        if (server == null) {
            throw new Exception("Unable to find server implementation for: " +
                DIGEST_MD5);
        }

        if (!client.isComplete() || !server.isComplete()) {
            challenge = server.evaluateResponse(response);

            logger.info("challenge: " + new String(challenge));

            if (challenge != null) {
                response = client.evaluateChallenge(challenge);
            }
        }

        String challengeString = new String(challenge, "UTF-8").toLowerCase();

        if (challengeString.indexOf("\"md5-sess\"") > 0 ||
            challengeString.indexOf("\"utf-8\"") > 0) {
            throw new Exception("The challenge string's charset and " +
                "algorithm values must not be enclosed within quotes");
        }

        client.dispose();
        server.dispose();
    }
 
Example 9
Source File: ClientServerTest.java    From hottub with GNU General Public License v2.0 4 votes vote down vote up
public void run() throws Exception {
    System.out.println("Host:" + host + " port: "
            + port);
    try (SaslEndpoint endpoint = SaslEndpoint.create(host, port)) {
        negotiateMechanism(endpoint);
        SaslClient client = createSaslClient();
        byte[] data = new byte[0];
        if (client.hasInitialResponse()) {
            data = client.evaluateChallenge(data);
        }
        endpoint.send(new Message(SaslStatus.CONTINUE, data));
        Message msg = getMessage(endpoint.receive());
        while (!client.isComplete()
                && msg.getStatus() != SaslStatus.FAILURE) {
            switch (msg.getStatus()) {
                case CONTINUE:
                    System.out.println("client continues");
                    data = client.evaluateChallenge(msg.getData());
                    endpoint.send(new Message(SaslStatus.CONTINUE,
                            data));
                    msg = getMessage(endpoint.receive());
                    break;
                case SUCCESS:
                    System.out.println("client succeeded");
                    data = client.evaluateChallenge(msg.getData());
                    if (data != null) {
                        throw new SaslException("data should be null");
                    }
                    break;
                default:
                    throw new RuntimeException("Wrong status:"
                            + msg.getStatus());
            }
        }

        if (msg.getStatus() == SaslStatus.FAILURE) {
            throw new RuntimeException("Status is FAILURE");
        }
    }

    System.out.println("Done");
}
 
Example 10
Source File: NoQuoteParams.java    From hottub with GNU General Public License v2.0 4 votes vote down vote up
public static void main(String[] args) throws Exception {

        Map<String, String> props = new TreeMap<String, String>();
        props.put(Sasl.QOP, "auth");

        // client
        SaslClient client = Sasl.createSaslClient(new String[]{ DIGEST_MD5 },
            "user1", "xmpp", "127.0.0.1", props, authCallbackHandler);
        if (client == null) {
            throw new Exception("Unable to find client implementation for: " +
                DIGEST_MD5);
        }

        byte[] response = client.hasInitialResponse()
            ? client.evaluateChallenge(EMPTY) : EMPTY;
        logger.info("initial: " + new String(response));

        // server
        byte[] challenge = null;
        SaslServer server = Sasl.createSaslServer(DIGEST_MD5, "xmpp",
          "127.0.0.1", props, authCallbackHandler);
        if (server == null) {
            throw new Exception("Unable to find server implementation for: " +
                DIGEST_MD5);
        }

        if (!client.isComplete() || !server.isComplete()) {
            challenge = server.evaluateResponse(response);

            logger.info("challenge: " + new String(challenge));

            if (challenge != null) {
                response = client.evaluateChallenge(challenge);
            }
        }

        String challengeString = new String(challenge, "UTF-8").toLowerCase();

        if (challengeString.indexOf("\"md5-sess\"") > 0 ||
            challengeString.indexOf("\"utf-8\"") > 0) {
            throw new Exception("The challenge string's charset and " +
                "algorithm values must not be enclosed within quotes");
        }

        client.dispose();
        server.dispose();
    }
 
Example 11
Source File: ClientServerTest.java    From jdk8u-jdk with GNU General Public License v2.0 4 votes vote down vote up
public void run() throws Exception {
    System.out.println("Host:" + host + " port: "
            + port);
    try (SaslEndpoint endpoint = SaslEndpoint.create(host, port)) {
        negotiateMechanism(endpoint);
        SaslClient client = createSaslClient();
        byte[] data = new byte[0];
        if (client.hasInitialResponse()) {
            data = client.evaluateChallenge(data);
        }
        endpoint.send(new Message(SaslStatus.CONTINUE, data));
        Message msg = getMessage(endpoint.receive());
        while (!client.isComplete()
                && msg.getStatus() != SaslStatus.FAILURE) {
            switch (msg.getStatus()) {
                case CONTINUE:
                    System.out.println("client continues");
                    data = client.evaluateChallenge(msg.getData());
                    endpoint.send(new Message(SaslStatus.CONTINUE,
                            data));
                    msg = getMessage(endpoint.receive());
                    break;
                case SUCCESS:
                    System.out.println("client succeeded");
                    data = client.evaluateChallenge(msg.getData());
                    if (data != null) {
                        throw new SaslException("data should be null");
                    }
                    break;
                default:
                    throw new RuntimeException("Wrong status:"
                            + msg.getStatus());
            }
        }

        if (msg.getStatus() == SaslStatus.FAILURE) {
            throw new RuntimeException("Status is FAILURE");
        }
    }

    System.out.println("Done");
}
 
Example 12
Source File: NoQuoteParams.java    From jdk8u-jdk with GNU General Public License v2.0 4 votes vote down vote up
public static void main(String[] args) throws Exception {

        Map<String, String> props = new TreeMap<String, String>();
        props.put(Sasl.QOP, "auth");

        // client
        SaslClient client = Sasl.createSaslClient(new String[]{ DIGEST_MD5 },
            "user1", "xmpp", "127.0.0.1", props, authCallbackHandler);
        if (client == null) {
            throw new Exception("Unable to find client implementation for: " +
                DIGEST_MD5);
        }

        byte[] response = client.hasInitialResponse()
            ? client.evaluateChallenge(EMPTY) : EMPTY;
        logger.info("initial: " + new String(response));

        // server
        byte[] challenge = null;
        SaslServer server = Sasl.createSaslServer(DIGEST_MD5, "xmpp",
          "127.0.0.1", props, authCallbackHandler);
        if (server == null) {
            throw new Exception("Unable to find server implementation for: " +
                DIGEST_MD5);
        }

        if (!client.isComplete() || !server.isComplete()) {
            challenge = server.evaluateResponse(response);

            logger.info("challenge: " + new String(challenge));

            if (challenge != null) {
                response = client.evaluateChallenge(challenge);
            }
        }

        String challengeString = new String(challenge, "UTF-8").toLowerCase();

        if (challengeString.indexOf("\"md5-sess\"") > 0 ||
            challengeString.indexOf("\"utf-8\"") > 0) {
            throw new Exception("The challenge string's charset and " +
                "algorithm values must not be enclosed within quotes");
        }

        client.dispose();
        server.dispose();
    }
 
Example 13
Source File: ClientServerTest.java    From openjdk-jdk9 with GNU General Public License v2.0 4 votes vote down vote up
public void run() throws Exception {
    System.out.println("Host:" + host + " port: "
            + port);
    try (SaslEndpoint endpoint = SaslEndpoint.create(host, port)) {
        negotiateMechanism(endpoint);
        SaslClient client = createSaslClient();
        byte[] data = new byte[0];
        if (client.hasInitialResponse()) {
            data = client.evaluateChallenge(data);
        }
        endpoint.send(new Message(SaslStatus.CONTINUE, data));
        Message msg = getMessage(endpoint.receive());
        while (!client.isComplete()
                && msg.getStatus() != SaslStatus.FAILURE) {
            switch (msg.getStatus()) {
                case CONTINUE:
                    System.out.println("client continues");
                    data = client.evaluateChallenge(msg.getData());
                    endpoint.send(new Message(SaslStatus.CONTINUE,
                            data));
                    msg = getMessage(endpoint.receive());
                    break;
                case SUCCESS:
                    System.out.println("client succeeded");
                    data = client.evaluateChallenge(msg.getData());
                    if (data != null) {
                        throw new SaslException("data should be null");
                    }
                    break;
                default:
                    throw new RuntimeException("Wrong status:"
                            + msg.getStatus());
            }
        }

        if (msg.getStatus() == SaslStatus.FAILURE) {
            throw new RuntimeException("Status is FAILURE");
        }
    }

    System.out.println("Done");
}
 
Example 14
Source File: NoQuoteParams.java    From openjdk-jdk9 with GNU General Public License v2.0 4 votes vote down vote up
public static void main(String[] args) throws Exception {

        Map<String, String> props = new TreeMap<String, String>();
        props.put(Sasl.QOP, "auth");

        // client
        SaslClient client = Sasl.createSaslClient(new String[]{ DIGEST_MD5 },
            "user1", "xmpp", "127.0.0.1", props, authCallbackHandler);
        if (client == null) {
            throw new Exception("Unable to find client implementation for: " +
                DIGEST_MD5);
        }

        byte[] response = client.hasInitialResponse()
            ? client.evaluateChallenge(EMPTY) : EMPTY;
        logger.info("initial: " + new String(response));

        // server
        byte[] challenge = null;
        SaslServer server = Sasl.createSaslServer(DIGEST_MD5, "xmpp",
          "127.0.0.1", props, authCallbackHandler);
        if (server == null) {
            throw new Exception("Unable to find server implementation for: " +
                DIGEST_MD5);
        }

        if (!client.isComplete() || !server.isComplete()) {
            challenge = server.evaluateResponse(response);

            logger.info("challenge: " + new String(challenge));

            if (challenge != null) {
                response = client.evaluateChallenge(challenge);
            }
        }

        String challengeString = new String(challenge, "UTF-8").toLowerCase();

        if (challengeString.indexOf("\"md5-sess\"") > 0 ||
            challengeString.indexOf("\"utf-8\"") > 0) {
            throw new Exception("The challenge string's charset and " +
                "algorithm values must not be enclosed within quotes");
        }

        client.dispose();
        server.dispose();
    }
 
Example 15
Source File: NoQuoteParams.java    From openjdk-jdk8u-backup with GNU General Public License v2.0 4 votes vote down vote up
public static void main(String[] args) throws Exception {

        Map<String, String> props = new TreeMap<String, String>();
        props.put(Sasl.QOP, "auth");

        // client
        SaslClient client = Sasl.createSaslClient(new String[]{ DIGEST_MD5 },
            "user1", "xmpp", "127.0.0.1", props, authCallbackHandler);
        if (client == null) {
            throw new Exception("Unable to find client implementation for: " +
                DIGEST_MD5);
        }

        byte[] response = client.hasInitialResponse()
            ? client.evaluateChallenge(EMPTY) : EMPTY;
        logger.info("initial: " + new String(response));

        // server
        byte[] challenge = null;
        SaslServer server = Sasl.createSaslServer(DIGEST_MD5, "xmpp",
          "127.0.0.1", props, authCallbackHandler);
        if (server == null) {
            throw new Exception("Unable to find server implementation for: " +
                DIGEST_MD5);
        }

        if (!client.isComplete() || !server.isComplete()) {
            challenge = server.evaluateResponse(response);

            logger.info("challenge: " + new String(challenge));

            if (challenge != null) {
                response = client.evaluateChallenge(challenge);
            }
        }

        String challengeString = new String(challenge, "UTF-8").toLowerCase();

        if (challengeString.indexOf("\"md5-sess\"") > 0 ||
            challengeString.indexOf("\"utf-8\"") > 0) {
            throw new Exception("The challenge string's charset and " +
                "algorithm values must not be enclosed within quotes");
        }

        client.dispose();
        server.dispose();
    }
 
Example 16
Source File: ClientServerTest.java    From openjdk-jdk8u with GNU General Public License v2.0 4 votes vote down vote up
public void run() throws Exception {
    System.out.println("Host:" + host + " port: "
            + port);
    try (SaslEndpoint endpoint = SaslEndpoint.create(host, port)) {
        negotiateMechanism(endpoint);
        SaslClient client = createSaslClient();
        byte[] data = new byte[0];
        if (client.hasInitialResponse()) {
            data = client.evaluateChallenge(data);
        }
        endpoint.send(new Message(SaslStatus.CONTINUE, data));
        Message msg = getMessage(endpoint.receive());
        while (!client.isComplete()
                && msg.getStatus() != SaslStatus.FAILURE) {
            switch (msg.getStatus()) {
                case CONTINUE:
                    System.out.println("client continues");
                    data = client.evaluateChallenge(msg.getData());
                    endpoint.send(new Message(SaslStatus.CONTINUE,
                            data));
                    msg = getMessage(endpoint.receive());
                    break;
                case SUCCESS:
                    System.out.println("client succeeded");
                    data = client.evaluateChallenge(msg.getData());
                    if (data != null) {
                        throw new SaslException("data should be null");
                    }
                    break;
                default:
                    throw new RuntimeException("Wrong status:"
                            + msg.getStatus());
            }
        }

        if (msg.getStatus() == SaslStatus.FAILURE) {
            throw new RuntimeException("Status is FAILURE");
        }
    }

    System.out.println("Done");
}
 
Example 17
Source File: NoQuoteParams.java    From jdk8u60 with GNU General Public License v2.0 4 votes vote down vote up
public static void main(String[] args) throws Exception {

        Map<String, String> props = new TreeMap<String, String>();
        props.put(Sasl.QOP, "auth");

        // client
        SaslClient client = Sasl.createSaslClient(new String[]{ DIGEST_MD5 },
            "user1", "xmpp", "127.0.0.1", props, authCallbackHandler);
        if (client == null) {
            throw new Exception("Unable to find client implementation for: " +
                DIGEST_MD5);
        }

        byte[] response = client.hasInitialResponse()
            ? client.evaluateChallenge(EMPTY) : EMPTY;
        logger.info("initial: " + new String(response));

        // server
        byte[] challenge = null;
        SaslServer server = Sasl.createSaslServer(DIGEST_MD5, "xmpp",
          "127.0.0.1", props, authCallbackHandler);
        if (server == null) {
            throw new Exception("Unable to find server implementation for: " +
                DIGEST_MD5);
        }

        if (!client.isComplete() || !server.isComplete()) {
            challenge = server.evaluateResponse(response);

            logger.info("challenge: " + new String(challenge));

            if (challenge != null) {
                response = client.evaluateChallenge(challenge);
            }
        }

        String challengeString = new String(challenge, "UTF-8").toLowerCase();

        if (challengeString.indexOf("\"md5-sess\"") > 0 ||
            challengeString.indexOf("\"utf-8\"") > 0) {
            throw new Exception("The challenge string's charset and " +
                "algorithm values must not be enclosed within quotes");
        }

        client.dispose();
        server.dispose();
    }
 
Example 18
Source File: ClientServerTest.java    From jdk8u_jdk with GNU General Public License v2.0 4 votes vote down vote up
public void run() throws Exception {
    System.out.println("Host:" + host + " port: "
            + port);
    try (SaslEndpoint endpoint = SaslEndpoint.create(host, port)) {
        negotiateMechanism(endpoint);
        SaslClient client = createSaslClient();
        byte[] data = new byte[0];
        if (client.hasInitialResponse()) {
            data = client.evaluateChallenge(data);
        }
        endpoint.send(new Message(SaslStatus.CONTINUE, data));
        Message msg = getMessage(endpoint.receive());
        while (!client.isComplete()
                && msg.getStatus() != SaslStatus.FAILURE) {
            switch (msg.getStatus()) {
                case CONTINUE:
                    System.out.println("client continues");
                    data = client.evaluateChallenge(msg.getData());
                    endpoint.send(new Message(SaslStatus.CONTINUE,
                            data));
                    msg = getMessage(endpoint.receive());
                    break;
                case SUCCESS:
                    System.out.println("client succeeded");
                    data = client.evaluateChallenge(msg.getData());
                    if (data != null) {
                        throw new SaslException("data should be null");
                    }
                    break;
                default:
                    throw new RuntimeException("Wrong status:"
                            + msg.getStatus());
            }
        }

        if (msg.getStatus() == SaslStatus.FAILURE) {
            throw new RuntimeException("Status is FAILURE");
        }
    }

    System.out.println("Done");
}
 
Example 19
Source File: ClientServerTest.java    From dragonwell8_jdk with GNU General Public License v2.0 4 votes vote down vote up
public void run() throws Exception {
    System.out.println("Host:" + host + " port: "
            + port);
    try (SaslEndpoint endpoint = SaslEndpoint.create(host, port)) {
        negotiateMechanism(endpoint);
        SaslClient client = createSaslClient();
        byte[] data = new byte[0];
        if (client.hasInitialResponse()) {
            data = client.evaluateChallenge(data);
        }
        endpoint.send(new Message(SaslStatus.CONTINUE, data));
        Message msg = getMessage(endpoint.receive());
        while (!client.isComplete()
                && msg.getStatus() != SaslStatus.FAILURE) {
            switch (msg.getStatus()) {
                case CONTINUE:
                    System.out.println("client continues");
                    data = client.evaluateChallenge(msg.getData());
                    endpoint.send(new Message(SaslStatus.CONTINUE,
                            data));
                    msg = getMessage(endpoint.receive());
                    break;
                case SUCCESS:
                    System.out.println("client succeeded");
                    data = client.evaluateChallenge(msg.getData());
                    if (data != null) {
                        throw new SaslException("data should be null");
                    }
                    break;
                default:
                    throw new RuntimeException("Wrong status:"
                            + msg.getStatus());
            }
        }

        if (msg.getStatus() == SaslStatus.FAILURE) {
            throw new RuntimeException("Status is FAILURE");
        }
    }

    System.out.println("Done");
}
 
Example 20
Source File: NoQuoteParams.java    From jdk8u-jdk with GNU General Public License v2.0 4 votes vote down vote up
public static void main(String[] args) throws Exception {

        Map<String, String> props = new TreeMap<String, String>();
        props.put(Sasl.QOP, "auth");

        // client
        SaslClient client = Sasl.createSaslClient(new String[]{ DIGEST_MD5 },
            "user1", "xmpp", "127.0.0.1", props, authCallbackHandler);
        if (client == null) {
            throw new Exception("Unable to find client implementation for: " +
                DIGEST_MD5);
        }

        byte[] response = client.hasInitialResponse()
            ? client.evaluateChallenge(EMPTY) : EMPTY;
        logger.info("initial: " + new String(response));

        // server
        byte[] challenge = null;
        SaslServer server = Sasl.createSaslServer(DIGEST_MD5, "xmpp",
          "127.0.0.1", props, authCallbackHandler);
        if (server == null) {
            throw new Exception("Unable to find server implementation for: " +
                DIGEST_MD5);
        }

        if (!client.isComplete() || !server.isComplete()) {
            challenge = server.evaluateResponse(response);

            logger.info("challenge: " + new String(challenge));

            if (challenge != null) {
                response = client.evaluateChallenge(challenge);
            }
        }

        String challengeString = new String(challenge, "UTF-8").toLowerCase();

        if (challengeString.indexOf("\"md5-sess\"") > 0 ||
            challengeString.indexOf("\"utf-8\"") > 0) {
            throw new Exception("The challenge string's charset and " +
                "algorithm values must not be enclosed within quotes");
        }

        client.dispose();
        server.dispose();
    }