-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathoAuthRightSignature.cs
executable file
·340 lines (288 loc) · 11.8 KB
/
oAuthRightSignature.cs
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
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Net;
using System.IO;
using System.Collections.Specialized;
using System.Runtime.Remoting.Messaging;
using System.Text;
namespace RightSignature
{
public class OAuthRightSignature : oAuthBase
{
public enum Method { GET, POST, PUT, DELETE };
private static string _token;
private static string _tokenSecret;
#region PublicProperties
public static string Token { get { return _token; } set { _token = value; } }
public static string TokenSecret { get { return _tokenSecret; } set { _tokenSecret = value; } }
#endregion
public void _initialize()
{
String requestToken = this.getRequestToken();
Console.WriteLine(this.AuthorizationLink);
Console.WriteLine("Visit the link above to authorize and then paste the oauth_verifier value here and hit ENTER:");
String verifier = Console.ReadLine();
this.setVerifier(verifier);
String accessToken = this.getAccessToken();
Console.WriteLine(_getHeader("https://staging.rightsignaturedev.com/api/documents.xml","GET"));
}
/// <summary>
/// Get the linkedin request token using the consumer key and secret. Also initializes tokensecret
/// </summary>
/// <returns>The request token.</returns>
public String getRequestToken() {
string ret = null;
string response = oAuthWebRequest(Method.POST, Configuration.RequestTokenUrl, String.Empty);
if (response.Length > 0)
{
NameValueCollection qs = HttpUtility.ParseQueryString(response);
if (qs["oauth_token"] != null)
{
Token = qs["oauth_token"];
TokenSecret = qs["oauth_token_secret"];
ret = Token;
}
}
return ret;
}
/// <summary>
/// Get the access token
/// </summary>
/// <returns>The access token.</returns>
public String getAccessToken() {
if (string.IsNullOrEmpty(Token) || string.IsNullOrEmpty(Verifier))
{
Exception e = new Exception("The request token and verifier were not set");
throw e;
}
string response = oAuthWebRequest(Method.POST, Configuration.AccessTokenUrl, string.Empty);
if (response.Length > 0)
{
NameValueCollection qs = HttpUtility.ParseQueryString(response);
if (qs["oauth_token"] != null)
{
Token = qs["oauth_token"];
}
if (qs["oauth_token_secret"] != null)
{
TokenSecret = qs["oauth_token_secret"];
}
}
return Token;
}
public void setVerifier(string _verifier)
{
Verifier = _verifier;
}
/// <summary>
/// Get the link to Linked In's authorization page for this application.
/// </summary>
/// <returns>The url with a valid request token, or a null string.</returns>
public string AuthorizationLink
{
get { return Configuration.AuthorizeUrl + "?oauth_token=" + Token; }
}
/// <summary>
/// Submit a web request using oAuth.
/// </summary>
/// <param name="method">GET or POST</param>
/// <param name="url">The full url, including the querystring.</param>
/// <param name="postData">Data to post (querystring format)</param>
/// <returns>The web server response.</returns>
public string oAuthWebRequest(Method method, string url, string postData)
{
string outUrl = "";
string querystring = "";
string ret = "";
//Setup postData for signing.
//Add the postData to the querystring.
if (method == Method.POST)
{
if (postData.Length > 0)
{
//Decode the parameters and re-encode using the oAuth UrlEncode method.
NameValueCollection qs = HttpUtility.ParseQueryString(postData);
postData = "";
foreach (string key in qs.AllKeys)
{
if (postData.Length > 0)
{
postData += "&";
}
qs[key] = HttpUtility.UrlDecode(qs[key]);
qs[key] = this.UrlEncode(qs[key]);
postData += key + "=" + qs[key];
}
if (url.IndexOf("?") > 0)
{
url += "&";
}
else
{
url += "?";
}
url += postData;
}
}
Uri uri = new Uri(url);
string nonce = this.GenerateNonce();
string timeStamp = this.GenerateTimeStamp();
string callback = "";
if (url.ToString().Contains(Configuration.RequestTokenUrl))
callback = Configuration.CallbackUrl;
//Generate Signature
string sig = this.GenerateSignature(uri,
Configuration.ConsumerKey,
Configuration.ConsumerSecret,
Token,
TokenSecret,
method.ToString(),
timeStamp,
nonce,
callback,
out outUrl,
out querystring);
querystring += "&oauth_signature=" + HttpUtility.UrlEncode(sig);
//Convert the querystring to postData
if (method == Method.POST)
{
postData = querystring;
querystring = "";
}
if (querystring.Length > 0)
{
outUrl += "?";
}
if (method == Method.POST || method == Method.GET)
ret = WebRequest(method, outUrl + querystring, postData);
return ret;
}
/// <summary>
/// WebRequestWithPut
/// </summary>
/// <param name="method">WebRequestWithPut</param>
/// <param name="url"></param>
/// <param name="postData"></param>
/// <returns></returns>
public string APIWebRequest(string method, string url, string postData = null)
{
HttpWebRequest webRequest = null;
url = Configuration.BaseUrl + url;
webRequest = System.Net.WebRequest.Create(url) as HttpWebRequest;
if (method == "POST") {
webRequest.Method = method;
webRequest.ContentType = "text/xml";
}
if (Configuration.AuthType == "securetoken")
{
webRequest.Headers.Add("api-token", Configuration.ApiToken);
}
else if (Configuration.AuthType == "oauthtoken")
{
webRequest.Credentials = CredentialCache.DefaultCredentials;
webRequest.AllowWriteStreamBuffering = true;
webRequest.PreAuthenticate = true;
webRequest.ServicePoint.Expect100Continue = false;
//ServicePointManager.SecurityProtocol = SecurityProtocolType.Ssl3;
string header = _getHeader(url, method);
webRequest.Headers.Add("Authorization", header);
}
if (postData != null)
{
byte[] fileToSend = Encoding.UTF8.GetBytes(postData);
webRequest.ContentLength = fileToSend.Length;
Stream reqStream = webRequest.GetRequestStream();
reqStream.Write(fileToSend, 0, fileToSend.Length);
reqStream.Close();
}
string returned = WebResponseGet(webRequest);
return returned;
}
/// <summary>
/// Web Request Wrapper
/// </summary>
/// <param name="method">Http Method</param>
/// <param name="url">Full url to the web resource</param>
/// <param name="postData">Data to post in querystring format</param>
/// <returns>The web server response.</returns>
public string WebRequest(Method method, string url, string postData)
{
HttpWebRequest webRequest = null;
StreamWriter requestWriter = null;
string responseData = "";
webRequest = System.Net.WebRequest.Create(url) as HttpWebRequest;
webRequest.Method = method.ToString();
webRequest.ServicePoint.Expect100Continue = false;
webRequest.Timeout = 20000;
if (method == Method.POST)
{
webRequest.ContentType = "application/x-www-form-urlencoded";
requestWriter = new StreamWriter(webRequest.GetRequestStream());
try
{
requestWriter.Write(postData);
}
catch
{
throw;
}
finally
{
requestWriter.Close();
requestWriter = null;
}
}
responseData = WebResponseGet(webRequest);
webRequest = null;
return responseData;
}
/// <summary>
/// Process the web response.
/// </summary>
/// <param name="webRequest">The request object.</param>
/// <returns>The response data.</returns>
public string WebResponseGet(HttpWebRequest webRequest)
{
StreamReader responseReader = null;
string responseData = "";
try
{
responseReader = new StreamReader(webRequest.GetResponse().GetResponseStream());
responseData = responseReader.ReadToEnd();
}
catch (Exception e)
{
throw e;
}
finally
{
webRequest.GetResponse().GetResponseStream().Close();
responseReader.Close();
responseReader = null;
}
return responseData;
}
public string _getHeader(string url, string method)
{
Uri uri = new Uri(url);
string nonce = this.GenerateNonce();
string timeStamp = this.GenerateTimeStamp();
string outUrl, querystring;
//Generate Signature
string sig = this.GenerateSignature(uri,
Configuration.ConsumerKey,
Configuration.ConsumerSecret,
Token,
TokenSecret,
method,
timeStamp,
nonce,
null,
out outUrl,
out querystring);
return ("OAuth realm=\"" + Configuration.BaseUrl + "\",oauth_consumer_key=\"" + Configuration.ConsumerKey + "\",oauth_token=\"" + Token + "\",oauth_signature_method=\"HMAC-SHA1\",oauth_signature=\"" + HttpUtility.UrlEncode(sig) + "\",oauth_timestamp=\"" + timeStamp + "\",oauth_nonce=\"" + nonce + "\",oauth_verifier=\"" + Verifier + "\", oauth_version=\"1.0\"");
}
}
}