From c13ca234c83d42fd71e0bde7c3b77f3d5c34a0c4 Mon Sep 17 00:00:00 2001 From: Loporrit <141286461+loporrit@users.noreply.github.com> Date: Fri, 1 Sep 2023 19:47:30 +0000 Subject: [PATCH] Account registration support V1 --- MareSynchronos/UI/IntroUI.cs | 89 +++++++++++++++++++++++++++++++-- MareSynchronos/UI/SettingsUi.cs | 77 ++++++++++++++++++++++++++-- 2 files changed, 159 insertions(+), 7 deletions(-) diff --git a/MareSynchronos/UI/IntroUI.cs b/MareSynchronos/UI/IntroUI.cs index c35cbd3..79dd240 100644 --- a/MareSynchronos/UI/IntroUI.cs +++ b/MareSynchronos/UI/IntroUI.cs @@ -1,4 +1,5 @@ -using Dalamud.Interface.Colors; +using Dalamud.Interface; +using Dalamud.Interface.Colors; using Dalamud.Interface.Utility; using Dalamud.Utility; using ImGuiNET; @@ -8,13 +9,20 @@ using MareSynchronos.MareConfiguration; using MareSynchronos.MareConfiguration.Models; using MareSynchronos.Services.Mediator; using MareSynchronos.Services.ServerConfiguration; +using MareSynchronos.WebAPI; +using MareSynchronos.API.Dto.Account; +using MareSynchronos.API.Routes; using Microsoft.Extensions.Logging; +using System.Net.Http.Headers; +using System.Net.Http.Json; using System.Numerics; +using System.Reflection; namespace MareSynchronos.UI; public class IntroUi : WindowMediatorSubscriberBase { + private readonly ApiController _apiController; private readonly MareConfigService _configService; private readonly PeriodicFileScanner _fileCacheManager; private readonly Dictionary _languages = new(StringComparer.Ordinal) { { "English", "en" }, { "Deutsch", "de" }, { "Français", "fr" } }; @@ -27,12 +35,17 @@ public class IntroUi : WindowMediatorSubscriberBase private string _timeoutLabel = string.Empty; private Task? _timeoutTask; private string[]? _tosParagraphs; + private bool _registrationInProgress = false; + private bool _registrationSuccess = false; + private string? _registrationMessage; + private RegisterReplyDto? _registrationReply; - public IntroUi(ILogger logger, UiSharedService uiShared, MareConfigService configService, + public IntroUi(ILogger logger, UiSharedService uiShared, MareConfigService configService, ApiController apiController, PeriodicFileScanner fileCacheManager, ServerConfigurationManager serverConfigurationManager, MareMediator mareMediator) : base(logger, mareMediator, "Loporrit Setup") { _uiShared = uiShared; _configService = configService; + _apiController = apiController; _fileCacheManager = fileCacheManager; _serverConfigurationManager = serverConfigurationManager; @@ -199,6 +212,7 @@ public class IntroUi : WindowMediatorSubscriberBase UiSharedService.TextWrapped("Once you have received a secret key you can connect to the service using the tools provided below."); + ImGui.BeginDisabled(_registrationInProgress); _ = _uiShared.DrawServiceSelection(selectOnChange: true); var text = "Enter Secret Key"; @@ -219,10 +233,15 @@ public class IntroUi : WindowMediatorSubscriberBase ImGui.SameLine(); if (ImGui.Button(buttonText)) { + string keyName; if (_serverConfigurationManager.CurrentServer == null) _serverConfigurationManager.SelectServer(0); + if (_registrationReply != null && _secretKey == _registrationReply.SecretKey) + keyName = _registrationReply.UID + $" (registered {DateTime.Now:yyyy-MM-dd})"; + else + keyName = $"Secret Key added on Setup ({DateTime.Now:yyyy-MM-dd})"; _serverConfigurationManager.CurrentServer!.SecretKeys.Add(_serverConfigurationManager.CurrentServer.SecretKeys.Select(k => k.Key).LastOrDefault() + 1, new SecretKey() { - FriendlyName = $"Secret Key added on Setup ({DateTime.Now:yyyy-MM-dd})", + FriendlyName = keyName, Key = _secretKey, }); _serverConfigurationManager.AddCurrentCharacterToServer(addLastSecretKey: true); @@ -230,6 +249,68 @@ public class IntroUi : WindowMediatorSubscriberBase _ = Task.Run(() => _uiShared.ApiController.CreateConnections()); } } + + if (_serverConfigurationManager.CurrentApiUrl == ApiController.LoporritServiceUri) + { + ImGui.BeginDisabled(_registrationInProgress || _registrationSuccess || _secretKey.Length > 0); + ImGui.Separator(); + ImGui.TextUnformatted("If you do not have a secret key already click below to register a new account."); + if (UiSharedService.NormalizedIconTextButton(FontAwesomeIcon.Plus, "Register a new Loporrit account")) + { + _registrationInProgress = true; + _ = Task.Run(async () => { + try + { + using HttpClient httpClient = new(); + var ver = Assembly.GetExecutingAssembly().GetName().Version; + httpClient.DefaultRequestHeaders.UserAgent.Add(new ProductInfoHeaderValue("MareSynchronos", ver!.Major + "." + ver!.Minor + "." + ver!.Build)); + var postUri = MareAuth.AuthRegisterFullPath(new Uri(_serverConfigurationManager.CurrentApiUrl + .Replace("wss://", "https://", StringComparison.OrdinalIgnoreCase) + .Replace("ws://", "http://", StringComparison.OrdinalIgnoreCase))); + _logger.LogInformation("Registering new account: " + postUri.ToString()); + var result = await httpClient.PostAsync(postUri, null).ConfigureAwait(false); + result.EnsureSuccessStatusCode(); + var reply = await result.Content.ReadFromJsonAsync().ConfigureAwait(false) ?? new(); + if (!reply.Success) + { + _logger.LogWarning("Registration failed: " + reply.ErrorMessage); + _registrationMessage = reply.ErrorMessage; + if (_registrationMessage.IsNullOrEmpty()) + _registrationMessage = "An unknown error occured. Please try again later."; + return; + } + _registrationMessage = "New account registered.\nPlease keep a copy of your secret key in case you need to reset your plugins, or to use it on another PC."; + _secretKey = reply.SecretKey ?? ""; + _registrationReply = reply; + _registrationSuccess = true; + } + catch (Exception ex) + { + _logger.LogWarning(ex, "Registration failed"); + _registrationSuccess = false; + _registrationMessage = "An unknown error occured. Please try again later."; + } + finally + { + _registrationInProgress = false; + } + }); + } + ImGui.EndDisabled(); // _registrationInProgress || _registrationSuccess + if (_registrationInProgress) + { + ImGui.TextUnformatted("Sending request..."); + } + else if (!_registrationMessage.IsNullOrEmpty()) + { + if (!_registrationSuccess) + ImGui.TextColored(ImGuiColors.DalamudYellow, _registrationMessage); + else + ImGui.TextUnformatted(_registrationMessage); + } + } + + ImGui.EndDisabled(); // _registrationInProgress } else { @@ -247,4 +328,4 @@ public class IntroUi : WindowMediatorSubscriberBase _tosParagraphs = [Strings.ToS.Paragraph1, Strings.ToS.Paragraph2, Strings.ToS.Paragraph3, Strings.ToS.Paragraph4, Strings.ToS.Paragraph5, Strings.ToS.Paragraph6]; } -} \ No newline at end of file +} diff --git a/MareSynchronos/UI/SettingsUi.cs b/MareSynchronos/UI/SettingsUi.cs index 005659e..0ff952f 100644 --- a/MareSynchronos/UI/SettingsUi.cs +++ b/MareSynchronos/UI/SettingsUi.cs @@ -6,6 +6,8 @@ using Dalamud.Utility; using ImGuiNET; using MareSynchronos.API.Data; using MareSynchronos.API.Data.Comparer; +using MareSynchronos.API.Dto.Account; +using MareSynchronos.API.Routes; using MareSynchronos.FileCache; using MareSynchronos.MareConfiguration; using MareSynchronos.MareConfiguration.Models; @@ -24,6 +26,9 @@ using System.Collections.Concurrent; using System.Globalization; using System.Numerics; using System.Text.Json; +using System.Net.Http.Headers; +using System.Net.Http.Json; +using System.Reflection; namespace MareSynchronos.UI; @@ -56,6 +61,10 @@ public class SettingsUi : WindowMediatorSubscriberBase private (int, int, FileCacheEntity) _currentProgress; private Task? _exportTask; + private bool _registrationInProgress = false; + private bool _registrationSuccess = false; + private string? _registrationMessage; + public SettingsUi(ILogger logger, UiSharedService uiShared, MareConfigService configService, MareCharaFileManager mareCharaFileManager, PairManager pairManager, @@ -1054,6 +1063,67 @@ public class SettingsUi : WindowMediatorSubscriberBase _serverConfigurationManager.Save(); } + if (selectedServer.ServerUri == ApiController.LoporritServiceUri) + { + ImGui.SameLine(); + if (UiSharedService.NormalizedIconTextButton(FontAwesomeIcon.Plus, "Register a new Loporrit account")) + { + _registrationInProgress = true; + _ = Task.Run(async () => { + try + { + using HttpClient httpClient = new(); + var ver = Assembly.GetExecutingAssembly().GetName().Version; + httpClient.DefaultRequestHeaders.UserAgent.Add(new ProductInfoHeaderValue("MareSynchronos", ver!.Major + "." + ver!.Minor + "." + ver!.Build)); + var postUri = MareAuth.AuthRegisterFullPath(new Uri(selectedServer.ServerUri + .Replace("wss://", "https://", StringComparison.OrdinalIgnoreCase) + .Replace("ws://", "http://", StringComparison.OrdinalIgnoreCase))); + _logger.LogInformation("Registering new account: " + postUri.ToString()); + var result = await httpClient.PostAsync(postUri, null).ConfigureAwait(false); + result.EnsureSuccessStatusCode(); + var reply = await result.Content.ReadFromJsonAsync().ConfigureAwait(false) ?? new(); + if (!reply.Success) + { + _logger.LogWarning("Registration failed: " + reply.ErrorMessage); + _registrationMessage = reply.ErrorMessage; + if (_registrationMessage.IsNullOrEmpty()) + _registrationMessage = "An unknown error occured. Please try again later."; + return; + } + _registrationMessage = "New account registered.\nPlease keep a copy of your secret key in case you need to reset your plugins, or to use it on another PC."; + _registrationSuccess = true; + selectedServer.SecretKeys.Add(selectedServer.SecretKeys.Any() ? selectedServer.SecretKeys.Max(p => p.Key) + 1 : 0, new SecretKey() + { + FriendlyName = reply.UID + $" (registered {DateTime.Now:yyyy-MM-dd})", + Key = reply.SecretKey ?? "" + }); + _serverConfigurationManager.Save(); + } + catch (Exception ex) + { + _logger.LogWarning(ex, "Registration failed"); + _registrationSuccess = false; + _registrationMessage = "An unknown error occured. Please try again later."; + } + finally + { + _registrationInProgress = false; + } + }); + } + if (_registrationInProgress) + { + ImGui.TextUnformatted("Sending request..."); + } + else if (!_registrationMessage.IsNullOrEmpty()) + { + if (!_registrationSuccess) + ImGui.TextColored(ImGuiColors.DalamudYellow, _registrationMessage); + else + ImGui.TextUnformatted(_registrationMessage); + } + } + ImGui.EndTabItem(); } @@ -1061,8 +1131,7 @@ public class SettingsUi : WindowMediatorSubscriberBase { var serverName = selectedServer.ServerName; var serverUri = selectedServer.ServerUri; - var isMain = string.Equals(serverName, ApiController.LoporritServer, StringComparison.OrdinalIgnoreCase) - || string.Equals(serverName, ApiController.MainServer, StringComparison.OrdinalIgnoreCase); + var isMain = string.Equals(serverName, ApiController.LoporritServer, StringComparison.OrdinalIgnoreCase); var flags = isMain ? ImGuiInputTextFlags.ReadOnly : ImGuiInputTextFlags.None; if (ImGui.InputText("Service URI", ref serverUri, 255, flags)) @@ -1137,8 +1206,10 @@ public class SettingsUi : WindowMediatorSubscriberBase if (ImGui.BeginTabItem("Service Settings")) { + ImGui.BeginDisabled(_registrationInProgress); DrawServerConfiguration(); ImGui.EndTabItem(); + ImGui.EndDisabled(); // _registrationInProgress } if (ImGui.BeginTabItem("Debug")) @@ -1161,4 +1232,4 @@ public class SettingsUi : WindowMediatorSubscriberBase _wasOpen = IsOpen; IsOpen = false; } -} \ No newline at end of file +}