001/**
002 *
003 * Copyright © 2014 Daniele Ricci
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.StringUtils;
024import org.jxmpp.util.XmppStringUtils;
025
026/**
027 * Implementation of the SASL-EXTERNAL authentication mechanism
028 * 
029 * @author Daniele Ricci
030 */
031public class SASLExternalMechanism extends SASLMechanism {
032
033    public static final String NAME = EXTERNAL;
034
035    @Override
036    protected void authenticateInternal(CallbackHandler cbh) throws SmackException {
037        // Do nothing. Auth will be done external to Smack and which will receive the localpart
038        // after the resource binding
039    }
040
041    @Override
042    protected byte[] getAuthenticationText() throws SmackException {
043        if (StringUtils.isNullOrEmpty(authenticationId)) {
044            return null;
045        }
046
047        return toBytes(XmppStringUtils.completeJidFrom(authenticationId, serviceName));
048    }
049
050    @Override
051    public String getName() {
052        return NAME;
053    }
054
055    @Override
056    public int getPriority() {
057        return 510;
058    }
059
060    @Override
061    protected SASLMechanism newInstance() {
062        return new SASLExternalMechanism();
063    }
064
065    @Override
066    public void checkIfSuccessfulOrThrow() throws SmackException {
067        // No check performed
068    }
069
070}