SASLXOauth2Mechanism.java

  1. /**
  2.  *
  3.  * Copyright 2014-2015 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.SmackException;
  20. import org.jivesoftware.smack.sasl.SASLMechanism;
  21. import org.jivesoftware.smack.util.stringencoder.Base64;

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

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

  63.     @Override
  64.     protected void authenticateInternal(CallbackHandler cbh) throws SmackException {
  65.         throw new UnsupportedOperationException("CallbackHandler not (yet) supported");
  66.     }

  67.     @Override
  68.     protected byte[] getAuthenticationText() throws SmackException {
  69.         // base64("\0" + user_name + "\0" + oauth_token)
  70.         return Base64.encode(toBytes('\u0000' + authenticationId + '\u0000' + password));
  71.     }

  72.     @Override
  73.     public String getName() {
  74.         return NAME;
  75.     }

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

  81.     @Override
  82.     public SASLXOauth2Mechanism newInstance() {
  83.         return new SASLXOauth2Mechanism();
  84.     }

  85.     @Override
  86.     public void checkIfSuccessfulOrThrow() throws SmackException {
  87.         // No check performed
  88.     }
  89. }