uploaded liboauth for tizen_3.0
[platform/upstream/liboauth.git] / tests / oauthbodyhash.c
1 /**
2  *  @brief experimental code to sign data uploads
3  *  @file oauthbodysign.c
4  *  @author Robin Gareus <robin@gareus.org>
5  *
6  * Copyright 2009, 2012 Robin Gareus <robin@gareus.org>
7  *
8  * Permission is hereby granted, free of charge, to any person obtaining a copy
9  * of this software and associated documentation files (the "Software"), to deal
10  * in the Software without restriction, including without limitation the rights
11  * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
12  * copies of the Software, and to permit persons to whom the Software is
13  * furnished to do so, subject to the following conditions:
14  * 
15  * The above copyright notice and this permission notice shall be included in
16  * all copies or substantial portions of the Software.
17  * 
18  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
19  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
20  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
21  * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
22  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
23  * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
24  * THE SOFTWARE.
25  */
26
27 #include <string.h>
28 #include <stdio.h>
29 #include <stdlib.h>
30 #include <oauth.h>
31
32 int my_data_post(char *url, char *data) {
33   const char *c_key         = "key"; //< consumer key
34   const char *c_secret      = "secret"; //< consumer secret
35   char *t_key               = "tkey"; //< access token key
36   char *t_secret            = "tsecret"; //< access token secret
37
38   char *postarg = NULL;
39   char *req_url = NULL;
40   char *reply   = NULL;
41   char *bh;
42   char *uh;
43   char *sig_url;
44
45   bh=oauth_body_hash_data(strlen(data), data);
46   uh = (char*) malloc((strlen(url)+strlen(bh)+2) * sizeof(char));
47   if (!uh) return -1;
48
49   strcat(uh, url);
50   strcat(uh, "?");
51   strcat(uh, bh);
52
53   printf("URL: %s\n", uh);
54   req_url = oauth_sign_url2(uh, &postarg, OA_HMAC, NULL, c_key, c_secret, t_key, t_secret);
55   printf("POST: %s?%s\n", req_url, postarg);
56   if (uh) free(uh);
57
58   sig_url = malloc(2+strlen(req_url)+strlen(postarg));
59   sprintf(sig_url,"%s?%s",req_url, postarg);
60   reply = oauth_post_data(sig_url, data, strlen(data), "Content-Type: application/json");
61   if(sig_url) free(sig_url);
62
63   if (reply) {
64     printf("REPLY: %s\n", reply);
65     free(reply);
66   } else {
67     printf("Error performing the request\n");
68   }
69   return 0;
70 }
71
72 int main (int argc, char **argv) {
73   char *base_url = "http://localhost/oauthtest.php";
74   char *teststring="Hello World!";
75
76   /* TEST_BODY_HASH_FILE and TEST_BODY_HASH_DATA are only
77    * here as examples and for testing during development.
78    *
79    * the my_data_post() function above uses oauth_body_hash_data() 
80    */
81
82 #if defined TEST_BODY_HASH_FILE || defined TEST_BODY_HASH_DATA
83   char *bh=NULL;
84 #endif
85
86 #ifdef TEST_BODY_HASH_FILE // example hash file
87   char *filename="/tmp/test";
88   bh=oauth_body_hash_file(filename);
89   if (bh) printf("%s\n", bh);
90   if (bh) free(bh);
91 #endif
92
93 #ifdef TEST_BODY_HASH_DATA // example hash data
94   bh=oauth_body_hash_data(strlen(teststring), teststring);
95   if (bh) printf("%s\n", bh);
96   if (bh) free(bh);
97 #endif
98
99   my_data_post(base_url, teststring);
100   return(0);
101 }