Skip to content

Commit 182b025

Browse files
committed
merged
1 parent aa8a364 commit 182b025

File tree

79 files changed

+11720
-745
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

79 files changed

+11720
-745
lines changed
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
#region License
2+
// Copyright 2010 John Sheehan
3+
//
4+
// Licensed under the Apache License, Version 2.0 (the "License");
5+
// you may not use this file except in compliance with the License.
6+
// You may obtain a copy of the License at
7+
//
8+
// http://www.apache.org/licenses/LICENSE-2.0
9+
//
10+
// Unless required by applicable law or agreed to in writing, software
11+
// distributed under the License is distributed on an "AS IS" BASIS,
12+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
// See the License for the specific language governing permissions and
14+
// limitations under the License.
15+
#endregion
16+
17+
using System;
18+
using System.Linq;
19+
using System.Text;
20+
21+
namespace RestSharp
22+
{
23+
public class HttpBasicAuthenticator : IAuthenticator
24+
{
25+
private readonly string _username;
26+
private readonly string _password;
27+
28+
public HttpBasicAuthenticator(string username, string password) {
29+
_password = password;
30+
_username = username;
31+
}
32+
33+
public void Authenticate(IRestClient client, IRestRequest request) {
34+
// NetworkCredentials always makes two trips, even if with PreAuthenticate,
35+
// it is also unsafe for many partial trust scenarios
36+
// request.Credentials = Credentials;
37+
// thanks TweetSharp!
38+
39+
// request.Credentials = new NetworkCredential(_username, _password);
40+
41+
// only add the Authorization parameter if it hasn't been added by a previous Execute
42+
if (!request.Parameters.Any(p => p.Name.Equals("Authorization", StringComparison.OrdinalIgnoreCase)))
43+
{
44+
var token = Convert.ToBase64String(Encoding.UTF8.GetBytes(string.Format("{0}:{1}", _username, _password)));
45+
var authHeader = string.Format("Basic {0}", token);
46+
request.AddParameter("Authorization", authHeader, ParameterType.HttpHeader);
47+
}
48+
}
49+
}
50+
}
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
#region License
2+
// Copyright 2010 John Sheehan
3+
//
4+
// Licensed under the Apache License, Version 2.0 (the "License");
5+
// you may not use this file except in compliance with the License.
6+
// You may obtain a copy of the License at
7+
//
8+
// http://www.apache.org/licenses/LICENSE-2.0
9+
//
10+
// Unless required by applicable law or agreed to in writing, software
11+
// distributed under the License is distributed on an "AS IS" BASIS,
12+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
// See the License for the specific language governing permissions and
14+
// limitations under the License.
15+
#endregion
16+
17+
namespace RestSharp
18+
{
19+
public interface IAuthenticator
20+
{
21+
void Authenticate(IRestClient client, IRestRequest request);
22+
}
23+
}
Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
#region License
2+
// Copyright 2010 John Sheehan
3+
//
4+
// Licensed under the Apache License, Version 2.0 (the "License");
5+
// you may not use this file except in compliance with the License.
6+
// You may obtain a copy of the License at
7+
//
8+
// http://www.apache.org/licenses/LICENSE-2.0
9+
//
10+
// Unless required by applicable law or agreed to in writing, software
11+
// distributed under the License is distributed on an "AS IS" BASIS,
12+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
// See the License for the specific language governing permissions and
14+
// limitations under the License.
15+
#endregion
16+
17+
using System;
18+
using System.Net;
19+
20+
#if FRAMEWORK
21+
22+
namespace RestSharp
23+
{
24+
/// <summary>
25+
/// Tries to Authenticate with the credentials of the currently logged in user, or impersonate a user
26+
/// </summary>
27+
public class NtlmAuthenticator : IAuthenticator
28+
{
29+
private readonly ICredentials credentials;
30+
31+
/// <summary>
32+
/// Authenticate with the credentials of the currently logged in user
33+
/// </summary>
34+
public NtlmAuthenticator()
35+
: this(CredentialCache.DefaultCredentials)
36+
{
37+
}
38+
39+
/// <summary>
40+
/// Authenticate by impersonation
41+
/// </summary>
42+
/// <param name="username"></param>
43+
/// <param name="password"></param>
44+
public NtlmAuthenticator(string username, string password) : this(new NetworkCredential(username, password))
45+
{
46+
}
47+
48+
/// <summary>
49+
/// Authenticate by impersonation, using an existing <c>ICredentials</c> instance
50+
/// </summary>
51+
/// <param name="credentials"></param>
52+
public NtlmAuthenticator(ICredentials credentials)
53+
{
54+
if (credentials == null) throw new ArgumentNullException("credentials");
55+
this.credentials = credentials;
56+
}
57+
58+
public void Authenticate(IRestClient client, IRestRequest request)
59+
{
60+
request.Credentials = credentials;
61+
}
62+
}
63+
}
64+
65+
#endif
Lines changed: 110 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,110 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Collections.Specialized;
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+
foreach (var item in items)
38+
{
39+
var record = item as T;
40+
yield return record;
41+
}
42+
}
43+
44+
public static void ForEach<T>(this IEnumerable<T> items, Action<T> action)
45+
{
46+
foreach (var item in items)
47+
{
48+
action(item);
49+
}
50+
}
51+
52+
#if !WINDOWS_PHONE && !SILVERLIGHT && !PocketPC
53+
54+
public static void AddRange(this IDictionary<string, string> collection, NameValueCollection range)
55+
{
56+
foreach (var key in range.AllKeys)
57+
{
58+
collection.Add(key, range[key]);
59+
}
60+
}
61+
62+
public static string ToQueryString(this NameValueCollection collection)
63+
{
64+
var sb = new StringBuilder();
65+
if (collection.Count > 0)
66+
{
67+
sb.Append("?");
68+
}
69+
70+
var count = 0;
71+
foreach (var key in collection.AllKeys)
72+
{
73+
sb.AppendFormat("{0}={1}", key, collection[key].UrlEncode());
74+
count++;
75+
76+
if (count >= collection.Count)
77+
{
78+
continue;
79+
}
80+
sb.Append("&");
81+
}
82+
return sb.ToString();
83+
}
84+
85+
#endif
86+
87+
public static string Concatenate(this WebParameterCollection collection, string separator, string spacer)
88+
{
89+
var sb = new StringBuilder();
90+
91+
var total = collection.Count;
92+
var count = 0;
93+
94+
foreach (var item in collection)
95+
{
96+
sb.Append(item.Name);
97+
sb.Append(separator);
98+
sb.Append(item.Value);
99+
100+
count++;
101+
if (count < total)
102+
{
103+
sb.Append(spacer);
104+
}
105+
}
106+
107+
return sb.ToString();
108+
}
109+
}
110+
}
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
using System;
2+
using System.Security.Cryptography;
3+
using System.Text;
4+
5+
namespace RestSharp.Authenticators.OAuth.Extensions
6+
{
7+
internal static class OAuthExtensions
8+
{
9+
public static string ToRequestValue(this OAuthSignatureMethod signatureMethod)
10+
{
11+
var value = signatureMethod.ToString().ToUpper();
12+
var shaIndex = value.IndexOf("SHA1");
13+
return shaIndex > -1 ? value.Insert(shaIndex, "-") : value;
14+
}
15+
16+
public static OAuthSignatureMethod FromRequestValue(this string signatureMethod)
17+
{
18+
switch (signatureMethod)
19+
{
20+
case "HMAC-SHA1":
21+
return OAuthSignatureMethod.HmacSha1;
22+
case "RSA-SHA1":
23+
return OAuthSignatureMethod.RsaSha1;
24+
default:
25+
return OAuthSignatureMethod.PlainText;
26+
}
27+
}
28+
29+
public static string HashWith(this string input, HashAlgorithm algorithm)
30+
{
31+
var data = Encoding.UTF8.GetBytes(input);
32+
var hash = algorithm.ComputeHash(data);
33+
return Convert.ToBase64String(hash);
34+
}
35+
}
36+
}

0 commit comments

Comments
 (0)