-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathtwitter_api_oauth.c
448 lines (345 loc) · 14.8 KB
/
twitter_api_oauth.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
#include "twitter_api_oauth.h"
void _generate_random_string(char *s, const int len) {
static const char alphanum[] =
"0123456789"
"ABCDEFGHIJKLMNOPQRSTUVWXYZ"
"abcdefghijklmnopqrstuvwxyz";
srand(time(NULL));
int i;
for (i = 0; i < len; ++i) {
s[i] = alphanum[rand() % (sizeof(alphanum) - 1)];
}
s[len] = 0;
}
HTTPParameter *_create_parameter(char *key, char *value) {
HTTPParameter *parameter = malloc(sizeof(*parameter));
HTTPParameter_initialize(parameter);
parameter->key = key;
parameter->value = value;
return parameter;
}
int compare_parameters(const void *a, const void *b)
{
HTTPParameter *parameter_a = (HTTPParameter *)a;
HTTPParameter *parameter_b = (HTTPParameter *)b;
return strcmp(parameter_a->key, parameter_b->key);;
}
void OAuthOAuthToken_initialize(OAuthOAuthToken *oauth_token) {
oauth_token->oauth_token = NULL;
oauth_token->oauth_token_secret = NULL;
oauth_token->oauth_verifier = NULL;
}
void OAuthOAuthToken_free(OAuthOAuthToken *oauth_token) {
free(oauth_token->oauth_token);
free(oauth_token->oauth_token_secret);
free(oauth_token->oauth_verifier);
}
void OAuthAccessToken_initialize(OAuthAccessToken *access_token) {
access_token->access_token = NULL;
access_token->access_token_secret = NULL;
}
void OAuthAccessToken_free(OAuthAccessToken *access_token) {
free(access_token->access_token);
free(access_token->access_token_secret);
}
int OAuthAccessToken_load_from_file() {
wordexp_t p;
char **w;
int i;
wordexp(ACCESS_TOKEN_FILE, &p, 0);
w = p.we_wordv;
FILE *file_descriptor = fopen(w[0], "rt");
wordfree(&p);
if(file_descriptor == NULL) {
return -1;
}
char line[80];
int line_number = 0;
if(current_access_token) {
OAuthAccessToken_free(current_access_token);
}
current_access_token = malloc(sizeof(*current_access_token));
while(fgets(line, 80, file_descriptor) != NULL)
{
if(line[strlen(line) - 1] == '\n') {
line[strlen(line) - 1] = '\0';
}
if(line_number == 0) {
current_access_token->access_token = strdup(line);
} else {
current_access_token->access_token_secret = strdup(line);
}
line_number += 1;
}
fclose(file_descriptor);
if(current_access_token->access_token == NULL && current_access_token->access_token_secret == NULL) {
return -2;
}
return 0;
}
int OAuthAccessToken_save_current_to_file() {
wordexp_t p;
char **w;
int i;
wordexp(ACCESS_TOKEN_FILE, &p, 0);
w = p.we_wordv;
FILE *file_descriptor = fopen(w[0], "wt");
wordfree(&p);
if(file_descriptor == NULL) {
return -1;
}
if(!current_access_token) {
return -2;
}
fprintf(file_descriptor, "%s\n", current_access_token->access_token);
fprintf(file_descriptor, "%s", current_access_token->access_token_secret);
fclose(file_descriptor);
return 0;
}
char *_generate_signature_for_parameters(HTTPParameter *first_parameter, HTTPConnection *http_connection) {
int parameters_count = 0;
HTTPParameter *current_parameter = first_parameter;
while(current_parameter != NULL) {
parameters_count++;
current_parameter = (HTTPParameter *)current_parameter->next_parameter;
}
HTTPParameter http_parameters[parameters_count];
int i = 0;
current_parameter = first_parameter;
while(current_parameter != NULL) {
http_parameters[i] = *current_parameter;
i++;
current_parameter = (HTTPParameter *)current_parameter->next_parameter;
}
// sort parameters alphabetically
qsort(http_parameters, parameters_count, sizeof(HTTPParameter), compare_parameters);
char *signature_base_string = malloc(sizeof(char) * 500);
for(i = 0; i < parameters_count; i++) {
current_parameter = &(http_parameters[i]);
if(current_parameter->type != HTTPParameterTypeAuthorizationHeader) {
sprintf(signature_base_string, "%s%s=%s&", signature_base_string, current_parameter->key, current_parameter->value);
}
}
signature_base_string[strlen(signature_base_string) - 1] = '\0'; // removes last '&'
sprintf(signature_base_string, "%s&%s&%s", (http_connection->connection_method == HTTPConnectionMethodPOST ? "POST" : "GET"), _html_escape_string(http_connection->url), _html_escape_string(signature_base_string));
unsigned char sha1_result[30];// = malloc(sizeof(char) * 32);
char *key_string = malloc(sizeof(char) * 200);
if(current_oauth_token) {
sprintf(key_string, "%s&%s", OAUTH_CONSUMER_SECRET, current_oauth_token->oauth_token_secret);
} else if(current_access_token) {
sprintf(key_string, "%s&%s", OAUTH_CONSUMER_SECRET, current_access_token->access_token_secret);
} else {
sprintf(key_string, "%s&", OAUTH_CONSUMER_SECRET);
}
printf("signature_base_string: %s\n", signature_base_string);
unsigned int md_len = 30;
// result = HMAC(EVP_sha256(), key, 4, data, 28, NULL, NULL);
int success = hmac_sha1(key_string, strlen(key_string), signature_base_string, strlen(signature_base_string), sha1_result);
// int success = HMAC(EVP_sha1(), key_string, strlen(key_string), signature_base_string, strlen(signature_base_string), sha1_result, &md_len);
// if(success != 0) {
// printf(" Error generating oauth_signature (HMAC-SHA1). \n");
// return NULL;
// }
free(key_string);
free(signature_base_string);
char *final_signature = malloc(sizeof(char) * 100);
b64_encode(&sha1_result, final_signature);
return final_signature;
}
void TwitterAPI_oauth_authenticate_connection(HTTPConnection *http_connection) {
HTTPParameter *oauth_consumer_key_parameter = _create_parameter(strdup("oauth_consumer_key"), strdup(OAUTH_CONSUMER_KEY));
char *random_oauth_nonce = malloc(sizeof(char) * 32);
_generate_random_string(random_oauth_nonce, 32);
HTTPParameter *oauth_nonce_parameter = _create_parameter(strdup("oauth_nonce"), strdup(random_oauth_nonce));
oauth_consumer_key_parameter->next_parameter = oauth_nonce_parameter;
free(random_oauth_nonce);
HTTPParameter *oauth_signature_method_parameter = _create_parameter(strdup("oauth_signature_method"), strdup("HMAC-SHA1"));
oauth_nonce_parameter->previous_parameter = oauth_consumer_key_parameter;
oauth_nonce_parameter->next_parameter = oauth_signature_method_parameter;
char *oauth_timestamp = malloc(sizeof(char) * 30);
sprintf(oauth_timestamp, "%i", (int)time(0));
HTTPParameter *oauth_timestamp_parameter = _create_parameter(strdup("oauth_timestamp"), oauth_timestamp);
oauth_signature_method_parameter->previous_parameter = oauth_nonce_parameter;
oauth_signature_method_parameter->next_parameter = oauth_timestamp_parameter;
HTTPParameter *oauth_version_parameter = _create_parameter(strdup("oauth_version"), strdup("1.0"));
oauth_timestamp_parameter->previous_parameter = oauth_signature_method_parameter;
oauth_timestamp_parameter->next_parameter = oauth_version_parameter;
oauth_version_parameter->previous_parameter = oauth_signature_method_parameter;
HTTPParameter *current_parameter = http_connection->first_parameter;
HTTPParameter *last_configured_parameter = oauth_version_parameter;
if(current_access_token != NULL) {
HTTPParameter *oauth_token_parameter = _create_parameter(strdup("oauth_token"), _html_escape_string(current_access_token->access_token));
oauth_token_parameter->previous_parameter = oauth_version_parameter;
oauth_version_parameter->next_parameter = oauth_token_parameter;
last_configured_parameter = oauth_token_parameter;
}
while(current_parameter != NULL) {
if(current_parameter->type == HTTPParameterTypeAuthorizationHeader) { // needs to be on authorization header
last_configured_parameter->next_parameter = malloc(sizeof(*current_parameter));
// memcpy(last_configured_parameter->next_parameter, current_parameter, sizeof(*current_parameter));
// duplicating...
((HTTPParameter *)last_configured_parameter->next_parameter)->key = strdup(current_parameter->key);
((HTTPParameter *)last_configured_parameter->next_parameter)->value = strdup(current_parameter->value);
((HTTPParameter *)last_configured_parameter->next_parameter)->previous_parameter = last_configured_parameter;
((HTTPParameter *)last_configured_parameter->next_parameter)->type = HTTPParameterTypeIgnore;
last_configured_parameter = last_configured_parameter->next_parameter;
}
current_parameter = current_parameter->next_parameter;
}
last_configured_parameter->next_parameter = http_connection->first_parameter;
if(http_connection->first_parameter) {
http_connection->first_parameter->previous_parameter = last_configured_parameter;
}
char *signature_string = _generate_signature_for_parameters(oauth_consumer_key_parameter, http_connection);
printf(" signature_string: %s\n", signature_string);
// undoing header scheme created to generate signature
last_configured_parameter->next_parameter = NULL;
if(http_connection->first_parameter) {
http_connection->first_parameter->previous_parameter = NULL;
}
HTTPParameter *oauth_signature_parameter = _create_parameter(strdup("oauth_signature"), _html_escape_string(signature_string));
oauth_signature_parameter->type = HTTPParameterTypeAuthorizationHeader;
free(signature_string);
last_configured_parameter->next_parameter = oauth_signature_parameter;
oauth_signature_parameter->previous_parameter = last_configured_parameter;
char *authorization_string = malloc(sizeof(char) * 500);
strcat(authorization_string, "OAuth ");
current_parameter = oauth_consumer_key_parameter;
while(current_parameter != NULL) {
sprintf(authorization_string, "%s%s=\"%s\", ", authorization_string, current_parameter->key, current_parameter->value);
current_parameter = current_parameter->next_parameter;
}
authorization_string[strlen(authorization_string) - 2] = '\0'; // removes last ','
printf("authorization_string: %s\n", authorization_string);
HTTPParameter *authorization_parameter = malloc(sizeof(*authorization_parameter));
HTTPParameter_initialize(authorization_parameter);
authorization_parameter->type = HTTPParameterTypeHeader;
authorization_parameter->key = strdup("Authorization");
authorization_parameter->value = authorization_string;
HTTPParameter_free(oauth_consumer_key_parameter, 1);
HTTPParameter *last_parameter = http_connection->first_parameter;
while(last_parameter != NULL) {
if(last_parameter->next_parameter == NULL) {
break;
}
last_parameter = last_parameter->next_parameter;
}
if(http_connection->first_parameter) {
last_parameter->next_parameter = authorization_parameter;
} else {
http_connection->first_parameter = authorization_parameter;
}
}
int TwitterAPI_oauth_request_token_url(char *request_token_url) {
HTTPConnection http_connection;
HTTPConnection_initialize(&http_connection);
http_connection.url = strdup("http://api.twitter.com/oauth/request_token");
http_connection.connection_method = HTTPConnectionMethodPOST;
http_connection.first_parameter = malloc(sizeof(*http_connection.first_parameter));
HTTPParameter_initialize(http_connection.first_parameter);
http_connection.first_parameter->key = strdup("oauth_callback");
http_connection.first_parameter->value = strdup("oob");
http_connection.first_parameter->type = HTTPParameterTypeAuthorizationHeader;
// printf("http_connection.first_parameter: %i\n", http_connection.first_parameter);
// TwitterAPI_oauth_
TwitterAPI_oauth_authenticate_connection(&http_connection);
int status = HTTPConnection_perform_request(&http_connection);
if(status != 0) return status;
printf(" body: %s\n", http_connection.response_buffer);
current_oauth_token = malloc(sizeof(*current_oauth_token));
OAuthOAuthToken_initialize(current_oauth_token);
int i = 0;
char *substring = strtok(http_connection.response_buffer, "=&");
while(substring != NULL)
{
if(i == 1) { // oauth_token
current_oauth_token->oauth_token = strdup(substring);
} else if(i == 3) { // oauth_token_secret
current_oauth_token->oauth_token_secret = strdup(substring);
}
i += 1;
substring = strtok(NULL, "=&");
}
if(i < 5) {
return -1;
}
printf("oauth_token: %s\n", current_oauth_token->oauth_token);
printf("oauth_token_secret: %s\n", current_oauth_token->oauth_token_secret);
HTTPConnection_free(&http_connection);
sprintf(request_token_url, "http://api.twitter.com/oauth/authenticate?oauth_token=%s", current_oauth_token->oauth_token);
return 0;
}
int TwitterAPI_oauth_authorize_from_pin(char *pin) {
if(current_oauth_token->oauth_token_secret == NULL) {
return -1;
}
current_oauth_token->oauth_verifier = strdup(pin);
HTTPConnection http_connection;
HTTPConnection_initialize(&http_connection);
http_connection.url = strdup("http://api.twitter.com/oauth/access_token");
http_connection.connection_method = HTTPConnectionMethodPOST;
http_connection.first_parameter = malloc(sizeof(*http_connection.first_parameter));
HTTPParameter_initialize(http_connection.first_parameter);
http_connection.first_parameter->key = strdup("oauth_verifier");
http_connection.first_parameter->value = strdup(current_oauth_token->oauth_verifier);
HTTPParameter *second_parameter = malloc(sizeof(*http_connection.first_parameter));
HTTPParameter_initialize(second_parameter);
second_parameter->key = strdup("oauth_token");
second_parameter->value = strdup(current_oauth_token->oauth_token);
http_connection.first_parameter->next_parameter = second_parameter;
TwitterAPI_oauth_authenticate_connection(&http_connection);
int status = HTTPConnection_perform_request(&http_connection);
if(status != 0) return status;
printf(" body: %s\n", http_connection.response_buffer);
OAuthOAuthToken_free(current_oauth_token);
current_oauth_token = NULL;
current_access_token = malloc(sizeof(*current_access_token));
OAuthAccessToken_initialize(current_access_token);
int i = 0;
char *substring = strtok(http_connection.response_buffer, "=&");
while(substring != NULL)
{
if(i == 1) { // oauth_token
current_access_token->access_token = strdup(substring);
} else if(i == 3) { // oauth_token_secret
current_access_token->access_token_secret = strdup(substring);
}
i += 1;
substring = strtok(NULL, "=&");
}
if(i < 5) {
return -1;
}
printf("access_token: %s\n", current_access_token->access_token);
printf("access_token_secret: %s\n", current_access_token->access_token_secret);
OAuthAccessToken_save_current_to_file();
// current_oauth_token = malloc(sizeof(*current_oauth_token));
// OAuthOAuthToken_initialize(current_oauth_token);
//
// int i = 0;
// char *substring = strtok(http_connection.response_buffer, "=&");
//
// while(substring != NULL)
// {
// if(i == 1) { // oauth_token
// current_oauth_token->oauth_token = strdup(substring);
// } else if(i == 3) { // oauth_token_secret
// current_oauth_token->oauth_token_secret = strdup(substring);
// }
//
// i += 1;
// substring = strtok(NULL, "=&");
// }
//
// if(i < 5) {
// return -1;
// }
//
// printf("oauth_token: %s\n", current_oauth_token->oauth_token);
// printf("oauth_token_secret: %s\n", current_oauth_token->oauth_token_secret);
//
HTTPConnection_free(&http_connection);
//
// sprintf(request_token_url, "http://api.twitter.com/oauth/authenticate?oauth_token=%s", current_oauth_token->oauth_token);
return 0;
}