From cc19b0bb7c7b6620b09158483cbc80ed10fe8883 Mon Sep 17 00:00:00 2001 From: barelyprofessional <150058423+barelyprofessional@users.noreply.github.com> Date: Mon, 2 Sep 2024 20:29:54 +0800 Subject: [PATCH] Cleaned up the duplicated login page request code --- KfChatDotNetBot/Services/KfTokenService.cs | 25 ++++++++++------------ 1 file changed, 11 insertions(+), 14 deletions(-) diff --git a/KfChatDotNetBot/Services/KfTokenService.cs b/KfChatDotNetBot/Services/KfTokenService.cs index e496a31..1239145 100644 --- a/KfChatDotNetBot/Services/KfTokenService.cs +++ b/KfChatDotNetBot/Services/KfTokenService.cs @@ -77,7 +77,7 @@ public class KfTokenService throw new Exception("Failed to solve the challenge"); } - public async Task IsLoggedIn() + private async Task GetLoginPage() { _logger.Debug("Checking clearance token is actually valid first"); await CheckClearanceToken(); @@ -89,8 +89,13 @@ public class KfTokenService throw new KiwiFlareChallengedException(); } response.EnsureSuccessStatusCode(); + return await response.Content.ReadAsStreamAsync(_ctx); + } + + public async Task IsLoggedIn() + { var document = new HtmlDocument(); - document.Load(await response.Content.ReadAsStreamAsync(_ctx)); + document.Load(await GetLoginPage()); var html = document.DocumentNode.SelectSingleNode("//html"); if (html == null) throw new Exception("Caught a null when retrieving html element"); if (!html.Attributes.Contains("data-logged-in")) @@ -103,18 +108,8 @@ public class KfTokenService public async Task PerformLogin(string username, string password) { - _logger.Debug("Checking clearance token is actually valid first"); - await CheckClearanceToken(); - using var client = new HttpClient(GetHttpClientHandler()); - var response = await client.GetAsync($"https://{_kfDomain}/login", _ctx); - if (response.StatusCode == HttpStatusCode.NonAuthoritativeInformation) - { - _logger.Error("Caught a 203 response when trying to load logon page which means we were KiwiFlare challenged"); - throw new KiwiFlareChallengedException(); - } - response.EnsureSuccessStatusCode(); var document = new HtmlDocument(); - document.Load(await response.Content.ReadAsStreamAsync(_ctx)); + document.Load(await GetLoginPage()); var html = document.DocumentNode.SelectSingleNode("//html"); if (html == null) throw new Exception("Caught a null when retrieving html element"); // Already logged in @@ -128,6 +123,7 @@ public class KfTokenService new("password", password), new("_xfRedirect", $"https://{_kfDomain}/") }); + using var client = new HttpClient(GetHttpClientHandler()); var postResponse = await client.PostAsync($"https://{_kfDomain}/login/login", formData, _ctx); if (postResponse.StatusCode == HttpStatusCode.SeeOther) { @@ -135,7 +131,7 @@ public class KfTokenService return; } postResponse.EnsureSuccessStatusCode(); - _logger.Error($"Received HTTP response {postResponse.StatusCode}, checking to see if we're logged in"); + _logger.Info($"Received HTTP response {postResponse.StatusCode}, checking to see if we're logged in"); var postDocument = new HtmlDocument(); postDocument.Load(await postResponse.Content.ReadAsStreamAsync(_ctx)); html = postDocument.DocumentNode.SelectSingleNode("//html"); @@ -158,6 +154,7 @@ public class KfTokenService public async Task SaveCookies() { + _logger.Debug("Saving cookies"); var cookiesToSave = _cookies.GetAllCookies().ToDictionary(cookie => cookie.Name, cookie => cookie.Value); await Helpers.SetValueAsJsonObject(BuiltIn.Keys.KiwiFarmsCookies, cookiesToSave); }