1 | /***************************************************************************\ |
---|
2 | * * |
---|
3 | * BitlBee - An IRC to IM gateway * |
---|
4 | * Simple OAuth client (consumer) implementation. * |
---|
5 | * * |
---|
6 | * Copyright 2010 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 | |
---|
24 | #include <glib.h> |
---|
25 | #include <gmodule.h> |
---|
26 | #include <stdlib.h> |
---|
27 | #include <string.h> |
---|
28 | #include "http_client.h" |
---|
29 | #include "base64.h" |
---|
30 | #include "misc.h" |
---|
31 | #include "sha1.h" |
---|
32 | #include "url.h" |
---|
33 | #include "oauth.h" |
---|
34 | |
---|
35 | #define HMAC_BLOCK_SIZE 64 |
---|
36 | |
---|
37 | static char *oauth_sign( const char *method, const char *url, |
---|
38 | const char *params, struct oauth_info *oi ) |
---|
39 | { |
---|
40 | uint8_t hash[sha1_hash_size]; |
---|
41 | GString *payload = g_string_new( "" ); |
---|
42 | char *key; |
---|
43 | char *s; |
---|
44 | |
---|
45 | key = g_strdup_printf( "%s&%s", oi->sp->consumer_secret, oi->token_secret ? oi->token_secret : "" ); |
---|
46 | |
---|
47 | g_string_append_printf( payload, "%s&", method ); |
---|
48 | |
---|
49 | s = g_new0( char, strlen( url ) * 3 + 1 ); |
---|
50 | strcpy( s, url ); |
---|
51 | http_encode( s ); |
---|
52 | g_string_append_printf( payload, "%s&", s ); |
---|
53 | g_free( s ); |
---|
54 | |
---|
55 | s = g_new0( char, strlen( params ) * 3 + 1 ); |
---|
56 | strcpy( s, params ); |
---|
57 | http_encode( s ); |
---|
58 | g_string_append( payload, s ); |
---|
59 | g_free( s ); |
---|
60 | |
---|
61 | sha1_hmac( key, 0, payload->str, 0, hash ); |
---|
62 | |
---|
63 | g_free( key ); |
---|
64 | g_string_free( payload, TRUE ); |
---|
65 | |
---|
66 | /* base64_encode + HTTP escape it (both consumers |
---|
67 | need it that away) and we're done. */ |
---|
68 | s = base64_encode( hash, sha1_hash_size ); |
---|
69 | s = g_realloc( s, strlen( s ) * 3 + 1 ); |
---|
70 | http_encode( s ); |
---|
71 | |
---|
72 | return s; |
---|
73 | } |
---|
74 | |
---|
75 | static char *oauth_nonce() |
---|
76 | { |
---|
77 | unsigned char bytes[21]; |
---|
78 | char *ret = g_new0( char, sizeof( bytes) / 3 * 4 + 1 ); |
---|
79 | |
---|
80 | random_bytes( bytes, sizeof( bytes ) ); |
---|
81 | base64_encode_real( bytes, sizeof( bytes), (unsigned char*) ret, "0123456789" |
---|
82 | "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0A" ); |
---|
83 | |
---|
84 | return ret; |
---|
85 | } |
---|
86 | |
---|
87 | void oauth_params_add( GSList **params, const char *key, const char *value ) |
---|
88 | { |
---|
89 | char *item; |
---|
90 | |
---|
91 | if( !key || !value ) |
---|
92 | return; |
---|
93 | |
---|
94 | item = g_strdup_printf( "%s=%s", key, value ); |
---|
95 | *params = g_slist_insert_sorted( *params, item, (GCompareFunc) strcmp ); |
---|
96 | } |
---|
97 | |
---|
98 | void oauth_params_del( GSList **params, const char *key ) |
---|
99 | { |
---|
100 | int key_len = strlen( key ); |
---|
101 | GSList *l, *n; |
---|
102 | |
---|
103 | if( params == NULL ) |
---|
104 | return; |
---|
105 | |
---|
106 | for( l = *params; l; l = n ) |
---|
107 | { |
---|
108 | n = l->next; |
---|
109 | |
---|
110 | if( strncmp( (char*) l->data, key, key_len ) == 0 && |
---|
111 | ((char*)l->data)[key_len] == '=' ) |
---|
112 | { |
---|
113 | g_free( l->data ); |
---|
114 | *params = g_slist_remove( *params, l->data ); |
---|
115 | } |
---|
116 | } |
---|
117 | } |
---|
118 | |
---|
119 | void oauth_params_set( GSList **params, const char *key, const char *value ) |
---|
120 | { |
---|
121 | oauth_params_del( params, key ); |
---|
122 | oauth_params_add( params, key, value ); |
---|
123 | } |
---|
124 | |
---|
125 | const char *oauth_params_get( GSList **params, const char *key ) |
---|
126 | { |
---|
127 | int key_len = strlen( key ); |
---|
128 | GSList *l; |
---|
129 | |
---|
130 | if( params == NULL ) |
---|
131 | return NULL; |
---|
132 | |
---|
133 | for( l = *params; l; l = l->next ) |
---|
134 | { |
---|
135 | if( strncmp( (char*) l->data, key, key_len ) == 0 && |
---|
136 | ((char*)l->data)[key_len] == '=' ) |
---|
137 | return (const char*) l->data + key_len + 1; |
---|
138 | } |
---|
139 | |
---|
140 | return NULL; |
---|
141 | } |
---|
142 | |
---|
143 | void oauth_params_parse( GSList **params, char *in ) |
---|
144 | { |
---|
145 | char *amp, *eq, *s; |
---|
146 | |
---|
147 | while( in && *in ) |
---|
148 | { |
---|
149 | eq = strchr( in, '=' ); |
---|
150 | if( !eq ) |
---|
151 | break; |
---|
152 | |
---|
153 | *eq = '\0'; |
---|
154 | if( ( amp = strchr( eq + 1, '&' ) ) ) |
---|
155 | *amp = '\0'; |
---|
156 | |
---|
157 | s = g_strdup( eq + 1 ); |
---|
158 | http_decode( s ); |
---|
159 | oauth_params_add( params, in, s ); |
---|
160 | g_free( s ); |
---|
161 | |
---|
162 | *eq = '='; |
---|
163 | if( amp == NULL ) |
---|
164 | break; |
---|
165 | |
---|
166 | *amp = '&'; |
---|
167 | in = amp + 1; |
---|
168 | } |
---|
169 | } |
---|
170 | |
---|
171 | void oauth_params_free( GSList **params ) |
---|
172 | { |
---|
173 | while( params && *params ) |
---|
174 | { |
---|
175 | g_free( (*params)->data ); |
---|
176 | *params = g_slist_remove( *params, (*params)->data ); |
---|
177 | } |
---|
178 | } |
---|
179 | |
---|
180 | char *oauth_params_string( GSList *params ) |
---|
181 | { |
---|
182 | GSList *l; |
---|
183 | GString *str = g_string_new( "" ); |
---|
184 | |
---|
185 | for( l = params; l; l = l->next ) |
---|
186 | { |
---|
187 | char *s, *eq; |
---|
188 | |
---|
189 | s = g_malloc( strlen( l->data ) * 3 + 1 ); |
---|
190 | strcpy( s, l->data ); |
---|
191 | if( ( eq = strchr( s, '=' ) ) ) |
---|
192 | http_encode( eq + 1 ); |
---|
193 | g_string_append( str, s ); |
---|
194 | g_free( s ); |
---|
195 | |
---|
196 | if( l->next ) |
---|
197 | g_string_append_c( str, '&' ); |
---|
198 | } |
---|
199 | |
---|
200 | return g_string_free( str, FALSE ); |
---|
201 | } |
---|
202 | |
---|
203 | void oauth_info_free( struct oauth_info *info ) |
---|
204 | { |
---|
205 | if( info ) |
---|
206 | { |
---|
207 | g_free( info->auth_url ); |
---|
208 | g_free( info->request_token ); |
---|
209 | g_free( info->token ); |
---|
210 | g_free( info->token_secret ); |
---|
211 | oauth_params_free( &info->params ); |
---|
212 | g_free( info ); |
---|
213 | } |
---|
214 | } |
---|
215 | |
---|
216 | static void oauth_add_default_params( GSList **params, const struct oauth_service *sp ) |
---|
217 | { |
---|
218 | char *s; |
---|
219 | |
---|
220 | oauth_params_set( params, "oauth_consumer_key", sp->consumer_key ); |
---|
221 | oauth_params_set( params, "oauth_signature_method", "HMAC-SHA1" ); |
---|
222 | |
---|
223 | s = g_strdup_printf( "%d", (int) time( NULL ) ); |
---|
224 | oauth_params_set( params, "oauth_timestamp", s ); |
---|
225 | g_free( s ); |
---|
226 | |
---|
227 | s = oauth_nonce(); |
---|
228 | oauth_params_set( params, "oauth_nonce", s ); |
---|
229 | g_free( s ); |
---|
230 | |
---|
231 | oauth_params_set( params, "oauth_version", "1.0" ); |
---|
232 | } |
---|
233 | |
---|
234 | static void *oauth_post_request( const char *url, GSList **params_, http_input_function func, struct oauth_info *oi ) |
---|
235 | { |
---|
236 | GSList *params = NULL; |
---|
237 | char *s, *params_s, *post; |
---|
238 | void *req; |
---|
239 | url_t url_p; |
---|
240 | |
---|
241 | if( !url_set( &url_p, url ) ) |
---|
242 | { |
---|
243 | oauth_params_free( params_ ); |
---|
244 | return NULL; |
---|
245 | } |
---|
246 | |
---|
247 | if( params_ ) |
---|
248 | params = *params_; |
---|
249 | |
---|
250 | oauth_add_default_params( ¶ms, oi->sp ); |
---|
251 | |
---|
252 | params_s = oauth_params_string( params ); |
---|
253 | oauth_params_free( ¶ms ); |
---|
254 | |
---|
255 | s = oauth_sign( "POST", url, params_s, oi ); |
---|
256 | post = g_strdup_printf( "%s&oauth_signature=%s", params_s, s ); |
---|
257 | g_free( params_s ); |
---|
258 | g_free( s ); |
---|
259 | |
---|
260 | s = g_strdup_printf( "POST %s HTTP/1.0\r\n" |
---|
261 | "Host: %s\r\n" |
---|
262 | "Content-Type: application/x-www-form-urlencoded\r\n" |
---|
263 | "Content-Length: %zd\r\n" |
---|
264 | "\r\n" |
---|
265 | "%s", url_p.file, url_p.host, strlen( post ), post ); |
---|
266 | g_free( post ); |
---|
267 | |
---|
268 | req = http_dorequest( url_p.host, url_p.port, url_p.proto == PROTO_HTTPS, |
---|
269 | s, func, oi ); |
---|
270 | g_free( s ); |
---|
271 | |
---|
272 | return req; |
---|
273 | } |
---|
274 | |
---|
275 | static void oauth_request_token_done( struct http_request *req ); |
---|
276 | |
---|
277 | struct oauth_info *oauth_request_token( const struct oauth_service *sp, oauth_cb func, void *data ) |
---|
278 | { |
---|
279 | struct oauth_info *st = g_new0( struct oauth_info, 1 ); |
---|
280 | GSList *params = NULL; |
---|
281 | |
---|
282 | st->func = func; |
---|
283 | st->data = data; |
---|
284 | st->sp = sp; |
---|
285 | |
---|
286 | oauth_params_add( ¶ms, "oauth_callback", "oob" ); |
---|
287 | |
---|
288 | if( !oauth_post_request( sp->url_request_token, ¶ms, oauth_request_token_done, st ) ) |
---|
289 | { |
---|
290 | oauth_info_free( st ); |
---|
291 | return NULL; |
---|
292 | } |
---|
293 | |
---|
294 | return st; |
---|
295 | } |
---|
296 | |
---|
297 | static void oauth_request_token_done( struct http_request *req ) |
---|
298 | { |
---|
299 | struct oauth_info *st = req->data; |
---|
300 | |
---|
301 | st->http = req; |
---|
302 | |
---|
303 | if( req->status_code == 200 ) |
---|
304 | { |
---|
305 | GSList *params = NULL; |
---|
306 | |
---|
307 | st->auth_url = g_strdup_printf( "%s?%s", st->sp->url_authorize, req->reply_body ); |
---|
308 | oauth_params_parse( ¶ms, req->reply_body ); |
---|
309 | st->request_token = g_strdup( oauth_params_get( ¶ms, "oauth_token" ) ); |
---|
310 | st->token_secret = g_strdup( oauth_params_get( ¶ms, "oauth_token_secret" ) ); |
---|
311 | oauth_params_free( ¶ms ); |
---|
312 | } |
---|
313 | |
---|
314 | st->stage = OAUTH_REQUEST_TOKEN; |
---|
315 | st->func( st ); |
---|
316 | } |
---|
317 | |
---|
318 | static void oauth_access_token_done( struct http_request *req ); |
---|
319 | |
---|
320 | gboolean oauth_access_token( const char *pin, struct oauth_info *st ) |
---|
321 | { |
---|
322 | GSList *params = NULL; |
---|
323 | |
---|
324 | oauth_params_add( ¶ms, "oauth_token", st->request_token ); |
---|
325 | oauth_params_add( ¶ms, "oauth_verifier", pin ); |
---|
326 | |
---|
327 | return oauth_post_request( st->sp->url_access_token, ¶ms, oauth_access_token_done, st ) != NULL; |
---|
328 | } |
---|
329 | |
---|
330 | static void oauth_access_token_done( struct http_request *req ) |
---|
331 | { |
---|
332 | struct oauth_info *st = req->data; |
---|
333 | |
---|
334 | st->http = req; |
---|
335 | |
---|
336 | if( req->status_code == 200 ) |
---|
337 | { |
---|
338 | oauth_params_parse( &st->params, req->reply_body ); |
---|
339 | st->token = g_strdup( oauth_params_get( &st->params, "oauth_token" ) ); |
---|
340 | g_free( st->token_secret ); |
---|
341 | st->token_secret = g_strdup( oauth_params_get( &st->params, "oauth_token_secret" ) ); |
---|
342 | } |
---|
343 | |
---|
344 | st->stage = OAUTH_ACCESS_TOKEN; |
---|
345 | if( st->func( st ) ) |
---|
346 | { |
---|
347 | /* Don't need these anymore, but keep the rest. */ |
---|
348 | g_free( st->auth_url ); |
---|
349 | st->auth_url = NULL; |
---|
350 | g_free( st->request_token ); |
---|
351 | st->request_token = NULL; |
---|
352 | oauth_params_free( &st->params ); |
---|
353 | } |
---|
354 | } |
---|
355 | |
---|
356 | char *oauth_http_header( struct oauth_info *oi, const char *method, const char *url, char *args ) |
---|
357 | { |
---|
358 | GSList *params = NULL, *l; |
---|
359 | char *sig = NULL, *params_s, *s; |
---|
360 | GString *ret = NULL; |
---|
361 | |
---|
362 | oauth_params_add( ¶ms, "oauth_token", oi->token ); |
---|
363 | oauth_add_default_params( ¶ms, oi->sp ); |
---|
364 | |
---|
365 | /* Start building the OAuth header. 'key="value", '... */ |
---|
366 | ret = g_string_new( "OAuth " ); |
---|
367 | for( l = params; l; l = l->next ) |
---|
368 | { |
---|
369 | char *kv = l->data; |
---|
370 | char *eq = strchr( kv, '=' ); |
---|
371 | char esc[strlen(kv)*3+1]; |
---|
372 | |
---|
373 | if( eq == NULL ) |
---|
374 | break; /* WTF */ |
---|
375 | |
---|
376 | strcpy( esc, eq + 1 ); |
---|
377 | http_encode( esc ); |
---|
378 | |
---|
379 | g_string_append_len( ret, kv, eq - kv + 1 ); |
---|
380 | g_string_append_c( ret, '"' ); |
---|
381 | g_string_append( ret, esc ); |
---|
382 | g_string_append( ret, "\", " ); |
---|
383 | } |
---|
384 | |
---|
385 | /* Now, before generating the signature, add GET/POST arguments to params |
---|
386 | since they should be included in the base signature string (but not in |
---|
387 | the HTTP header). */ |
---|
388 | if( args ) |
---|
389 | oauth_params_parse( ¶ms, args ); |
---|
390 | if( ( s = strchr( url, '?' ) ) ) |
---|
391 | { |
---|
392 | s = g_strdup( s + 1 ); |
---|
393 | oauth_params_parse( ¶ms, s ); |
---|
394 | g_free( s ); |
---|
395 | } |
---|
396 | |
---|
397 | /* Append the signature and we're done! */ |
---|
398 | params_s = oauth_params_string( params ); |
---|
399 | sig = oauth_sign( method, url, params_s, oi ); |
---|
400 | g_string_append_printf( ret, "oauth_signature=\"%s\"", sig ); |
---|
401 | g_free( params_s ); |
---|
402 | |
---|
403 | oauth_params_free( ¶ms ); |
---|
404 | g_free( sig ); |
---|
405 | |
---|
406 | return ret ? g_string_free( ret, FALSE ) : NULL; |
---|
407 | } |
---|
408 | |
---|
409 | char *oauth_to_string( struct oauth_info *oi ) |
---|
410 | { |
---|
411 | GSList *params = NULL; |
---|
412 | char *ret; |
---|
413 | |
---|
414 | oauth_params_add( ¶ms, "oauth_token", oi->token ); |
---|
415 | oauth_params_add( ¶ms, "oauth_token_secret", oi->token_secret ); |
---|
416 | ret = oauth_params_string( params ); |
---|
417 | oauth_params_free( ¶ms ); |
---|
418 | |
---|
419 | return ret; |
---|
420 | } |
---|
421 | |
---|
422 | struct oauth_info *oauth_from_string( char *in, const struct oauth_service *sp ) |
---|
423 | { |
---|
424 | struct oauth_info *oi = g_new0( struct oauth_info, 1 ); |
---|
425 | GSList *params = NULL; |
---|
426 | |
---|
427 | oauth_params_parse( ¶ms, in ); |
---|
428 | oi->token = g_strdup( oauth_params_get( ¶ms, "oauth_token" ) ); |
---|
429 | oi->token_secret = g_strdup( oauth_params_get( ¶ms, "oauth_token_secret" ) ); |
---|
430 | oauth_params_free( ¶ms ); |
---|
431 | oi->sp = sp; |
---|
432 | |
---|
433 | return oi; |
---|
434 | } |
---|