/** * parses all but 'oauth_signature' into an parameter array. * inverse: oauth_serialize_url() - here: format_array(); * * @param mode bit1 selects GET/POST type of escaping parameters. */ int url_to_array(int *argcp, char ***argvp, const int mode, const char *url) { if (mode&2) // POST (*argcp) = oauth_split_post_paramters(url, argvp, 2); // bit0(1): replace '+', bit1(2): don't replace '\001' -> '&' else if ((mode&2) == 0) // GET (*argcp) = oauth_split_url_parameters(url, argvp); // same as oauth_split_post_paramters(url, &argv, 1); else { (*argcp) = oauth_split_url_parameters(url, argvp); // same as oauth_split_post_paramters(url, &argv, 1); } return (*argcp); }
gboolean drop_oauth_from_file(gchar **appname, gchar **consumer_key, gchar **consumer_secret, gchar **access_key, gchar **access_secret) { gchar *response = NULL; gint argc = 0; gchar **argv = NULL; gchar **pair = NULL; gboolean result = FALSE; gchar *oauthfile = NULL; oauthfile = drop_oauth_expandfilename(OAUTHFILE); if(g_file_get_contents(oauthfile, &response, NULL, NULL)) { argc = oauth_split_post_paramters(response, &argv, 0); while(argc) { pair = g_strsplit(argv[argc - 1], "=", 0); if(g_strcmp0(pair[0], "appname") == 0) *appname = g_strdup(pair[1]); if(g_strcmp0(pair[0], "consumer_key") == 0) *consumer_key = g_strdup(pair[1]); if(g_strcmp0(pair[0], "consumer_secret") == 0) *consumer_secret = g_strdup(pair[1]); if(g_strcmp0(pair[0], "access_key") == 0) *access_key = g_strdup(pair[1]); if(g_strcmp0(pair[0], "access_secret") == 0) *access_secret = g_strdup(pair[1]); argc--; g_strfreev(pair); } if(*appname && *consumer_key && *consumer_secret && *access_key && *access_secret) result = TRUE; oauth_free_array(&argc, &argv); } g_free(response); g_free(oauthfile); return result; }
void drop_oauth_access_token(gchar *consumer_key, gchar *consumer_secret, gchar *request_key, gchar *request_secret, gchar **access_key, gchar **access_secret, GError **error) { gchar *response = NULL; gint argc = 0; gchar **argv = NULL; gchar *postarg = NULL; gchar **token = NULL; oauth_sign_url2(ACCESS_TOKEN_URL, &postarg, OA_HMAC, NULL, consumer_key, consumer_secret, request_key, request_secret); response = drop_soup_sync(ACCESS_TOKEN_URL, postarg, DROP_SOUP_OAUTH, error); argc = oauth_split_post_paramters(response, &argv, 0); while(argc) { token = g_strsplit(argv[argc - 1], "=", 0); if(g_strcmp0(token[0], "oauth_token") == 0) *access_key = g_strdup(token[1]); if(g_strcmp0(token[0], "oauth_token_secret") == 0) *access_secret = g_strdup(token[1]); argc--; } if(access_key == NULL || access_secret == NULL) { g_print("things went wrong!! not able to get access_{key,secret}!!\n"); exit(EXIT_FAILURE); } g_free(postarg); g_free(response); g_strfreev(token); oauth_free_array(&argc, &argv); }
void drop_oauth_request_token(gchar *consumer_key, gchar *consumer_secret, gchar **request_key, gchar **request_secret, GError **error) { gchar *request = NULL; gchar *response = NULL; gchar **token = NULL; gint argc = 0; gchar **argv = NULL; gchar *postarg = NULL; request = g_strdup_printf("%s", REQ_TOKEN_URL); oauth_sign_url2(request, &postarg, OA_HMAC, NULL, consumer_key, consumer_secret, NULL, NULL); response = drop_soup_sync(REQ_TOKEN_URL, postarg, 0, error); argc = oauth_split_post_paramters(response, &argv, 0); while(argc) { token = g_strsplit(argv[argc - 1], "=", 0); if(g_strcmp0(token[0], "oauth_token") == 0) *request_key = g_strdup(token[1]); if(g_strcmp0(token[0], "oauth_token_secret") == 0) *request_secret = g_strdup(token[1]); argc--; } g_free(request); g_free(postarg); g_free(response); g_strfreev(token); oauth_free_array(&argc, &argv); }
static int ckl_transport_run(ckl_transport_t *t, ckl_conf_t *conf, ckl_msg_t* m) { long httprc = -1; CURLcode res; char *url = strdup(conf->endpoint); if (t->append_url) { free(url); url = strappend(conf->endpoint, t->append_url); } if (conf->oauth_key && conf->oauth_secret) { int i; int argc; char **argv = NULL; char *url2; struct curl_httppost *tmp; argc = oauth_split_post_paramters(url, &argv, 0); for (tmp = t->formpost; tmp != NULL; tmp = tmp->next) { char *p = NULL; if (asprintf(&p, "%s=%s", tmp->name, tmp->contents) < 0) { fprintf(stderr, "Broken asprintf: %s = %s\n", tmp->name, tmp->contents); return -1; } oauth_add_param_to_array(&argc, &argv, p); free(p); } url2 = oauth_sign_array2(&argc, &argv, NULL, OA_HMAC, "POST", conf->oauth_key, conf->oauth_secret, "", ""); //fprintf(stderr, "url = %s\n", url2); free(url2); curl_formfree(t->formpost); t->formpost = NULL; t->lastptr = NULL; for (i = 1; i < argc; i++) { char *s = strdup(argv[i]); char *p = strchr(s, '='); if (p == NULL) { fprintf(stderr, "Broken argv: %s = %s\n", tmp->name, tmp->contents); return -1; } *p = '\0'; p++; //fprintf(stderr, "argv[%d]: %s = %s\n", i, s, p); curl_formadd(&t->formpost, &t->lastptr, CURLFORM_COPYNAME, s, CURLFORM_COPYCONTENTS, p, CURLFORM_END); } oauth_free_array(&argc, &argv); } if (m && m->script_log != NULL) { curl_formadd(&t->formpost, &t->lastptr, CURLFORM_COPYNAME, "scriptlog", CURLFORM_FILE, m->script_log, CURLFORM_FILENAME, "script.log", CURLFORM_CONTENTTYPE, "text/plain", CURLFORM_END); } curl_easy_setopt(t->curl, CURLOPT_HTTPPOST, t->formpost); curl_easy_setopt(t->curl, CURLOPT_URL, url); res = curl_easy_perform(t->curl); if (res != 0) { fprintf(stderr, "Failed talking to endpoint %s: (%d) %s\n\n", conf->endpoint, res, curl_easy_strerror(res)); return -1; } curl_easy_getinfo(t->curl, CURLINFO_RESPONSE_CODE, &httprc); if (httprc >299 || httprc <= 199) { fprintf(stderr, "Endpoint %s returned HTTP %d\n", conf->endpoint, (int)httprc); if (httprc == 403) { fprintf(stderr, "Are you sure your secret is correct?\n"); } return -1; } free(url); return 0; }