/
OAuthTools.cs
375 lines (317 loc) · 15.4 KB
/
OAuthTools.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
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
using System;
using System.Linq;
using System.Runtime.Serialization;
using System.Security.Cryptography;
using System.Text;
using System.Xml;
using RestSharp.Authenticators.OAuth.Extensions;
using RestSharp.Extensions;
namespace RestSharp.Authenticators.OAuth
{
[DataContract]
internal static class OAuthTools
{
private const string ALPHA_NUMERIC = UPPER + LOWER + DIGIT;
private const string DIGIT = "1234567890";
private const string LOWER = "abcdefghijklmnopqrstuvwxyz";
private const string UNRESERVED = ALPHA_NUMERIC + "-._~";
private const string UPPER = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
private static readonly Random random;
private static readonly object randomLock = new object();
private static readonly RandomNumberGenerator rng = RandomNumberGenerator.Create();
/// <summary>
/// All text parameters are UTF-8 encoded (per section 5.1).
/// </summary>
/// <seealso cref="http://www.hueniverse.com/hueniverse/2008/10/beginners-gui-1.html" />
private static readonly Encoding encoding = Encoding.UTF8;
/// <summary>
/// The set of characters that are unreserved in RFC 2396 but are NOT unreserved in RFC 3986.
/// </summary>
/// <seealso cref="http://stackoverflow.com/questions/846487/how-to-get-uri-escapedatastring-to-comply-with-rfc-3986" />
private static readonly string[] uriRfc3986CharsToEscape = {"!", "*", "'", "(", ")"};
private static readonly string[] uriRfc3968EscapedHex = {"%21", "%2A", "%27", "%28", "%29"};
static OAuthTools()
{
var bytes = new byte[4];
rng.GetBytes(bytes);
random = new Random(BitConverter.ToInt32(bytes, 0));
}
/// <summary>
/// Generates a random 16-byte lowercase alphanumeric string.
/// </summary>
/// <seealso cref="http://oauth.net/core/1.0#nonce" />
/// <returns></returns>
public static string GetNonce()
{
const string chars = LOWER + DIGIT;
var nonce = new char[16];
lock (randomLock)
{
for (var i = 0; i < nonce.Length; i++)
nonce[i] = chars[random.Next(0, chars.Length)];
}
return new string(nonce);
}
/// <summary>
/// Generates a timestamp based on the current elapsed seconds since '01/01/1970 0000 GMT"
/// </summary>
/// <seealso cref="http://oauth.net/core/1.0#nonce" />
/// <returns></returns>
public static string GetTimestamp()
{
return GetTimestamp(DateTime.UtcNow);
}
/// <summary>
/// Generates a timestamp based on the elapsed seconds of a given time since '01/01/1970 0000 GMT"
/// </summary>
/// <seealso cref="http://oauth.net/core/1.0#nonce" />
/// <param name="dateTime">A specified point in time.</param>
/// <returns></returns>
public static string GetTimestamp(DateTime dateTime)
{
var timestamp = dateTime.ToUnixTime();
return timestamp.ToString();
}
/// <summary>
/// URL encodes a string based on section 5.1 of the OAuth spec.
/// Namely, percent encoding with [RFC3986], avoiding unreserved characters,
/// upper-casing hexadecimal characters, and UTF-8 encoding for text value pairs.
/// </summary>
/// <param name="value">The value to escape.</param>
/// <returns>The escaped value.</returns>
/// <remarks>
/// The <see cref="Uri.EscapeDataString" /> method is <i>supposed</i> to take on
/// RFC 3986 behavior if certain elements are present in a .config file. Even if this
/// actually worked (which in my experiments it <i>doesn't</i>), we can't rely on every
/// host actually having this configuration element present.
/// </remarks>
/// <seealso cref="http://oauth.net/core/1.0#encoding_parameters" />
/// <seealso cref="http://stackoverflow.com/questions/846487/how-to-get-uri-escapedatastring-to-comply-with-rfc-3986" />
public static string UrlEncodeRelaxed(string value)
{
// Escape RFC 3986 chars first.
var escapedRfc3986 = new StringBuilder(value);
for (var i = 0; i < uriRfc3986CharsToEscape.Length; i++)
{
var t = uriRfc3986CharsToEscape[i];
escapedRfc3986.Replace(t, uriRfc3968EscapedHex[i]);
}
// Do RFC 2396 escaping by calling the .NET method to do the work.
string escapedRfc2396 = Uri.EscapeDataString(escapedRfc3986.ToString());
// Return the fully-RFC3986-escaped string.
return escapedRfc2396;
}
/// <summary>
/// URL encodes a string based on section 5.1 of the OAuth spec.
/// Namely, percent encoding with [RFC3986], avoiding unreserved characters,
/// upper-casing hexadecimal characters, and UTF-8 encoding for text value pairs.
/// </summary>
/// <param name="value"></param>
/// <seealso cref="http://oauth.net/core/1.0#encoding_parameters" />
public static string UrlEncodeStrict(string value)
{
// From oauth spec above: -
// Characters not in the unreserved character set ([RFC3986]
// (Berners-Lee, T., "Uniform Resource Identifiers (URI):
// Generic Syntax," .) section 2.3) MUST be encoded.
// ...
// unreserved = ALPHA, DIGIT, '-', '.', '_', '~'
var result = "";
value.ForEach(c =>
{
result += UNRESERVED.Contains(c)
? c.ToString()
: c.ToString()
.PercentEncode();
});
return result;
}
/// <summary>
/// Sorts a collection of key-value pairs by name, and then value if equal,
/// concatenating them into a single string. This string should be encoded
/// prior to, or after normalization is run.
/// </summary>
/// <seealso cref="http://oauth.net/core/1.0#rfc.section.9.1.1" />
/// <param name="parameters"></param>
/// <returns></returns>
public static string NormalizeRequestParameters(WebParameterCollection parameters)
{
var copy = SortParametersExcludingSignature(parameters);
var concatenated = copy.Concatenate("=", "&");
return concatenated;
}
/// <summary>
/// Sorts a <see cref="WebParameterCollection" /> by name, and then value if equal.
/// </summary>
/// <param name="parameters">A collection of parameters to sort</param>
/// <returns>A sorted parameter collection</returns>
public static WebParameterCollection SortParametersExcludingSignature(WebParameterCollection parameters)
{
var copy = new WebParameterCollection(parameters);
var exclusions = copy.Where(n => n.Name.EqualsIgnoreCase("oauth_signature"));
copy.RemoveAll(exclusions);
copy.ForEach(p =>
{
p.Name = UrlEncodeStrict(p.Name);
p.Value = UrlEncodeStrict(p.Value);
});
copy.Sort((x, y) => string.CompareOrdinal(x.Name, y.Name) != 0
? string.CompareOrdinal(x.Name, y.Name)
: string.CompareOrdinal(x.Value, y.Value));
return copy;
}
/// <summary>
/// Creates a request URL suitable for making OAuth requests.
/// Resulting URLs must exclude port 80 or port 443 when accompanied by HTTP and HTTPS, respectively.
/// Resulting URLs must be lower case.
/// </summary>
/// <seealso cref="http://oauth.net/core/1.0#rfc.section.9.1.2" />
/// <param name="url">The original request URL</param>
/// <returns></returns>
public static string ConstructRequestUrl(Uri url)
{
if (url == null)
throw new ArgumentNullException("url");
var sb = new StringBuilder();
var requestUrl = "{0}://{1}".FormatWith(url.Scheme, url.Host);
var qualified = ":{0}".FormatWith(url.Port);
var basic = url.Scheme == "http" && url.Port == 80;
var secure = url.Scheme == "https" && url.Port == 443;
sb.Append(requestUrl);
sb.Append(!basic && !secure
? qualified
: "");
sb.Append(url.AbsolutePath);
return sb.ToString(); //.ToLower();
}
/// <summary>
/// Creates a request elements concatentation value to send with a request.
/// This is also known as the signature base.
/// </summary>
/// <seealso cref="http://oauth.net/core/1.0#rfc.section.9.1.3" />
/// <seealso cref="http://oauth.net/core/1.0#sig_base_example" />
/// <param name="method">The request's HTTP method type</param>
/// <param name="url">The request URL</param>
/// <param name="parameters">The request's parameters</param>
/// <returns>A signature base string</returns>
public static string ConcatenateRequestElements(string method, string url, WebParameterCollection parameters)
{
var sb = new StringBuilder();
// Separating &'s are not URL encoded
var requestMethod = method.ToUpper().Then("&");
var requestUrl = UrlEncodeRelaxed(ConstructRequestUrl(url.AsUri())).Then("&");
var requestParameters = UrlEncodeRelaxed(NormalizeRequestParameters(parameters));
sb.Append(requestMethod);
sb.Append(requestUrl);
sb.Append(requestParameters);
return sb.ToString();
}
/// <summary>
/// Creates a signature value given a signature base and the consumer secret.
/// This method is used when the token secret is currently unknown.
/// </summary>
/// <seealso cref="http://oauth.net/core/1.0#rfc.section.9.2" />
/// <param name="signatureMethod">The hashing method</param>
/// <param name="signatureBase">The signature base</param>
/// <param name="consumerSecret">The consumer key</param>
/// <returns></returns>
public static string GetSignature(OAuthSignatureMethod signatureMethod, string signatureBase,
string consumerSecret)
{
return GetSignature(signatureMethod, OAuthSignatureTreatment.Escaped, signatureBase, consumerSecret, null);
}
/// <summary>
/// Creates a signature value given a signature base and the consumer secret.
/// This method is used when the token secret is currently unknown.
/// </summary>
/// <seealso cref="http://oauth.net/core/1.0#rfc.section.9.2" />
/// <param name="signatureMethod">The hashing method</param>
/// <param name="signatureTreatment">The treatment to use on a signature value</param>
/// <param name="signatureBase">The signature base</param>
/// <param name="consumerSecret">The consumer key</param>
/// <returns></returns>
public static string GetSignature(OAuthSignatureMethod signatureMethod,
OAuthSignatureTreatment signatureTreatment,
string signatureBase, string consumerSecret)
{
return GetSignature(signatureMethod, signatureTreatment, signatureBase, consumerSecret, null);
}
/// <summary>
/// Creates a signature value given a signature base and the consumer secret and a known token secret.
/// </summary>
/// <seealso cref="http://oauth.net/core/1.0#rfc.section.9.2" />
/// <param name="signatureMethod">The hashing method</param>
/// <param name="signatureBase">The signature base</param>
/// <param name="consumerSecret">The consumer secret</param>
/// <param name="tokenSecret">The token secret</param>
/// <returns></returns>
public static string GetSignature(OAuthSignatureMethod signatureMethod, string signatureBase,
string consumerSecret,
string tokenSecret)
{
return GetSignature(signatureMethod, OAuthSignatureTreatment.Escaped, consumerSecret, tokenSecret);
}
/// <summary>
/// Creates a signature value given a signature base and the consumer secret and a known token secret.
/// </summary>
/// <seealso cref="http://oauth.net/core/1.0#rfc.section.9.2" />
/// <param name="signatureMethod">The hashing method</param>
/// <param name="signatureTreatment">The treatment to use on a signature value</param>
/// <param name="signatureBase">The signature base</param>
/// <param name="consumerSecret">The consumer secret</param>
/// <param name="tokenSecret">The token secret</param>
/// <returns></returns>
public static string GetSignature(OAuthSignatureMethod signatureMethod,
OAuthSignatureTreatment signatureTreatment,
string signatureBase, string consumerSecret, string tokenSecret)
{
if (tokenSecret.IsNullOrBlank())
tokenSecret = string.Empty;
var unencodedConsumerSecret = consumerSecret;
consumerSecret = Uri.EscapeDataString(consumerSecret);
tokenSecret = Uri.EscapeDataString(tokenSecret);
string signature;
switch (signatureMethod)
{
case OAuthSignatureMethod.HmacSha1:
{
var crypto = new HMACSHA1();
var key = "{0}&{1}".FormatWith(consumerSecret, tokenSecret);
crypto.Key = encoding.GetBytes(key);
signature = signatureBase.HashWith(crypto);
break;
}
case OAuthSignatureMethod.HmacSha256:
{
var crypto = new HMACSHA256();
var key = "{0}&{1}".FormatWith(consumerSecret, tokenSecret);
crypto.Key = encoding.GetBytes(key);
signature = signatureBase.HashWith(crypto);
break;
}
case OAuthSignatureMethod.RsaSha1:
{
using (var provider = new RSACryptoServiceProvider { PersistKeyInCsp = false })
{
provider.FromXmlString2(unencodedConsumerSecret);
SHA1Managed hasher = new SHA1Managed();
byte[] hash = hasher.ComputeHash(encoding.GetBytes(signatureBase));
signature = Convert.ToBase64String(provider.SignHash(hash, CryptoConfig.MapNameToOID("SHA1")));
}
break;
}
case OAuthSignatureMethod.PlainText:
{
signature = "{0}&{1}".FormatWith(consumerSecret, tokenSecret);
break;
}
default:
throw new NotImplementedException("Only HMAC-SHA1, HMAC-SHA256, and RSA-SHA1 are currently supported.");
}
var result = signatureTreatment == OAuthSignatureTreatment.Escaped
? UrlEncodeRelaxed(signature)
: signature;
return result;
}
}
}