aboutsummaryrefslogtreecommitdiffstats
path: root/lib
diff options
context:
space:
mode:
Diffstat (limited to 'lib')
-rw-r--r--lib/Makefile2
-rw-r--r--lib/md5.c11
-rw-r--r--lib/md5.h1
-rw-r--r--lib/oauth.c5
-rw-r--r--lib/oauth.h4
-rw-r--r--lib/oauth2.c191
-rw-r--r--lib/oauth2.h55
7 files changed, 267 insertions, 2 deletions
diff --git a/lib/Makefile b/lib/Makefile
index 3ae43935..5f24139d 100644
--- a/lib/Makefile
+++ b/lib/Makefile
@@ -12,7 +12,7 @@ SRCDIR := $(SRCDIR)lib/
endif
# [SH] Program variables
-objects = arc.o base64.o $(DES) $(EVENT_HANDLER) ftutil.o http_client.o ini.o md5.o misc.o oauth.o proxy.o sha1.o $(SSL_CLIENT) url.o xmltree.o
+objects = arc.o base64.o $(DES) $(EVENT_HANDLER) ftutil.o http_client.o ini.o md5.o misc.o oauth.o oauth2.o proxy.o sha1.o $(SSL_CLIENT) url.o xmltree.o
LFLAGS += -r
diff --git a/lib/md5.c b/lib/md5.c
index 3c39eccd..355f5495 100644
--- a/lib/md5.c
+++ b/lib/md5.c
@@ -23,6 +23,7 @@
#include <sys/types.h>
#include <string.h> /* for memcpy() */
+#include <stdio.h>
#include "md5.h"
static void md5_transform(uint32_t buf[4], uint32_t const in[16]);
@@ -161,6 +162,16 @@ void md5_finish(struct MD5Context *ctx, md5_byte_t digest[16])
memset(ctx, 0, sizeof(ctx)); /* In case it's sensitive */
}
+void md5_finish_ascii(struct MD5Context *context, char *ascii)
+{
+ md5_byte_t bin[16];
+ int i;
+
+ md5_finish(context, bin);
+ for (i = 0; i < 16; i ++)
+ sprintf(ascii + i * 2, "%02x", bin[i]);
+}
+
/* The four core functions - F1 is optimized somewhat */
/* #define F1(x, y, z) (x & y | ~x & z) */
diff --git a/lib/md5.h b/lib/md5.h
index 3ba28586..17da99b3 100644
--- a/lib/md5.h
+++ b/lib/md5.h
@@ -42,5 +42,6 @@ typedef struct MD5Context {
G_MODULE_EXPORT void md5_init(struct MD5Context *context);
G_MODULE_EXPORT void md5_append(struct MD5Context *context, const md5_byte_t *buf, unsigned int len);
G_MODULE_EXPORT void md5_finish(struct MD5Context *context, md5_byte_t digest[16]);
+G_MODULE_EXPORT void md5_finish_ascii(struct MD5Context *context, char *ascii);
#endif
diff --git a/lib/oauth.c b/lib/oauth.c
index 372a62d3..4f431ed6 100644
--- a/lib/oauth.c
+++ b/lib/oauth.c
@@ -121,6 +121,9 @@ void oauth_params_add( GSList **params, const char *key, const char *value )
{
char *item;
+ if( !key || !value )
+ return;
+
item = g_strdup_printf( "%s=%s", key, value );
*params = g_slist_insert_sorted( *params, item, (GCompareFunc) strcmp );
}
@@ -164,7 +167,7 @@ const char *oauth_params_get( GSList **params, const char *key )
return NULL;
}
-static void oauth_params_parse( GSList **params, char *in )
+void oauth_params_parse( GSList **params, char *in )
{
char *amp, *eq, *s;
diff --git a/lib/oauth.h b/lib/oauth.h
index 8270a545..b7388503 100644
--- a/lib/oauth.h
+++ b/lib/oauth.h
@@ -91,4 +91,8 @@ char *oauth_to_string( struct oauth_info *oi );
struct oauth_info *oauth_from_string( char *in, const struct oauth_service *sp );
/* For reading misc. data. */
+void oauth_params_add( GSList **params, const char *key, const char *value );
+void oauth_params_parse( GSList **params, char *in );
+void oauth_params_free( GSList **params );
+char *oauth_params_string( GSList *params );
const char *oauth_params_get( GSList **params, const char *key );
diff --git a/lib/oauth2.c b/lib/oauth2.c
new file mode 100644
index 00000000..7cb28105
--- /dev/null
+++ b/lib/oauth2.c
@@ -0,0 +1,191 @@
+/***************************************************************************\
+* *
+* BitlBee - An IRC to IM gateway *
+* Simple OAuth client (consumer) implementation. *
+* *
+* Copyright 2010-2011 Wilmer van der Gaast <wilmer@gaast.net> *
+* *
+* This program is free software; you can redistribute it and/or modify *
+* it under the terms of the GNU General Public License as published by *
+* the Free Software Foundation; either version 2 of the License, or *
+* (at your option) any later version. *
+* *
+* This program is distributed in the hope that it will be useful, *
+* but WITHOUT ANY WARRANTY; without even the implied warranty of *
+* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
+* GNU General Public License for more details. *
+* *
+* You should have received a copy of the GNU General Public License along *
+* with this program; if not, write to the Free Software Foundation, Inc., *
+* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. *
+* *
+\***************************************************************************/
+
+#include <glib.h>
+#include "http_client.h"
+#include "oauth2.h"
+#include "oauth.h"
+#include "url.h"
+
+struct oauth2_service oauth2_service_google =
+{
+ "https://accounts.google.com/o/oauth2/auth",
+ "https://accounts.google.com/o/oauth2/token",
+ "urn:ietf:wg:oauth:2.0:oob",
+ "783993391592.apps.googleusercontent.com",
+ "6C-Zgf7Tr7gEQTPlBhMUgo7R",
+};
+struct oauth2_service oauth2_service_facebook =
+{
+ "https://www.facebook.com/dialog/oauth",
+ "https://graph.facebook.com/oauth/access_token",
+ "http://www.bitlbee.org/main.php/fb.html",
+ "126828914005625",
+ "4b100f0f244d620bf3f15f8b217d4c32",
+};
+
+char *oauth2_url( const struct oauth2_service *sp, const char *scope )
+{
+ return g_strconcat( sp->auth_url,
+ "?scope=", scope,
+ "&response_type=code"
+ "&redirect_uri=", sp->redirect_url,
+ "&client_id=", sp->consumer_key,
+ NULL );
+}
+
+struct oauth2_access_token_data
+{
+ oauth2_token_callback func;
+ gpointer data;
+};
+
+static char *oauth2_json_dumb_get( const char *json, const char *key );
+static void oauth2_access_token_done( struct http_request *req );
+
+int oauth2_access_token( const struct oauth2_service *sp,
+ const char *auth_type, const char *auth,
+ oauth2_token_callback func, gpointer data )
+{
+ GSList *args = NULL;
+ char *args_s, *s;
+ url_t url_p;
+ struct http_request *req;
+ struct oauth2_access_token_data *cb_data;
+
+ if( !url_set( &url_p, sp->token_url ) )
+ return 0;
+
+ oauth_params_add( &args, "client_id", sp->consumer_key );
+ oauth_params_add( &args, "client_secret", sp->consumer_secret );
+ oauth_params_add( &args, "grant_type", auth_type );
+ if( strcmp( auth_type, OAUTH2_AUTH_CODE ) == 0 )
+ {
+ oauth_params_add( &args, "redirect_uri", sp->redirect_url );
+ oauth_params_add( &args, "code", auth );
+ }
+ else
+ {
+ oauth_params_add( &args, "refresh_token", auth );
+ }
+ args_s = oauth_params_string( args );
+ oauth_params_free( &args );
+
+ s = g_strdup_printf( "POST %s HTTP/1.0\r\n"
+ "Host: %s\r\n"
+ "Content-Type: application/x-www-form-urlencoded\r\n"
+ "Content-Length: %zd\r\n"
+ "Connection: close\r\n"
+ "\r\n"
+ "%s", url_p.file, url_p.host, strlen( args_s ), args_s );
+ g_free( args_s );
+
+ cb_data = g_new0( struct oauth2_access_token_data, 1 );
+ cb_data->func = func;
+ cb_data->data = data;
+
+ req = http_dorequest( url_p.host, url_p.port, url_p.proto == PROTO_HTTPS,
+ s, oauth2_access_token_done, cb_data );
+
+ g_free( s );
+
+ if( req == NULL )
+ g_free( cb_data );
+
+ return req != NULL;
+}
+
+static void oauth2_access_token_done( struct http_request *req )
+{
+ struct oauth2_access_token_data *cb_data = req->data;
+ char *atoken = NULL, *rtoken = NULL;
+
+ if( req->status_code == 200 )
+ {
+ atoken = oauth2_json_dumb_get( req->reply_body, "access_token" );
+ rtoken = oauth2_json_dumb_get( req->reply_body, "refresh_token" );
+ }
+ cb_data->func( cb_data->data, atoken, rtoken );
+ g_free( atoken );
+ g_free( rtoken );
+ g_free( cb_data );
+}
+
+/* Super dumb. I absolutely refuse to use/add a complete json parser library
+ (adding a new dependency to BitlBee for the first time in.. 6 years?) just
+ to parse 100 bytes of data. So I have to do my own parsing because OAuth2
+ dropped support for XML. (GRRR!) This is very dumb and for example won't
+ work for integer values, nor will it strip/handle backslashes. */
+static char *oauth2_json_dumb_get( const char *json, const char *key )
+{
+ int is_key; /* 1 == reading key, 0 == reading value */
+ int found_key = 0;
+
+ while( json && *json )
+ {
+ /* Grab strings and see if they're what we're looking for. */
+ if( *json == '"' || *json == '\'' )
+ {
+ char q = *json;
+ const char *str_start;
+ json ++;
+ str_start = json;
+
+ while( *json )
+ {
+ /* \' and \" are not string terminators. */
+ if( *json == '\\' && json[1] == q )
+ json ++;
+ /* But without a \ it is. */
+ else if( *json == q )
+ break;
+ json ++;
+ }
+ if( *json == '\0' )
+ return NULL;
+
+ if( is_key && strncmp( str_start, key, strlen( key ) ) == 0 )
+ {
+ found_key = 1;
+ }
+ else if( !is_key && found_key )
+ {
+ char *ret = g_memdup( str_start, json - str_start + 1 );
+ ret[json-str_start] = '\0';
+ return ret;
+ }
+
+ }
+ else if( *json == '{' || *json == ',' )
+ {
+ found_key = 0;
+ is_key = 1;
+ }
+ else if( *json == ':' )
+ is_key = 0;
+
+ json ++;
+ }
+
+ return NULL;
+}
diff --git a/lib/oauth2.h b/lib/oauth2.h
new file mode 100644
index 00000000..6f56b426
--- /dev/null
+++ b/lib/oauth2.h
@@ -0,0 +1,55 @@
+/***************************************************************************\
+* *
+* BitlBee - An IRC to IM gateway *
+* Simple OAuth2 client (consumer) implementation. *
+* *
+* Copyright 2010-2011 Wilmer van der Gaast <wilmer@gaast.net> *
+* *
+* This program is free software; you can redistribute it and/or modify *
+* it under the terms of the GNU General Public License as published by *
+* the Free Software Foundation; either version 2 of the License, or *
+* (at your option) any later version. *
+* *
+* This program is distributed in the hope that it will be useful, *
+* but WITHOUT ANY WARRANTY; without even the implied warranty of *
+* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
+* GNU General Public License for more details. *
+* *
+* You should have received a copy of the GNU General Public License along *
+* with this program; if not, write to the Free Software Foundation, Inc., *
+* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. *
+* *
+\***************************************************************************/
+
+/* Implementation mostly based on my experience with writing the previous OAuth
+ module, and from http://code.google.com/apis/accounts/docs/OAuth2.html . */
+
+typedef void (*oauth2_token_callback)( gpointer data, const char *atoken, const char *rtoken );
+
+struct oauth2_service
+{
+ char *auth_url;
+ char *token_url;
+ char *redirect_url;
+ char *consumer_key;
+ char *consumer_secret;
+};
+
+/* Currently suitable for authenticating to Google Talk only, and only for
+ accounts that have 2-factor authorization enabled. */
+extern struct oauth2_service oauth2_service_google;
+
+extern struct oauth2_service oauth2_service_facebook;
+
+#define OAUTH2_AUTH_CODE "authorization_code"
+#define OAUTH2_AUTH_REFRESH "refresh_token"
+
+/* Generate a URL the user should open in his/her browser to get an
+ authorization code. */
+char *oauth2_url( const struct oauth2_service *sp, const char *scope );
+
+/* Exchanges an auth code or refresh token for an access token.
+ auth_type is one of the two OAUTH2_AUTH_.. constants above. */
+int oauth2_access_token( const struct oauth2_service *sp,
+ const char *auth_type, const char *auth,
+ oauth2_token_callback func, gpointer data );