001/**
002 *
003 * Copyright 2014-2019 Florian Schmaus
004 *
005 * Licensed under the Apache License, Version 2.0 (the "License");
006 * you may not use this file except in compliance with the License.
007 * You may obtain a copy of the License at
008 *
009 *     http://www.apache.org/licenses/LICENSE-2.0
010 *
011 * Unless required by applicable law or agreed to in writing, software
012 * distributed under the License is distributed on an "AS IS" BASIS,
013 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014 * See the License for the specific language governing permissions and
015 * limitations under the License.
016 */
017package org.jivesoftware.smack.sasl.provided;
018
019import javax.security.auth.callback.CallbackHandler;
020
021import org.jivesoftware.smack.sasl.SASLMechanism;
022import org.jivesoftware.smack.util.ByteUtils;
023
024public class SASLPlainMechanism extends SASLMechanism {
025
026    public static final String NAME = PLAIN;
027
028    @Override
029    protected void authenticateInternal(CallbackHandler cbh) {
030        throw new UnsupportedOperationException("CallbackHandler not (yet) supported");
031    }
032
033    @Override
034    protected byte[] getAuthenticationText() {
035        // concatenate and encode username (authcid) and password
036        String authzid;
037        if (authorizationId == null) {
038          authzid = "";
039        } else {
040          authzid = authorizationId.toString();
041        }
042        byte[] authcid = toBytes(authzid + '\u0000' + authenticationId);
043        byte[] passw = toBytes('\u0000' + password);
044
045        return ByteUtils.concat(authcid, passw);
046    }
047
048    @Override
049    public String getName() {
050        return NAME;
051    }
052
053    @Override
054    public int getPriority() {
055        return 410;
056    }
057
058    @Override
059    public SASLPlainMechanism newInstance() {
060        return new SASLPlainMechanism();
061    }
062
063    @Override
064    public void checkIfSuccessfulOrThrow() {
065        // No check performed
066    }
067
068    @Override
069    public boolean authzidSupported() {
070      return true;
071    }
072}