Example #1
0
 public void receiveSaslMechanisms(final SaslMechanisms saslMechanisms) {
   if (Arrays.asList(saslMechanisms.getSaslServerMechanisms()).contains(Symbol.valueOf("PLAIN"))) {
     SaslInit init = new SaslInit();
     init.setMechanism(Symbol.valueOf("PLAIN"));
     init.setHostname(_remoteHostname);
     byte[] usernameBytes = _user.getName().getBytes(Charset.forName("UTF-8"));
     byte[] passwordBytes = _password.getBytes(Charset.forName("UTF-8"));
     byte[] initResponse = new byte[usernameBytes.length + passwordBytes.length + 2];
     System.arraycopy(usernameBytes, 0, initResponse, 1, usernameBytes.length);
     System.arraycopy(
         passwordBytes, 0, initResponse, usernameBytes.length + 2, passwordBytes.length);
     init.setInitialResponse(new Binary(initResponse));
     _saslFrameOutput.send(new SASLFrame(init), null);
   }
 }
Example #2
0
  public void receiveSaslInit(final SaslInit saslInit) {
    Symbol mechanism = saslInit.getMechanism();
    final Binary initialResponse = saslInit.getInitialResponse();
    byte[] response = initialResponse == null ? new byte[0] : initialResponse.getArray();

    try {
      _saslServer = _saslServerProvider.getSaslServer(mechanism.toString(), "localhost");

      // Process response from the client
      byte[] challenge = _saslServer.evaluateResponse(response != null ? response : new byte[0]);

      if (_saslServer.isComplete()) {
        SaslOutcome outcome = new SaslOutcome();

        outcome.setCode(SaslCode.OK);
        _saslFrameOutput.send(new SASLFrame(outcome), null);
        synchronized (getLock()) {
          _saslComplete = true;
          _authenticated = true;
          getLock().notifyAll();
        }

        if (_onSaslCompleteTask != null) {
          _onSaslCompleteTask.run();
        }

      } else {
        SaslChallenge challengeBody = new SaslChallenge();
        challengeBody.setChallenge(new Binary(challenge));
        _saslFrameOutput.send(new SASLFrame(challengeBody), null);
      }
    } catch (SaslException e) {
      SaslOutcome outcome = new SaslOutcome();

      outcome.setCode(SaslCode.AUTH);
      _saslFrameOutput.send(new SASLFrame(outcome), null);
      synchronized (getLock()) {
        _saslComplete = true;
        _authenticated = false;
        getLock().notifyAll();
      }
      if (_onSaslCompleteTask != null) {
        _onSaslCompleteTask.run();
      }
    }
  }