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