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