SASLXOauth2Mechanism.java

  1. /**
  2.  *
  3.  * Copyright 2014-2019 Florian Schmaus
  4.  *
  5.  * Licensed under the Apache License, Version 2.0 (the "License");
  6.  * you may not use this file except in compliance with the License.
  7.  * You may obtain a copy of the License at
  8.  *
  9.  *     http://www.apache.org/licenses/LICENSE-2.0
  10.  *
  11.  * Unless required by applicable law or agreed to in writing, software
  12.  * distributed under the License is distributed on an "AS IS" BASIS,
  13.  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  14.  * See the License for the specific language governing permissions and
  15.  * limitations under the License.
  16.  */
  17. package org.jivesoftware.smack.sasl.core;

  18. import javax.security.auth.callback.CallbackHandler;

  19. import org.jivesoftware.smack.sasl.SASLMechanism;

  20. /**
  21.  * The SASL X-OAUTH2 mechanism as described in <a
  22.  * href="https://developers.google.com/talk/jep_extensions/oauth">https://developers.google
  23.  * .com/talk/jep_extensions/oauth</a>
  24.  * <p>
  25.  * The given password will be used as OAUTH token.
  26.  * </p>
  27.  * <p>
  28.  * Note that X-OAUTH2 is experimental in Smack. This is because Google defined, besides being a bad practice (XEP-134),
  29.  * custom attributes to the 'auth' stanza, as can be seen here
  30.  * </p>
  31.  *
  32.  * <pre>
  33.  * {@code
  34.  * <auth xmlns="urn:ietf:params:xml:ns:xmpp-sasl" mechanism="X-OAUTH2"
  35.  *    auth:service="chromiumsync" auth:allow-generated-jid="true"
  36.  *    auth:client-uses-full-bind-result="true" xmlns:auth="http://www.google.com/talk/protocol/auth">
  37.  * }
  38.  * </pre>
  39.  *
  40.  * from https://developers.google.com/cloud-print/docs/rawxmpp and here
  41.  *
  42.  * <pre>
  43.  * {@code
  44.  * <auth xmlns="urn:ietf:params:xml:ns:xmpp-sasl"
  45.  *   mechanism="X-OAUTH2"
  46.  *   auth:service="oauth2"
  47.  *   xmlns:auth="http://www.google.com/talk/protocol/auth">
  48.  * base64("\0" + user_name + "\0" + oauth_token)
  49.  * </auth>
  50.  * }
  51.  * </pre>
  52.  *
  53.  * from https://developers.google.com/talk/jep_extensions/oauth
  54.  * <p>
  55.  * Those attribute extensions are currently not supported by Smack, and it's unclear how it affects authorization and
  56.  * how widely they are used.
  57.  * </p>
  58.  */
  59. public class SASLXOauth2Mechanism extends SASLMechanism {

  60.     public static final String NAME = "X-OAUTH2";

  61.     @Override
  62.     protected void authenticateInternal(CallbackHandler cbh) {
  63.         throw new UnsupportedOperationException("CallbackHandler not (yet) supported");
  64.     }

  65.     @Override
  66.     protected byte[] getAuthenticationText() {
  67.         // Note that base64 encoding is done in SASLMechanism for the bytes return by getAuthenticationText().
  68.         return toBytes('\u0000' + authenticationId + '\u0000' + password);
  69.     }

  70.     @Override
  71.     public String getName() {
  72.         return NAME;
  73.     }

  74.     @Override
  75.     public int getPriority() {
  76.         // Same priority as SASL PLAIN
  77.         return 410;
  78.     }

  79.     @Override
  80.     public SASLXOauth2Mechanism newInstance() {
  81.         return new SASLXOauth2Mechanism();
  82.     }

  83.     @Override
  84.     public void checkIfSuccessfulOrThrow() {
  85.         // No check performed
  86.     }
  87. }