aboutsummaryrefslogtreecommitdiffstats
path: root/protocols/jabber
diff options
context:
space:
mode:
authorWilmer van der Gaast <wilmer@google.com>2011-07-26 12:58:38 +0100
committerWilmer van der Gaast <wilmer@google.com>2011-07-26 12:58:38 +0100
commit4a5d88504235e1df5d01a3a5701b83dd82d6695d (patch)
tree95eda026f565bc43ad8aea62470d710ba3245369 /protocols/jabber
parent59c9adb4e3e27ded95ec9ccee4f57cf79490bd12 (diff)
Working OAuth2 support. Needs some more debugging (error handling is not
great and imc_logout() gets (rightfully) confused when jabber_data is empty).
Diffstat (limited to 'protocols/jabber')
-rw-r--r--protocols/jabber/jabber.c41
-rw-r--r--protocols/jabber/jabber.h8
-rw-r--r--protocols/jabber/sasl.c111
3 files changed, 151 insertions, 9 deletions
diff --git a/protocols/jabber/jabber.c b/protocols/jabber/jabber.c
index bd9bbe23..64858de2 100644
--- a/protocols/jabber/jabber.c
+++ b/protocols/jabber/jabber.c
@@ -97,9 +97,7 @@ static void jabber_login( account_t *acc )
{
struct im_connection *ic = imcb_new( acc );
struct jabber_data *jd = g_new0( struct jabber_data, 1 );
- struct ns_srv_reply **srvl = NULL, *srv = NULL;
- char *connect_to, *s;
- int i;
+ char *s;
/* For now this is needed in the _connected() handlers if using
GLib event handling, to make sure we're not handling events
@@ -139,6 +137,30 @@ static void jabber_login( account_t *acc )
jd->node_cache = g_hash_table_new_full( g_str_hash, g_str_equal, NULL, jabber_cache_entry_free );
jd->buddies = g_hash_table_new( g_str_hash, g_str_equal );
+ if( set_getbool( &acc->set, "oauth" ) )
+ {
+ /* For the first login with OAuth, we have to authenticate via the browser.
+ For subsequent logins, exchange the refresh token for a valid access
+ token (even though the last one maybe didn't expire yet). */
+ if( strncmp( acc->pass, "refresh_token=", 14 ) != 0 )
+ sasl_oauth2_init( ic );
+ else
+ sasl_oauth2_refresh( ic, acc->pass + 14 );
+ }
+ else
+ jabber_connect( ic );
+}
+
+/* Separate this from jabber_login() so we can do OAuth first if necessary.
+ Putting this in io.c would probably be more correct. */
+void jabber_connect( struct im_connection *ic )
+{
+ account_t *acc = ic->acc;
+ struct jabber_data *jd = ic->proto_data;
+ int i;
+ char *connect_to;
+ struct ns_srv_reply **srvl = NULL, *srv = NULL;
+
/* Figure out the hostname to connect to. */
if( acc->server && *acc->server )
connect_to = acc->server;
@@ -280,6 +302,19 @@ static int jabber_buddy_msg( struct im_connection *ic, char *who, char *message,
if( g_strcasecmp( who, JABBER_XMLCONSOLE_HANDLE ) == 0 )
return jabber_write( ic, message, strlen( message ) );
+ if( g_strcasecmp( who, "jabber_oauth" ) == 0 )
+ {
+ if( sasl_oauth2_get_refresh_token( ic, message ) )
+ {
+ return 1;
+ }
+ else
+ {
+ imcb_error( ic, "OAuth failure" );
+ imc_logout( ic, TRUE );
+ }
+ }
+
if( ( s = strchr( who, '=' ) ) && jabber_chat_by_jid( ic, s + 1 ) )
bud = jabber_buddy_by_ext_jid( ic, who, 0 );
else
diff --git a/protocols/jabber/jabber.h b/protocols/jabber/jabber.h
index adf9a291..8d65a7e3 100644
--- a/protocols/jabber/jabber.h
+++ b/protocols/jabber/jabber.h
@@ -92,6 +92,8 @@ struct jabber_data
char *username; /* USERNAME@server */
char *server; /* username@SERVER -=> server/domain, not hostname */
+ char *oauth2_access_token;
+
/* After changing one of these two (or the priority setting), call
presence_send_update() to inform the server about the changes. */
const struct jabber_away_state *away_state;
@@ -231,6 +233,9 @@ struct jabber_transfer
#define XMLNS_BYTESTREAMS "http://jabber.org/protocol/bytestreams" /* XEP-0065 */
#define XMLNS_IBB "http://jabber.org/protocol/ibb" /* XEP-0047 */
+/* jabber.c */
+void jabber_connect( struct im_connection *ic );
+
/* iq.c */
xt_status jabber_pkt_iq( struct xt_node *node, gpointer data );
int jabber_init_iq_auth( struct im_connection *ic );
@@ -315,6 +320,9 @@ xt_status sasl_pkt_mechanisms( struct xt_node *node, gpointer data );
xt_status sasl_pkt_challenge( struct xt_node *node, gpointer data );
xt_status sasl_pkt_result( struct xt_node *node, gpointer data );
gboolean sasl_supported( struct im_connection *ic );
+void sasl_oauth2_init( struct im_connection *ic );
+int sasl_oauth2_get_refresh_token( struct im_connection *ic, const char *msg );
+int sasl_oauth2_refresh( struct im_connection *ic, const char *refresh_token );
/* conference.c */
struct groupchat *jabber_chat_join( struct im_connection *ic, const char *room, const char *nick, const char *password );
diff --git a/protocols/jabber/sasl.c b/protocols/jabber/sasl.c
index 0bbbae11..a7c3dd6f 100644
--- a/protocols/jabber/sasl.c
+++ b/protocols/jabber/sasl.c
@@ -75,13 +75,31 @@ xt_status sasl_pkt_mechanisms( struct xt_node *node, gpointer data )
reply = xt_new_node( "auth", NULL, NULL );
xt_add_attr( reply, "xmlns", XMLNS_SASL );
- if( sup_oauth2 && set_getbool( &ic->acc->set, "oauth" ) )
+ if( set_getbool( &ic->acc->set, "oauth" ) )
{
- imcb_log( ic, "Open this URL in your browser to authenticate: %s",
- oauth2_url( &oauth2_service_google,
- "https://www.googleapis.com/auth/googletalk" ) );
- xt_free_node( reply );
- reply = NULL;
+ int len;
+
+ if( !sup_oauth2 )
+ {
+ imcb_error( ic, "OAuth requested, but not supported by server" );
+ imc_logout( ic, FALSE );
+ xt_free_node( reply );
+ return XT_ABORT;
+ }
+
+ /* X-OAUTH2 is, not *the* standard OAuth2 SASL/XMPP implementation.
+ It's currently used by GTalk and vaguely documented on
+ http://code.google.com/apis/cloudprint/docs/rawxmpp.html . */
+ xt_add_attr( reply, "mechanism", "X-OAUTH2" );
+
+ len = strlen( jd->username ) + strlen( jd->oauth2_access_token ) + 2;
+ s = g_malloc( len + 1 );
+ s[0] = 0;
+ strcpy( s + 1, jd->username );
+ strcpy( s + 2 + strlen( jd->username ), jd->oauth2_access_token );
+ reply->text = base64_encode( (unsigned char *)s, len );
+ reply->text_len = strlen( reply->text );
+ g_free( s );
}
else if( sup_digest )
{
@@ -357,3 +375,84 @@ gboolean sasl_supported( struct im_connection *ic )
return ( jd->xt && jd->xt->root && xt_find_attr( jd->xt->root, "version" ) ) != 0;
}
+
+void sasl_oauth2_init( struct im_connection *ic )
+{
+ char *msg, *url;
+
+ imcb_log( ic, "Starting OAuth authentication" );
+
+ /* Temporary contact, just used to receive the OAuth response. */
+ imcb_add_buddy( ic, "jabber_oauth", NULL );
+ url = oauth2_url( &oauth2_service_google,
+ "https://www.googleapis.com/auth/googletalk" );
+ msg = g_strdup_printf( "Open this URL in your browser to authenticate: %s", url );
+ imcb_buddy_msg( ic, "jabber_oauth", msg, 0, 0 );
+ imcb_buddy_msg( ic, "jabber_oauth", "Respond to this message with the returned "
+ "authorization token.", 0, 0 );
+
+ g_free( msg );
+ g_free( url );
+}
+
+static gboolean sasl_oauth2_remove_contact( gpointer data, gint fd, b_input_condition cond )
+{
+ struct im_connection *ic = data;
+ imcb_remove_buddy( ic, "jabber_oauth", NULL );
+ return FALSE;
+}
+
+static void sasl_oauth2_got_token( gpointer data, const char *access_token, const char *refresh_token );
+
+int sasl_oauth2_get_refresh_token( struct im_connection *ic, const char *msg )
+{
+ char *code;
+ int ret;
+
+ imcb_log( ic, "Requesting OAuth access token" );
+
+ /* Don't do it here because the caller may get confused if the contact
+ we're currently sending a message to is deleted. */
+ b_timeout_add( 1, sasl_oauth2_remove_contact, ic );
+
+ code = g_strdup( msg );
+ g_strstrip( code );
+ ret = oauth2_access_token( &oauth2_service_google, OAUTH2_AUTH_CODE,
+ code, sasl_oauth2_got_token, ic );
+
+ g_free( code );
+ return ret;
+}
+
+int sasl_oauth2_refresh( struct im_connection *ic, const char *refresh_token )
+{
+ return oauth2_access_token( &oauth2_service_google, OAUTH2_AUTH_REFRESH,
+ refresh_token, sasl_oauth2_got_token, ic );
+}
+
+static void sasl_oauth2_got_token( gpointer data, const char *access_token, const char *refresh_token )
+{
+ struct im_connection *ic = data;
+ struct jabber_data *jd;
+
+ if( g_slist_find( jabber_connections, ic ) == NULL )
+ return;
+
+ jd = ic->proto_data;
+
+ if( access_token == NULL )
+ {
+ imcb_error( ic, "OAuth failure (missing access token)" );
+ imc_logout( ic, TRUE );
+ }
+ if( refresh_token != NULL )
+ {
+ g_free( ic->acc->pass );
+ ic->acc->pass = g_strdup_printf( "refresh_token=%s", refresh_token );
+ }
+
+ g_free( jd->oauth2_access_token );
+ jd->oauth2_access_token = g_strdup( access_token );
+
+ jabber_connect( ic );
+}