add oauth2 to client

This commit is contained in:
Stanley Dimant 2024-10-29 13:05:33 +01:00
parent 6f9347917d
commit ed427de0ab
19 changed files with 1060 additions and 539 deletions

View file

@ -1,5 +1,6 @@
using Dalamud.Interface.Colors;
using Dalamud.Interface.Utility;
using Dalamud.Interface.Utility.Raii;
using Dalamud.Utility;
using ImGuiNET;
using MareSynchronos.FileCache;
@ -30,6 +31,7 @@ public partial class IntroUi : WindowMediatorSubscriberBase
private string _timeoutLabel = string.Empty;
private Task? _timeoutTask;
private string[]? _tosParagraphs;
private bool _useLegacyLogin = false;
public IntroUi(ILogger<IntroUi> logger, UiSharedService uiShared, MareConfigService configService,
CacheMonitor fileCacheManager, ServerConfigurationManager serverConfigurationManager, MareMediator mareMediator,
@ -60,6 +62,8 @@ public partial class IntroUi : WindowMediatorSubscriberBase
});
}
private int _prevIdx = -1;
protected override void DrawInternal()
{
if (_uiShared.IsInGpose) return;
@ -216,50 +220,111 @@ public partial class IntroUi : WindowMediatorSubscriberBase
UiSharedService.TextWrapped("Once you have received a secret key you can connect to the service using the tools provided below.");
_ = _uiShared.DrawServiceSelection(selectOnChange: true);
var serverIdx = _uiShared.DrawServiceSelection(selectOnChange: true, showConnect: false);
if (serverIdx != _prevIdx)
{
_uiShared.RestOAuthTasksState();
_prevIdx = serverIdx;
}
var selectedServer = _serverConfigurationManager.GetServerByIndex(serverIdx);
_useLegacyLogin = !selectedServer.UseOAuth2;
var text = "Enter Secret Key";
var buttonText = "Save";
var buttonWidth = _secretKey.Length != 64 ? 0 : ImGuiHelpers.GetButtonSize(buttonText).X + ImGui.GetStyle().ItemSpacing.X;
var textSize = ImGui.CalcTextSize(text);
ImGui.AlignTextToFramePadding();
ImGui.TextUnformatted(text);
ImGui.SameLine();
ImGui.SetNextItemWidth(UiSharedService.GetWindowContentRegionWidth() - ImGui.GetWindowContentRegionMin().X - buttonWidth - textSize.X);
ImGui.InputText("", ref _secretKey, 64);
if (_secretKey.Length > 0 && _secretKey.Length != 64)
if (ImGui.Checkbox("Use Legacy Login with Secret Key", ref _useLegacyLogin))
{
UiSharedService.ColorTextWrapped("Your secret key must be exactly 64 characters long. Don't enter your Lodestone auth here.", ImGuiColors.DalamudRed);
_serverConfigurationManager.GetServerByIndex(serverIdx).UseOAuth2 = !_useLegacyLogin;
_serverConfigurationManager.Save();
}
else if (_secretKey.Length == 64 && !HexRegex().IsMatch(_secretKey))
{
UiSharedService.ColorTextWrapped("Your secret key can only contain ABCDEF and the numbers 0-9.", ImGuiColors.DalamudRed);
}
else if (_secretKey.Length == 64)
if (_useLegacyLogin)
{
var text = "Enter Secret Key";
var buttonText = "Save";
var buttonWidth = _secretKey.Length != 64 ? 0 : ImGuiHelpers.GetButtonSize(buttonText).X + ImGui.GetStyle().ItemSpacing.X;
var textSize = ImGui.CalcTextSize(text);
ImGui.AlignTextToFramePadding();
ImGui.TextUnformatted(text);
ImGui.SameLine();
if (ImGui.Button(buttonText))
ImGui.SetNextItemWidth(UiSharedService.GetWindowContentRegionWidth() - ImGui.GetWindowContentRegionMin().X - buttonWidth - textSize.X);
ImGui.InputText("", ref _secretKey, 64);
if (_secretKey.Length > 0 && _secretKey.Length != 64)
{
if (_serverConfigurationManager.CurrentServer == null) _serverConfigurationManager.SelectServer(0);
if (!_serverConfigurationManager.CurrentServer!.SecretKeys.Any())
UiSharedService.ColorTextWrapped("Your secret key must be exactly 64 characters long. Don't enter your Lodestone auth here.", ImGuiColors.DalamudRed);
}
else if (_secretKey.Length == 64 && !HexRegex().IsMatch(_secretKey))
{
UiSharedService.ColorTextWrapped("Your secret key can only contain ABCDEF and the numbers 0-9.", ImGuiColors.DalamudRed);
}
else if (_secretKey.Length == 64)
{
ImGui.SameLine();
if (ImGui.Button(buttonText))
{
_serverConfigurationManager.CurrentServer!.SecretKeys.Add(_serverConfigurationManager.CurrentServer.SecretKeys.Select(k => k.Key).LastOrDefault() + 1, new SecretKey()
if (_serverConfigurationManager.CurrentServer == null) _serverConfigurationManager.SelectServer(0);
if (!_serverConfigurationManager.CurrentServer!.SecretKeys.Any())
{
FriendlyName = $"Secret Key added on Setup ({DateTime.Now:yyyy-MM-dd})",
Key = _secretKey,
_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})",
Key = _secretKey,
});
_serverConfigurationManager.AddCurrentCharacterToServer();
}
else
{
_serverConfigurationManager.CurrentServer!.SecretKeys[0] = new SecretKey()
{
FriendlyName = $"Secret Key added on Setup ({DateTime.Now:yyyy-MM-dd})",
Key = _secretKey,
};
}
_secretKey = string.Empty;
_ = Task.Run(() => _uiShared.ApiController.CreateConnectionsAsync());
}
}
}
else
{
if (selectedServer.OAuthToken == null)
{
UiSharedService.TextWrapped("Press the button below to verify the server has OAuth2 capabilities. Afterwards, authenticate using Discord in the Browser window.");
_uiShared.DrawOAuth(selectedServer);
}
else
{
UiSharedService.ColorTextWrapped($"OAuth2 is enabled, linked to: Discord User {_serverConfigurationManager.GetDiscordUserFromToken(selectedServer)}", ImGuiColors.HealerGreen);
UiSharedService.TextWrapped("Now press the update UIDs button to get a list of all of your UIDs on the server.");
_uiShared.DrawUpdateOAuthUIDsButton(selectedServer);
var playerName = _dalamudUtilService.GetPlayerName();
var playerWorld = _dalamudUtilService.GetHomeWorldId();
UiSharedService.TextWrapped($"Once pressed, select the UID you want to use for your current character {_dalamudUtilService.GetPlayerName()}. If no UIDs are visible, make sure you are connected to the correct Discord account. " +
$"If that is not the case, use the unlink button below (hold CTRL to unlink).");
_uiShared.DrawUnlinkOAuthButton(selectedServer);
var auth = selectedServer.Authentications.Find(a => string.Equals(a.CharacterName, playerName, StringComparison.Ordinal) && a.WorldId == playerWorld);
if (auth == null)
{
selectedServer.Authentications.Add(new Authentication()
{
CharacterName = playerName,
WorldId = playerWorld
});
_serverConfigurationManager.AddCurrentCharacterToServer();
_serverConfigurationManager.Save();
}
else
if (auth != null)
{
_serverConfigurationManager.CurrentServer!.SecretKeys[0] = new SecretKey()
_uiShared.DrawUIDComboForAuthentication(0, auth, selectedServer.ServerUri);
using (ImRaii.Disabled(string.IsNullOrEmpty(auth.UID)))
{
FriendlyName = $"Secret Key added on Setup ({DateTime.Now:yyyy-MM-dd})",
Key = _secretKey,
};
if (_uiShared.IconTextButton(Dalamud.Interface.FontAwesomeIcon.Link, "Connect to Service"))
{
_ = Task.Run(() => _uiShared.ApiController.CreateConnectionsAsync());
}
}
if (string.IsNullOrEmpty(auth.UID))
UiSharedService.AttachToolTip("Select a UID to be able to connect to the service");
}
_secretKey = string.Empty;
_ = Task.Run(() => _uiShared.ApiController.CreateConnections());
}
}
}