Exemplo n.º 1
0
int API::login(const std::string& email, const std::string& password)
{
    int res = 0;
    std::string url;

    std::string token, secret;

    // Get temporary request token
    url = oauth_sign_url2(this->config.oauth_get_temp_token.c_str(), NULL, OA_HMAC, NULL, CONSUMER_KEY.c_str(), CONSUMER_SECRET.c_str(), NULL /* token */, NULL /* secret */);

    std::string request_token_resp = this->getResponse(url);

    char **rv = NULL;
    int rc = oauth_split_url_parameters(request_token_resp.c_str(), &rv);
    qsort(rv, rc, sizeof(char *), oauth_cmpstringp);
    if (rc == 3 && !strncmp(rv[1], "oauth_token=", OAUTH_TOKEN_LENGTH) && !strncmp(rv[2], "oauth_token_secret=", OAUTH_SECRET_LENGTH)) {
        token = rv[1]+OAUTH_TOKEN_LENGTH+1;
        secret = rv[2]+OAUTH_SECRET_LENGTH+1;
        rv = NULL;
    }
    else
    {
        return res;
    }

    // Authorize temporary token and get verifier
    url = this->config.oauth_authorize_temp_token + "?username="******"&password="******"oauth_verifier=", OAUTH_VERIFIER_LENGTH)) {
        verifier = rv[1]+OAUTH_VERIFIER_LENGTH+1;
        rv = NULL;
    }
    else
    {
        return res;
    }

    // Get final token and secret
    url = this->config.oauth_get_token + "?oauth_verifier=" + verifier;
    url = oauth_sign_url2(url.c_str(), NULL, OA_HMAC, NULL, CONSUMER_KEY.c_str(), CONSUMER_SECRET.c_str(), token.c_str(), secret.c_str());
    std::string token_resp = this->getResponse(url);

    rc = oauth_split_url_parameters(token_resp.c_str(), &rv);
    qsort(rv, rc, sizeof(char *), oauth_cmpstringp);
    if (rc == 2 && !strncmp(rv[0], "oauth_token=", OAUTH_TOKEN_LENGTH) && !strncmp(rv[1], "oauth_token_secret=", OAUTH_SECRET_LENGTH)) {
        this->config.oauth_token = rv[0]+OAUTH_TOKEN_LENGTH+1;
        this->config.oauth_secret = rv[1]+OAUTH_SECRET_LENGTH+1;
        free(rv);
        res = 1;
    }

    return res;
}
Exemplo n.º 2
0
/**
 * 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);
}
Exemplo n.º 3
0
int unim_oauth_request(struct unim_login_info *login_info)
{
	char *req_url, *reply;
	int rc = -1;
	int argc = 0;
	char **argv = NULL;
	int i;

	argc = oauth_split_url_parameters(login_info->request_token_uri, &argv);
	if (login_info->request_added_argc) {
		for (i = 0; i < login_info->request_added_argc; i++) {
			oauth_add_param_to_array(&argc, &argv,
					login_info->request_added_argv[i]);
		}
	}
	req_url = oauth_sign_array2(&argc, &argv,
				NULL, OA_HMAC, NULL,
				login_info->consumer_key, login_info->consumer_secret,
				NULL, NULL);
	reply = oauth_http_get(req_url, NULL);
	if (reply) {
		char **rv = NULL;

		printf("HTTP-reply:\n\t%s\n", reply);
		rc = oauth_split_url_parameters(reply, &rv);
		qsort(rv, rc, sizeof(char *), oauth_cmpstringp);
		if (rc >= 2) {
			for (i = 0; i < rc; i++) {
				if (!strncmp(rv[i], "oauth_token=", 12)) {
					login_info->res_token_key = strdup(&(rv[i][12]));
				} else if (!strncmp(rv[i], "oauth_token_secret=", 19)) {
					login_info->res_token_secret = strdup(&(rv[i][19]));
				}
			}
			rc = 0;
		}
		if (rv)
			free(rv);
	}

	if (req_url)
		free(req_url);
	if (reply)
		free(reply);
	if (argc)
		oauth_free_array(&argc, &argv);

	return rc;
}
Exemplo n.º 4
0
/**
 * @brief static function for parse the key/secret info from raw data
 *
 * @param raw raw data
 * @param key key
 * @param secret secret
 *
 * @return error on 1
 */
inline static int get_keysecret(const char* raw, 
                                char** key,
                                char** secret)
{
    int rc;
    char **rv = NULL;
    if (*key) free(*key);
    if (*secret) free(*secret);
#if SAMUEL_DEBUG
    printf("SAMUEL_DEBUG: HTTP-reply: %s\n",raw);
#endif
    rc = oauth_split_url_parameters(raw, &rv);
    //qsort(rv, rc, sizeof(char *), oauth_cmpstringp);
    if( rc>=2
            && !strncmp(rv[0],"oauth_token_secret=",18) 
            && !strncmp(rv[1],"oauth_token=",11)
      ){
        *key=strdup(&(rv[1][12]));
        *secret=strdup(&(rv[0][19]));
#if SAMUEL_DEBUG
        printf("SAMUEL_DEBUG: rv1: '%s'\trv0: '%s'\n", rv[1],rv[0]);
        printf("SAMUEL_DEBUG: key: '%s'\tsecret: '%s'\n",*key, *secret);
#endif
    }
    if(rv) free(rv);
    return 0;
}
Exemplo n.º 5
0
int weibo_oauth_api_call(struct unim_login_info *login_info,
				struct unim_api_call_info *api_call_info)
{
	char *req_param, *reply;
	int rc = -1;
	int argc = 0;
	char **argv = NULL;
	int i;

	argc = oauth_split_url_parameters(api_call_info->uri, &argv);
	if (login_info->request_added_argc) {
		for (i = 0; i < login_info->request_added_argc; i++) {
			oauth_add_param_to_array(&argc, &argv,
					login_info->request_added_argv[i]);
		}
	}
	req_param = oauth_serialize_url_parameters(argc, argv);
	printf("req_param: %s\n", req_param);
	reply = oauth_http_get(api_call_info->uri, req_param);
	if (reply) {
		printf("HTTP-reply:\n\t%s\n", reply);
		api_call_info->result = reply;
		rc = 0;
	}

	if (req_param)
		free(req_param);

	return rc;
}
Exemplo n.º 6
0
static kMap *knh_parseReply(CTX ctx, char *reply)
{
    int rc;
    char **rv = NULL;
    kMap *rmap = NULL;
    DBG_P("HTTP-reply: %s", reply);
    rc = oauth_split_url_parameters(reply, &rv);
    knh_qsort(rv, rc, sizeof(char *), oauth_cmpstringp);
    if (rv != NULL) {
        rmap = new_DataMap(ctx);
        const char *key, *val;
        int i;
        for (i = 0; i < rc; i++) {
            DBG_P("rv[%d]=%s", i, rv[i]);
            val = strchr(rv[i], '=');
            if (val != NULL) {
                val += 1;
                key = rv[i];
                rv[i][val - rv[i] - 1] = '\0';
                knh_DataMap_setString(ctx, rmap, key, val);
            }
            free(rv[i]);
        }
        free(rv);
    }
    return rmap;
}
Exemplo n.º 7
0
int weibo_statuses_update(struct unim_login_info *login_info,
				struct unim_api_call_info *api_call_info)
{
	char *req_param, *reply;
	int rc = -1;
	int argc = 0;
	char **argv = NULL;
	int i;

	argc = oauth_split_url_parameters(api_call_info->uri, &argv);
	if (login_info->request_added_argc) {
		for (i = 0; i < login_info->request_added_argc; i++) {
			oauth_add_param_to_array(&argc, &argv,
					login_info->request_added_argv[i]);
		}
	}
	req_param = oauth_serialize_url_parameters(argc, argv);
	reply = oauth_http_post(api_call_info->uri, req_param);
	if (reply) {
		/* TODO: check replay! */
		api_call_info->result = reply;
		rc = 0;
	}

	if (req_param)
		free(req_param);

	return rc;
}
Exemplo n.º 8
0
int gsocial_parse_reply_access(char *reply, char **token, char **secret)
{
    int retval = 1;
    int rc;  /* Number of url parameters */
    char **rv = NULL; /* url parameters */
    rc = oauth_split_url_parameters(reply, &rv);
    qsort(rv, rc, sizeof(char *), oauth_cmpstringp);
    if (rc == 2 || rc == 4) {
        if (!strncmp(rv[0], "oauth_token=", 11) &&
                !strncmp(rv[1], "oauth_token_secret=", 18)) {
            if (token)
                *token = strdup(&(rv[0][12]));
            if (secret)
                *secret = strdup(&(rv[1][19]));

            retval = 0;
        }
    } else if (rc == 3)
        if(!strncmp(rv[1], "oauth_token=", 11) &&
                !strncmp(rv[2], "oauth_token_secret=", 18)) {
            if(token)
                *token = strdup(&rv[1][12]);
            if(secret)
                *secret = strdup(&rv[2][19]);
            retval = 0;
        }
    if(rv)
        free(rv);

    return retval;
}
Exemplo n.º 9
0
/**
 * split and parse URL parameters replied by a oauth-server
 * into <em>oauth_token</em> and <em>oauth_token_secret</em>.
 */
int parse_reply(const char *reply, char **token, char **secret, int *flags) {
    int rc;
    int ok=0; // error
    char **rv = NULL;
    if (flags) *flags=0;
    rc = oauth_split_url_parameters(reply, &rv);
//qsort(rv, rc, sizeof(char *), oauth_cmpstringp);
    if( rc>=2 ) {
        int i;
        for (i=0; i<rc; i++) {
            if (token && !strncmp(rv[i],"oauth_token=",12)) {
                *token = (char*) xstrdup(&(rv[i][12]));
                ok|=1;
            }
            if (secret && !strncmp(rv[i],"oauth_token_secret=",19) ) {
                *secret= (char*) xstrdup(&(rv[i][19]));
                ok|=2;
            }
            if (strcmp(rv[i],"oauth_callback_confirmed=true") ) {
                if (flags) *flags|=1;
            }
        }
    }
    if(rv) free(rv);
    return ok==3?0:-1;
}
Exemplo n.º 10
0
int
u1db__get_oauth_authorization(u1db_sync_target *st,
    const char *http_method, const char *url,
    char **oauth_authorization)
{
    int status = U1DB_OK;
    struct _http_state *state;
    char *oauth_data = NULL;
    char *http_hdr = NULL;
    int argc = 0;
    int hdr_size = 0, oauth_size = 0;
    char **argv = NULL;

    status = impl_as_http_state(st->implementation, &state);
    if (status != U1DB_OK) {
        return status;
    }
    if (state->consumer_key == NULL || state->consumer_secret == NULL
        || state->token_key == NULL || state->token_secret == NULL)
    {
        return U1DB_INVALID_PARAMETER;
    }
    argc = oauth_split_url_parameters(url, &argv);
    oauth_sign_array2_process(&argc, &argv, NULL, OA_HMAC, http_method,
        state->consumer_key, state->consumer_secret,
        state->token_key, state->token_secret);
    oauth_data = oauth_serialize_url_sep(argc, 1, argv, ", ", 6);
    if (oauth_data == NULL) {
        status = U1DB_INTERNAL_ERROR;
        goto finish;
    }
    oauth_size = strlen(oauth_data);
    // sizeof(auth_header_prefix) includes the trailing null, so we don't
    // need to add 1
    hdr_size = sizeof(auth_header_prefix) + oauth_size;
    http_hdr = (char *)calloc(hdr_size, 1);
    if (http_hdr == NULL) {
        status = U1DB_NOMEM;
        goto finish;
    }
    memcpy(http_hdr, auth_header_prefix, sizeof(auth_header_prefix));
    memcpy(http_hdr + sizeof(auth_header_prefix)-1, oauth_data, oauth_size);
finish:
    if (oauth_data != NULL) {
        free(oauth_data);
    }
    oauth_free_array(&argc, &argv);
    if (status == U1DB_OK) {
        *oauth_authorization = http_hdr;
    } else if (http_hdr != NULL) {
        free(http_hdr);
    }
    return status;
}
Exemplo n.º 11
0
int weibo_oauth_access(struct unim_login_info *login_info)
{
	char *req_param, *reply;
	int rc = -1;
	int argc = 0;
	char **argv = NULL;
	char *postargs = NULL;
	int i;

	argc = oauth_split_url_parameters(login_info->access_token_uri, &argv);
	if (login_info->request_added_argc) {
		for (i = 0; i < login_info->request_added_argc; i++) {
			oauth_add_param_to_array(&argc, &argv,
					login_info->request_added_argv[i]);
		}
	}
	req_param = oauth_serialize_url_parameters(argc, argv);
	printf("req_param: %s\n", req_param);
	reply = oauth_http_post(login_info->access_token_uri, req_param);
	if (reply) {
		JsonParser *jparser;
		GError *err;
		gboolean rb;

		printf("HTTP-reply:\n\t%s\n", reply);
		jparser = json_parser_new();
		rb = json_parser_load_from_data(jparser, reply, strlen(reply), &err);
		if (!rb) {
			printf("json parse failed! err: %s\n", err->message);
			g_object_unref(jparser);
			goto error_out;
		}
		JsonReader *jreader = json_reader_new(json_parser_get_root(jparser));
		rb = json_reader_read_member(jreader, "access_token");
		if (rb) {
			login_info->access_token_key =
				strdup(json_reader_get_string_value(jreader));
			rc = 0;
		}
		g_object_unref(jreader);
	}

error_out:
	if (req_param)
		free(req_param);
	if (reply)
		free(reply);
	if (postargs)
		free(postargs);

	return rc;
}
Exemplo n.º 12
0
/* 
 * a example requesting and parsing a request-token from an OAuth
 * service-provider, excercising the oauth-HTTP GET function.
 * it is almost the same as \ref request_token_example_post below. 
 */
void request_token_example_get()
{
	const char *request_token_uri =
			"http://term.ie/oauth/example/request_token.php";
	const char *req_c_key = "key";
	const char *req_c_secret = "secret";
	char *res_t_key = NULL; // replied key
	char *res_t_secret = NULL; // replied secret
	char *req_url;
	char *reply;

	printf("***** request token example, use method get *****\n");
	req_url = oauth_sign_url2(request_token_uri, NULL, OA_HMAC, NULL,
					req_c_key, req_c_secret, NULL, NULL);
	printf("request url:\n\t%s -> \n\t%s\n", request_token_uri, req_url);
	reply = oauth_http_get(req_url, NULL);
	if (!reply) {
		printf("HTTP request for an oauth request-token failed!\n");
	} else {
		/*
		 * parse reply - example:
		 * "oauth_token=2a71d1c73d2771b00f13ca0acb9836a10477d3c56&oauth_token_secret=a1b5c00c1f3e23fb314a0aa22e990266"
		 */
		int rc;
		char **rv = NULL;

		printf("HTTP-reply:\n\t%s\n", reply);
		rc = oauth_split_url_parameters(reply, &rv);
		qsort(rv, rc, sizeof(char *), oauth_cmpstringp);
		if (rc == 2 &&
			!strncmp(rv[0], "oauth_token=", 11) &&
			!strncmp(rv[1], "oauth_token_secret=", 18)) {
			res_t_key = strdup(&(rv[0][12]));
			res_t_secret = strdup(&(rv[1][19]));
			printf("key:    '%s'\nsecret: '%s'\n", res_t_key, res_t_secret);
		}
		if (rv)
			free(rv);
	}

	if (req_url)
		free(req_url);
	if (reply)
		free(reply);
	if (res_t_key)
		free(res_t_key);
	if (res_t_secret)
		free(res_t_secret);
}
Exemplo n.º 13
0
/*
 * a example requesting and parsing a request-token from an OAuth
 * service-provider, using the oauth-HTTP POST function.
 */
void request_token_example_post()
{
	const char *request_token_uri =
				"http://term.ie/oauth/example/request_token.php";
	const char *req_c_key = "key";
	const char *req_c_secret = "secret";
	char *res_t_key = NULL;
	char *res_t_secret = NULL;
	char *postarg = NULL;
	char *req_url;
	char *reply;

	printf("***** request token example, use method post *****\n");
	req_url = oauth_sign_url2(request_token_uri, &postarg, OA_HMAC, NULL,
						req_c_key, req_c_secret, NULL, NULL);
	printf("request url:\n\t%s -> \n\t%s\n", request_token_uri, req_url);
	reply = oauth_http_post(req_url, postarg);
	printf("post arg:\n\t%s\n", postarg);
	if (!reply)  {
		printf("HTTP request for an oauth request-token failed!\n");
	} else {
		int rc;
		char **rv = NULL;
		printf("HTTP-reply:\n\t%s\n", reply);
		rc = oauth_split_url_parameters(reply, &rv);
		qsort(rv, rc, sizeof(char *), oauth_cmpstringp);
		if (rc == 2 &&
			!strncmp(rv[0], "oauth_token=", 11) &&
			!strncmp(rv[1], "oauth_token_secret=", 18)) {
			res_t_key = strdup(&(rv[0][12]));
			res_t_secret = strdup(&(rv[1][19]));
			printf("key:    '%s'\nsecret: '%s'\n", res_t_key, res_t_secret);
		}
		if (rv)
			free(rv);
	}

	if (req_url)
		free(req_url);
	if (postarg)
		free(postarg);
	if (reply)
		free(reply);
	if (res_t_key)
		free(res_t_key);
	if (res_t_secret)
		free(res_t_secret);
}
Exemplo n.º 14
0
/*
 * a example requesting and parsing a request-token from an OAuth service-provider
 * using the oauth-HTTP POST function.
 */
void request_token_example_post(void) {
#if 0
  const char *request_token_uri = "http://oauth-sandbox.mediamatic.nl/module/OAuth/request_token";
  const char *req_c_key         = "17b09ea4c9a4121145936f0d7d8daa28047583796"; //< consumer key
  const char *req_c_secret      = "942295b08ffce77b399419ee96ac65be"; //< consumer secret
#else
  const char *request_token_uri = "http://term.ie/oauth/example/request_token.php";
  const char *req_c_key         = "key"; //< consumer key
  const char *req_c_secret      = "secret"; //< consumer secret
#endif
  char *res_t_key    = NULL; //< replied key
  char *res_t_secret = NULL; //< replied secret

  char *postarg = NULL;
  char *req_url;
  char *reply;

  req_url = oauth_sign_url2(request_token_uri, &postarg, OA_HMAC, NULL, req_c_key, req_c_secret, NULL, NULL);

  printf("request URL:%s\n\n", req_url);
  reply = oauth_http_post(req_url,postarg);
  if (!reply) 
    printf("HTTP request for an oauth request-token failed.\n");
  else {
    //parse reply - example:
    //"oauth_token=2a71d1c73d2771b00f13ca0acb9836a10477d3c56&oauth_token_secret=a1b5c00c1f3e23fb314a0aa22e990266"
    int rc;
    char **rv = NULL;
    printf("HTTP-reply: %s\n", reply);
    rc = oauth_split_url_parameters(reply, &rv);
    qsort(rv, rc, sizeof(char *), oauth_cmpstringp);
    if( rc==2 
	&& !strncmp(rv[0],"oauth_token=",11)
	&& !strncmp(rv[1],"oauth_token_secret=",18) ){
	  res_t_key=strdup(&(rv[0][12]));
	  res_t_secret=strdup(&(rv[1][19]));
	  printf("key:    '%s'\nsecret: '%s'\n",res_t_key, res_t_secret);
    }
    if(rv) free(rv);
  }

  if(req_url) free(req_url);
  if(postarg) free(postarg);
  if(reply) free(reply);
  if(res_t_key) free(res_t_key);
  if(res_t_secret) free(res_t_secret);
}
Exemplo n.º 15
0
/**
 * split and parse URL parameters replied by the server
 * into <em>oauth_token</em> and <em>oauth_token_secret</em>.
 */
static gboolean liboauth_parse_reply ( const char *reply, gchar **token, gchar **secret )
{
	int rc;
	gboolean parsed = FALSE;
	char **rv = NULL;
	rc = oauth_split_url_parameters ( reply, &rv );
	qsort ( rv, rc, sizeof(char *), oauth_cmpstringp );
	if ( rc==2
	     && !strncmp(rv[0],"oauth_token=",11)
	     && !strncmp(rv[1],"oauth_token_secret=",18) ) {
		parsed = TRUE;
		if (token)
			*token = g_strdup ( &(rv[0][12]) );
		if (secret)
			*secret = g_strdup ( &(rv[1][19]) );
	}
	g_free ( rv );
	return parsed;
}
Exemplo n.º 16
0
/*
 * test request normalization
 */
int test_normalize(char *param, char *expected) {
  int rv=2;
  int  i, argc;
  char **argv = NULL;
  char *testcase;

  argc = oauth_split_url_parameters(param, &argv);
  qsort(argv, argc, sizeof(char *), oauth_cmpstringp);
  testcase= oauth_serialize_url(argc,0, argv);

  rv=strcmp(testcase,expected);
  if (rv) {
    printf("parameter normalization test failed for: '%s'.\n"
           " got: '%s' expected: '%s'\n", param, testcase, expected);
  }
  else if (loglevel) printf("parameter normalization ok. ('%s')\n", testcase);
  for (i=0;i<argc;i++) free(argv[i]);
  if (argv) free(argv);
  if (testcase) free(testcase);
  return (rv);
}
Exemplo n.º 17
0
/*!
 * \brief   Found a copy the OAuth key and secret from the server answer.
 * \param       answer   server raw answer
 * \param[out]  key      char* where the OAuth key should be stored
 * \param[out]  secret   char* where the OAuth secret should be stored
 * \return  indicates whether the key and the secret were founded or not.
 */
bool drbParseOauthTokenReply(const char *answer, char **key, char **secret) {
    bool ok = true;
    char **rv = NULL;
    char* params[] = { "oauth_token=", "oauth_token_secret=" };
    
    *key = *secret = NULL;
    
    int nbParam = sizeof(params) / sizeof(params[0]);
    
    int rc = oauth_split_url_parameters(answer, &rv);
    qsort(rv, rc, sizeof(char *), oauth_cmpstringp);
    
    if (rc >= nbParam) {
        int *paramIndex, j = 0;
        size_t paramLen = strlen(params[j]);
        if ((paramIndex = malloc(nbParam * sizeof(int))) != NULL) {
            for (int i = 0; i < rc && j < nbParam; i++)
                if (!strncmp(params[j], rv[i], paramLen)) {
                    paramIndex[j++] = i;
                    if (j < nbParam)
                        paramLen = strlen(params[j]);
                }
            if (j == nbParam) {
                if (key)    *key    = strdup(&(rv[paramIndex[0]][12]));
                if (secret) *secret = strdup(&(rv[paramIndex[1]][19]));
            }
            free(paramIndex);
        }else  ok = false;
    } else  ok = false;
    
    if (!ok) {
        free(*key), *key = NULL;
        free(*secret), *secret = NULL;
    }
    
    free(rv);
    return ok;
}
Exemplo n.º 18
0
/*
 * test request concatenation
 */
int test_request(char *http_method, char *request, char *expected) {
  int rv=2;
  int  i, argc;
  char **argv = NULL;
  char *query, *testcase;

  argc = oauth_split_url_parameters(request, &argv);
  qsort(&argv[1], argc-1, sizeof(char *), oauth_cmpstringp);
  query= oauth_serialize_url(argc,1, argv);
  testcase = oauth_catenc(3, http_method, argv[0], query);

  rv=strcmp(testcase,expected);
  if (rv) {
    printf("request concatenation test failed for: '%s'.\n"
           " got:      '%s'\n expected: '%s'\n", request, testcase, expected);
  }
  else if (loglevel) printf("request concatenation ok.\n");
  for (i=0;i<argc;i++) free(argv[i]);
  if (argv) free(argv);
  if (query) free(query);
  if (testcase) free(testcase);
  return (rv);
}
static void
sign_message (SoupMessage *message, OAuthMethod method,
	      GsAuth *auth)
{
	g_autofree gchar *url = NULL, *oauth_authorization_parameters = NULL, *authorization_text = NULL;
	gchar **url_parameters = NULL;
	int url_parameters_length;
	const gchar *consumer_key;
	const gchar *consumer_secret;
	const gchar *token_key;
	const gchar *token_secret;

	if (auth == NULL)
		return;

	consumer_key = gs_auth_get_metadata_item (auth, "consumer-key");
	consumer_secret = gs_auth_get_metadata_item (auth, "consumer-secret");
	token_key = gs_auth_get_metadata_item (auth, "token-key");
	token_secret = gs_auth_get_metadata_item (auth, "token-secret");
	if (consumer_key == NULL || consumer_secret == NULL || token_key == NULL || token_secret == NULL)
		return;

	url = soup_uri_to_string (soup_message_get_uri (message), FALSE);

	url_parameters_length = oauth_split_url_parameters(url, &url_parameters);
	oauth_sign_array2_process (&url_parameters_length, &url_parameters,
				   NULL,
				   method,
				   message->method,
				   consumer_key, consumer_secret,
				   token_key, token_secret);
	oauth_authorization_parameters = oauth_serialize_url_sep (url_parameters_length, 1, url_parameters, ", ", 6);
	oauth_free_array (&url_parameters_length, &url_parameters);
	authorization_text = g_strdup_printf ("OAuth realm=\"Ratings and Reviews\", %s",
					      oauth_authorization_parameters);
	soup_message_headers_append (message->request_headers, "Authorization", authorization_text);
}
Exemplo n.º 20
0
static int parse_osp_reply(const char *reply, char **token, char **secret)
{
	int rc;
	int retval = 1;
	char **rv = NULL;
	rc = oauth_split_url_parameters(reply, &rv);
	qsort(rv, rc, sizeof(char *), oauth_cmpstringp);
	if (rc == 2 || rc == 4) {
		if (!strncmp(rv[0], "oauth_token=", 11) &&
		    !strncmp(rv[1], "oauth_token_secret=", 18)) {
			if (token)
				*token = strdup(&(rv[0][12]));
			if (secret)
				*secret = strdup(&(rv[1][19]));

			retval = 0;
		}
	} else if (rc == 3) {
		if (!strncmp(rv[1], "oauth_token=", 11) &&
		    !strncmp(rv[2], "oauth_token_secret=", 18)) {
			if (token)
				*token = strdup(&(rv[1][12]));
			if (secret)
				*secret = strdup(&(rv[2][19]));

			retval = 0;
		}
	}

	dbg("token: %s\n", *token);
	dbg("secret: %s\n", *secret);

	if (rv)
		free(rv);

	return retval;
}
Exemplo n.º 21
0
/**
 * @brief Use API key, response key-pair and verification code 
 * to retrive the permanent key-pair
 *
 * @param req_key API key
 * @param req_secret API secret key
 * @param res_key the address of the response key
 * @param res_secret the address of the response secret key
 * @param verifier verification code
 *
 * @return error on 1
 */
inline static int get_access_token(char* req_key, 
                                   char* req_secret,
                                   char** res_key, 
                                   char** res_secret,
                                   char* verifier){
    
    int fail = 0;
    char *request_token_uri = s_concate(&(plurk_url), &(plurk_uri_access));

    char *postarg = NULL;
    char *req_url;
    char *reply;

    int verifier_len = strlen(verifier);
    char *verifier_perm = malloc(sizeof(char)*(15 + verifier_len + 1));
    memset(verifier_perm, 0, 15 + verifier_len + 1);
    verifier_perm = memcpy(verifier_perm, "oauth_verifier=", 15);
    verifier_perm = strncat(verifier_perm, verifier, verifier_len);


    //req_url = oauth_sign_url2(request_token_uri, // const char *url
    //        &postarg,                            // char **postarg 
    //        OA_HMAC,                             // OAuthMethod method
    //        "POST",                              // const char *http_method
    //        req_key,                           // const char *key
    //        req_secret,                        // const char *secret
    //        res_t_key,                        // const char *t_key
    //        res_t_secret                        // char *t_secret
    //        );
    // transfer oauth_sign_url2() in steps
    // example edited from: 
    // http://liboauth.sourceforge.net/tests_2oauthtest2_8c-example.html#a0
    int argc=0;
    int i;
    char **argv=NULL;
    char *req_hdr = NULL;
    char *http_hdr= NULL;

    argc = oauth_split_url_parameters(request_token_uri, &argv);
#if SAMUEL_DEBUG
    printf("SAMUEL_DEBUG, before add parameters to array\n");
    for (i=0;i<argc; i++)
        printf("%d:%s\n",i,argv[i]);
#endif

    // the most important step here!! add parameter
    oauth_add_param_to_array(&argc, &argv, verifier_perm);

#if SAMUEL_DEBUG
    printf("====\nSAMUEL_DEBUG, after add parameters to array\n");
    for (i=0;i<argc; i++)
        printf("%d:%s\n",i,argv[i]);
#endif

    //do something important here??
    oauth_sign_array2_process(&argc, &argv,
            NULL, //< postargs (unused)
            OA_HMAC,
            "POST", //< HTTP method (defaults to "GET")
            req_key, req_secret,//NULL, NULL);
            *res_key, *res_secret);

    req_hdr = oauth_serialize_url_sep(argc, 1, argv, ", ", 6);
    req_url = oauth_serialize_url_sep(argc, 0, argv, "&", 1);
    oauth_free_array(&argc, &argv);
#if SAMUEL_DEBUG
    printf("SAMUEL_DEBUG, req_hdr: %s\n",req_hdr);
    printf("SAMUEL_DEBUG, req_url: %s\n",req_url);
#endif
    http_hdr = malloc(strlen(req_hdr) + 100);
    memset(http_hdr,0,100);
    sprintf(http_hdr, "Authorization: OAuth realm=\"\", %s", req_hdr);
#if SAMUEL_DEBUG
    printf("request URL=%s\n", req_url);
    printf("request header=%s\n\n", http_hdr);
#endif

    // POST HTTPMethod
    reply = oauth_http_post2(req_url,&postarg,http_hdr);
    // GET HTTPMethod
    //reply = oauth_http_get2(req_url,postarg,http_hdr);

    if(!reply){
        printf("SAMUEL_DEBUG, HTTP request for an oauth request-token failed.\n");
        return (fail = 1);
    }
    else 
        fail = get_keysecret(reply, res_key, res_secret);
#if SAMUEL_DEBUG
    printf("SAMUEL_DEBUG, res_key: '%s'\t res_secret: '%s'\n", *res_key, *res_secret);
    printf("Samuel_DEBUG, final %x\n",res_key);
    printf("SAMUEL_DEBUG, postarg: %s\n",postarg);
#endif


    if(http_hdr) free(http_hdr);
    if(req_hdr) free(req_hdr);
    if(verifier_perm) free(verifier_perm);
    if(reply) free(reply);
    if(req_url) free(req_url);
    if(postarg) free(postarg);
    if(request_token_uri) free(request_token_uri);

    return fail;
}
Exemplo n.º 22
0
/* 
 * a example requesting and parsing a request-token from an OAuth service-provider
 * using OAuth HTTP Authorization header:
 * see http://oauth.net/core/1.0a/#auth_header
 * and http://oauth.net/core/1.0a/#consumer_req_param
 */
void request_token_example_get(void) {
#if 0
  const char *request_token_uri = "http://localhost/oauthtest.php?test=test";
  const char *req_c_key         = "17b09ea4c9a4121145936f0d7d8daa28047583796"; //< consumer key
  const char *req_c_secret      = "942295b08ffce77b399419ee96ac65be"; //< consumer secret
#else
  const char *request_token_uri = "http://term.ie/oauth/example/request_token.php";
  const char *req_c_key         = "key"; //< consumer key
  const char *req_c_secret      = "secret"; //< consumer secret
#endif
  char *res_t_key    = NULL; //< replied key
  char *res_t_secret = NULL; //< replied secret

  char *req_url = NULL;
  char *req_hdr = NULL;
  char *http_hdr= NULL;
  char *reply;


  //req_url = oauth_sign_url2(request_token_uri, NULL, OA_HMAC, NULL, req_c_key, req_c_secret, NULL, NULL);

  // oauth_sign_url2 (see oauth.h) in steps
  int  argc;
  char **argv = NULL;

  argc = oauth_split_url_parameters(request_token_uri, &argv);

  oauth_sign_array2_process(&argc, &argv,
          NULL, //< postargs (unused)
          OA_HMAC,
          NULL, //< HTTP method (defaults to "GET")
          req_c_key, req_c_secret, NULL, NULL);

  // 'default' oauth_sign_url2 would do:
  // req_url = oauth_serialize_url(argc, 0, argv);
 
  // we split [x_]oauth_ parameters (for use in HTTP Authorization header)
  req_hdr = oauth_serialize_url_sep(argc, 1, argv, ", ", 6);
  // and other URL parameters 
  req_url = oauth_serialize_url_sep(argc, 0, argv, "&", 1);

  oauth_free_array(&argc, &argv);

  // done with OAuth stuff, now perform the HTTP request.
  http_hdr = malloc(strlen(req_hdr) + 55);

  // Note that (optional) 'realm' is not to be 
  // included in the oauth signed parameters and thus only added here.
  // see 9.1.1 in http://oauth.net/core/1.0/#anchor14
  sprintf(http_hdr, "Authorization: OAuth realm=\"http://example.org/\", %s", req_hdr);

  printf("request URL=%s\n", req_url);
  printf("request header=%s\n\n", http_hdr);
  reply = oauth_http_get2(req_url,NULL, http_hdr);
  if (!reply) 
    printf("HTTP request for an oauth request-token failed.\n");
  else {
    // parse reply - example:
    //"oauth_token=2a71d1c73d2771b00f13ca0acb9836a10477d3c56&oauth_token_secret=a1b5c00c1f3e23fb314a0aa22e990266"
    int rc;
    char **rv = NULL;

    printf("HTTP-reply: %s\n", reply);
    rc = oauth_split_url_parameters(reply, &rv);
    qsort(rv, rc, sizeof(char *), oauth_cmpstringp);
    if( rc==2 
	&& !strncmp(rv[0],"oauth_token=",11)
	&& !strncmp(rv[1],"oauth_token_secret=",18) ){
	  res_t_key=strdup(&(rv[0][12]));
	  res_t_secret=strdup(&(rv[1][19]));
	  printf("key:    '%s'\nsecret: '%s'\n",res_t_key, res_t_secret);
    }
    if(rv) free(rv);
  }

  if(req_url) free(req_url);
  if(req_hdr) free(req_hdr);
  if(http_hdr)free(http_hdr);
  if(reply) free(reply);
  if(res_t_key) free(res_t_key);
  if(res_t_secret) free(res_t_secret);
}
Exemplo n.º 23
0
int plurk_api( key_pair* request
              ,key_pair* permanent
              ,const char* api_uri
              ,const char* invoke_method
              ,int arg_count, ...
              )
{

    char* request_uri = s_concate(&(plurk_url), &(plurk_uri_request));
    char* api_url = s_concate( &(plurk_url) ,&(api_uri));

    int argc = 0;
    int i = 0;
    char** argv    = NULL;
    char* req_hdr  = NULL;
    char* req_url  = NULL;
    char* http_hdr = NULL;
    char* reply    = NULL;
    char* postarg  = NULL;

    argc = oauth_split_url_parameters(api_url, &argv);

#if SAMUEL_DEBUG
    printf("SAMUEL_DEBUG, before add parameters to array\n");
    for (i=0;i<argc; i++)
        printf("%d:%s\n",i,argv[i]);
#endif
    
    int arg_index;
    char* arg_from_valist;
    va_list vaarg;
    va_start(vaarg, arg_count);
    for (arg_index = 0; arg_index < arg_count; arg_index++) {
        arg_from_valist = va_arg(vaarg, char*);
        oauth_add_param_to_array(&argc, &argv, arg_from_valist);
#if SAMUEL_DEBUG
        printf("SAMUEL_DEBUG, arg_from_valist: %s\n", arg_from_valist);
        printf("SAMUEL_DEBUG, argc:%d\n", argc);
#endif
    }
    va_end(vaarg);

#if SAMUEL_DEBUG
    printf("SAMUEL_DEBUG, before add parameters to array\n");
    for (i=0;i<argc; i++)
        printf("%d:%s\n",i,argv[i]);
#endif
    oauth_sign_array2_process(&argc, &argv,
            NULL, //< postargs (unused)
            OA_HMAC,
            "POST", //< HTTP method (defaults to "GET")
            request->key, request->secret,//NULL, NULL);
            permanent->key, permanent->secret);

    req_hdr = oauth_serialize_url_sep(argc, 1, argv, ", ", 100);
    req_url = oauth_serialize_url_sep(argc, 0, argv, "&", 1);
    oauth_free_array(&argc, &argv);
#if SAMUEL_DEBUG
    printf("SAMUEL_DEBUG, req_hdr: %s\n", req_hdr);
    printf("SAMUEL_DEBUG, req_url: %s\n", req_url);
#endif

    http_hdr = malloc(strlen(req_hdr) + 200);
    memset(http_hdr,0,100);
    sprintf(http_hdr, "Authorization: OAuth realm=\"\", %s", req_hdr);

#if SAMUEL_DEBUG
    printf("request URL=%s\n", req_url);
    printf("request header=%s\n\n", http_hdr);
#endif

    reply = oauth_http_post2(req_url, &postarg, http_hdr);
    if(!reply){
        printf("SAMUEL_DEBUG, HTTP request for an oauth request-token failed.\n");
    }
    else 
        printf("SAMUEL_DEBUG, reply: %s\n", reply);

    if (reply)    free(reply);
    if (req_hdr)  free(req_hdr);
    if (req_url)  free(req_url);
    if (http_hdr) free(http_hdr);
    if (postarg)  free(postarg);
    if (api_url)  free(api_url);

    return 0;
}