This repository has been archived by the owner on Aug 4, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Oauth.cpp
296 lines (243 loc) · 11.3 KB
/
Oauth.cpp
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
/*
* oauth.cpp
*
* Copyright 2009 Ramy d. <[email protected]>
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
* MA 02110-1301, USA.
*/
#include "Oauth.h"
#include "format.h"
#include "data.h"
Oauth::Oauth(std::string cKey, std::string cSecret, std::string cbURL, std::string authMethod) {
consumerKey = cKey;
consumerSecret = cSecret;
signatureMethod = authMethod;
oauthVersion = "1.0";
callBack = cbURL;
curl = curl_easy_init();
}
std::string Oauth::getTime() {
time_t seconds;
std::ostringstream temporaryStream;
seconds = time (NULL);
temporaryStream << seconds;
return temporaryStream.str();
}
std::string Oauth::getNoonce() {
std::ostringstream temporaryStream;
char tempChar;
char junk[] = {':',';','<','=','>','?','@','[','\\',']','^','_','`'}; //junk characters from '0' in decimal to 'z' in decimal
std::set<char> junkCharacters(junk, junk + sizeof(junk) );
const int noonceLength = 32;
srand((unsigned) (clock () + time(NULL) * CLOCKS_PER_SEC ) ); //gives a seed based on time for the random function
for (int i = 0; i < noonceLength; i++) {
tempChar = (char) (rand() % 75 + '0'); //75 is the number of lowercase letters + uppercase letters + numbers from 0 to 9 + 13 junk letters in between
if ( junkCharacters.find(tempChar) == junkCharacters.end() ) //find the temporary character in the set, if it is not found, proceede
temporaryStream << (char) tempChar;
}
return temporaryStream.str();
}
std::string Oauth::getBaseString(std::vector <std::string> extraParameters) {
std::vector <std::string> baseString;
std::string returnString;
baseString.push_back("oauth_consumer_key=" + format::encode( consumerKey.c_str(), curl ) + "&");
baseString.push_back("oauth_signature_method=" + format::encode( signatureMethod.c_str(), curl ) + "&");
baseString.push_back("oauth_timestamp=" + format::encode( timeStamp.c_str(), curl ) + "&");
baseString.push_back("oauth_nonce=" + format::encode( noonce.c_str(), curl ) + "&");
baseString.push_back("oauth_version=" + format::encode( oauthVersion.c_str(), curl ) + "&");
for (size_t i=0; i<extraParameters.size(); i++) {
baseString.push_back( extraParameters[i] + "&" );
}
baseString = format::sortVector(baseString, '&');
returnString = format::vectorToString(baseString);
return returnString;
}
std::string Oauth::getSignatureBaseString(std::string httpMethod, std::string requestUrl, std::string baseString) {
std::vector <std::string> signatureBaseString;
std::string returnString;
signatureBaseString.push_back(format::encode( httpMethod.c_str(), curl ) + "&");
signatureBaseString.push_back(format::encode( requestUrl.c_str(), curl ) + "&");
signatureBaseString.push_back(format::encode( baseString, curl ) );
returnString = format::vectorToString(signatureBaseString);
//std::cout << "signatureBaseString for signature: " << returnString << std::endl << std::endl;
return returnString;
}
std::string Oauth::generateHeaders(std::string url, std::string method, std::string key, std::vector <std::string> extraParameters, std::vector <std::string> extraHeaders) {
std::vector <std::string> basicHeader;
std::string headerData;
std::string signatureBaseString;
std::string signature;
timeStamp = getTime();
noonce = getNoonce();
if ( signatureMethod == "HMAC-SHA1") {
signatureBaseString = getSignatureBaseString( method, url, getBaseString( extraParameters ) );
signature = format::encrypt(signatureBaseString.c_str(), key.c_str());
} else {
signature = key;
}
basicHeader.push_back("oauth_consumer_key=\"" + format::encode( consumerKey.c_str(), curl ) +"\",");
basicHeader.push_back("oauth_nonce=\"" + format::encode( noonce.c_str(), curl ) + "\",");
basicHeader.push_back("oauth_signature=\"" + format::encode( signature.c_str(), curl ) + "\",");
basicHeader.push_back("oauth_signature_method=\"" + format::encode( signatureMethod.c_str(), curl ) + "\",");
basicHeader.push_back("oauth_timestamp=\"" + format::encode( timeStamp.c_str(), curl ) + "\",");
basicHeader.push_back("oauth_version=\"" + format::encode( oauthVersion.c_str(), curl ) + "\",");
for (size_t i=0; i<extraHeaders.size(); i++) {
basicHeader.push_back( extraHeaders[i] + ",");
}
basicHeader = format::sortVector(basicHeader, ',');
headerData = "Authorization: OAuth " + format::vectorToString(basicHeader);
return headerData;
}
std::string Oauth::requestRequestToken(std::string url, std::string method) {
std::vector <std::string> extraParameters;
std::vector <std::string> extraHeaders;
std::string basicHeaders;
std::string requestUrl;
std::string postData;
std::string key;
std::string postReturn;
struct curl_slist *headers=NULL;
requestUrl = url;
extraParameters.push_back("oauth_callback=" + format::encode( callBack.c_str(), curl ) );
extraHeaders.push_back("oauth_callback=\"" + format::encode( callBack.c_str(), curl ) + "\"");
key = consumerSecret + "&";
basicHeaders = generateHeaders(requestUrl, method, key, extraParameters, extraHeaders);
headers = curl_slist_append(headers, basicHeaders.c_str() );
headers = curl_slist_append(headers, "Connection: Keep-Alive" );
headers = curl_slist_append(headers, "Content-Type: text/html; charset=utf-8" );
postData = "";
postReturn = post(postData, headers, requestUrl, method);
//verify call back function
if ( ( postReturn.find("oauth_token=") != std::string::npos ) && ( postReturn.find("&oauth_token_secret=") != std::string::npos ) ) {
return postReturn;
} else {
std::cout << "authentication failed request token request" << std::endl;
return 0;
}
}
std::string Oauth::requestAccessToken(std::string token, std::string tokenSecret, std::string pinCode , std::string url, std::string method) {
std::vector <std::string> extraParameters;
std::vector <std::string> extraHeaders;
std::string basicHeaders;
std::string requestUrl;
std::string postData;
std::string key;
std::string postReturn;
struct curl_slist *headers=NULL;
requestUrl = url+"?oauth_token="+token;
extraParameters.push_back("oauth_token=" + format::encode( token.c_str(), curl ) );
extraParameters.push_back("oauth_verifier=" + format::encode( pinCode.c_str(), curl ) );
extraHeaders.push_back("oauth_token=\"" + format::encode( token.c_str(), curl ) + "\"");
extraHeaders.push_back("oauth_verifier=\"" + format::encode( pinCode.c_str(), curl ) + "\"");
key = consumerSecret + "&" + tokenSecret;
basicHeaders = generateHeaders(requestUrl, method, key, extraParameters, extraHeaders);
headers = curl_slist_append(headers, basicHeaders.c_str() );
headers = curl_slist_append(headers, "Connection: Keep-Alive" );
headers = curl_slist_append(headers, "Content-Type: text/html; charset=utf-8" );
postData = "";
postReturn = post(postData, headers, requestUrl, method);
//verify call back function
if ( ( postReturn.find("oauth_token=") != std::string::npos ) && ( postReturn.find("&oauth_token_secret=") != std::string::npos ) ) {
return postReturn;
} else {
std::cout << "authentication failed request token request" << std::endl;
return 0;
}
}
std::string Oauth::requestResourceToken(std::string token, std::string tokenSecret, std::string url, std::string method, std::string fieldName, std::string fieldData) {
std::vector <std::string> extraParameters;
std::vector <std::string> extraHeaders;
std::string basicHeaders;
std::string requestUrl;
std::string postData;
std::string key;
std::string postReturn;
struct curl_slist *headers=NULL;
requestUrl = url;
extraParameters.push_back("oauth_token=" + format::encode( token.c_str(), curl ) );
extraHeaders.push_back("oauth_token=\"" + format::encode( token.c_str(), curl ) + "\"");
if (fieldName != "") {
extraParameters.push_back(fieldName + format::encode( fieldData.c_str(), curl ) );
}
key = consumerSecret + "&" + tokenSecret;
basicHeaders = generateHeaders(requestUrl, method, key, extraParameters, extraHeaders);
headers = curl_slist_append(headers, basicHeaders.c_str() );
headers = curl_slist_append(headers, "Connection: Keep-Alive" );
headers = curl_slist_append(headers, "Content-Type: application/x-www-form-urlencoded; charset=utf-8" );
if (fieldName != "") {
postData = fieldName + format::encode( fieldData.c_str(), curl );
} else {
postData = "";
}
postReturn = post(postData, headers, requestUrl, method);
//verify call back function
//euh, what are we verifying for?
//if ( ( postReturn.find("oauth_token=") != std::string::npos ) && ( postReturn.find("&oauth_token_secret=") != std::string::npos ) ) {
//return postReturn;
//} else {
//std::cout << "authentication failed request token request" << std::endl;
//return 0;
//}
return postReturn;
}
std::string Oauth::post(std::string postData, struct curl_slist *headers, std::string url, std::string httpMethod) {
CURLcode curlResult;
std::string curlapiPageBuffer;
std::string curlHeaderBuffer;
///*debug and verbose instructions*/
/*
struct data config;
config.trace_ascii = 1; //enable ascii tracing
curl_easy_setopt(curl, CURLOPT_DEBUGFUNCTION, format::my_trace);
curl_easy_setopt(curl, CURLOPT_DEBUGDATA, &config);
curl_easy_setopt(curl, CURLOPT_VERBOSE, 1); //the DEBUGFUNCTION has no effect until we enable VERBOSE
*/
///*callback function instructions*/
curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, format::writer);
curl_easy_setopt(curl, CURLOPT_WRITEHEADER, &curlHeaderBuffer); //shows response headers
curl_easy_setopt(curl, CURLOPT_WRITEDATA, &curlapiPageBuffer);
///*url and header instructions*/
curl_easy_setopt(curl, CURLOPT_HTTPHEADER, headers);
curl_easy_setopt(curl, CURLOPT_URL, url.c_str() );
curl_easy_setopt(curl, CURLOPT_FOLLOWLOCATION, 1);
///*ssl instructions*/
//curl_easy_setopt(curl, CURLOPT_CAINFO, "./VeriSignClass3ExtendedValidationSSLCA");
//curl_easy_setopt(curl, CURLOPT_SSL_VERIFYPEER, false);
//curl_easy_setopt(curl, CURLOPT_SSL_VERIFYHOST, false);
if (httpMethod == "GET") {
///*get request*/
curl_easy_setopt(curl, CURLOPT_HTTPGET, 1);
} else if (httpMethod == "POST") {
///*post request*/
curl_easy_setopt(curl, CURLOPT_POST, 1);
curl_easy_setopt(curl, CURLOPT_POSTFIELDS, postData.c_str() );
}
///*send the request*/
curlResult = curl_easy_perform(curl);
///*clean up*/
curl_slist_free_all(headers); //free the header list
//curl_easy_cleanup(curl); //clean up
//std::cout << curlHeaderBuffer << std::endl;
//std::cout << curlapiPageBuffer << std::endl;
// Did we succeed?
if (curlResult == CURLE_OK) {
//std::cout << std::endl << "SUCCESS! message sent to Twitter!" << std::endl; //unix style coding requires no confirmation for expected behaviours
return curlapiPageBuffer;
} else {
std::cout << std::endl << "OUPS! there was an error. The message was not sent to URL." << std::endl;
return 0;
}
}