diff --git a/CHANGELIST.md b/CHANGELIST.md index 2b98a535..2f4104a5 100644 --- a/CHANGELIST.md +++ b/CHANGELIST.md @@ -54,6 +54,7 @@ - Add multisession helper method skeleton - Move and update options loader; clean up Check - Move helper methods around +- Consolidate Redump login testing ### 2.3 (2022-02-05) - Start overhauling Redump information pulling, again diff --git a/MPF.Check/Program.cs b/MPF.Check/Program.cs index 4391849f..b1649413 100644 --- a/MPF.Check/Program.cs +++ b/MPF.Check/Program.cs @@ -51,7 +51,9 @@ namespace MPF.Check protectionProgress.ProgressChanged += ProgressUpdated; // Validate the supplied credentials - ValidateCredentials(options); + (bool? _, string message) = RedumpWebClient.ValidateCredentials(options?.RedumpUsername, options?.RedumpPassword); + if (!string.IsNullOrWhiteSpace(message)) + Console.WriteLine(message); // Loop through all the rest of the args for (int i = startIndex; i < args.Length; i++) @@ -170,28 +172,5 @@ namespace MPF.Check { Console.WriteLine($"{value.Percentage * 100:N2}%: {value.Filename} - {value.Protection}"); } - - /// - /// Validate supplied credentials - /// - /// TODO: Move to a common location - private static void ValidateCredentials(Options options) - { - // If options are invalid or we're missing something key, just return - if (string.IsNullOrWhiteSpace(options?.RedumpUsername) || string.IsNullOrWhiteSpace(options?.RedumpPassword)) - return; - - // Try logging in with the supplied credentials otherwise - using (RedumpWebClient wc = new RedumpWebClient()) - { - bool? loggedIn = wc.Login(options.RedumpUsername, options.RedumpPassword); - if (loggedIn == true) - Console.WriteLine("Redump username and password accepted!"); - else if (loggedIn == false) - Console.WriteLine("Redump username and password denied!"); - else - Console.WriteLine("An error occurred validating your crendentials!"); - } - } } } diff --git a/MPF/ViewModels/OptionsViewModel.cs b/MPF/ViewModels/OptionsViewModel.cs index 8fa280d5..b4d0e16b 100644 --- a/MPF/ViewModels/OptionsViewModel.cs +++ b/MPF/ViewModels/OptionsViewModel.cs @@ -189,16 +189,13 @@ namespace MPF.UI.ViewModels /// private void TestRedumpLogin() { - using (RedumpWebClient wc = new RedumpWebClient()) - { - bool? loggedIn = wc.Login(Parent.RedumpUsernameTextBox.Text, Parent.RedumpPasswordBox.Password); - if (loggedIn == true) - CustomMessageBox.Show(Parent, "Redump login credentials accepted!", "Success", MessageBoxButton.OK, MessageBoxImage.Information); - else if (loggedIn == false) - CustomMessageBox.Show(Parent, "Redump login credentials denied!", "Failure", MessageBoxButton.OK, MessageBoxImage.Error); - else - CustomMessageBox.Show(Parent, "Error validating credentials!", "Error", MessageBoxButton.OK, MessageBoxImage.Error); - } + (bool? success, string message) = RedumpWebClient.ValidateCredentials(Parent.RedumpUsernameTextBox.Text, Parent.RedumpPasswordBox.Password); + if (success == true) + CustomMessageBox.Show(Parent, message, "Success", MessageBoxButton.OK, MessageBoxImage.Information); + else if (success == false) + CustomMessageBox.Show(Parent, message, "Failure", MessageBoxButton.OK, MessageBoxImage.Error); + else + CustomMessageBox.Show(Parent, message, "Error", MessageBoxButton.OK, MessageBoxImage.Error); } #endregion diff --git a/RedumpLib/Web/RedumpWebClient.cs b/RedumpLib/Web/RedumpWebClient.cs index 5611fc2a..b329a9ec 100644 --- a/RedumpLib/Web/RedumpWebClient.cs +++ b/RedumpLib/Web/RedumpWebClient.cs @@ -55,6 +55,28 @@ namespace RedumpLib.Web return request; } + /// + /// Validate supplied credentials + /// + public static (bool?, string) ValidateCredentials(string username, string password) + { + // If options are invalid or we're missing something key, just return + if (string.IsNullOrWhiteSpace(username) || string.IsNullOrWhiteSpace(password)) + return (false, null); + + // Try logging in with the supplied credentials otherwise + using (RedumpWebClient wc = new RedumpWebClient()) + { + bool? loggedIn = wc.Login(username, password); + if (loggedIn == true) + return (true, "Redump username and password accepted!"); + else if (loggedIn == false) + return (false, "Redump username and password denied!"); + else + return (null, "An error occurred validating your credentials!"); + } + } + /// /// Login to Redump, if possible ///