001/**
002 *
003 * Copyright 2014 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.SmackException;
022import org.jivesoftware.smack.sasl.SASLMechanism;
023import org.jivesoftware.smack.util.ByteUtils;
024
025public class SASLPlainMechanism extends SASLMechanism {
026
027    public static final String NAME = PLAIN;
028
029    @Override
030    protected void authenticateInternal(CallbackHandler cbh) throws SmackException {
031        throw new UnsupportedOperationException("CallbackHandler not (yet) supported");
032    }
033
034    @Override
035    protected byte[] getAuthenticationText() throws SmackException {
036        // concatenate and encode username (authcid) and password
037        String authzid;
038        if (authorizationId == null) {
039          authzid = "";
040        } else {
041          authzid = authorizationId.toString();
042        }
043        byte[] authcid = toBytes(authzid + '\u0000' + authenticationId);
044        byte[] passw = toBytes('\u0000' + password);
045
046        return ByteUtils.concat(authcid, passw);
047    }
048
049    @Override
050    public String getName() {
051        return NAME;
052    }
053
054    @Override
055    public int getPriority() {
056        return 410;
057    }
058
059    @Override
060    public SASLPlainMechanism newInstance() {
061        return new SASLPlainMechanism();
062    }
063
064    @Override
065    public void checkIfSuccessfulOrThrow() throws SmackException {
066        // No check performed
067    }
068
069    @Override
070    public boolean authzidSupported() {
071      return true;
072    }
073}