SASLJavaXMechanism.java

  1. /**
  2.  *
  3.  * Copyright © 2014-2019 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.SmackSaslException;
  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 SmackSaslException {
  39.         if (!sc.isComplete()) {
  40.             throw new SmackSaslException(getName() + " was not completed");
  41.         }
  42.     }

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

  91.                             });
  92.         }
  93.         catch (SaslException e) {
  94.             throw new SmackJavaxSaslException(e);
  95.         }
  96.     }

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

  109.     @Override
  110.     protected byte[] getAuthenticationText() throws SmackJavaxSaslException {
  111.         if (sc.hasInitialResponse()) {
  112.             try {
  113.                 return sc.evaluateChallenge(new byte[0]);
  114.             }
  115.             catch (SaslException e) {
  116.                 throw new SmackJavaxSaslException(e);
  117.             }
  118.         }
  119.         return null;
  120.     }

  121.     @Override
  122.     protected byte[] evaluateChallenge(byte[] challenge) throws SmackJavaxSaslException {
  123.         try {
  124.             if (challenge != null) {
  125.                 return sc.evaluateChallenge(challenge);
  126.             }
  127.             else {
  128.                 return sc.evaluateChallenge(new byte[0]);
  129.             }
  130.         }
  131.         catch (SaslException e) {
  132.             throw new SmackJavaxSaslException(e);
  133.         }
  134.     }

  135.     protected Map<String, String> getSaslProps() {
  136.         return new HashMap<>();
  137.     }

  138.     protected String getServerName() {
  139.         return serviceName.toString();
  140.     }
  141. }