SASLJavaXMechanism.java

  1. /**
  2.  *
  3.  * Copyright © 2014 Florian Schmaus
  4.  *
  5.  * Licensed under the Apache License, Version 2.0 (the "License");
  6.  * you may not use this file except in compliance with the License.
  7.  * You may obtain a copy of the License at
  8.  *
  9.  *     http://www.apache.org/licenses/LICENSE-2.0
  10.  *
  11.  * Unless required by applicable law or agreed to in writing, software
  12.  * distributed under the License is distributed on an "AS IS" BASIS,
  13.  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  14.  * See the License for the specific language governing permissions and
  15.  * limitations under the License.
  16.  */
  17. package org.jivesoftware.smack.sasl.javax;

  18. import java.io.IOException;
  19. import java.util.HashMap;
  20. import java.util.Map;

  21. import javax.security.auth.callback.Callback;
  22. import javax.security.auth.callback.CallbackHandler;
  23. import javax.security.auth.callback.NameCallback;
  24. import javax.security.auth.callback.PasswordCallback;
  25. import javax.security.auth.callback.UnsupportedCallbackException;
  26. import javax.security.sasl.RealmCallback;
  27. import javax.security.sasl.RealmChoiceCallback;
  28. import javax.security.sasl.Sasl;
  29. import javax.security.sasl.SaslClient;
  30. import javax.security.sasl.SaslException;

  31. import org.jivesoftware.smack.SmackException;
  32. import org.jivesoftware.smack.sasl.SASLMechanism;

  33. public abstract class SASLJavaXMechanism extends SASLMechanism {

  34.     protected SaslClient sc;

  35.     @Override
  36.     public abstract String getName();

  37.     @Override
  38.     public final void checkIfSuccessfulOrThrow() throws SmackException {
  39.         if (!sc.isComplete()) {
  40.             throw new SmackException(getName() + " was not completed");
  41.         }
  42.     }

  43.     @Override
  44.     protected void authenticateInternal()
  45.                     throws SmackException {
  46.         String[] mechanisms = { getName() };
  47.         Map<String, String> props = getSaslProps();
  48.         try {
  49.             sc = Sasl.createSaslClient(mechanisms, null, "xmpp", getServerName().toString(), props,
  50.                             new CallbackHandler() {
  51.                                 @Override
  52.                                 public void handle(Callback[] callbacks) throws IOException,
  53.                                                 UnsupportedCallbackException {
  54.                                     for (int i = 0; i < callbacks.length; i++) {
  55.                                         if (callbacks[i] instanceof NameCallback) {
  56.                                             NameCallback ncb = (NameCallback) callbacks[i];
  57.                                             ncb.setName(authenticationId);
  58.                                         }
  59.                                         else if (callbacks[i] instanceof PasswordCallback) {
  60.                                             PasswordCallback pcb = (PasswordCallback) callbacks[i];
  61.                                             pcb.setPassword(password.toCharArray());
  62.                                         }
  63.                                         else if (callbacks[i] instanceof RealmCallback) {
  64.                                             RealmCallback rcb = (RealmCallback) callbacks[i];
  65.                                             // Retrieve the REALM from the challenge response that
  66.                                             // the server returned when the client initiated the
  67.                                             // authentication exchange. If this value is not null or
  68.                                             // empty, *this value* has to be sent back to the server
  69.                                             // in the client's response to the server's challenge
  70.                                             String text = rcb.getDefaultText();
  71.                                             // The SASL client (sc) created in smack uses
  72.                                             // rcb.getText when creating the negotiatedRealm to send
  73.                                             // it back to the server. Make sure that this value
  74.                                             // matches the server's realm
  75.                                             rcb.setText(text);
  76.                                         }
  77.                                         else if (callbacks[i] instanceof RealmChoiceCallback) {
  78.                                             // unused, prevents UnsupportedCallbackException
  79.                                             // RealmChoiceCallback rccb =
  80.                                             // (RealmChoiceCallback)callbacks[i];
  81.                                         }
  82.                                         else {
  83.                                             throw new UnsupportedCallbackException(callbacks[i]);
  84.                                         }
  85.                                     }
  86.                                 }

  87.                             });
  88.         }
  89.         catch (SaslException e) {
  90.             throw new SmackException(e);
  91.         }
  92.     }

  93.     @Override
  94.     protected void authenticateInternal(CallbackHandler cbh)
  95.                     throws SmackException {
  96.         String[] mechanisms = { getName() };
  97.         Map<String, String> props = getSaslProps();
  98.         try {
  99.             sc = Sasl.createSaslClient(mechanisms, null, "xmpp", host, props, cbh);
  100.         }
  101.         catch (SaslException e) {
  102.             throw new SmackException(e);
  103.         }
  104.     }

  105.     @Override
  106.     protected byte[] getAuthenticationText() throws SmackException {
  107.         if (sc.hasInitialResponse()) {
  108.             try {
  109.                 return sc.evaluateChallenge(new byte[0]);
  110.             }
  111.             catch (SaslException e) {
  112.                 throw new SmackException(e);
  113.             }
  114.         }
  115.         return null;
  116.     }

  117.     @Override
  118.     protected byte[] evaluateChallenge(byte[] challenge) throws SmackException {
  119.         try {
  120.             if (challenge != null) {
  121.                 return sc.evaluateChallenge(challenge);
  122.             }
  123.             else {
  124.                 return sc.evaluateChallenge(new byte[0]);
  125.             }
  126.         }
  127.         catch (SaslException e) {
  128.             throw new SmackException(e);
  129.         }
  130.     }

  131.     protected Map<String,String> getSaslProps() {
  132.         return new HashMap<String, String>();
  133.     }

  134.     protected String getServerName() {
  135.         return serviceName.toString();
  136.     }
  137. }