Exemple #1
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;
}
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);
}
Exemple #3
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);
}
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;
}
/**
 * @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;
}