Files
NebulaLauncher/Nebula.Launcher/ViewModels/Pages/AccountInfoViewModel.cs

274 lines
8.2 KiB
C#
Raw Normal View History

2025-01-27 15:55:30 +03:00
using System;
2025-05-03 18:20:24 +03:00
using System.Collections.Generic;
2024-12-22 21:38:19 +03:00
using System.Collections.ObjectModel;
2024-12-27 08:22:17 +03:00
using System.Linq;
2025-02-01 18:19:18 +03:00
using System.Text.Json.Serialization;
2025-01-27 15:55:30 +03:00
using System.Threading.Tasks;
2024-12-22 21:38:19 +03:00
using System.Windows.Input;
using CommunityToolkit.Mvvm.ComponentModel;
using CommunityToolkit.Mvvm.Input;
2025-01-14 22:10:16 +03:00
using Nebula.Launcher.Services;
using Nebula.Launcher.ViewModels.Popup;
2024-12-21 13:11:30 +03:00
using Nebula.Launcher.Views.Pages;
2025-01-05 17:05:23 +03:00
using Nebula.Shared;
using Nebula.Shared.Services;
2025-05-05 22:01:59 +03:00
using Nebula.Shared.Services.Logging;
2025-01-05 17:05:23 +03:00
using Nebula.Shared.Utils;
2024-12-21 13:11:30 +03:00
2025-01-14 22:10:16 +03:00
namespace Nebula.Launcher.ViewModels.Pages;
2024-12-21 13:11:30 +03:00
2025-01-05 17:05:23 +03:00
[ViewModelRegister(typeof(AccountInfoView))]
2025-01-14 22:10:16 +03:00
[ConstructGenerator]
2025-01-29 12:32:42 +03:00
public partial class AccountInfoViewModel : ViewModelBase, IViewModelPage
2024-12-21 13:11:30 +03:00
{
2025-01-14 22:10:16 +03:00
[ObservableProperty] private bool _authMenuExpand;
2024-12-22 21:38:19 +03:00
2024-12-27 19:15:33 +03:00
[ObservableProperty] private bool _authUrlConfigExpand;
[ObservableProperty] private int _authViewSpan = 1;
2025-01-14 22:10:16 +03:00
[ObservableProperty] private string _currentAuthServer = string.Empty;
[ObservableProperty] private string _currentLogin = string.Empty;
[ObservableProperty] private string _currentPassword = string.Empty;
2024-12-27 08:22:17 +03:00
2024-12-27 19:15:33 +03:00
[ObservableProperty] private bool _isLogged;
2024-12-22 21:38:19 +03:00
2025-01-14 22:10:16 +03:00
private bool _isProfilesEmpty;
[GenerateProperty] private PopupMessageService PopupMessageService { get; } = default!;
[GenerateProperty] private ConfigurationService ConfigurationService { get; } = default!;
2025-05-05 22:01:59 +03:00
[GenerateProperty] private DebugService DebugService { get; }
2025-01-14 22:10:16 +03:00
[GenerateProperty] private AuthService AuthService { get; } = default!;
2025-01-22 21:06:05 +03:00
[GenerateProperty, DesignConstruct] private ViewHelperService ViewHelperService { get; } = default!;
2025-01-14 22:10:16 +03:00
2025-02-01 18:19:18 +03:00
public ObservableCollection<ProfileAuthCredentials> Accounts { get; } = new();
2025-05-03 18:20:24 +03:00
public ObservableCollection<AuthServerCredentials> AuthUrls { get; } = new();
2025-01-14 22:10:16 +03:00
2025-05-03 18:20:24 +03:00
[ObservableProperty] private AuthServerCredentials _authItemSelect;
2025-01-14 22:10:16 +03:00
2025-05-05 22:01:59 +03:00
private ILogger _logger;
2024-12-22 21:38:19 +03:00
//Design think
2025-01-14 22:10:16 +03:00
protected override void InitialiseInDesignMode()
2024-12-22 21:38:19 +03:00
{
2025-01-14 22:10:16 +03:00
AddAccount(new AuthLoginPassword("Binka", "12341", ""));
2025-03-12 14:51:47 +03:00
AddAccount(new AuthLoginPassword("Binka", "12341", ""));
2025-05-03 18:20:24 +03:00
AuthUrls.Add(new AuthServerCredentials("Test",["example.com"]));
2024-12-22 21:38:19 +03:00
}
2025-01-14 22:10:16 +03:00
2024-12-22 21:38:19 +03:00
//Real think
2025-01-14 22:10:16 +03:00
protected override void Initialise()
2024-12-22 21:38:19 +03:00
{
2025-05-05 22:01:59 +03:00
_logger = DebugService.GetLogger(this);
2024-12-27 08:22:17 +03:00
ReadAuthConfig();
2024-12-22 21:38:19 +03:00
}
2025-02-01 18:19:18 +03:00
public void AuthByProfile(ProfileAuthCredentials credentials)
2024-12-22 21:38:19 +03:00
{
2025-05-03 18:20:24 +03:00
CurrentLogin = credentials.Login;
CurrentPassword = credentials.Password;
CurrentAuthServer = credentials.AuthServer;
2024-12-22 21:38:19 +03:00
DoAuth();
}
2025-02-01 18:19:18 +03:00
public void DoAuth(string? code = null)
2024-12-22 21:38:19 +03:00
{
2025-01-14 22:10:16 +03:00
var message = ViewHelperService.GetViewModel<InfoPopupViewModel>();
2025-01-08 18:00:06 +03:00
message.InfoText = "Auth think, please wait...";
2025-01-27 15:55:30 +03:00
message.IsInfoClosable = false;
2025-01-14 22:10:16 +03:00
PopupMessageService.Popup(message);
2025-05-03 18:20:24 +03:00
var serverCandidates = new List<string>();
if (string.IsNullOrWhiteSpace(CurrentAuthServer))
serverCandidates.AddRange(AuthItemSelect.Servers);
else
serverCandidates.Add(CurrentAuthServer);
2025-01-27 15:55:30 +03:00
Task.Run(async () =>
2024-12-27 08:22:17 +03:00
{
2025-05-03 18:20:24 +03:00
Exception? exception = null;
foreach (var server in serverCandidates)
2025-02-01 18:19:18 +03:00
{
2025-05-03 18:20:24 +03:00
try
2025-02-01 18:19:18 +03:00
{
2025-05-03 18:20:24 +03:00
await TryAuth(CurrentLogin, CurrentPassword, server,code);
break;
}
catch (Exception e)
{
exception = e;
2025-02-01 18:19:18 +03:00
}
}
2025-05-03 18:20:24 +03:00
message.Dispose();
if (!IsLogged)
2025-01-27 15:55:30 +03:00
{
2025-05-03 18:20:24 +03:00
PopupMessageService.Popup(new Exception("No one of auth server is available.", exception));
2025-01-27 15:55:30 +03:00
}
});
2024-12-27 19:15:33 +03:00
}
2025-05-03 18:20:24 +03:00
private async Task TryAuth(string login, string password, string authServer,string? code)
{
try
{
await AuthService.Auth(new AuthLoginPassword(login, password, authServer), code);
CurrentLogin = login;
CurrentPassword = password;
CurrentAuthServer = authServer;
IsLogged = true;
ConfigurationService.SetConfigValue(LauncherConVar.AuthCurrent, AuthService.SelectedAuth);
}
catch (AuthException e)
{
switch (e.Error.Code)
{
case AuthenticateDenyCode.TfaRequired:
case AuthenticateDenyCode.TfaInvalid:
var p = ViewHelperService.GetViewModel<TfaViewModel>();
p.OnTfaEntered += OnTfaEntered;
PopupMessageService.Popup(p);
2025-05-05 22:01:59 +03:00
_logger.Log("TFA required");
2025-05-03 18:20:24 +03:00
break;
case AuthenticateDenyCode.InvalidCredentials:
PopupMessageService.Popup("Invalid Credentials!");
2025-05-05 22:01:59 +03:00
_logger.Error($"Invalid credentials");
2025-05-03 18:20:24 +03:00
break;
default:
throw;
}
}
}
2025-02-01 18:19:18 +03:00
private void OnTfaEntered(string code)
{
DoAuth(code);
}
2024-12-27 19:15:33 +03:00
public void Logout()
{
IsLogged = false;
2025-01-14 22:10:16 +03:00
AuthService.ClearAuth();
2024-12-27 19:15:33 +03:00
}
private void UpdateAuthMenu()
{
if (AuthMenuExpand || _isProfilesEmpty)
AuthViewSpan = 2;
else
AuthViewSpan = 1;
2024-12-22 21:38:19 +03:00
}
private void AddAccount(AuthLoginPassword authLoginPassword)
{
2025-02-01 18:19:18 +03:00
var onDelete = new DelegateCommand<ProfileAuthCredentials>(OnDeleteProfile);
var onSelect = new DelegateCommand<ProfileAuthCredentials>(AuthByProfile);
2025-01-14 22:10:16 +03:00
2025-02-01 18:19:18 +03:00
var alpm = new ProfileAuthCredentials(
2025-01-14 22:10:16 +03:00
authLoginPassword.Login,
2024-12-22 21:38:19 +03:00
authLoginPassword.Password,
2025-01-14 22:10:16 +03:00
authLoginPassword.AuthServer,
onSelect,
2024-12-22 21:38:19 +03:00
onDelete);
onDelete.TRef.Value = alpm;
onSelect.TRef.Value = alpm;
2025-01-14 22:10:16 +03:00
2024-12-22 21:38:19 +03:00
Accounts.Add(alpm);
}
2025-02-01 18:19:18 +03:00
private async void ReadAuthConfig()
2024-12-22 21:38:19 +03:00
{
2025-02-01 18:19:18 +03:00
var message = ViewHelperService.GetViewModel<InfoPopupViewModel>();
message.InfoText = "Read configuration file, please wait...";
message.IsInfoClosable = false;
PopupMessageService.Popup(message);
2025-01-14 22:10:16 +03:00
foreach (var profile in
2025-02-01 18:19:18 +03:00
ConfigurationService.GetConfigValue(LauncherConVar.AuthProfiles)!)
AddAccount(new AuthLoginPassword(profile.Login, profile.Password, profile.AuthServer));
2024-12-27 08:22:17 +03:00
2025-01-14 22:10:16 +03:00
if (Accounts.Count == 0) UpdateAuthMenu();
2024-12-27 19:15:33 +03:00
2025-02-01 18:19:18 +03:00
AuthUrls.Clear();
var authUrls = ConfigurationService.GetConfigValue(LauncherConVar.AuthServers)!;
foreach (var url in authUrls) AuthUrls.Add(url);
2025-05-03 18:20:24 +03:00
if(authUrls.Length > 0) AuthItemSelect = authUrls[0];
2025-02-01 18:19:18 +03:00
var currProfile = ConfigurationService.GetConfigValue(LauncherConVar.AuthCurrent);
2024-12-27 08:22:17 +03:00
if (currProfile != null)
{
2025-02-01 18:19:18 +03:00
try
{
2025-05-03 18:20:24 +03:00
CurrentLogin = currProfile.Login;
CurrentAuthServer = currProfile.AuthServer;
2025-02-01 18:19:18 +03:00
IsLogged = await AuthService.SetAuth(currProfile);
}
catch (Exception e)
{
message.Dispose();
PopupMessageService.Popup(e);
}
2024-12-27 08:22:17 +03:00
}
2025-02-01 18:19:18 +03:00
message.Dispose();
2024-12-22 21:38:19 +03:00
}
2024-12-27 08:22:17 +03:00
[RelayCommand]
2024-12-27 19:15:33 +03:00
private void OnSaveProfile()
2024-12-22 21:38:19 +03:00
{
2025-05-03 18:20:24 +03:00
AddAccount(new AuthLoginPassword(CurrentLogin, CurrentPassword, CurrentAuthServer));
2024-12-27 19:15:33 +03:00
_isProfilesEmpty = Accounts.Count == 0;
UpdateAuthMenu();
DirtyProfile();
}
2025-01-14 22:10:16 +03:00
2025-02-01 18:19:18 +03:00
private void OnDeleteProfile(ProfileAuthCredentials account)
2024-12-27 19:15:33 +03:00
{
Accounts.Remove(account);
_isProfilesEmpty = Accounts.Count == 0;
UpdateAuthMenu();
DirtyProfile();
}
[RelayCommand]
private void OnExpandAuthUrl()
{
AuthUrlConfigExpand = !AuthUrlConfigExpand;
}
[RelayCommand]
private void OnExpandAuthView()
{
AuthMenuExpand = !AuthMenuExpand;
UpdateAuthMenu();
}
private void DirtyProfile()
{
2025-02-01 18:19:18 +03:00
ConfigurationService.SetConfigValue(LauncherConVar.AuthProfiles,
Accounts.ToArray());
2024-12-22 21:38:19 +03:00
}
2025-01-29 12:32:42 +03:00
public void OnPageOpen(object? args)
{
}
2024-12-22 21:38:19 +03:00
}
2025-02-01 18:19:18 +03:00
public sealed record ProfileAuthCredentials(
2025-01-14 22:10:16 +03:00
string Login,
string Password,
string AuthServer,
2025-02-01 18:19:18 +03:00
[property: JsonIgnore] ICommand OnSelect = default!,
[property: JsonIgnore] ICommand OnDelete = default!
2025-05-03 18:20:24 +03:00
);
public sealed record AuthServerCredentials(
string Name,
string[] Servers
);