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.sasl.SASLMechanism;
022import org.jivesoftware.smack.util.StringUtils;
023
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) {
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() {
043        if (authorizationId != null) {
044          return toBytes(authorizationId.toString());
045        }
046
047        if (StringUtils.isNullOrEmpty(authenticationId)) {
048            return null;
049        }
050
051        return toBytes(XmppStringUtils.completeJidFrom(authenticationId, serviceName));
052    }
053
054    @Override
055    public String getName() {
056        return NAME;
057    }
058
059    @Override
060    public int getPriority() {
061        return 510;
062    }
063
064    @Override
065    protected SASLMechanism newInstance() {
066        return new SASLExternalMechanism();
067    }
068
069    @Override
070    public void checkIfSuccessfulOrThrow() {
071        // No check performed
072    }
073
074    @Override
075    public boolean authzidSupported() {
076      return true;
077    }
078
079    @Override
080    public boolean requiresPassword() {
081        return false;
082    }
083}