Fixed Authorization on redirect

This commit is contained in:
glax 2023-05-24 23:52:25 +02:00
parent fd3423d03c
commit fc89537f63

View File

@ -1,4 +1,5 @@
using System.Net.Http.Headers; using System.Net;
using System.Net.Http.Headers;
using System.Text.Json.Nodes; using System.Text.Json.Nodes;
using Logging; using Logging;
using Newtonsoft.Json; using Newtonsoft.Json;
@ -45,9 +46,17 @@ public class Komga
{ {
logger?.WriteLine(this.GetType().ToString(), $"Getting Libraries"); logger?.WriteLine(this.GetType().ToString(), $"Getting Libraries");
Stream data = NetClient.MakeRequest($"{baseUrl}/api/v1/libraries", auth); Stream data = NetClient.MakeRequest($"{baseUrl}/api/v1/libraries", auth);
if (data == Stream.Null)
{
logger?.WriteLine(this.GetType().ToString(), $"No libraries returned");
return Array.Empty<KomgaLibrary>();
}
JsonArray? result = JsonSerializer.Deserialize<JsonArray>(data); JsonArray? result = JsonSerializer.Deserialize<JsonArray>(data);
if (result is null) if (result is null)
{
logger?.WriteLine(this.GetType().ToString(), $"No libraries returned");
return Array.Empty<KomgaLibrary>(); return Array.Empty<KomgaLibrary>();
}
HashSet<KomgaLibrary> ret = new(); HashSet<KomgaLibrary> ret = new();
@ -89,43 +98,51 @@ public class Komga
{ {
public static Stream MakeRequest(string url, string auth) public static Stream MakeRequest(string url, string auth)
{ {
HttpClientHandler clientHandler = new HttpClientHandler(); HttpClientHandler clientHandler = new ();
clientHandler.ServerCertificateCustomValidationCallback = (message, cert, chain, sslPolicyErrors) => clientHandler.ServerCertificateCustomValidationCallback = (message, cert, chain, sslPolicyErrors) => true;
{
return true;
};
HttpClient client = new(clientHandler); HttpClient client = new(clientHandler);
client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Basic", auth);
HttpRequestMessage requestMessage = new HttpRequestMessage HttpRequestMessage requestMessage = new ()
{ {
Method = HttpMethod.Get, Method = HttpMethod.Get,
RequestUri = new Uri(url), RequestUri = new Uri(url)
Headers =
{
{ "Accept", "application/json" },
{ "Authorization", new AuthenticationHeaderValue("Basic", auth).ToString() }
}
}; };
HttpResponseMessage response = client.Send(requestMessage); HttpResponseMessage response = client.Send(requestMessage);
Stream resultString = response.IsSuccessStatusCode ? response.Content.ReadAsStream() : Stream.Null; Stream ret;
return resultString; if (response.StatusCode is HttpStatusCode.Unauthorized)
{
ret = MakeRequest(response.RequestMessage!.RequestUri!.AbsoluteUri, auth);
}else
return response.IsSuccessStatusCode ? response.Content.ReadAsStream() : Stream.Null;
return ret;
} }
public static bool MakePost(string url, string auth) public static bool MakePost(string url, string auth)
{ {
HttpClient client = new(); HttpClientHandler clientHandler = new HttpClientHandler();
HttpRequestMessage requestMessage = new HttpRequestMessage clientHandler.ServerCertificateCustomValidationCallback = (message, cert, chain, sslPolicyErrors) => true;
HttpClient client = new(clientHandler)
{ {
Method = HttpMethod.Post, DefaultRequestHeaders =
RequestUri = new Uri(url),
Headers =
{ {
{ "Accept", "application/json" }, { "Accept", "application/json" },
{ "Authorization", new AuthenticationHeaderValue("Basic", auth).ToString() } { "Authorization", new AuthenticationHeaderValue("Basic", auth).ToString() }
} }
}; };
HttpRequestMessage requestMessage = new HttpRequestMessage
{
Method = HttpMethod.Post,
RequestUri = new Uri(url)
};
HttpResponseMessage response = client.Send(requestMessage); HttpResponseMessage response = client.Send(requestMessage);
bool ret;
if (response.StatusCode is HttpStatusCode.Unauthorized)
{
ret = MakePost(response.RequestMessage!.RequestUri!.AbsoluteUri, auth);
}else
return response.IsSuccessStatusCode; return response.IsSuccessStatusCode;
return ret;
} }
} }
} }