Support for digest auth with HttpRequests

[common]
This commit is contained in:
ta264 2020-08-06 21:17:45 +01:00
parent 655017e1ed
commit 1e2d931f9a
14 changed files with 48 additions and 29 deletions

View 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)
{
}
}
}

View File

@ -2,6 +2,7 @@ using System;
using System.IO;
using System.Net;
using System.Reflection;
using System.Text;
using NLog;
using NLog.Fluent;
using NzbDrone.Common.EnvironmentInfo;
@ -52,6 +53,22 @@ namespace NzbDrone.Common.Http.Dispatchers
webRequest.AllowAutoRedirect = false;
webRequest.CookieContainer = cookies;
if (request.Credentials != null)
{
if (request.Credentials is BasicNetworkCredential nc)
{
// Manually set header to avoid initial challenge response
var authInfo = nc.UserName + ":" + nc.Password;
authInfo = Convert.ToBase64String(Encoding.GetEncoding("ISO-8859-1").GetBytes(authInfo));
webRequest.Headers.Add("Authorization", "Basic " + authInfo);
}
else
{
webRequest.PreAuthenticate = true;
webRequest.Credentials = request.Credentials;
}
}
if (request.RequestTimeout != TimeSpan.Zero)
{
webRequest.Timeout = (int)Math.Ceiling(request.RequestTimeout.TotalMilliseconds);

View File

@ -1,6 +1,7 @@
using System;
using System.Collections.Generic;
using System.IO;
using System.Net;
using System.Text;
using NzbDrone.Common.EnvironmentInfo;
using NzbDrone.Common.Extensions;
@ -33,6 +34,7 @@ namespace NzbDrone.Common.Http
public HttpHeader Headers { get; set; }
public byte[] ContentData { get; set; }
public string ContentSummary { get; set; }
public ICredentials Credentials { get; set; }
public bool SuppressHttpError { get; set; }
public bool UseSimplifiedUserAgent { get; set; }
public bool AllowAutoRedirect { get; set; }
@ -80,12 +82,5 @@ namespace NzbDrone.Common.Http
var encoding = HttpHeader.GetEncodingFromContentType(Headers.ContentType);
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);
}
}
}

View File

@ -24,10 +24,9 @@ namespace NzbDrone.Common.Http
public bool ConnectionKeepAlive { get; set; }
public TimeSpan RateLimit { 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 List<HttpFormData> FormData { get; private set; }
public Action<HttpRequest> PostProcess { get; set; }
public HttpRequestBuilder(string baseUrl)
@ -105,13 +104,7 @@ namespace NzbDrone.Common.Http
request.ConnectionKeepAlive = ConnectionKeepAlive;
request.RateLimit = RateLimit;
request.LogResponseContent = LogResponseContent;
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);
}
request.Credentials = NetworkCredential;
foreach (var header in Headers)
{

View File

@ -39,7 +39,7 @@ namespace NzbDrone.Common.Http.Proxy
case ProxyType.Http:
if (proxySettings.Username.IsNotNullOrWhiteSpace() && proxySettings.Password.IsNotNullOrWhiteSpace())
{
return new WebProxy(proxySettings.Host + ":" + proxySettings.Port, proxySettings.BypassLocalAddress, proxySettings.BypassListAsArray, new NetworkCredential(proxySettings.Username, proxySettings.Password));
return new WebProxy(proxySettings.Host + ":" + proxySettings.Port, proxySettings.BypassLocalAddress, proxySettings.BypassListAsArray, new BasicNetworkCredential(proxySettings.Username, proxySettings.Password));
}
else
{

View File

@ -74,7 +74,7 @@ namespace NzbDrone.Core.Download.Clients.Hadouken
var requestBuilder = new JsonRpcRequestBuilder(baseUrl, method, parameters);
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");
var httpRequest = requestBuilder.Build();

View File

@ -229,7 +229,7 @@ namespace NzbDrone.Core.Download.Clients.Nzbget
var requestBuilder = new JsonRpcRequestBuilder(baseUrl, method, parameters);
requestBuilder.LogResponseContent = true;
requestBuilder.NetworkCredential = new NetworkCredential(settings.Username, settings.Password);
requestBuilder.NetworkCredential = new BasicNetworkCredential(settings.Username, settings.Password);
var httpRequest = requestBuilder.Build();

View File

@ -261,7 +261,7 @@ namespace NzbDrone.Core.Download.Clients.QBittorrent
var requestBuilder = new HttpRequestBuilder(settings.UseSsl, settings.Host, settings.Port, settings.UrlBase)
{
LogResponseContent = true,
NetworkCredential = new NetworkCredential(settings.Username, settings.Password)
NetworkCredential = new BasicNetworkCredential(settings.Username, settings.Password)
};
return requestBuilder;
}

View File

@ -270,7 +270,7 @@ namespace NzbDrone.Core.Download.Clients.QBittorrent
var requestBuilder = new HttpRequestBuilder(settings.UseSsl, settings.Host, settings.Port, settings.UrlBase)
{
LogResponseContent = true,
NetworkCredential = new NetworkCredential(settings.Username, settings.Password)
NetworkCredential = new BasicNetworkCredential(settings.Username, settings.Password)
};
return requestBuilder;
}

View File

@ -200,7 +200,7 @@ namespace NzbDrone.Core.Download.Clients.Transmission
.Accept(HttpAccept.Json);
requestBuilder.LogResponseContent = true;
requestBuilder.NetworkCredential = new NetworkCredential(settings.Username, settings.Password);
requestBuilder.NetworkCredential = new BasicNetworkCredential(settings.Username, settings.Password);
requestBuilder.AllowAutoRedirect = false;
return requestBuilder;

View File

@ -4,6 +4,7 @@ using System.Net;
using CookComputing.XmlRpc;
using NLog;
using NzbDrone.Common.Extensions;
using NzbDrone.Common.Http;
namespace NzbDrone.Core.Download.Clients.RTorrent
{
@ -246,7 +247,7 @@ namespace NzbDrone.Core.Download.Clients.RTorrent
if (!settings.Username.IsNullOrWhiteSpace())
{
client.Credentials = new NetworkCredential(settings.Username, settings.Password);
client.Credentials = new BasicNetworkCredential(settings.Username, settings.Password);
}
return client;

View File

@ -196,7 +196,7 @@ namespace NzbDrone.Core.Download.Clients.UTorrent
.Accept(HttpAccept.Json);
requestBuilder.LogResponseContent = true;
requestBuilder.NetworkCredential = new NetworkCredential(settings.Username, settings.Password);
requestBuilder.NetworkCredential = new BasicNetworkCredential(settings.Username, settings.Password);
return requestBuilder;
}

View File

@ -1,10 +1,10 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Net.Mail;
using FluentValidation.Results;
using NLog;
using NzbDrone.Common.Http;
namespace NzbDrone.Core.Notifications.Email
{
@ -44,11 +44,11 @@ namespace NzbDrone.Core.Notifications.Email
}
}
NetworkCredential credentials = null;
BasicNetworkCredential credentials = null;
if (!string.IsNullOrWhiteSpace(settings.Username))
{
credentials = new NetworkCredential(settings.Username, settings.Password);
credentials = new BasicNetworkCredential(settings.Username, settings.Password);
}
try
@ -63,7 +63,7 @@ namespace NzbDrone.Core.Notifications.Email
}
}
private void Send(MailMessage email, string server, int port, bool ssl, NetworkCredential credentials)
private void Send(MailMessage email, string server, int port, bool ssl, BasicNetworkCredential credentials)
{
var smtp = new SmtpClient(server, port);
smtp.EnableSsl = ssl;

View File

@ -1,3 +1,4 @@
using System.Net;
using NzbDrone.Common.Extensions;
using NzbDrone.Common.Http;
using NzbDrone.Common.Serializer;
@ -33,7 +34,7 @@ namespace NzbDrone.Core.Notifications.Webhook
if (settings.Username.IsNotNullOrWhiteSpace() || settings.Password.IsNotNullOrWhiteSpace())
{
request.AddBasicAuthentication(settings.Username, settings.Password);
request.Credentials = new BasicNetworkCredential(settings.Username, settings.Password);
}
_httpClient.Execute(request);