mirror of
https://github.com/Sonarr/Sonarr.git
synced 2024-12-14 11:23:42 +02:00
Cleanup OAuth Dep
Co-Authored-By: ta264 <ta264@users.noreply.github.com>
This commit is contained in:
parent
da5716cca2
commit
d740e995a3
@ -32,8 +32,6 @@ public class OAuthRequest
|
||||
/// <seealso cref="http://oauth.net/core/1.0#request_urls"/>
|
||||
public virtual string RequestUrl { get; set; }
|
||||
|
||||
#region Authorization Header
|
||||
|
||||
#if !WINRT
|
||||
public string GetAuthorizationHeader(NameValueCollection parameters)
|
||||
{
|
||||
@ -121,10 +119,6 @@ private string WriteAuthorizationHeader(WebParameterCollection parameters)
|
||||
return authorization;
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
#region Authorization Query
|
||||
|
||||
#if !WINRT
|
||||
public string GetAuthorizationQuery(NameValueCollection parameters)
|
||||
{
|
||||
@ -209,8 +203,6 @@ private static string WriteAuthorizationQuery(WebParameterCollection parameters)
|
||||
return authorization;
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
private string GetNewSignature(WebParameterCollection parameters)
|
||||
{
|
||||
var timestamp = OAuthTools.GetTimestamp();
|
||||
@ -241,8 +233,6 @@ private string GetNewSignatureXAuth(WebParameterCollection parameters)
|
||||
return signature;
|
||||
}
|
||||
|
||||
#region Static Helpers
|
||||
|
||||
public static OAuthRequest ForRequestToken(string consumerKey, string consumerSecret)
|
||||
{
|
||||
var credentials = new OAuthRequest
|
||||
@ -335,8 +325,6 @@ public static OAuthRequest ForProtectedResource(string method, string consumerKe
|
||||
return credentials;
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
private void ValidateRequestState()
|
||||
{
|
||||
if (IsNullOrBlank(Method))
|
||||
@ -500,7 +488,7 @@ public static string ToRequestValue(OAuthSignatureMethod signatureMethod)
|
||||
|
||||
private static bool IsNullOrBlank(string value)
|
||||
{
|
||||
return String.IsNullOrEmpty(value) || (!String.IsNullOrEmpty(value) && value.Trim() == String.Empty);
|
||||
return string.IsNullOrEmpty(value) || (!string.IsNullOrEmpty(value) && string.IsNullOrEmpty(value.Trim()));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -11,4 +11,4 @@ public enum OAuthRequestType
|
||||
ProtectedResource,
|
||||
ClientAuthentication
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -9,4 +9,4 @@ public enum OAuthSignatureMethod
|
||||
PlainText,
|
||||
RsaSha1
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -10,4 +10,4 @@ public enum OAuthSignatureTreatment
|
||||
Escaped,
|
||||
Unescaped
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -55,13 +55,13 @@ static OAuthTools()
|
||||
#endif
|
||||
|
||||
/// <summary>
|
||||
/// Generates a random 16-byte lowercase alphanumeric string.
|
||||
/// Generates a random 16-byte lowercase alphanumeric string.
|
||||
/// </summary>
|
||||
/// <seealso href="http://oauth.net/core/1.0#nonce"/>
|
||||
/// <returns></returns>
|
||||
public static string GetNonce()
|
||||
{
|
||||
const string chars = (Lower + Digit);
|
||||
const string chars = Lower + Digit;
|
||||
|
||||
var nonce = new char[16];
|
||||
lock (_randomLock)
|
||||
@ -98,7 +98,7 @@ public static string GetTimestamp(DateTime dateTime)
|
||||
|
||||
private static long ToUnixTime(DateTime dateTime)
|
||||
{
|
||||
var timeSpan = (dateTime - new DateTime(1970, 1, 1));
|
||||
var timeSpan = dateTime - new DateTime(1970, 1, 1);
|
||||
var timestamp = (long)timeSpan.TotalSeconds;
|
||||
|
||||
return timestamp;
|
||||
@ -155,8 +155,7 @@ public static string UrlEncodeStrict(string value)
|
||||
var ret = original.OfType<char>().Where(
|
||||
c => !Unreserved.OfType<char>().Contains(c) && c != '%').Aggregate(
|
||||
value, (current, c) => current.Replace(
|
||||
c.ToString(), PercentEncode(c.ToString())
|
||||
));
|
||||
c.ToString(), PercentEncode(c.ToString())));
|
||||
|
||||
return ret.Replace("%%", "%25%"); // Revisit to encode actual %'s
|
||||
}
|
||||
@ -211,7 +210,7 @@ public static WebParameterCollection SortParametersExcludingSignature(WebParamet
|
||||
|
||||
copy.RemoveAll(exclusions);
|
||||
|
||||
foreach(var parameter in copy)
|
||||
foreach (var parameter in copy)
|
||||
{
|
||||
parameter.Value = UrlEncodeStrict(parameter.Value);
|
||||
}
|
||||
@ -225,7 +224,7 @@ private static bool EqualsIgnoreCase(string left, string right)
|
||||
#if WINRT
|
||||
return CultureInfo.InvariantCulture.CompareInfo.Compare(left, right, CompareOptions.IgnoreCase) == 0;
|
||||
#else
|
||||
return String.Compare(left, right, StringComparison.InvariantCultureIgnoreCase) == 0;
|
||||
return string.Compare(left, right, StringComparison.InvariantCultureIgnoreCase) == 0;
|
||||
#endif
|
||||
}
|
||||
|
||||
@ -259,7 +258,7 @@ public static string ConstructRequestUrl(Uri url)
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Creates a request elements concatentation value to send with a request.
|
||||
/// Creates a request elements concatentation value to send with a request.
|
||||
/// This is also known as the signature base.
|
||||
/// </summary>
|
||||
/// <seealso href="http://oauth.net/core/1.0#rfc.section.9.1.3"/>
|
||||
@ -276,7 +275,7 @@ public static string ConcatenateRequestElements(string method, string url, WebPa
|
||||
var requestMethod = string.Concat(method.ToUpper(), "&");
|
||||
var requestUrl = string.Concat(UrlEncodeRelaxed(ConstructRequestUrl(new Uri(url))), "&");
|
||||
var requestParameters = UrlEncodeRelaxed(NormalizeRequestParameters(parameters));
|
||||
|
||||
|
||||
sb.Append(requestMethod);
|
||||
sb.Append(requestUrl);
|
||||
sb.Append(requestParameters);
|
||||
@ -293,7 +292,7 @@ public static string ConcatenateRequestElements(string method, string url, WebPa
|
||||
/// <param name="signatureBase">The signature base</param>
|
||||
/// <param name="consumerSecret">The consumer key</param>
|
||||
/// <returns></returns>
|
||||
public static string GetSignature(OAuthSignatureMethod signatureMethod,
|
||||
public static string GetSignature(OAuthSignatureMethod signatureMethod,
|
||||
string signatureBase,
|
||||
string consumerSecret)
|
||||
{
|
||||
@ -311,7 +310,7 @@ public static string GetSignature(OAuthSignatureMethod signatureMethod,
|
||||
/// <param name="consumerSecret">The consumer key</param>
|
||||
/// <returns></returns>
|
||||
public static string GetSignature(OAuthSignatureMethod signatureMethod,
|
||||
OAuthSignatureTreatment signatureTreatment,
|
||||
OAuthSignatureTreatment signatureTreatment,
|
||||
string signatureBase,
|
||||
string consumerSecret)
|
||||
{
|
||||
@ -327,7 +326,7 @@ public static string GetSignature(OAuthSignatureMethod signatureMethod,
|
||||
/// <param name="consumerSecret">The consumer secret</param>
|
||||
/// <param name="tokenSecret">The token secret</param>
|
||||
/// <returns></returns>
|
||||
public static string GetSignature(OAuthSignatureMethod signatureMethod,
|
||||
public static string GetSignature(OAuthSignatureMethod signatureMethod,
|
||||
string signatureBase,
|
||||
string consumerSecret,
|
||||
string tokenSecret)
|
||||
@ -345,7 +344,7 @@ public static string GetSignature(OAuthSignatureMethod signatureMethod,
|
||||
/// <param name="consumerSecret">The consumer secret</param>
|
||||
/// <param name="tokenSecret">The token secret</param>
|
||||
/// <returns></returns>
|
||||
public static string GetSignature(OAuthSignatureMethod signatureMethod,
|
||||
public static string GetSignature(OAuthSignatureMethod signatureMethod,
|
||||
OAuthSignatureTreatment signatureTreatment,
|
||||
string signatureBase,
|
||||
string consumerSecret,
|
||||
@ -353,7 +352,7 @@ public static string GetSignature(OAuthSignatureMethod signatureMethod,
|
||||
{
|
||||
if (IsNullOrBlank(tokenSecret))
|
||||
{
|
||||
tokenSecret = String.Empty;
|
||||
tokenSecret = string.Empty;
|
||||
}
|
||||
|
||||
consumerSecret = UrlEncodeRelaxed(consumerSecret);
|
||||
@ -403,7 +402,7 @@ private static string HashWith(string input, HashAlgorithm algorithm)
|
||||
|
||||
private static bool IsNullOrBlank(string value)
|
||||
{
|
||||
return String.IsNullOrEmpty(value) || (!String.IsNullOrEmpty(value) && value.Trim() == String.Empty);
|
||||
return string.IsNullOrEmpty(value) || (!string.IsNullOrEmpty(value) && string.IsNullOrEmpty(value.Trim()));
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -11,7 +11,6 @@
|
||||
<PackageReference Include="MailKit" Version="2.10.1" />
|
||||
<PackageReference Include="Newtonsoft.Json" Version="12.0.2" />
|
||||
<PackageReference Include="NLog" Version="4.6.6" />
|
||||
<PackageReference Include="OAuth" Version="1.0.3" />
|
||||
<PackageReference Include="RestSharp" Version="106.15.0" />
|
||||
<PackageReference Include="TinyTwitter" Version="1.1.2" />
|
||||
<PackageReference Include="Kveer.XmlRPC" Version="1.2.0" />
|
||||
|
@ -29,59 +29,57 @@ public class Tweet
|
||||
|
||||
public class TinyTwitter
|
||||
{
|
||||
private readonly OAuthInfo oauth;
|
||||
private readonly OAuthInfo _oauth;
|
||||
|
||||
public TinyTwitter(OAuthInfo oauth)
|
||||
{
|
||||
this.oauth = oauth;
|
||||
_oauth = oauth;
|
||||
}
|
||||
|
||||
public void UpdateStatus(string message)
|
||||
{
|
||||
new RequestBuilder(oauth, "POST", "https://api.twitter.com/1.1/statuses/update.json")
|
||||
new RequestBuilder(_oauth, "POST", "https://api.twitter.com/1.1/statuses/update.json")
|
||||
.AddParameter("status", message)
|
||||
.Execute();
|
||||
}
|
||||
|
||||
/**
|
||||
*
|
||||
*
|
||||
* As of June 26th 2015 Direct Messaging is not part of TinyTwitter.
|
||||
* I have added it to Sonarr's copy to make our implementation easier
|
||||
* and added this banner so it's not blindly updated.
|
||||
*
|
||||
*
|
||||
**/
|
||||
|
||||
public void DirectMessage(string message, string screenName)
|
||||
{
|
||||
new RequestBuilder(oauth, "POST", "https://api.twitter.com/1.1/direct_messages/new.json")
|
||||
new RequestBuilder(_oauth, "POST", "https://api.twitter.com/1.1/direct_messages/new.json")
|
||||
.AddParameter("text", message)
|
||||
.AddParameter("screen_name", screenName)
|
||||
.Execute();
|
||||
}
|
||||
|
||||
#region RequestBuilder
|
||||
|
||||
public class RequestBuilder
|
||||
{
|
||||
private const string VERSION = "1.0";
|
||||
private const string SIGNATURE_METHOD = "HMAC-SHA1";
|
||||
|
||||
private readonly OAuthInfo oauth;
|
||||
private readonly string method;
|
||||
private readonly IDictionary<string, string> customParameters;
|
||||
private readonly string url;
|
||||
private readonly OAuthInfo _oauth;
|
||||
private readonly string _method;
|
||||
private readonly IDictionary<string, string> _customParameters;
|
||||
private readonly string _url;
|
||||
|
||||
public RequestBuilder(OAuthInfo oauth, string method, string url)
|
||||
{
|
||||
this.oauth = oauth;
|
||||
this.method = method;
|
||||
this.url = url;
|
||||
customParameters = new Dictionary<string, string>();
|
||||
_oauth = oauth;
|
||||
_method = method;
|
||||
_url = url;
|
||||
_customParameters = new Dictionary<string, string>();
|
||||
}
|
||||
|
||||
public RequestBuilder AddParameter(string name, string value)
|
||||
{
|
||||
customParameters.Add(name, value.EncodeRFC3986());
|
||||
_customParameters.Add(name, value.EncodeRFC3986());
|
||||
return this;
|
||||
}
|
||||
|
||||
@ -90,14 +88,14 @@ public string Execute()
|
||||
var timespan = GetTimestamp();
|
||||
var nonce = CreateNonce();
|
||||
|
||||
var parameters = new Dictionary<string, string>(customParameters);
|
||||
var parameters = new Dictionary<string, string>(_customParameters);
|
||||
AddOAuthParameters(parameters, timespan, nonce);
|
||||
|
||||
var signature = GenerateSignature(parameters);
|
||||
var headerValue = GenerateAuthorizationHeaderValue(parameters, signature);
|
||||
|
||||
var request = (HttpWebRequest)WebRequest.Create(GetRequestUrl());
|
||||
request.Method = method;
|
||||
request.Method = _method;
|
||||
request.ContentType = "application/x-www-form-urlencoded";
|
||||
|
||||
request.Headers.Add("Authorization", headerValue);
|
||||
@ -105,7 +103,7 @@ public string Execute()
|
||||
WriteRequestBody(request);
|
||||
|
||||
// It looks like a bug in HttpWebRequest. It throws random TimeoutExceptions
|
||||
// after some requests. Abort the request seems to work. More info:
|
||||
// after some requests. Abort the request seems to work. More info:
|
||||
// http://stackoverflow.com/questions/2252762/getrequeststream-throws-timeout-exception-randomly
|
||||
|
||||
var response = request.GetResponse();
|
||||
@ -127,25 +125,31 @@ public string Execute()
|
||||
|
||||
private void WriteRequestBody(HttpWebRequest request)
|
||||
{
|
||||
if (method == "GET")
|
||||
if (_method == "GET")
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
var requestBody = Encoding.ASCII.GetBytes(GetCustomParametersString());
|
||||
using (var stream = request.GetRequestStream())
|
||||
{
|
||||
stream.Write(requestBody, 0, requestBody.Length);
|
||||
}
|
||||
}
|
||||
|
||||
private string GetRequestUrl()
|
||||
{
|
||||
if (method != "GET" || customParameters.Count == 0)
|
||||
return url;
|
||||
if (_method != "GET" || _customParameters.Count == 0)
|
||||
{
|
||||
return _url;
|
||||
}
|
||||
|
||||
return string.Format("{0}?{1}", url, GetCustomParametersString());
|
||||
return string.Format("{0}?{1}", _url, GetCustomParametersString());
|
||||
}
|
||||
|
||||
private string GetCustomParametersString()
|
||||
{
|
||||
return customParameters.Select(x => string.Format("{0}={1}", x.Key, x.Value)).Join("&");
|
||||
return _customParameters.Select(x => string.Format("{0}={1}", x.Key, x.Value)).Join("&");
|
||||
}
|
||||
|
||||
private string GenerateAuthorizationHeaderValue(IEnumerable<KeyValuePair<string, string>> parameters, string signature)
|
||||
@ -161,15 +165,15 @@ private string GenerateAuthorizationHeaderValue(IEnumerable<KeyValuePair<string,
|
||||
private string GenerateSignature(IEnumerable<KeyValuePair<string, string>> parameters)
|
||||
{
|
||||
var dataToSign = new StringBuilder()
|
||||
.Append(method).Append("&")
|
||||
.Append(url.EncodeRFC3986()).Append("&")
|
||||
.Append(_method).Append('&')
|
||||
.Append(_url.EncodeRFC3986()).Append('&')
|
||||
.Append(parameters
|
||||
.OrderBy(x => x.Key)
|
||||
.Select(x => string.Format("{0}={1}", x.Key, x.Value))
|
||||
.Join("&")
|
||||
.EncodeRFC3986());
|
||||
|
||||
var signatureKey = string.Format("{0}&{1}", oauth.ConsumerSecret.EncodeRFC3986(), oauth.AccessSecret.EncodeRFC3986());
|
||||
var signatureKey = string.Format("{0}&{1}", _oauth.ConsumerSecret.EncodeRFC3986(), _oauth.AccessSecret.EncodeRFC3986());
|
||||
var sha1 = new HMACSHA1(Encoding.ASCII.GetBytes(signatureKey));
|
||||
|
||||
var signatureBytes = sha1.ComputeHash(Encoding.ASCII.GetBytes(dataToSign.ToString()));
|
||||
@ -179,11 +183,11 @@ private string GenerateSignature(IEnumerable<KeyValuePair<string, string>> param
|
||||
private void AddOAuthParameters(IDictionary<string, string> parameters, string timestamp, string nonce)
|
||||
{
|
||||
parameters.Add("oauth_version", VERSION);
|
||||
parameters.Add("oauth_consumer_key", oauth.ConsumerKey);
|
||||
parameters.Add("oauth_consumer_key", _oauth.ConsumerKey);
|
||||
parameters.Add("oauth_nonce", nonce);
|
||||
parameters.Add("oauth_signature_method", SIGNATURE_METHOD);
|
||||
parameters.Add("oauth_timestamp", timestamp);
|
||||
parameters.Add("oauth_token", oauth.AccessToken);
|
||||
parameters.Add("oauth_token", _oauth.AccessToken);
|
||||
}
|
||||
|
||||
private static string GetTimestamp()
|
||||
@ -196,8 +200,6 @@ private static string CreateNonce()
|
||||
return new Random().Next(0x0000000, 0x7fffffff).ToString("X8");
|
||||
}
|
||||
}
|
||||
|
||||
#endregion
|
||||
}
|
||||
|
||||
public static class TinyTwitterHelperExtensions
|
||||
@ -217,7 +219,9 @@ public static string EncodeRFC3986(this string value)
|
||||
// From Twitterizer http://www.twitterizer.net/
|
||||
|
||||
if (string.IsNullOrEmpty(value))
|
||||
{
|
||||
return string.Empty;
|
||||
}
|
||||
|
||||
var encoded = Uri.EscapeDataString(value);
|
||||
|
||||
@ -232,4 +236,4 @@ public static string EncodeRFC3986(this string value)
|
||||
.Replace("%7E", "~");
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user