Skip to content

Commit ae0ed0a

Browse files
committed
First cut of putting OAuth1 support back in to the WinRT build
Did a basic test of logging in to an app and getting the tokens at login, this has come back well so far. Need to test accessing secure resources next to ensure it functions as expected.
1 parent 46eaf77 commit ae0ed0a

20 files changed

+1736
-0
lines changed
Lines changed: 106 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,106 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Linq;
4+
using System.Text;
5+
6+
namespace RestSharp.Authenticators.OAuth.Extensions
7+
{
8+
internal static class CollectionExtensions
9+
{
10+
public static IEnumerable<T> AsEnumerable<T>(this T item)
11+
{
12+
return new[] { item };
13+
}
14+
15+
public static IEnumerable<T> And<T>(this T item, T other)
16+
{
17+
return new[] { item, other };
18+
}
19+
20+
public static IEnumerable<T> And<T>(this IEnumerable<T> items, T item)
21+
{
22+
foreach (var i in items)
23+
{
24+
yield return i;
25+
}
26+
27+
yield return item;
28+
}
29+
30+
public static K TryWithKey<T, K>(this IDictionary<T, K> dictionary, T key)
31+
{
32+
return dictionary.ContainsKey(key) ? dictionary[key] : default(K);
33+
}
34+
35+
public static IEnumerable<T> ToEnumerable<T>(this object[] items) where T : class
36+
{
37+
return items.Select(item => item as T);
38+
}
39+
40+
public static void ForEach<T>(this IEnumerable<T> items, Action<T> action)
41+
{
42+
foreach (var item in items)
43+
{
44+
action(item);
45+
}
46+
}
47+
48+
#if !WINDOWS_PHONE && !SILVERLIGHT
49+
50+
public static void AddRange(this IDictionary<string, string> collection, Dictionary<string, string> range)
51+
{
52+
foreach (var key in range.Keys)
53+
{
54+
collection.Add(key, range[key]);
55+
}
56+
}
57+
58+
public static string ToQueryString(this Dictionary<string, string> collection)
59+
{
60+
var sb = new StringBuilder();
61+
if (collection.Count > 0)
62+
{
63+
sb.Append("?");
64+
}
65+
66+
var count = 0;
67+
foreach (var key in collection.Keys)
68+
{
69+
sb.AppendFormat("{0}={1}", key, collection[key].UrlEncode());
70+
count++;
71+
72+
if (count >= collection.Count)
73+
{
74+
continue;
75+
}
76+
sb.Append("&");
77+
}
78+
return sb.ToString();
79+
}
80+
81+
#endif
82+
83+
public static string Concatenate(this WebParameterCollection collection, string separator, string spacer)
84+
{
85+
var sb = new StringBuilder();
86+
87+
var total = collection.Count;
88+
var count = 0;
89+
90+
foreach (var item in collection)
91+
{
92+
sb.Append(item.Name);
93+
sb.Append(separator);
94+
sb.Append(item.Value);
95+
96+
count++;
97+
if (count < total)
98+
{
99+
sb.Append(spacer);
100+
}
101+
}
102+
103+
return sb.ToString();
104+
}
105+
}
106+
}
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
namespace RestSharp.Authenticators.OAuth.Extensions
2+
{
3+
internal static class OAuthExtensions
4+
{
5+
public static string ToRequestValue(this OAuthSignatureMethod signatureMethod)
6+
{
7+
var value = signatureMethod.ToString().ToUpper();
8+
var shaIndex = value.IndexOf("SHA1");
9+
return shaIndex > -1 ? value.Insert(shaIndex, "-") : value;
10+
}
11+
12+
public static OAuthSignatureMethod FromRequestValue(this string signatureMethod)
13+
{
14+
switch (signatureMethod)
15+
{
16+
case "HMAC-SHA1":
17+
return OAuthSignatureMethod.HmacSha1;
18+
case "RSA-SHA1":
19+
return OAuthSignatureMethod.RsaSha1;
20+
default:
21+
return OAuthSignatureMethod.PlainText;
22+
}
23+
}
24+
}
25+
}
Lines changed: 108 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,108 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Globalization;
4+
using System.Linq;
5+
using System.Text;
6+
using System.Text.RegularExpressions;
7+
8+
namespace RestSharp.Authenticators.OAuth.Extensions
9+
{
10+
internal static class StringExtensions
11+
{
12+
public static bool IsNullOrBlank(this string value)
13+
{
14+
return String.IsNullOrEmpty(value) ||
15+
(!String.IsNullOrEmpty(value) && value.Trim() == String.Empty);
16+
}
17+
18+
public static bool EqualsIgnoreCase(this string left, string right)
19+
{
20+
return String.Compare(left, right, StringComparison.OrdinalIgnoreCase) == 0;
21+
}
22+
23+
public static bool EqualsAny(this string input, params string[] args)
24+
{
25+
return args.Aggregate(false, (current, arg) => current | input.Equals(arg));
26+
}
27+
28+
public static string FormatWith(this string format, params object[] args)
29+
{
30+
return String.Format(format, args);
31+
}
32+
33+
public static string FormatWithInvariantCulture(this string format, params object[] args)
34+
{
35+
return String.Format(CultureInfo.InvariantCulture, format, args);
36+
}
37+
38+
public static string Then(this string input, string value)
39+
{
40+
return String.Concat(input, value);
41+
}
42+
43+
public static string UrlEncode(this string value)
44+
{
45+
// [DC] This is more correct than HttpUtility; it escapes spaces as %20, not +
46+
return Uri.EscapeDataString(value);
47+
}
48+
49+
public static string UrlDecode(this string value)
50+
{
51+
return Uri.UnescapeDataString(value);
52+
}
53+
54+
public static Uri AsUri(this string value)
55+
{
56+
return new Uri(value);
57+
}
58+
59+
public static string ToBase64String(this byte[] input)
60+
{
61+
return Convert.ToBase64String(input);
62+
}
63+
64+
public static byte[] GetBytes(this string input)
65+
{
66+
return Encoding.UTF8.GetBytes(input);
67+
}
68+
69+
public static string PercentEncode(this string s)
70+
{
71+
var bytes = s.GetBytes();
72+
var sb = new StringBuilder();
73+
foreach (var b in bytes)
74+
{
75+
// [DC]: Support proper encoding of special characters (\n\r\t\b)
76+
if ((b > 7 && b < 11) || b == 13)
77+
{
78+
sb.Append(string.Format("%0{0:X}", b));
79+
}
80+
else
81+
{
82+
sb.Append(string.Format("%{0:X}", b));
83+
}
84+
}
85+
return sb.ToString();
86+
}
87+
88+
public static IDictionary<string, string> ParseQueryString(this string query)
89+
{
90+
// [DC]: This method does not URL decode, and cannot handle decoded input
91+
if (query.StartsWith("?")) query = query.Substring(1);
92+
93+
if (query.Equals(string.Empty))
94+
{
95+
return new Dictionary<string, string>();
96+
}
97+
98+
var parts = query.Split(new[] { '&' });
99+
100+
return parts.Select(
101+
part => part.Split(new[] { '=' })).ToDictionary(
102+
pair => pair[0], pair => pair[1]
103+
);
104+
}
105+
106+
private const RegexOptions Options = RegexOptions.IgnoreCase;
107+
}
108+
}
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
using System;
2+
3+
namespace RestSharp.Authenticators.OAuth.Extensions
4+
{
5+
internal static class TimeExtensions
6+
{
7+
public static DateTime FromNow(this TimeSpan value)
8+
{
9+
return new DateTime((DateTime.Now + value).Ticks);
10+
}
11+
12+
public static DateTime FromUnixTime(this long seconds)
13+
{
14+
var time = new DateTime(1970, 1, 1);
15+
time = time.AddSeconds(seconds);
16+
17+
return time.ToLocalTime();
18+
}
19+
20+
public static long ToUnixTime(this DateTime dateTime)
21+
{
22+
var timeSpan = (dateTime - new DateTime(1970, 1, 1));
23+
var timestamp = (long)timeSpan.TotalSeconds;
24+
25+
return timestamp;
26+
}
27+
}
28+
}
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
using System.IO;
2+
3+
namespace RestSharp.Authenticators.OAuth
4+
{
5+
internal class HttpPostParameter : WebParameter
6+
{
7+
public HttpPostParameter(string name, string value)
8+
: base(name, value)
9+
{
10+
}
11+
12+
public virtual HttpPostParameterType Type { get; private set; }
13+
public virtual string FileName { get; private set; }
14+
public virtual string FilePath { get; private set; }
15+
public virtual Stream FileStream { get; set; }
16+
public virtual string ContentType { get; private set; }
17+
18+
public static HttpPostParameter CreateFile(string name, string fileName, string filePath, string contentType)
19+
{
20+
var parameter = new HttpPostParameter(name, string.Empty)
21+
{
22+
Type = HttpPostParameterType.File,
23+
FileName = fileName,
24+
FilePath = filePath,
25+
ContentType = contentType,
26+
};
27+
return parameter;
28+
}
29+
30+
public static HttpPostParameter CreateFile(string name, string fileName, Stream fileStream, string contentType)
31+
{
32+
var parameter = new HttpPostParameter(name, string.Empty)
33+
{
34+
Type = HttpPostParameterType.File,
35+
FileName = fileName,
36+
FileStream = fileStream,
37+
ContentType = contentType,
38+
};
39+
40+
return parameter;
41+
}
42+
}
43+
}
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
namespace RestSharp.Authenticators.OAuth
2+
{
3+
internal enum HttpPostParameterType
4+
{
5+
Field,
6+
File
7+
}
8+
}
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
namespace RestSharp.Authenticators.OAuth
2+
{
3+
public enum OAuthParameterHandling
4+
{
5+
HttpAuthorizationHeader,
6+
UrlOrPostParameters
7+
}
8+
}
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
namespace RestSharp.Authenticators.OAuth
2+
{
3+
public enum OAuthSignatureMethod
4+
{
5+
HmacSha1,
6+
PlainText,
7+
RsaSha1
8+
}
9+
}
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
namespace RestSharp.Authenticators.OAuth
2+
{
3+
public enum OAuthSignatureTreatment
4+
{
5+
Escaped,
6+
Unescaped
7+
}
8+
}

0 commit comments

Comments
 (0)