SASLPlainMechanism.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.provided;

  18. import javax.security.auth.callback.CallbackHandler;

  19. import org.jivesoftware.smack.sasl.SASLMechanism;
  20. import org.jivesoftware.smack.util.ByteUtils;

  21. public class SASLPlainMechanism extends SASLMechanism {

  22.     public static final String NAME = PLAIN;

  23.     @Override
  24.     protected void authenticateInternal(CallbackHandler cbh) {
  25.         throw new UnsupportedOperationException("CallbackHandler not (yet) supported");
  26.     }

  27.     @Override
  28.     protected byte[] getAuthenticationText() {
  29.         // concatenate and encode username (authcid) and password
  30.         String authzid;
  31.         if (authorizationId == null) {
  32.           authzid = "";
  33.         } else {
  34.           authzid = authorizationId.toString();
  35.         }
  36.         byte[] authcid = toBytes(authzid + '\u0000' + authenticationId);
  37.         byte[] passw = toBytes('\u0000' + password);

  38.         return ByteUtils.concat(authcid, passw);
  39.     }

  40.     @Override
  41.     public String getName() {
  42.         return NAME;
  43.     }

  44.     @Override
  45.     public int getPriority() {
  46.         return 410;
  47.     }

  48.     @Override
  49.     public SASLPlainMechanism newInstance() {
  50.         return new SASLPlainMechanism();
  51.     }

  52.     @Override
  53.     public void checkIfSuccessfulOrThrow() {
  54.         // No check performed
  55.     }

  56.     @Override
  57.     public boolean authzidSupported() {
  58.       return true;
  59.     }
  60. }