[57b4525] | 1 | /***************************************************************************\ |
---|
| 2 | * * |
---|
| 3 | * BitlBee - An IRC to IM gateway * |
---|
[c153808] | 4 | * Simple OAuth2 client (consumer) implementation. * |
---|
[57b4525] | 5 | * * |
---|
[c153808] | 6 | * Copyright 2010-2013 Wilmer van der Gaast <wilmer@gaast.net> * |
---|
[57b4525] | 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 | |
---|
[c153808] | 24 | /* Out of protest, I should rename this file. OAuth2 is a pathetic joke, and |
---|
| 25 | of all things, DEFINITELY NOT A STANDARD. The only thing various OAuth2 |
---|
| 26 | implementations have in common is that name, wrongfully stolen from |
---|
| 27 | a pretty nice standard called OAuth 1.0a. That, and the fact that they |
---|
| 28 | use JSON. Wait, no, Facebook's version doesn't use JSON. For some of its |
---|
| 29 | responses. |
---|
| 30 | |
---|
| 31 | Apparently too many people were too retarded to comprehend the elementary |
---|
| 32 | bits of crypto in OAuth 1.0a (took me one afternoon to implement) so |
---|
| 33 | the standard was replaced with what comes down to a complicated scheme |
---|
| 34 | around what's really just application-specific passwords. |
---|
| 35 | |
---|
| 36 | And then a bunch of mostly incompatible implementations. Great work, guys. |
---|
| 37 | |
---|
| 38 | http://hueniverse.com/2012/07/oauth-2-0-and-the-road-to-hell/ */ |
---|
| 39 | |
---|
[57b4525] | 40 | #include <glib.h> |
---|
[4a5d885] | 41 | #include "http_client.h" |
---|
[57b4525] | 42 | #include "oauth2.h" |
---|
[4a5d885] | 43 | #include "oauth.h" |
---|
[ba654ec] | 44 | #include "json.h" |
---|
[c153808] | 45 | #include "json_util.h" |
---|
[4a5d885] | 46 | #include "url.h" |
---|
[57b4525] | 47 | |
---|
[18c6d36] | 48 | char *oauth2_url( const struct oauth2_service *sp ) |
---|
[57b4525] | 49 | { |
---|
[39a939c] | 50 | return g_strconcat( sp->auth_url, |
---|
[18c6d36] | 51 | "?scope=", sp->scope, |
---|
[57b4525] | 52 | "&response_type=code" |
---|
[39a939c] | 53 | "&redirect_uri=", sp->redirect_url, |
---|
[57b4525] | 54 | "&client_id=", sp->consumer_key, |
---|
| 55 | NULL ); |
---|
| 56 | } |
---|
[4a5d885] | 57 | |
---|
| 58 | struct oauth2_access_token_data |
---|
| 59 | { |
---|
| 60 | oauth2_token_callback func; |
---|
| 61 | gpointer data; |
---|
| 62 | }; |
---|
| 63 | |
---|
| 64 | static void oauth2_access_token_done( struct http_request *req ); |
---|
| 65 | |
---|
| 66 | int oauth2_access_token( const struct oauth2_service *sp, |
---|
| 67 | const char *auth_type, const char *auth, |
---|
| 68 | oauth2_token_callback func, gpointer data ) |
---|
| 69 | { |
---|
| 70 | GSList *args = NULL; |
---|
| 71 | char *args_s, *s; |
---|
| 72 | url_t url_p; |
---|
| 73 | struct http_request *req; |
---|
| 74 | struct oauth2_access_token_data *cb_data; |
---|
| 75 | |
---|
[39a939c] | 76 | if( !url_set( &url_p, sp->token_url ) ) |
---|
[4a5d885] | 77 | return 0; |
---|
| 78 | |
---|
| 79 | oauth_params_add( &args, "client_id", sp->consumer_key ); |
---|
| 80 | oauth_params_add( &args, "client_secret", sp->consumer_secret ); |
---|
| 81 | oauth_params_add( &args, "grant_type", auth_type ); |
---|
| 82 | if( strcmp( auth_type, OAUTH2_AUTH_CODE ) == 0 ) |
---|
| 83 | { |
---|
[39a939c] | 84 | oauth_params_add( &args, "redirect_uri", sp->redirect_url ); |
---|
[4a5d885] | 85 | oauth_params_add( &args, "code", auth ); |
---|
| 86 | } |
---|
| 87 | else |
---|
| 88 | { |
---|
| 89 | oauth_params_add( &args, "refresh_token", auth ); |
---|
| 90 | } |
---|
| 91 | args_s = oauth_params_string( args ); |
---|
| 92 | oauth_params_free( &args ); |
---|
| 93 | |
---|
[39a939c] | 94 | s = g_strdup_printf( "POST %s HTTP/1.0\r\n" |
---|
[4a5d885] | 95 | "Host: %s\r\n" |
---|
| 96 | "Content-Type: application/x-www-form-urlencoded\r\n" |
---|
| 97 | "Content-Length: %zd\r\n" |
---|
| 98 | "Connection: close\r\n" |
---|
| 99 | "\r\n" |
---|
[39a939c] | 100 | "%s", url_p.file, url_p.host, strlen( args_s ), args_s ); |
---|
[4a5d885] | 101 | g_free( args_s ); |
---|
| 102 | |
---|
| 103 | cb_data = g_new0( struct oauth2_access_token_data, 1 ); |
---|
| 104 | cb_data->func = func; |
---|
| 105 | cb_data->data = data; |
---|
| 106 | |
---|
| 107 | req = http_dorequest( url_p.host, url_p.port, url_p.proto == PROTO_HTTPS, |
---|
| 108 | s, oauth2_access_token_done, cb_data ); |
---|
| 109 | |
---|
| 110 | g_free( s ); |
---|
| 111 | |
---|
| 112 | if( req == NULL ) |
---|
| 113 | g_free( cb_data ); |
---|
| 114 | |
---|
| 115 | return req != NULL; |
---|
| 116 | } |
---|
| 117 | |
---|
[c153808] | 118 | static char* oauth2_parse_error( json_value *e ) |
---|
| 119 | { |
---|
| 120 | /* This does a reasonable job with some of the flavours of error |
---|
| 121 | responses I've seen. Because apparently it's not standardised. */ |
---|
| 122 | |
---|
| 123 | if( e->type == json_object ) |
---|
| 124 | { |
---|
| 125 | /* Facebook style */ |
---|
| 126 | const char *msg = json_o_str( e, "message" ); |
---|
| 127 | const char *type = json_o_str( e, "type" ); |
---|
| 128 | json_value *code_o = json_o_get( e, "code" ); |
---|
| 129 | int code = 0; |
---|
| 130 | |
---|
| 131 | if( code_o && code_o->type == json_integer ) |
---|
| 132 | code = code_o->u.integer; |
---|
| 133 | |
---|
| 134 | return g_strdup_printf( "Error %d: %s", code, msg ? msg : type ? type : "Unknown error" ); |
---|
| 135 | } |
---|
| 136 | else if( e->type == json_string ) |
---|
| 137 | { |
---|
| 138 | return g_strdup( e->u.string.ptr ); |
---|
| 139 | } |
---|
| 140 | return NULL; |
---|
| 141 | } |
---|
| 142 | |
---|
[4a5d885] | 143 | static void oauth2_access_token_done( struct http_request *req ) |
---|
| 144 | { |
---|
| 145 | struct oauth2_access_token_data *cb_data = req->data; |
---|
[c153808] | 146 | char *atoken = NULL, *rtoken = NULL, *error = NULL; |
---|
[3bda2c2] | 147 | char *content_type; |
---|
[4a5d885] | 148 | |
---|
[18c6d36] | 149 | if( getenv( "BITLBEE_DEBUG" ) && req->reply_body ) |
---|
| 150 | printf( "%s\n", req->reply_body ); |
---|
| 151 | |
---|
[bf57cd1] | 152 | content_type = get_rfc822_header( req->reply_headers, "Content-Type", 0 ); |
---|
| 153 | |
---|
[c153808] | 154 | if( content_type && ( strstr( content_type, "application/json" ) || |
---|
| 155 | strstr( content_type, "text/javascript" ) ) ) |
---|
[4a5d885] | 156 | { |
---|
[ba654ec] | 157 | json_value *js = json_parse( req->reply_body ); |
---|
| 158 | if( js && js->type == json_object ) |
---|
| 159 | { |
---|
[c153808] | 160 | JSON_O_FOREACH( js, k, v ) |
---|
[ba654ec] | 161 | { |
---|
[c153808] | 162 | if( strcmp( k, "error" ) == 0 ) |
---|
| 163 | error = oauth2_parse_error( v ); |
---|
| 164 | if( v->type != json_string ) |
---|
[ba654ec] | 165 | continue; |
---|
[c153808] | 166 | if( strcmp( k, "access_token" ) == 0 ) |
---|
| 167 | atoken = g_strdup( v->u.string.ptr ); |
---|
| 168 | if( strcmp( k, "refresh_token" ) == 0 ) |
---|
| 169 | rtoken = g_strdup( v->u.string.ptr ); |
---|
[ba654ec] | 170 | } |
---|
| 171 | } |
---|
| 172 | json_value_free( js ); |
---|
[4a5d885] | 173 | } |
---|
[bf57cd1] | 174 | else |
---|
| 175 | { |
---|
| 176 | /* Facebook use their own odd format here, seems to be URL-encoded. */ |
---|
| 177 | GSList *p_in = NULL; |
---|
| 178 | |
---|
| 179 | oauth_params_parse( &p_in, req->reply_body ); |
---|
| 180 | atoken = g_strdup( oauth_params_get( &p_in, "access_token" ) ); |
---|
| 181 | rtoken = g_strdup( oauth_params_get( &p_in, "refresh_token" ) ); |
---|
| 182 | oauth_params_free( &p_in ); |
---|
| 183 | } |
---|
[644b808] | 184 | if( getenv( "BITLBEE_DEBUG" ) ) |
---|
| 185 | printf( "Extracted atoken=%s rtoken=%s\n", atoken, rtoken ); |
---|
[c153808] | 186 | if( !atoken && !rtoken && !error ) |
---|
| 187 | error = g_strdup( "Unusuable response" ); |
---|
[644b808] | 188 | |
---|
[c153808] | 189 | cb_data->func( cb_data->data, atoken, rtoken, error ); |
---|
[3bda2c2] | 190 | g_free( content_type ); |
---|
[4a5d885] | 191 | g_free( atoken ); |
---|
| 192 | g_free( rtoken ); |
---|
[c153808] | 193 | g_free( error ); |
---|
[4a5d885] | 194 | g_free( cb_data ); |
---|
| 195 | } |
---|