-
Notifications
You must be signed in to change notification settings - Fork 2
/
OAuthRequest.cs
202 lines (184 loc) · 10.2 KB
/
OAuthRequest.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
using System;
using System.Security.Cryptography;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web;
using System.Net;
using System.Diagnostics;
namespace OAuth
{
public class OAuthRequest : OAuthBase
{
private OAuthConsumer _consumer;
public OAuthRequest(OAuthConsumer consumer, string debugType) : base (debugType)
{
this._consumer = consumer;
}
/// <summary>
/// Generates an OAuth Nonce
/// </summary>
/// <returns>a string to be used as the unique nonce</returns>
private string _generateNonce()
{
Random random = new Random();
return random.Next(123400, 9999999).ToString();
}
/// <summary>
/// Generates the OAuth timestamp in number of seconds since January 1st 1970
/// </summary>
/// <returns>a string to be used as the query timestamp</returns>
private string _generateTimestamp()
{
TimeSpan ts = DateTime.UtcNow - new DateTime(1970, 1, 1, 0, 0, 0, 0);
return Convert.ToInt64(ts.TotalSeconds).ToString();
}
/// <summary>
/// Normalizes the query parameters by sorting them according to the OAuth specification
/// Generates the signature base
/// </summary>
/// <param name="url">the base url of the service called</param>
/// <param name="parameters">the list of query parameters passed</param>
/// <param name="consumerSecret">consumer secret</param>
/// <param name="normalizedUrl">the normalized Url (returned)</param>
/// <param name="normalizedRequestParameters">the normalized parameters (returned)</param>
/// <returns>a string to be used as the request signature base</returns>
private string _generateSignatureBase(Uri url, string httpMethod, List<QueryParameter> parameters, string consumerSecret, out string normalizedUrl, out string normalizedRequestParameters)
{
normalizedUrl = null;
normalizedRequestParameters = null;
// Sort the parameters
parameters.Sort(new QueryParameterComparer());
normalizedUrl = string.Format("{0}://{1}", url.Scheme, url.Host);
if (!((url.Scheme == "http" && url.Port == 80) || (url.Scheme == "https" && url.Port == 443)))
{
normalizedUrl += ":" + url.Port;
}
normalizedUrl += url.AbsolutePath;
normalizedRequestParameters = base.NormalizeRequestParameters(parameters);
StringBuilder signatureBase = new StringBuilder();
signatureBase.AppendFormat("{0}&", httpMethod);
signatureBase.AppendFormat("{0}&", OAuthBase.UrlEncode(normalizedUrl));
signatureBase.AppendFormat("{0}", OAuthBase.UrlEncode(normalizedRequestParameters));
return signatureBase.ToString();
}
/// <summary>
/// Generates the request signature using the hash
/// </summary>
/// <param name="signatureBase">string base url</param>
/// <param name="hmacsha1">hash key</param>
/// <returns>string to be used as the request signature</returns>
private string _generateSignatureUsingHash(string signatureBase, HMACSHA1 hmacsha1)
{
byte[] dataBuffer = System.Text.Encoding.UTF8.GetBytes(signatureBase);
byte[] hashBytes = hmacsha1.ComputeHash(dataBuffer);
return Convert.ToBase64String(hashBytes);
}
/// <summary>
/// Generates the request signature for OAuth call
/// </summary>
/// <param name="url">the Url of the service</param>
/// <param name="parameters">the List of Query Parameters passed to the service</param>
/// <param name="signatureMethod">the signature method to be used</param>
/// <param name="consumerSecret">the consumer secret</param>
/// <param name="oauth_token">the oauth token</param>
/// <param name="normalizedUrl">the normalized url of the request</param>
/// <param name="normalizedUrlWithParameters">the normalized url of the request with the parameters</param>
/// <returns></returns>
private string _generateSignature(Uri url, string httpMethod, List<QueryParameter> parameters, string signatureMethod, string consumerSecret, string oauth_token, out string normalizedUrl, out string normalizedUrlWithParameters)
{
string tokenSecret = oauth_token;
switch (signatureMethod)
{
case "HMAC-SHA1":
string signatureBase = this._generateSignatureBase(url, httpMethod, parameters, consumerSecret, out normalizedUrl, out normalizedUrlWithParameters);
HMACSHA1 hmacsha1 = new HMACSHA1();
hmacsha1.Key = Encoding.UTF8.GetBytes(string.Format("{0}&{1}", UrlEncode(consumerSecret), string.IsNullOrEmpty(tokenSecret) ? "" : UrlEncode(tokenSecret)));
return this._generateSignatureUsingHash(signatureBase, hmacsha1);
default:
throw new Exception("signature method not yet implemented");
}
}
/// <summary>
/// Calls a webservice specified by its url and passing it the parameters
/// </summary>
/// <param name="url">Url of the service to call</param>
/// <param name="oauthToken">oauth token (if exists)</param>
/// <param name="oauthTokenSecret">oauth token secret (if exists)</param>
/// <param name="extraParameters">the list of parameters to send the service</param>
/// <returns></returns>
public string request(Uri url, string httpMethod, string oauthToken, string oauthTokenSecret, List<QueryParameter> extraParameters)
{
httpMethod = httpMethod.ToUpper();
string normalizedUrl = String.Empty;
string normalizedUrlWithParameters = String.Empty;
// Generate the parameters based on the Oauth Configuration of the Consumer
List<QueryParameter> parameters = new List<QueryParameter>();
parameters.Add(new QueryParameter("oauth_consumer_key", this._consumer.OauthConfig.ConsumerKey));
parameters.Add(new QueryParameter("oauth_nonce", this._generateNonce()));
parameters.Add(new QueryParameter("oauth_timestamp", this._generateTimestamp()));
parameters.Add(new QueryParameter("oauth_signature_method", this._consumer.OauthConfig.OauthSignatureMethod));
parameters.Add(new QueryParameter("oauth_version", this._consumer.OauthConfig.OauthVersion));
// Add the OauthToken and OAuthTokenSecret if they are valued
if (oauthToken != null && oauthToken != String.Empty && oauthToken.Length > 0)
{
parameters.Add(new QueryParameter("oauth_token", oauthToken));
}
if (oauthTokenSecret != null && oauthTokenSecret != String.Empty && oauthTokenSecret.Length > 0)
{
parameters.Add(new QueryParameter("oauth_token_secret", oauthTokenSecret));
}
if (extraParameters != null)
{
foreach (QueryParameter param in extraParameters)
{
parameters.Add(param);
}
}
// Generate the OAuth Signature and add it to the parameters
string signature = this._generateSignature(url, httpMethod, parameters, this._consumer.OauthConfig.OauthSignatureMethod, this._consumer.OauthConfig.ConsumerSecret, oauthTokenSecret, out normalizedUrl, out normalizedUrlWithParameters);
parameters.Add(new QueryParameter("oauth_signature", signature));
try
{
string oauthUrl = "";
if (httpMethod == "GET")
{
oauthUrl = normalizedUrl + '?' + normalizedUrlWithParameters + "&oauth_signature=" + signature;
}
else
{
oauthUrl = normalizedUrl;
}
System.Net.HttpWebRequest request = System.Net.HttpWebRequest.Create(oauthUrl) as System.Net.HttpWebRequest;
request.ProtocolVersion = HttpVersion.Version10;
if (httpMethod == "POST")
{
/*StringBuilder postRequest = new StringBuilder();
int countParams = 0;
foreach (QueryParameter param in extraParameters)
{
if (countParams > 0) postRequest.Append('&');
postRequest.Append(param.Name + "=" + HttpUtility.UrlEncode(param.Value));
}*/
string postRequest = normalizedUrlWithParameters;
Byte[] bufferrequest = System.Text.Encoding.UTF8.GetBytes(postRequest + "&oauth_signature=" + signature);
request.Method = "POST";
request.ContentType = "application/x-www-form-urlencoded";
request.ContentLength = bufferrequest.Length;
System.IO.Stream requestStream = request.GetRequestStream();
requestStream.Write(bufferrequest, 0, bufferrequest.Length);
requestStream.Close();
}
System.Net.HttpWebResponse response = request.GetResponse() as System.Net.HttpWebResponse;
System.IO.StreamReader reader = new System.IO.StreamReader(response.GetResponseStream());
String content = reader.ReadToEnd();
return content;
}
catch (WebException e)
{
base._debug(e.Message);
return "";
}
}
}
}