001/**
002 *
003 * Copyright 2014-2016 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.core;
018
019import javax.security.auth.callback.CallbackHandler;
020
021import org.jivesoftware.smack.SmackException;
022import org.jivesoftware.smack.sasl.SASLMechanism;
023
024/**
025 * The SASL X-OAUTH2 mechanism as described in <a
026 * href="https://developers.google.com/talk/jep_extensions/oauth">https://developers.google
027 * .com/talk/jep_extensions/oauth</a>
028 * <p>
029 * The given password will be used as OAUTH token.
030 * </p>
031 * <p>
032 * Note that X-OAUTH2 is experimental in Smack. This is because Google defined, besides being a bad practice (XEP-134),
033 * custom attributes to the 'auth' stanza, as can be seen here
034 * </p>
035 *
036 * <pre>
037 * {@code
038 * <auth xmlns="urn:ietf:params:xml:ns:xmpp-sasl" mechanism="X-OAUTH2"
039 *    auth:service="chromiumsync" auth:allow-generated-jid="true"
040 *    auth:client-uses-full-bind-result="true" xmlns:auth="http://www.google.com/talk/protocol/auth">
041 * }
042 * </pre>
043 *
044 * from https://developers.google.com/cloud-print/docs/rawxmpp and here
045 *
046 * <pre>
047 * {@code
048 * <auth xmlns="urn:ietf:params:xml:ns:xmpp-sasl"
049 *   mechanism="X-OAUTH2"
050 *   auth:service="oauth2"
051 *   xmlns:auth="http://www.google.com/talk/protocol/auth">
052 * base64("\0" + user_name + "\0" + oauth_token)
053 * </auth>
054 * }
055 * </pre>
056 *
057 * from https://developers.google.com/talk/jep_extensions/oauth
058 * <p>
059 * Those attribute extensions are currently not supported by Smack, and it's unclear how it affects authorization and
060 * how widely they are used.
061 * </p>
062 */
063public class SASLXOauth2Mechanism extends SASLMechanism {
064
065    public static final String NAME = "X-OAUTH2";
066
067    @Override
068    protected void authenticateInternal(CallbackHandler cbh) throws SmackException {
069        throw new UnsupportedOperationException("CallbackHandler not (yet) supported");
070    }
071
072    @Override
073    protected byte[] getAuthenticationText() throws SmackException {
074        // Note that base64 encoding is done in SASLMechanism for the bytes return by getAuthenticationText().
075        return toBytes('\u0000' + authenticationId + '\u0000' + password);
076    }
077
078    @Override
079    public String getName() {
080        return NAME;
081    }
082
083    @Override
084    public int getPriority() {
085        // Same priority as SASL PLAIN
086        return 410;
087    }
088
089    @Override
090    public SASLXOauth2Mechanism newInstance() {
091        return new SASLXOauth2Mechanism();
092    }
093
094    @Override
095    public void checkIfSuccessfulOrThrow() throws SmackException {
096        // No check performed
097    }
098}