mirror of
https://github.com/Radarr/Radarr.git
synced 2024-08-16 15:29:40 +02:00
Support for digest auth with HttpRequests
(cherry picked from commit 1e2d931f9a
)
This commit is contained in:
parent
4d1f251c1f
commit
de243991dd
12
src/NzbDrone.Common/Http/BasicNetworkCredential.cs
Normal file
12
src/NzbDrone.Common/Http/BasicNetworkCredential.cs
Normal file
@ -0,0 +1,12 @@
|
|||||||
|
using System.Net;
|
||||||
|
|
||||||
|
namespace NzbDrone.Common.Http
|
||||||
|
{
|
||||||
|
public class BasicNetworkCredential : NetworkCredential
|
||||||
|
{
|
||||||
|
public BasicNetworkCredential(string user, string pass)
|
||||||
|
: base(user, pass)
|
||||||
|
{
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
@ -4,9 +4,9 @@
|
|||||||
using System.Net.Http;
|
using System.Net.Http;
|
||||||
using System.Net.Security;
|
using System.Net.Security;
|
||||||
using System.Net.Sockets;
|
using System.Net.Sockets;
|
||||||
|
using System.Text;
|
||||||
using System.Threading;
|
using System.Threading;
|
||||||
using System.Threading.Tasks;
|
using System.Threading.Tasks;
|
||||||
using NLog;
|
|
||||||
using NzbDrone.Common.Cache;
|
using NzbDrone.Common.Cache;
|
||||||
using NzbDrone.Common.Extensions;
|
using NzbDrone.Common.Extensions;
|
||||||
using NzbDrone.Common.Http.Proxy;
|
using NzbDrone.Common.Http.Proxy;
|
||||||
@ -26,6 +26,7 @@ public class ManagedHttpDispatcher : IHttpDispatcher
|
|||||||
private readonly ICertificateValidationService _certificateValidationService;
|
private readonly ICertificateValidationService _certificateValidationService;
|
||||||
private readonly IUserAgentBuilder _userAgentBuilder;
|
private readonly IUserAgentBuilder _userAgentBuilder;
|
||||||
private readonly ICached<System.Net.Http.HttpClient> _httpClientCache;
|
private readonly ICached<System.Net.Http.HttpClient> _httpClientCache;
|
||||||
|
private readonly ICached<CredentialCache> _credentialCache;
|
||||||
|
|
||||||
public ManagedHttpDispatcher(IHttpProxySettingsProvider proxySettingsProvider,
|
public ManagedHttpDispatcher(IHttpProxySettingsProvider proxySettingsProvider,
|
||||||
ICreateManagedWebProxy createManagedWebProxy,
|
ICreateManagedWebProxy createManagedWebProxy,
|
||||||
@ -39,6 +40,7 @@ public ManagedHttpDispatcher(IHttpProxySettingsProvider proxySettingsProvider,
|
|||||||
_userAgentBuilder = userAgentBuilder;
|
_userAgentBuilder = userAgentBuilder;
|
||||||
|
|
||||||
_httpClientCache = cacheManager.GetCache<System.Net.Http.HttpClient>(typeof(ManagedHttpDispatcher));
|
_httpClientCache = cacheManager.GetCache<System.Net.Http.HttpClient>(typeof(ManagedHttpDispatcher));
|
||||||
|
_credentialCache = cacheManager.GetCache<CredentialCache>(typeof(ManagedHttpDispatcher), "credentialcache");
|
||||||
}
|
}
|
||||||
|
|
||||||
public HttpResponse GetResponse(HttpRequest request, CookieContainer cookies)
|
public HttpResponse GetResponse(HttpRequest request, CookieContainer cookies)
|
||||||
@ -64,6 +66,26 @@ public HttpResponse GetResponse(HttpRequest request, CookieContainer cookies)
|
|||||||
cts.CancelAfter(TimeSpan.FromSeconds(100));
|
cts.CancelAfter(TimeSpan.FromSeconds(100));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if (request.Credentials != null)
|
||||||
|
{
|
||||||
|
if (request.Credentials is BasicNetworkCredential bc)
|
||||||
|
{
|
||||||
|
// Manually set header to avoid initial challenge response
|
||||||
|
var authInfo = bc.UserName + ":" + bc.Password;
|
||||||
|
authInfo = Convert.ToBase64String(Encoding.GetEncoding("ISO-8859-1").GetBytes(authInfo));
|
||||||
|
requestMessage.Headers.Add("Authorization", "Basic " + authInfo);
|
||||||
|
}
|
||||||
|
else if (request.Credentials is NetworkCredential nc)
|
||||||
|
{
|
||||||
|
var creds = GetCredentialCache();
|
||||||
|
foreach (var authtype in new[] { "Basic", "Digest" })
|
||||||
|
{
|
||||||
|
creds.Remove((Uri)request.Url, authtype);
|
||||||
|
creds.Add((Uri)request.Url, authtype, nc);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
if (request.ContentData != null)
|
if (request.ContentData != null)
|
||||||
{
|
{
|
||||||
requestMessage.Content = new ByteArrayContent(request.ContentData);
|
requestMessage.Content = new ByteArrayContent(request.ContentData);
|
||||||
@ -120,6 +142,8 @@ protected virtual System.Net.Http.HttpClient CreateHttpClient(HttpProxySettings
|
|||||||
AutomaticDecompression = DecompressionMethods.GZip | DecompressionMethods.Brotli,
|
AutomaticDecompression = DecompressionMethods.GZip | DecompressionMethods.Brotli,
|
||||||
UseCookies = false, // sic - we don't want to use a shared cookie container
|
UseCookies = false, // sic - we don't want to use a shared cookie container
|
||||||
AllowAutoRedirect = false,
|
AllowAutoRedirect = false,
|
||||||
|
Credentials = GetCredentialCache(),
|
||||||
|
PreAuthenticate = true,
|
||||||
MaxConnectionsPerServer = 12,
|
MaxConnectionsPerServer = 12,
|
||||||
ConnectCallback = onConnect,
|
ConnectCallback = onConnect,
|
||||||
SslOptions = new SslClientAuthenticationOptions
|
SslOptions = new SslClientAuthenticationOptions
|
||||||
@ -204,6 +228,11 @@ private void AddContentHeader(HttpRequestMessage request, string header, string
|
|||||||
headers.Add(header, value);
|
headers.Add(header, value);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private CredentialCache GetCredentialCache()
|
||||||
|
{
|
||||||
|
return _credentialCache.Get("credentialCache", () => new CredentialCache());
|
||||||
|
}
|
||||||
|
|
||||||
private static async ValueTask<Stream> onConnect(SocketsHttpConnectionContext context, CancellationToken cancellationToken)
|
private static async ValueTask<Stream> onConnect(SocketsHttpConnectionContext context, CancellationToken cancellationToken)
|
||||||
{
|
{
|
||||||
// Until .NET supports an implementation of Happy Eyeballs (https://tools.ietf.org/html/rfc8305#section-2), let's make IPv4 fallback work in a simple way.
|
// Until .NET supports an implementation of Happy Eyeballs (https://tools.ietf.org/html/rfc8305#section-2), let's make IPv4 fallback work in a simple way.
|
||||||
|
@ -38,6 +38,7 @@ public HttpRequest(string url, HttpAccept httpAccept = null)
|
|||||||
public HttpHeader Headers { get; set; }
|
public HttpHeader Headers { get; set; }
|
||||||
public byte[] ContentData { get; set; }
|
public byte[] ContentData { get; set; }
|
||||||
public string ContentSummary { get; set; }
|
public string ContentSummary { get; set; }
|
||||||
|
public ICredentials Credentials { get; set; }
|
||||||
public bool SuppressHttpError { get; set; }
|
public bool SuppressHttpError { get; set; }
|
||||||
public IEnumerable<HttpStatusCode> SuppressHttpErrorStatusCodes { get; set; }
|
public IEnumerable<HttpStatusCode> SuppressHttpErrorStatusCodes { get; set; }
|
||||||
public bool UseSimplifiedUserAgent { get; set; }
|
public bool UseSimplifiedUserAgent { get; set; }
|
||||||
@ -89,12 +90,5 @@ public void SetContent(string data)
|
|||||||
var encoding = HttpHeader.GetEncodingFromContentType(Headers.ContentType);
|
var encoding = HttpHeader.GetEncodingFromContentType(Headers.ContentType);
|
||||||
ContentData = encoding.GetBytes(data);
|
ContentData = encoding.GetBytes(data);
|
||||||
}
|
}
|
||||||
|
|
||||||
public void AddBasicAuthentication(string username, string password)
|
|
||||||
{
|
|
||||||
var authInfo = Convert.ToBase64String(Encoding.GetEncoding("ISO-8859-1").GetBytes($"{username}:{password}"));
|
|
||||||
|
|
||||||
Headers.Set("Authorization", "Basic " + authInfo);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -26,10 +26,9 @@ public class HttpRequestBuilder
|
|||||||
public bool ConnectionKeepAlive { get; set; }
|
public bool ConnectionKeepAlive { get; set; }
|
||||||
public TimeSpan RateLimit { get; set; }
|
public TimeSpan RateLimit { get; set; }
|
||||||
public bool LogResponseContent { get; set; }
|
public bool LogResponseContent { get; set; }
|
||||||
public NetworkCredential NetworkCredential { get; set; }
|
public ICredentials NetworkCredential { get; set; }
|
||||||
public Dictionary<string, string> Cookies { get; private set; }
|
public Dictionary<string, string> Cookies { get; private set; }
|
||||||
public List<HttpFormData> FormData { get; private set; }
|
public List<HttpFormData> FormData { get; private set; }
|
||||||
|
|
||||||
public Action<HttpRequest> PostProcess { get; set; }
|
public Action<HttpRequest> PostProcess { get; set; }
|
||||||
|
|
||||||
public HttpRequestBuilder(string baseUrl)
|
public HttpRequestBuilder(string baseUrl)
|
||||||
@ -109,13 +108,7 @@ protected virtual void Apply(HttpRequest request)
|
|||||||
request.ConnectionKeepAlive = ConnectionKeepAlive;
|
request.ConnectionKeepAlive = ConnectionKeepAlive;
|
||||||
request.RateLimit = RateLimit;
|
request.RateLimit = RateLimit;
|
||||||
request.LogResponseContent = LogResponseContent;
|
request.LogResponseContent = LogResponseContent;
|
||||||
|
request.Credentials = NetworkCredential;
|
||||||
if (NetworkCredential != null)
|
|
||||||
{
|
|
||||||
var authInfo = NetworkCredential.UserName + ":" + NetworkCredential.Password;
|
|
||||||
authInfo = Convert.ToBase64String(Encoding.GetEncoding("ISO-8859-1").GetBytes(authInfo));
|
|
||||||
request.Headers.Set("Authorization", "Basic " + authInfo);
|
|
||||||
}
|
|
||||||
|
|
||||||
foreach (var header in Headers)
|
foreach (var header in Headers)
|
||||||
{
|
{
|
||||||
|
@ -73,7 +73,7 @@ private T ProcessRequest<T>(HadoukenSettings settings, string method, params obj
|
|||||||
baseUrl = HttpUri.CombinePath(baseUrl, "api");
|
baseUrl = HttpUri.CombinePath(baseUrl, "api");
|
||||||
var requestBuilder = new JsonRpcRequestBuilder(baseUrl, method, parameters);
|
var requestBuilder = new JsonRpcRequestBuilder(baseUrl, method, parameters);
|
||||||
requestBuilder.LogResponseContent = true;
|
requestBuilder.LogResponseContent = true;
|
||||||
requestBuilder.NetworkCredential = new NetworkCredential(settings.Username, settings.Password);
|
requestBuilder.NetworkCredential = new BasicNetworkCredential(settings.Username, settings.Password);
|
||||||
requestBuilder.Headers.Add("Accept-Encoding", "gzip,deflate");
|
requestBuilder.Headers.Add("Accept-Encoding", "gzip,deflate");
|
||||||
|
|
||||||
var httpRequest = requestBuilder.Build();
|
var httpRequest = requestBuilder.Build();
|
||||||
|
@ -229,7 +229,7 @@ private T ProcessRequest<T>(NzbgetSettings settings, string method, params objec
|
|||||||
|
|
||||||
var requestBuilder = new JsonRpcRequestBuilder(baseUrl, method, parameters);
|
var requestBuilder = new JsonRpcRequestBuilder(baseUrl, method, parameters);
|
||||||
requestBuilder.LogResponseContent = true;
|
requestBuilder.LogResponseContent = true;
|
||||||
requestBuilder.NetworkCredential = new NetworkCredential(settings.Username, settings.Password);
|
requestBuilder.NetworkCredential = new BasicNetworkCredential(settings.Username, settings.Password);
|
||||||
|
|
||||||
var httpRequest = requestBuilder.Build();
|
var httpRequest = requestBuilder.Build();
|
||||||
|
|
||||||
|
@ -293,7 +293,7 @@ private HttpRequestBuilder BuildRequest(QBittorrentSettings settings)
|
|||||||
var requestBuilder = new HttpRequestBuilder(settings.UseSsl, settings.Host, settings.Port, settings.UrlBase)
|
var requestBuilder = new HttpRequestBuilder(settings.UseSsl, settings.Host, settings.Port, settings.UrlBase)
|
||||||
{
|
{
|
||||||
LogResponseContent = true,
|
LogResponseContent = true,
|
||||||
NetworkCredential = new NetworkCredential(settings.Username, settings.Password)
|
NetworkCredential = new BasicNetworkCredential(settings.Username, settings.Password)
|
||||||
};
|
};
|
||||||
return requestBuilder;
|
return requestBuilder;
|
||||||
}
|
}
|
||||||
|
@ -335,7 +335,7 @@ private HttpRequestBuilder BuildRequest(QBittorrentSettings settings)
|
|||||||
var requestBuilder = new HttpRequestBuilder(settings.UseSsl, settings.Host, settings.Port, settings.UrlBase)
|
var requestBuilder = new HttpRequestBuilder(settings.UseSsl, settings.Host, settings.Port, settings.UrlBase)
|
||||||
{
|
{
|
||||||
LogResponseContent = true,
|
LogResponseContent = true,
|
||||||
NetworkCredential = new NetworkCredential(settings.Username, settings.Password)
|
NetworkCredential = new BasicNetworkCredential(settings.Username, settings.Password)
|
||||||
};
|
};
|
||||||
return requestBuilder;
|
return requestBuilder;
|
||||||
}
|
}
|
||||||
|
@ -200,7 +200,7 @@ private HttpRequestBuilder BuildRequest(TransmissionSettings settings)
|
|||||||
.Accept(HttpAccept.Json);
|
.Accept(HttpAccept.Json);
|
||||||
|
|
||||||
requestBuilder.LogResponseContent = true;
|
requestBuilder.LogResponseContent = true;
|
||||||
requestBuilder.NetworkCredential = new NetworkCredential(settings.Username, settings.Password);
|
requestBuilder.NetworkCredential = new BasicNetworkCredential(settings.Username, settings.Password);
|
||||||
requestBuilder.AllowAutoRedirect = false;
|
requestBuilder.AllowAutoRedirect = false;
|
||||||
|
|
||||||
return requestBuilder;
|
return requestBuilder;
|
||||||
|
@ -196,7 +196,7 @@ private HttpRequestBuilder BuildRequest(UTorrentSettings settings)
|
|||||||
.Accept(HttpAccept.Json);
|
.Accept(HttpAccept.Json);
|
||||||
|
|
||||||
requestBuilder.LogResponseContent = true;
|
requestBuilder.LogResponseContent = true;
|
||||||
requestBuilder.NetworkCredential = new NetworkCredential(settings.Username, settings.Password);
|
requestBuilder.NetworkCredential = new BasicNetworkCredential(settings.Username, settings.Password);
|
||||||
|
|
||||||
return requestBuilder;
|
return requestBuilder;
|
||||||
}
|
}
|
||||||
|
@ -102,7 +102,7 @@ public List<PushBulletDevice> GetDevices(PushBulletSettings settings)
|
|||||||
var request = requestBuilder.Build();
|
var request = requestBuilder.Build();
|
||||||
|
|
||||||
request.Method = HttpMethod.Get;
|
request.Method = HttpMethod.Get;
|
||||||
request.AddBasicAuthentication(settings.ApiKey, string.Empty);
|
request.Credentials = new BasicNetworkCredential(settings.ApiKey, string.Empty);
|
||||||
|
|
||||||
var response = _httpClient.Execute(request);
|
var response = _httpClient.Execute(request);
|
||||||
|
|
||||||
@ -198,7 +198,7 @@ private void SendNotification(string title, string message, HttpRequestBuilder r
|
|||||||
|
|
||||||
var request = requestBuilder.Build();
|
var request = requestBuilder.Build();
|
||||||
|
|
||||||
request.AddBasicAuthentication(settings.ApiKey, string.Empty);
|
request.Credentials = new BasicNetworkCredential(settings.ApiKey, string.Empty);
|
||||||
|
|
||||||
_httpClient.Execute(request);
|
_httpClient.Execute(request);
|
||||||
}
|
}
|
||||||
|
@ -40,7 +40,7 @@ public void SendWebhook(WebhookPayload body, WebhookSettings settings)
|
|||||||
|
|
||||||
if (settings.Username.IsNotNullOrWhiteSpace() || settings.Password.IsNotNullOrWhiteSpace())
|
if (settings.Username.IsNotNullOrWhiteSpace() || settings.Password.IsNotNullOrWhiteSpace())
|
||||||
{
|
{
|
||||||
request.AddBasicAuthentication(settings.Username, settings.Password);
|
request.Credentials = new BasicNetworkCredential(settings.Username, settings.Password);
|
||||||
}
|
}
|
||||||
|
|
||||||
_httpClient.Execute(request);
|
_httpClient.Execute(request);
|
||||||
|
@ -84,7 +84,7 @@ private string ProcessRequest(XbmcSettings settings, string method, params objec
|
|||||||
|
|
||||||
if (!settings.Username.IsNullOrWhiteSpace())
|
if (!settings.Username.IsNullOrWhiteSpace())
|
||||||
{
|
{
|
||||||
request.AddBasicAuthentication(settings.Username, settings.Password);
|
request.Credentials = new BasicNetworkCredential(settings.Username, settings.Password);
|
||||||
}
|
}
|
||||||
|
|
||||||
var response = _httpClient.Execute(request);
|
var response = _httpClient.Execute(request);
|
||||||
|
Loading…
Reference in New Issue
Block a user