[5997488] | 1 | /***************************************************************************\ |
---|
| 2 | * * |
---|
| 3 | * BitlBee - An IRC to IM gateway * |
---|
| 4 | * Jabber module - SASL authentication * |
---|
| 5 | * * |
---|
| 6 | * Copyright 2006 Wilmer van der Gaast <wilmer@gaast.net> * |
---|
| 7 | * * |
---|
| 8 | * This program is free software; you can redistribute it and/or modify * |
---|
| 9 | * it under the terms of the GNU General Public License as published by * |
---|
| 10 | * the Free Software Foundation; either version 2 of the License, or * |
---|
| 11 | * (at your option) any later version. * |
---|
| 12 | * * |
---|
| 13 | * This program is distributed in the hope that it will be useful, * |
---|
| 14 | * but WITHOUT ANY WARRANTY; without even the implied warranty of * |
---|
| 15 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * |
---|
| 16 | * GNU General Public License for more details. * |
---|
| 17 | * * |
---|
| 18 | * You should have received a copy of the GNU General Public License along * |
---|
| 19 | * with this program; if not, write to the Free Software Foundation, Inc., * |
---|
| 20 | * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. * |
---|
| 21 | * * |
---|
| 22 | \***************************************************************************/ |
---|
| 23 | |
---|
[af97b23] | 24 | #include <ctype.h> |
---|
| 25 | |
---|
[5997488] | 26 | #include "jabber.h" |
---|
| 27 | #include "base64.h" |
---|
[57b4525] | 28 | #include "oauth2.h" |
---|
[e1c926f] | 29 | #include "oauth.h" |
---|
[5997488] | 30 | |
---|
[18c6d36] | 31 | const struct oauth2_service oauth2_service_google = |
---|
| 32 | { |
---|
| 33 | "https://accounts.google.com/o/oauth2/auth", |
---|
| 34 | "https://accounts.google.com/o/oauth2/token", |
---|
| 35 | "urn:ietf:wg:oauth:2.0:oob", |
---|
| 36 | "https://www.googleapis.com/auth/googletalk", |
---|
| 37 | "783993391592.apps.googleusercontent.com", |
---|
| 38 | "6C-Zgf7Tr7gEQTPlBhMUgo7R", |
---|
| 39 | }; |
---|
| 40 | const struct oauth2_service oauth2_service_facebook = |
---|
| 41 | { |
---|
| 42 | "https://www.facebook.com/dialog/oauth", |
---|
| 43 | "https://graph.facebook.com/oauth/access_token", |
---|
| 44 | "http://www.bitlbee.org/main.php/Facebook/oauth2.html", |
---|
| 45 | "offline_access,xmpp_login", |
---|
| 46 | "126828914005625", |
---|
| 47 | "4b100f0f244d620bf3f15f8b217d4c32", |
---|
| 48 | }; |
---|
| 49 | const struct oauth2_service oauth2_service_mslive = |
---|
| 50 | { |
---|
| 51 | "https://oauth.live.com/authorize", |
---|
| 52 | "https://oauth.live.com/token", |
---|
| 53 | "http://www.bitlbee.org/main.php/Messenger/oauth2.html", |
---|
[64b6635] | 54 | "wl.offline_access%20wl.messenger", |
---|
[18c6d36] | 55 | "000000004C06FCD1", |
---|
| 56 | "IRKlBPzJJAWcY-TbZjiTEJu9tn7XCFaV", |
---|
| 57 | }; |
---|
| 58 | |
---|
[5997488] | 59 | xt_status sasl_pkt_mechanisms( struct xt_node *node, gpointer data ) |
---|
| 60 | { |
---|
[0da65d5] | 61 | struct im_connection *ic = data; |
---|
| 62 | struct jabber_data *jd = ic->proto_data; |
---|
[5997488] | 63 | struct xt_node *c, *reply; |
---|
| 64 | char *s; |
---|
[18c6d36] | 65 | int sup_plain = 0, sup_digest = 0, sup_gtalk = 0, sup_fb = 0, sup_ms = 0; |
---|
| 66 | int want_oauth = FALSE; |
---|
[4be0e34] | 67 | GString *mechs; |
---|
[5997488] | 68 | |
---|
[0da65d5] | 69 | if( !sasl_supported( ic ) ) |
---|
[8d74291] | 70 | { |
---|
| 71 | /* Should abort this now, since we should already be doing |
---|
| 72 | IQ authentication. Strange things happen when you try |
---|
| 73 | to do both... */ |
---|
[84b045d] | 74 | imcb_log( ic, "XMPP 1.0 non-compliant server seems to support SASL, please report this as a BitlBee bug!" ); |
---|
[8d74291] | 75 | return XT_HANDLED; |
---|
| 76 | } |
---|
| 77 | |
---|
[5997488] | 78 | s = xt_find_attr( node, "xmlns" ); |
---|
[47d3ac4] | 79 | if( !s || strcmp( s, XMLNS_SASL ) != 0 ) |
---|
[5997488] | 80 | { |
---|
[84b045d] | 81 | imcb_log( ic, "Stream error while authenticating" ); |
---|
[c2fb3809] | 82 | imc_logout( ic, FALSE ); |
---|
[5997488] | 83 | return XT_ABORT; |
---|
| 84 | } |
---|
| 85 | |
---|
[4be0e34] | 86 | mechs = g_string_new( "" ); |
---|
[5997488] | 87 | c = node->children; |
---|
| 88 | while( ( c = xt_find_node( c, "mechanism" ) ) ) |
---|
| 89 | { |
---|
| 90 | if( c->text && g_strcasecmp( c->text, "PLAIN" ) == 0 ) |
---|
| 91 | sup_plain = 1; |
---|
[64b6635] | 92 | else if( c->text && g_strcasecmp( c->text, "DIGEST-MD5" ) == 0 ) |
---|
[5997488] | 93 | sup_digest = 1; |
---|
[64b6635] | 94 | else if( c->text && g_strcasecmp( c->text, "X-OAUTH2" ) == 0 ) |
---|
[18c6d36] | 95 | sup_gtalk = 1; |
---|
[64b6635] | 96 | else if( c->text && g_strcasecmp( c->text, "X-FACEBOOK-PLATFORM" ) == 0 ) |
---|
[e1c926f] | 97 | sup_fb = 1; |
---|
[64b6635] | 98 | else if( c->text && g_strcasecmp( c->text, "X-MESSENGER-OAUTH2" ) == 0 ) |
---|
[18c6d36] | 99 | sup_ms = 1; |
---|
[5997488] | 100 | |
---|
[4be0e34] | 101 | if( c->text ) |
---|
| 102 | g_string_append_printf( mechs, " %s", c->text ); |
---|
| 103 | |
---|
[5997488] | 104 | c = c->next; |
---|
| 105 | } |
---|
| 106 | |
---|
[18c6d36] | 107 | if( !sup_plain && !sup_digest && !sup_gtalk && !sup_fb && !sup_ms ) |
---|
[5997488] | 108 | { |
---|
[4be0e34] | 109 | imcb_error( ic, "BitlBee does not support any of the offered SASL " |
---|
| 110 | "authentication schemes:%s", mechs->str ); |
---|
[c2fb3809] | 111 | imc_logout( ic, FALSE ); |
---|
[4be0e34] | 112 | g_string_free( mechs, TRUE ); |
---|
[5997488] | 113 | return XT_ABORT; |
---|
| 114 | } |
---|
[4be0e34] | 115 | g_string_free( mechs, TRUE ); |
---|
[5997488] | 116 | |
---|
| 117 | reply = xt_new_node( "auth", NULL, NULL ); |
---|
[47d3ac4] | 118 | xt_add_attr( reply, "xmlns", XMLNS_SASL ); |
---|
[18c6d36] | 119 | want_oauth = set_getbool( &ic->acc->set, "oauth" ); |
---|
[5997488] | 120 | |
---|
[18c6d36] | 121 | if( sup_gtalk && want_oauth ) |
---|
[57b4525] | 122 | { |
---|
[4a5d885] | 123 | int len; |
---|
| 124 | |
---|
| 125 | /* X-OAUTH2 is, not *the* standard OAuth2 SASL/XMPP implementation. |
---|
| 126 | It's currently used by GTalk and vaguely documented on |
---|
| 127 | http://code.google.com/apis/cloudprint/docs/rawxmpp.html . */ |
---|
| 128 | xt_add_attr( reply, "mechanism", "X-OAUTH2" ); |
---|
| 129 | |
---|
| 130 | len = strlen( jd->username ) + strlen( jd->oauth2_access_token ) + 2; |
---|
| 131 | s = g_malloc( len + 1 ); |
---|
| 132 | s[0] = 0; |
---|
| 133 | strcpy( s + 1, jd->username ); |
---|
| 134 | strcpy( s + 2 + strlen( jd->username ), jd->oauth2_access_token ); |
---|
| 135 | reply->text = base64_encode( (unsigned char *)s, len ); |
---|
| 136 | reply->text_len = strlen( reply->text ); |
---|
| 137 | g_free( s ); |
---|
[57b4525] | 138 | } |
---|
[18c6d36] | 139 | else if( sup_ms && want_oauth ) |
---|
| 140 | { |
---|
| 141 | xt_add_attr( reply, "mechanism", "X-MESSENGER-OAUTH2" ); |
---|
| 142 | reply->text = g_strdup( jd->oauth2_access_token ); |
---|
| 143 | reply->text_len = strlen( jd->oauth2_access_token ); |
---|
| 144 | } |
---|
[64b6635] | 145 | else if( sup_fb && want_oauth ) |
---|
[e1c926f] | 146 | { |
---|
| 147 | xt_add_attr( reply, "mechanism", "X-FACEBOOK-PLATFORM" ); |
---|
| 148 | jd->flags |= JFLAG_SASL_FB; |
---|
| 149 | } |
---|
[18c6d36] | 150 | else if( want_oauth ) |
---|
| 151 | { |
---|
| 152 | imcb_error( ic, "OAuth requested, but not supported by server" ); |
---|
| 153 | imc_logout( ic, FALSE ); |
---|
| 154 | xt_free_node( reply ); |
---|
| 155 | return XT_ABORT; |
---|
| 156 | } |
---|
[57b4525] | 157 | else if( sup_digest ) |
---|
[fe7a554] | 158 | { |
---|
| 159 | xt_add_attr( reply, "mechanism", "DIGEST-MD5" ); |
---|
| 160 | |
---|
| 161 | /* The rest will be done later, when we receive a <challenge/>. */ |
---|
| 162 | } |
---|
| 163 | else if( sup_plain ) |
---|
[5997488] | 164 | { |
---|
| 165 | int len; |
---|
| 166 | |
---|
| 167 | xt_add_attr( reply, "mechanism", "PLAIN" ); |
---|
| 168 | |
---|
| 169 | /* With SASL PLAIN in XMPP, the text should be b64(\0user\0pass) */ |
---|
[0da65d5] | 170 | len = strlen( jd->username ) + strlen( ic->acc->pass ) + 2; |
---|
[5997488] | 171 | s = g_malloc( len + 1 ); |
---|
| 172 | s[0] = 0; |
---|
| 173 | strcpy( s + 1, jd->username ); |
---|
[0da65d5] | 174 | strcpy( s + 2 + strlen( jd->username ), ic->acc->pass ); |
---|
[3b6eadc] | 175 | reply->text = base64_encode( (unsigned char *)s, len ); |
---|
[5997488] | 176 | reply->text_len = strlen( reply->text ); |
---|
| 177 | g_free( s ); |
---|
| 178 | } |
---|
| 179 | |
---|
[57b4525] | 180 | if( reply && !jabber_write_packet( ic, reply ) ) |
---|
[5997488] | 181 | { |
---|
| 182 | xt_free_node( reply ); |
---|
| 183 | return XT_ABORT; |
---|
| 184 | } |
---|
| 185 | xt_free_node( reply ); |
---|
| 186 | |
---|
| 187 | /* To prevent classic authentication from happening. */ |
---|
| 188 | jd->flags |= JFLAG_STREAM_STARTED; |
---|
| 189 | |
---|
| 190 | return XT_HANDLED; |
---|
| 191 | } |
---|
| 192 | |
---|
[af97b23] | 193 | /* Non-static function, but not mentioned in jabber.h because it's for internal |
---|
| 194 | use, just that the unittest should be able to reach it... */ |
---|
| 195 | char *sasl_get_part( char *data, char *field ) |
---|
[d8e0484] | 196 | { |
---|
| 197 | int i, len; |
---|
| 198 | |
---|
| 199 | len = strlen( field ); |
---|
| 200 | |
---|
[af97b23] | 201 | while( isspace( *data ) || *data == ',' ) |
---|
| 202 | data ++; |
---|
| 203 | |
---|
[d8e0484] | 204 | if( g_strncasecmp( data, field, len ) == 0 && data[len] == '=' ) |
---|
| 205 | { |
---|
| 206 | i = strlen( field ) + 1; |
---|
| 207 | } |
---|
| 208 | else |
---|
| 209 | { |
---|
| 210 | for( i = 0; data[i]; i ++ ) |
---|
| 211 | { |
---|
| 212 | /* If we have a ", skip until it's closed again. */ |
---|
| 213 | if( data[i] == '"' ) |
---|
| 214 | { |
---|
| 215 | i ++; |
---|
| 216 | while( data[i] != '"' || data[i-1] == '\\' ) |
---|
| 217 | i ++; |
---|
| 218 | } |
---|
| 219 | |
---|
[af97b23] | 220 | /* If we got a comma, we got a new field. Check it, |
---|
| 221 | find the next key after it. */ |
---|
| 222 | if( data[i] == ',' ) |
---|
[d8e0484] | 223 | { |
---|
[af97b23] | 224 | while( isspace( data[i] ) || data[i] == ',' ) |
---|
| 225 | i ++; |
---|
| 226 | |
---|
| 227 | if( g_strncasecmp( data + i, field, len ) == 0 && |
---|
| 228 | data[i+len] == '=' ) |
---|
| 229 | { |
---|
| 230 | i += len + 1; |
---|
| 231 | break; |
---|
| 232 | } |
---|
[d8e0484] | 233 | } |
---|
| 234 | } |
---|
| 235 | } |
---|
| 236 | |
---|
| 237 | if( data[i] == '"' ) |
---|
| 238 | { |
---|
| 239 | int j; |
---|
| 240 | char *ret; |
---|
| 241 | |
---|
| 242 | i ++; |
---|
| 243 | len = 0; |
---|
| 244 | while( data[i+len] != '"' || data[i+len-1] == '\\' ) |
---|
| 245 | len ++; |
---|
| 246 | |
---|
| 247 | ret = g_strndup( data + i, len ); |
---|
| 248 | for( i = j = 0; ret[i]; i ++ ) |
---|
| 249 | { |
---|
| 250 | if( ret[i] == '\\' ) |
---|
| 251 | { |
---|
| 252 | ret[j++] = ret[++i]; |
---|
| 253 | } |
---|
| 254 | else |
---|
| 255 | { |
---|
| 256 | ret[j++] = ret[i]; |
---|
| 257 | } |
---|
| 258 | } |
---|
| 259 | ret[j] = 0; |
---|
| 260 | |
---|
| 261 | return ret; |
---|
| 262 | } |
---|
| 263 | else if( data[i] ) |
---|
| 264 | { |
---|
| 265 | len = 0; |
---|
| 266 | while( data[i+len] && data[i+len] != ',' ) |
---|
| 267 | len ++; |
---|
| 268 | |
---|
| 269 | return g_strndup( data + i, len ); |
---|
| 270 | } |
---|
| 271 | else |
---|
| 272 | { |
---|
| 273 | return NULL; |
---|
| 274 | } |
---|
| 275 | } |
---|
| 276 | |
---|
[5997488] | 277 | xt_status sasl_pkt_challenge( struct xt_node *node, gpointer data ) |
---|
| 278 | { |
---|
[0da65d5] | 279 | struct im_connection *ic = data; |
---|
| 280 | struct jabber_data *jd = ic->proto_data; |
---|
[f138bd2] | 281 | struct xt_node *reply_pkt = NULL; |
---|
[3b6eadc] | 282 | char *nonce = NULL, *realm = NULL, *cnonce = NULL; |
---|
| 283 | unsigned char cnonce_bin[30]; |
---|
[d8e0484] | 284 | char *digest_uri = NULL; |
---|
| 285 | char *dec = NULL; |
---|
[f138bd2] | 286 | char *s = NULL, *reply = NULL; |
---|
[d8e0484] | 287 | xt_status ret = XT_ABORT; |
---|
| 288 | |
---|
| 289 | if( node->text_len == 0 ) |
---|
| 290 | goto error; |
---|
| 291 | |
---|
| 292 | dec = frombase64( node->text ); |
---|
| 293 | |
---|
[e1c926f] | 294 | if( jd->flags & JFLAG_SASL_FB ) |
---|
| 295 | { |
---|
[f138bd2] | 296 | /* Facebook proprietary authentication. Not as useful as it seemed, but |
---|
| 297 | the code's written now, may as well keep it.. |
---|
| 298 | |
---|
| 299 | Mechanism is described on http://developers.facebook.com/docs/chat/ |
---|
| 300 | and in their Python module. It's all mostly useless because the tokens |
---|
| 301 | expire after 24h. */ |
---|
[64b6635] | 302 | GSList *p_in = NULL, *p_out = NULL; |
---|
| 303 | char time[33]; |
---|
[e1c926f] | 304 | |
---|
| 305 | oauth_params_parse( &p_in, dec ); |
---|
| 306 | oauth_params_add( &p_out, "nonce", oauth_params_get( &p_in, "nonce" ) ); |
---|
| 307 | oauth_params_add( &p_out, "method", oauth_params_get( &p_in, "method" ) ); |
---|
| 308 | oauth_params_free( &p_in ); |
---|
| 309 | |
---|
| 310 | g_snprintf( time, sizeof( time ), "%lld", (long long) ( gettime() * 1000 ) ); |
---|
| 311 | oauth_params_add( &p_out, "call_id", time ); |
---|
| 312 | oauth_params_add( &p_out, "api_key", oauth2_service_facebook.consumer_key ); |
---|
| 313 | oauth_params_add( &p_out, "v", "1.0" ); |
---|
| 314 | oauth_params_add( &p_out, "format", "XML" ); |
---|
[64b6635] | 315 | oauth_params_add( &p_out, "access_token", jd->oauth2_access_token ); |
---|
[e1c926f] | 316 | |
---|
[f138bd2] | 317 | reply = oauth_params_string( p_out ); |
---|
[e1c926f] | 318 | oauth_params_free( &p_out ); |
---|
| 319 | } |
---|
| 320 | else if( !( s = sasl_get_part( dec, "rspauth" ) ) ) |
---|
[d8e0484] | 321 | { |
---|
| 322 | /* See RFC 2831 for for information. */ |
---|
| 323 | md5_state_t A1, A2, H; |
---|
| 324 | md5_byte_t A1r[16], A2r[16], Hr[16]; |
---|
| 325 | char A1h[33], A2h[33], Hh[33]; |
---|
| 326 | int i; |
---|
| 327 | |
---|
| 328 | nonce = sasl_get_part( dec, "nonce" ); |
---|
| 329 | realm = sasl_get_part( dec, "realm" ); |
---|
| 330 | |
---|
[d9282b4] | 331 | if( !nonce ) |
---|
[d8e0484] | 332 | goto error; |
---|
| 333 | |
---|
[d9282b4] | 334 | /* Jabber.Org considers the realm part optional and doesn't |
---|
| 335 | specify one. Oh well, actually they're right, but still, |
---|
| 336 | don't know if this is right... */ |
---|
| 337 | if( !realm ) |
---|
| 338 | realm = g_strdup( jd->server ); |
---|
| 339 | |
---|
[3b6eadc] | 340 | random_bytes( cnonce_bin, sizeof( cnonce_bin ) ); |
---|
[d8e0484] | 341 | cnonce = base64_encode( cnonce_bin, sizeof( cnonce_bin ) ); |
---|
| 342 | digest_uri = g_strdup_printf( "%s/%s", "xmpp", jd->server ); |
---|
| 343 | |
---|
| 344 | /* Generate the MD5 hash of username:realm:password, |
---|
| 345 | I decided to call it H. */ |
---|
| 346 | md5_init( &H ); |
---|
[0da65d5] | 347 | s = g_strdup_printf( "%s:%s:%s", jd->username, realm, ic->acc->pass ); |
---|
[d8e0484] | 348 | md5_append( &H, (unsigned char *) s, strlen( s ) ); |
---|
| 349 | g_free( s ); |
---|
| 350 | md5_finish( &H, Hr ); |
---|
| 351 | |
---|
| 352 | /* Now generate the hex. MD5 hash of H:nonce:cnonce, called A1. */ |
---|
| 353 | md5_init( &A1 ); |
---|
| 354 | s = g_strdup_printf( ":%s:%s", nonce, cnonce ); |
---|
| 355 | md5_append( &A1, Hr, 16 ); |
---|
| 356 | md5_append( &A1, (unsigned char *) s, strlen( s ) ); |
---|
| 357 | g_free( s ); |
---|
| 358 | md5_finish( &A1, A1r ); |
---|
| 359 | for( i = 0; i < 16; i ++ ) |
---|
| 360 | sprintf( A1h + i * 2, "%02x", A1r[i] ); |
---|
| 361 | |
---|
| 362 | /* A2... */ |
---|
| 363 | md5_init( &A2 ); |
---|
| 364 | s = g_strdup_printf( "%s:%s", "AUTHENTICATE", digest_uri ); |
---|
| 365 | md5_append( &A2, (unsigned char *) s, strlen( s ) ); |
---|
| 366 | g_free( s ); |
---|
| 367 | md5_finish( &A2, A2r ); |
---|
| 368 | for( i = 0; i < 16; i ++ ) |
---|
| 369 | sprintf( A2h + i * 2, "%02x", A2r[i] ); |
---|
| 370 | |
---|
| 371 | /* Final result: A1:nonce:00000001:cnonce:auth:A2. Let's reuse H for it. */ |
---|
| 372 | md5_init( &H ); |
---|
| 373 | s = g_strdup_printf( "%s:%s:%s:%s:%s:%s", A1h, nonce, "00000001", cnonce, "auth", A2h ); |
---|
| 374 | md5_append( &H, (unsigned char *) s, strlen( s ) ); |
---|
| 375 | g_free( s ); |
---|
| 376 | md5_finish( &H, Hr ); |
---|
| 377 | for( i = 0; i < 16; i ++ ) |
---|
| 378 | sprintf( Hh + i * 2, "%02x", Hr[i] ); |
---|
| 379 | |
---|
| 380 | /* Now build the SASL response string: */ |
---|
[f138bd2] | 381 | reply = g_strdup_printf( "username=\"%s\",realm=\"%s\",nonce=\"%s\",cnonce=\"%s\"," |
---|
| 382 | "nc=%08x,qop=auth,digest-uri=\"%s\",response=%s,charset=%s", |
---|
| 383 | jd->username, realm, nonce, cnonce, 1, digest_uri, Hh, "utf-8" ); |
---|
[d8e0484] | 384 | } |
---|
| 385 | else |
---|
| 386 | { |
---|
| 387 | /* We found rspauth, but don't really care... */ |
---|
| 388 | } |
---|
| 389 | |
---|
[f138bd2] | 390 | s = reply ? tobase64( reply ) : NULL; |
---|
| 391 | reply_pkt = xt_new_node( "response", s, NULL ); |
---|
| 392 | xt_add_attr( reply_pkt, "xmlns", XMLNS_SASL ); |
---|
[d8e0484] | 393 | |
---|
[f138bd2] | 394 | if( !jabber_write_packet( ic, reply_pkt ) ) |
---|
[d8e0484] | 395 | goto silent_error; |
---|
| 396 | |
---|
| 397 | ret = XT_HANDLED; |
---|
| 398 | goto silent_error; |
---|
| 399 | |
---|
| 400 | error: |
---|
[84b045d] | 401 | imcb_error( ic, "Incorrect SASL challenge received" ); |
---|
[c2fb3809] | 402 | imc_logout( ic, FALSE ); |
---|
[d8e0484] | 403 | |
---|
| 404 | silent_error: |
---|
| 405 | g_free( digest_uri ); |
---|
| 406 | g_free( cnonce ); |
---|
| 407 | g_free( nonce ); |
---|
[f138bd2] | 408 | g_free( reply ); |
---|
[d8e0484] | 409 | g_free( realm ); |
---|
| 410 | g_free( dec ); |
---|
| 411 | g_free( s ); |
---|
[f138bd2] | 412 | xt_free_node( reply_pkt ); |
---|
[d8e0484] | 413 | |
---|
| 414 | return ret; |
---|
[5997488] | 415 | } |
---|
| 416 | |
---|
| 417 | xt_status sasl_pkt_result( struct xt_node *node, gpointer data ) |
---|
| 418 | { |
---|
[0da65d5] | 419 | struct im_connection *ic = data; |
---|
| 420 | struct jabber_data *jd = ic->proto_data; |
---|
[5997488] | 421 | char *s; |
---|
| 422 | |
---|
| 423 | s = xt_find_attr( node, "xmlns" ); |
---|
[47d3ac4] | 424 | if( !s || strcmp( s, XMLNS_SASL ) != 0 ) |
---|
[5997488] | 425 | { |
---|
[84b045d] | 426 | imcb_log( ic, "Stream error while authenticating" ); |
---|
[c2fb3809] | 427 | imc_logout( ic, FALSE ); |
---|
[5997488] | 428 | return XT_ABORT; |
---|
| 429 | } |
---|
| 430 | |
---|
| 431 | if( strcmp( node->name, "success" ) == 0 ) |
---|
| 432 | { |
---|
[84b045d] | 433 | imcb_log( ic, "Authentication finished" ); |
---|
[5997488] | 434 | jd->flags |= JFLAG_AUTHENTICATED | JFLAG_STREAM_RESTART; |
---|
| 435 | } |
---|
| 436 | else if( strcmp( node->name, "failure" ) == 0 ) |
---|
| 437 | { |
---|
[84b045d] | 438 | imcb_error( ic, "Authentication failure" ); |
---|
[c2fb3809] | 439 | imc_logout( ic, FALSE ); |
---|
[5997488] | 440 | return XT_ABORT; |
---|
| 441 | } |
---|
| 442 | |
---|
| 443 | return XT_HANDLED; |
---|
| 444 | } |
---|
[8d74291] | 445 | |
---|
| 446 | /* This one is needed to judge if we'll do authentication using IQ or SASL. |
---|
| 447 | It's done by checking if the <stream:stream> from the server has a |
---|
| 448 | version attribute. I don't know if this is the right way though... */ |
---|
[0da65d5] | 449 | gboolean sasl_supported( struct im_connection *ic ) |
---|
[8d74291] | 450 | { |
---|
[0da65d5] | 451 | struct jabber_data *jd = ic->proto_data; |
---|
[8d74291] | 452 | |
---|
[ef5c185] | 453 | return ( jd->xt && jd->xt->root && xt_find_attr( jd->xt->root, "version" ) ) != 0; |
---|
[8d74291] | 454 | } |
---|
[4a5d885] | 455 | |
---|
| 456 | void sasl_oauth2_init( struct im_connection *ic ) |
---|
| 457 | { |
---|
[18c6d36] | 458 | struct jabber_data *jd = ic->proto_data; |
---|
[4a5d885] | 459 | char *msg, *url; |
---|
| 460 | |
---|
| 461 | imcb_log( ic, "Starting OAuth authentication" ); |
---|
| 462 | |
---|
| 463 | /* Temporary contact, just used to receive the OAuth response. */ |
---|
| 464 | imcb_add_buddy( ic, "jabber_oauth", NULL ); |
---|
[18c6d36] | 465 | url = oauth2_url( jd->oauth2_service ); |
---|
[4a5d885] | 466 | msg = g_strdup_printf( "Open this URL in your browser to authenticate: %s", url ); |
---|
| 467 | imcb_buddy_msg( ic, "jabber_oauth", msg, 0, 0 ); |
---|
| 468 | imcb_buddy_msg( ic, "jabber_oauth", "Respond to this message with the returned " |
---|
| 469 | "authorization token.", 0, 0 ); |
---|
| 470 | |
---|
| 471 | g_free( msg ); |
---|
| 472 | g_free( url ); |
---|
| 473 | } |
---|
| 474 | |
---|
| 475 | static gboolean sasl_oauth2_remove_contact( gpointer data, gint fd, b_input_condition cond ) |
---|
| 476 | { |
---|
| 477 | struct im_connection *ic = data; |
---|
[911d97a] | 478 | if( g_slist_find( jabber_connections, ic ) ) |
---|
| 479 | imcb_remove_buddy( ic, "jabber_oauth", NULL ); |
---|
[4a5d885] | 480 | return FALSE; |
---|
| 481 | } |
---|
| 482 | |
---|
| 483 | static void sasl_oauth2_got_token( gpointer data, const char *access_token, const char *refresh_token ); |
---|
| 484 | |
---|
| 485 | int sasl_oauth2_get_refresh_token( struct im_connection *ic, const char *msg ) |
---|
| 486 | { |
---|
[18c6d36] | 487 | struct jabber_data *jd = ic->proto_data; |
---|
[4a5d885] | 488 | char *code; |
---|
| 489 | int ret; |
---|
| 490 | |
---|
| 491 | imcb_log( ic, "Requesting OAuth access token" ); |
---|
| 492 | |
---|
| 493 | /* Don't do it here because the caller may get confused if the contact |
---|
| 494 | we're currently sending a message to is deleted. */ |
---|
| 495 | b_timeout_add( 1, sasl_oauth2_remove_contact, ic ); |
---|
| 496 | |
---|
| 497 | code = g_strdup( msg ); |
---|
| 498 | g_strstrip( code ); |
---|
[18c6d36] | 499 | ret = oauth2_access_token( jd->oauth2_service, OAUTH2_AUTH_CODE, |
---|
[4a5d885] | 500 | code, sasl_oauth2_got_token, ic ); |
---|
| 501 | |
---|
| 502 | g_free( code ); |
---|
| 503 | return ret; |
---|
| 504 | } |
---|
| 505 | |
---|
| 506 | int sasl_oauth2_refresh( struct im_connection *ic, const char *refresh_token ) |
---|
| 507 | { |
---|
[18c6d36] | 508 | struct jabber_data *jd = ic->proto_data; |
---|
| 509 | |
---|
| 510 | return oauth2_access_token( jd->oauth2_service, OAUTH2_AUTH_REFRESH, |
---|
[4a5d885] | 511 | refresh_token, sasl_oauth2_got_token, ic ); |
---|
| 512 | } |
---|
| 513 | |
---|
| 514 | static void sasl_oauth2_got_token( gpointer data, const char *access_token, const char *refresh_token ) |
---|
| 515 | { |
---|
| 516 | struct im_connection *ic = data; |
---|
| 517 | struct jabber_data *jd; |
---|
[36533bf] | 518 | GSList *auth = NULL; |
---|
[4a5d885] | 519 | |
---|
| 520 | if( g_slist_find( jabber_connections, ic ) == NULL ) |
---|
| 521 | return; |
---|
| 522 | |
---|
| 523 | jd = ic->proto_data; |
---|
| 524 | |
---|
| 525 | if( access_token == NULL ) |
---|
| 526 | { |
---|
| 527 | imcb_error( ic, "OAuth failure (missing access token)" ); |
---|
| 528 | imc_logout( ic, TRUE ); |
---|
[e1c926f] | 529 | return; |
---|
[4a5d885] | 530 | } |
---|
[36533bf] | 531 | |
---|
| 532 | oauth_params_parse( &auth, ic->acc->pass ); |
---|
| 533 | if( refresh_token ) |
---|
| 534 | oauth_params_set( &auth, "refresh_token", refresh_token ); |
---|
| 535 | if( access_token ) |
---|
| 536 | oauth_params_set( &auth, "access_token", access_token ); |
---|
| 537 | |
---|
| 538 | g_free( ic->acc->pass ); |
---|
| 539 | ic->acc->pass = oauth_params_string( auth ); |
---|
| 540 | oauth_params_free( &auth ); |
---|
[4a5d885] | 541 | |
---|
| 542 | g_free( jd->oauth2_access_token ); |
---|
| 543 | jd->oauth2_access_token = g_strdup( access_token ); |
---|
| 544 | |
---|
| 545 | jabber_connect( ic ); |
---|
| 546 | } |
---|